From bb429e3fc8da596c201f89b7a3a0106fff918edd Mon Sep 17 00:00:00 2001 From: lpf Date: Sun, 8 Mar 2026 12:11:38 +0800 Subject: [PATCH] fix: send mcp initialized as notification --- pkg/tools/mcp.go | 30 ++++++++++++++++++++++++++++-- pkg/tools/mcp_test.go | 14 ++++++++++---- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/pkg/tools/mcp.go b/pkg/tools/mcp.go index f0a66bd..db183f2 100644 --- a/pkg/tools/mcp.go +++ b/pkg/tools/mcp.go @@ -804,8 +804,7 @@ func (c *mcpHTTPClient) initialize(ctx context.Context) error { if _, ok := result["protocolVersion"]; !ok { return fmt.Errorf("mcp server %q initialize missing protocolVersion", c.serverName) } - _, _ = c.request(ctx, "notifications/initialized", map[string]interface{}{}) - return nil + return c.notify(ctx, "notifications/initialized", map[string]interface{}{}) } func (c *mcpHTTPClient) listAll(ctx context.Context, method, field string) (map[string]interface{}, error) { @@ -873,6 +872,33 @@ func (c *mcpHTTPClient) request(ctx context.Context, method string, params map[s return out, nil } +func (c *mcpHTTPClient) notify(ctx context.Context, method string, params map[string]interface{}) error { + payload := map[string]interface{}{ + "jsonrpc": "2.0", + "method": method, + "params": params, + } + body, err := json.Marshal(payload) + if err != nil { + return err + } + req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL, bytes.NewReader(body)) + if err != nil { + return err + } + req.Header.Set("Content-Type", "application/json") + resp, err := c.client.Do(req) + if err != nil { + return fmt.Errorf("mcp %s %s failed: %w", c.serverName, method, err) + } + defer resp.Body.Close() + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + data, _ := io.ReadAll(io.LimitReader(resp.Body, 4096)) + return fmt.Errorf("mcp %s %s failed: http %d %s", c.serverName, method, resp.StatusCode, strings.TrimSpace(string(data))) + } + return nil +} + func (c *mcpClient) Close() error { if c == nil || c.cmd == nil { return nil diff --git a/pkg/tools/mcp_test.go b/pkg/tools/mcp_test.go index b66be01..dd7a334 100644 --- a/pkg/tools/mcp_test.go +++ b/pkg/tools/mcp_test.go @@ -373,6 +373,7 @@ func TestMCPToolListTools(t *testing.T) { } func TestMCPToolHTTPTransport(t *testing.T) { + initializedNotified := false server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() var req map[string]interface{} @@ -393,11 +394,13 @@ func TestMCPToolHTTPTransport(t *testing.T) { }, } case "notifications/initialized": - resp = map[string]interface{}{ - "jsonrpc": "2.0", - "id": id, - "result": map[string]interface{}{}, + if _, hasID := req["id"]; hasID { + http.Error(w, "notification must not include id", http.StatusBadRequest) + return } + initializedNotified = true + w.WriteHeader(http.StatusAccepted) + return case "tools/list": resp = map[string]interface{}{ "jsonrpc": "2.0", @@ -455,6 +458,9 @@ func TestMCPToolHTTPTransport(t *testing.T) { if !strings.Contains(out, `"name": "echo"`) { t.Fatalf("expected http tool listing, got: %s", out) } + if !initializedNotified { + t.Fatal("expected initialized notification to be sent") + } } func TestMCPToolSSETransport(t *testing.T) {