The supervisor's MCP HTTP handler was answering every parsed request, including notifications (messages with no id field). Per JSON-RPC 2.0, notifications must not receive a response. The Apr-29 incident saw Claude Code's MCP client receive a -32601 error for the standard notifications/initialized handshake step and disconnect immediately after a successful initialize. Skip writing the response when req.ID == nil. Cover both the known-method (notifications/initialized) and unknown-method paths with tests. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
116 lines
3.3 KiB
Go
116 lines
3.3 KiB
Go
package mcp_test
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mathiasbq/supervisor/internal/mcp"
|
|
"github.com/mathiasbq/supervisor/internal/registry"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func jsonBody(t *testing.T, v any) *bytes.Buffer {
|
|
t.Helper()
|
|
b, err := json.Marshal(v)
|
|
require.NoError(t, err)
|
|
return bytes.NewBuffer(b)
|
|
}
|
|
|
|
func TestMCPInitialize(t *testing.T) {
|
|
reg := registry.New()
|
|
srv := mcp.NewServer(reg)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/mcp", jsonBody(t, map[string]any{
|
|
"jsonrpc": "2.0",
|
|
"id": 1,
|
|
"method": "initialize",
|
|
"params": map[string]any{},
|
|
}))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
rr := httptest.NewRecorder()
|
|
|
|
srv.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code)
|
|
var resp map[string]any
|
|
require.NoError(t, json.Unmarshal(rr.Body.Bytes(), &resp))
|
|
result := resp["result"].(map[string]any)
|
|
assert.Equal(t, "2024-11-05", result["protocolVersion"])
|
|
}
|
|
|
|
func TestMCPToolsList(t *testing.T) {
|
|
reg := registry.New()
|
|
srv := mcp.NewServer(reg)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/mcp", jsonBody(t, map[string]any{
|
|
"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": map[string]any{},
|
|
}))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
rr := httptest.NewRecorder()
|
|
srv.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code)
|
|
var resp map[string]any
|
|
require.NoError(t, json.Unmarshal(rr.Body.Bytes(), &resp))
|
|
result := resp["result"].(map[string]any)
|
|
assert.NotNil(t, result["tools"])
|
|
}
|
|
|
|
func TestMCPUnknownMethod(t *testing.T) {
|
|
reg := registry.New()
|
|
srv := mcp.NewServer(reg)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/mcp", jsonBody(t, map[string]any{
|
|
"jsonrpc": "2.0", "id": 3, "method": "unknown/method", "params": map[string]any{},
|
|
}))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
rr := httptest.NewRecorder()
|
|
srv.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code)
|
|
var resp map[string]any
|
|
require.NoError(t, json.Unmarshal(rr.Body.Bytes(), &resp))
|
|
assert.NotNil(t, resp["error"])
|
|
}
|
|
|
|
func TestMCPNotificationKnownMethodGetsNoResponseBody(t *testing.T) {
|
|
reg := registry.New()
|
|
srv := mcp.NewServer(reg)
|
|
|
|
// JSON-RPC 2.0 notification: "id" field absent. Per spec, server MUST NOT
|
|
// reply. notifications/initialized is part of the standard MCP handshake.
|
|
req := httptest.NewRequest(http.MethodPost, "/mcp", jsonBody(t, map[string]any{
|
|
"jsonrpc": "2.0",
|
|
"method": "notifications/initialized",
|
|
}))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
rr := httptest.NewRecorder()
|
|
srv.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code)
|
|
assert.Empty(t, strings.TrimSpace(rr.Body.String()),
|
|
"notifications must not receive a response body")
|
|
}
|
|
|
|
func TestMCPNotificationUnknownMethodGetsNoResponseBody(t *testing.T) {
|
|
reg := registry.New()
|
|
srv := mcp.NewServer(reg)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/mcp", jsonBody(t, map[string]any{
|
|
"jsonrpc": "2.0",
|
|
"method": "notifications/totally-unknown",
|
|
}))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
rr := httptest.NewRecorder()
|
|
srv.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rr.Code)
|
|
assert.Empty(t, strings.TrimSpace(rr.Body.String()),
|
|
"unknown notifications must also receive no response body")
|
|
}
|