Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions cmd/emcee/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,11 @@ Authentication values can be provided directly or as 1Password secret references
// Create SDK server and register tools from OpenAPI
impl := &mcp.Implementation{Name: cmd.Name(), Version: version}
server := mcp.NewServer(impl, nil)
if err := internal.RegisterTools(server, specData, client); err != nil {
var opts []internal.RegisterToolsOption
if noAnnotations {
opts = append(opts, internal.WithoutAnnotations())
}
if err := internal.RegisterTools(server, specData, client, opts...); err != nil {
return fmt.Errorf("error registering tools: %w", err)
}

Expand All @@ -213,8 +217,9 @@ var (
timeout time.Duration
rps int

verbose bool
silent bool
verbose bool
silent bool
noAnnotations bool

version = "dev"
commit = "none"
Expand All @@ -235,6 +240,8 @@ func init() {
rootCmd.Flags().BoolVarP(&silent, "silent", "s", false, "Disable all logging")
rootCmd.MarkFlagsMutuallyExclusive("verbose", "silent")

rootCmd.Flags().BoolVar(&noAnnotations, "no-annotations", false, "Disable generated tool annotations")

rootCmd.Version = fmt.Sprintf("%s (commit: %s, built at: %s)", version, commit, date)
}

Expand Down
61 changes: 60 additions & 1 deletion internal/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,22 @@ import (
"gopkg.in/yaml.v3"
)

// RegisterToolsOption configures RegisterTools behavior.
type RegisterToolsOption func(*registerToolsConfig)

type registerToolsConfig struct {
enableAnnotations bool
}

// WithoutAnnotations disables attaching REST-aware MCP ToolAnnotations for generated tools.
func WithoutAnnotations() RegisterToolsOption {
return func(cfg *registerToolsConfig) { cfg.enableAnnotations = false }
}

// RegisterTools parses the given OpenAPI specification and registers tools on the provided MCP server.
// All HTTP calls are executed using the provided http.Client. If the client is nil, http.DefaultClient is used.
func RegisterTools(server *mcp.Server, specData []byte, client *http.Client) error {
// By default, REST-aware MCP ToolAnnotations are attached to each tool. Pass options to change behavior.
func RegisterTools(server *mcp.Server, specData []byte, client *http.Client, opts ...RegisterToolsOption) error {
if len(specData) == 0 {
return fmt.Errorf("no OpenAPI spec data provided")
}
Expand All @@ -34,6 +47,14 @@ func RegisterTools(server *mcp.Server, specData []byte, client *http.Client) err
client = http.DefaultClient
}

// Defaults
cfg := &registerToolsConfig{enableAnnotations: true}
for _, opt := range opts {
if opt != nil {
opt(cfg)
}
}

doc, err := libopenapi.NewDocument(specData)
if err != nil {
return fmt.Errorf("error parsing OpenAPI spec: %w", err)
Expand Down Expand Up @@ -86,12 +107,14 @@ func RegisterTools(server *mcp.Server, specData []byte, client *http.Client) err
addParamToSchema(schema, param)
}
}

// Operation parameters
if op.op.Parameters != nil {
for _, param := range op.op.Parameters {
addParamToSchema(schema, param)
}
}

// Request body (application/json)
if op.op.RequestBody != nil && op.op.RequestBody.Content != nil {
if mediaType, ok := op.op.RequestBody.Content.Get("application/json"); ok && mediaType != nil {
Expand Down Expand Up @@ -121,6 +144,42 @@ func RegisterTools(server *mcp.Server, specData []byte, client *http.Client) err
InputSchema: schema,
}

if cfg.enableAnnotations {
// Derive MCP ToolAnnotations from REST conventions
title := op.op.Summary
if title == "" {
title = fmt.Sprintf("%s %s", op.method, p)
}
openWorld := true
destructiveTrue := true

Copilot AI Aug 14, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The variable destructiveTrue is defined but only used for POST, PUT, PATCH, and DELETE cases. Consider defining it only where needed or use a literal true directly in the assignments to reduce unnecessary variable declarations.

Suggested change
destructiveTrue := true

Copilot uses AI. Check for mistakes.
ann := &mcp.ToolAnnotations{
Title: title,
OpenWorldHint: &openWorld,
Comment thread
mattt marked this conversation as resolved.
}
switch op.method {
case "GET":
ann.ReadOnlyHint = true
ann.IdempotentHint = true
case "POST":
ann.ReadOnlyHint = false
ann.IdempotentHint = false
ann.DestructiveHint = &destructiveTrue
case "PUT":
ann.ReadOnlyHint = false
ann.IdempotentHint = true
ann.DestructiveHint = &destructiveTrue
case "PATCH":
ann.ReadOnlyHint = false
ann.IdempotentHint = false
ann.DestructiveHint = &destructiveTrue
case "DELETE":
ann.ReadOnlyHint = false
ann.IdempotentHint = true
ann.DestructiveHint = &destructiveTrue
}
tool.Annotations = ann
}

// Capture for handler
method := op.method
operation := op.op
Expand Down