Skip to content
Closed
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
5 changes: 4 additions & 1 deletion internal/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ func RegisterTools(server *mcp.Server, specData []byte, client *http.Client, opt
op *v3.Operation
}{
{"GET", item.Get},
{"HEAD", item.Head},
{"OPTIONS", item.Options},
{"POST", item.Post},
{"PUT", item.Put},
{"DELETE", item.Delete},
{"PATCH", item.Patch},
{"TRACE", item.Trace},
}
for _, op := range ops {
if op.op == nil || op.op.OperationId == "" {
Expand Down Expand Up @@ -185,7 +188,7 @@ func RegisterTools(server *mcp.Server, specData []byte, client *http.Client, opt
OpenWorldHint: &openWorld,
}
switch op.method {
case "GET":
case "GET", "HEAD", "OPTIONS", "TRACE":
ann.ReadOnlyHint = true
ann.IdempotentHint = true
case "POST":
Expand Down
79 changes: 64 additions & 15 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bufio"
"encoding/json"
"os"
"os/exec"
"path/filepath"
"testing"
Expand All @@ -12,15 +13,28 @@ import (
"github.com/stretchr/testify/require"
)

func TestIntegration(t *testing.T) {
type toolListResponse struct {
JSONRPC string `json:"jsonrpc"`
Result struct {
Tools []struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema json.RawMessage `json:"inputSchema"`
} `json:"tools"`
} `json:"result"`
ID int `json:"id"`
}

func listTools(t *testing.T, specPath string) toolListResponse {
t.Helper()

// Build the emcee binary for testing
tmpDir := t.TempDir()
binaryPath := filepath.Join(tmpDir, "emcee")
buildCmd := exec.Command("go", "build", "-o", binaryPath, "cmd/emcee/main.go")
require.NoError(t, buildCmd.Run(), "Failed to build emcee binary")

// Start emcee with the embedded test OpenAPI spec
specPath := "testdata/api.weather.gov/openapi.json"
cmd := exec.Command(binaryPath, specPath)
stdin, err := cmd.StdinPipe()
require.NoError(t, err)
Expand Down Expand Up @@ -91,24 +105,19 @@ func TestIntegration(t *testing.T) {
require.NoError(t, err)
require.True(t, scanner.Scan(), "Expected tools/list response")

var response struct {
JSONRPC string `json:"jsonrpc"`
Result struct {
Tools []struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema json.RawMessage `json:"inputSchema"`
} `json:"tools"`
} `json:"result"`
ID int `json:"id"`
}

var response toolListResponse
err = json.Unmarshal(scanner.Bytes(), &response)
require.NoError(t, err, "Failed to parse JSON response")

return response
}

func TestIntegration(t *testing.T) {
response := listTools(t, "testdata/api.weather.gov/openapi.json")

// Verify response
assert.Equal(t, "2.0", response.JSONRPC)
assert.Equal(t, listReqID, response.ID)
assert.Equal(t, 2, response.ID)
assert.NotEmpty(t, response.Result.Tools, "Expected at least one tool in response")

// Find and verify the point tool
Expand Down Expand Up @@ -189,3 +198,43 @@ func TestIntegration(t *testing.T) {
assert.Contains(t, zoneIdParam["description"].(string), "UGC identifier for a NWS")
assert.Contains(t, zoneTool.InputSchema.Required, "zoneId", "zoneId parameter should be required")
}

func TestRegistersStandardOpenAPIMethods(t *testing.T) {
tmpDir := t.TempDir()
specPath := filepath.Join(tmpDir, "openapi.json")
spec := []byte(`{
"openapi": "3.1.0",
"info": {"title": "Method coverage API", "version": "1.0.0"},
"servers": [{"url": "https://api.example.test"}],
"paths": {
"/resource": {
"head": {
"operationId": "headResource",
"summary": "Check whether the resource exists",
"responses": {"204": {"description": "No content"}}
},
"options": {
"operationId": "optionsResource",
"summary": "List supported methods",
"responses": {"200": {"description": "OK"}}
},
"trace": {
"operationId": "traceResource",
"summary": "Trace the request",
"responses": {"200": {"description": "OK"}}
}
}
}
}`)
require.NoError(t, os.WriteFile(specPath, spec, 0o600))

response := listTools(t, specPath)
toolNames := make(map[string]struct{})
for _, tool := range response.Result.Tools {
toolNames[tool.Name] = struct{}{}
}

assert.Contains(t, toolNames, "headResource")
assert.Contains(t, toolNames, "optionsResource")
assert.Contains(t, toolNames, "traceResource")
}