From 1490045f594493bc2a4eb89b2a0e173fce7e9413 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 3 Nov 2026 02:28:47 +0800 Subject: [PATCH] claude bridge: an unloaded deferred magpie MCP tool is handed over by its full name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit packinggoods (#feedback, 11-02): every first MCP call in Claude Code failed with "Error: No such tool available: magpie-web-search__web_search" and the retry worked. Claude Code with tool search on names a deferred MCP tool in a reminder and leaves it out of tools until ToolSearch loads it. Through the Claude subscription bridge the run's model reads that name and calls it, mcp__magpie__magpie-web-search__web_search; toolStart cut mcp__magpie__ and, the tool not being one the client sent, handed the client the bare magpie-web-search__web_search, which it has no tool of. After a ToolSearch the tool is in tools, the cut name maps back, and the call works: the retry. a3768cdd1 (the router sending a name it no longer lists to its server) doesn't reach this; nothing gets to the router. toolStart now hands such a call over under the name the model called, the client's own, when the cut name maps to nothing and isn't among the run's tools. A declared tool maps as before; with the run's tools not yet set the old behaviour stays. Reproduced in a sandbox HOME with Claude Code 2.1.316 against a fake Anthropic API and a stdio MCP server: a call of the bare name gives the reporter's exact error, and a call of the full name of an unloaded deferred tool runs. Run at this head (mote): go test -tags nogui ./internal/gateway -run 'TestClaudeBridgeHandsOverAnUnloadedMagpieTool|TestClaudeBridgeKeepsMagpieMCPNames|TestClaudeRunEndsAfterStructuredOutput|ToolChoice|ToolName' ok; then rebased on fb872a68e (a2837c1f5 and 2ff93ffd6 on top change only two provider movers' mins): gofmt -l internal/gateway clean, GOOS=linux and GOOS=windows go vet -tags nogui ./internal/gateway ok, go test -tags nogui ./internal/gateway -run 'Claude|MCP|Hub|ToolName|ToolChoice' ok (88s). Broken by hand: with the name = called line out, TestClaudeBridgeHandsOverAnUnloadedMagpieTool is red: handed "magpie-web-search__web_search", want "mcp__magpie__magpie-web-search__web_search". Not tried: a real bridge run on a real Claude account, and the reporter's own setup (agent, account) is inferred from the screenshot. Not changed: a deferred tool of another client MCP server (mcp__linear__…) is still answered by the run (#4956, related #4854). --- docs/subsystems/claude-subscription-bridge.md | 12 +++ internal/gateway/claude_subscription.go | 14 ++- internal/gateway/claude_unloaded_tool_test.go | 88 +++++++++++++++++++ 3 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 internal/gateway/claude_unloaded_tool_test.go diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 1b37a5854..21ffe01cf 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -75,6 +75,18 @@ run was offered it. `subscriptionRun.names` (`bridgeNames`) maps it back to the declared name in `toolStart`, for streamed and whole messages, and in `mcpCall` (#2649). +A client's MCP tool that it announced but hasn't loaded (Claude Code's +tool search names deferred tools in a reminder and leaves them out of +`tools` until `ToolSearch` loads them) is no tool of the run's, but the +run's model reads its name in the conversation and calls it, +`mcp__magpie____`. `toolStart` hands that call over under +the full name, which is the client's own, when the cut name maps to +nothing and isn't in `subscriptionRun.tools`: handed over cut, as +`__`, the client answered "No such tool available" and the +model had to load it and call again (packinggoods, 11-02, +`TestClaudeBridgeHandsOverAnUnloadedMagpieTool`). A deferred tool of +another client MCP server (`mcp__linear__…`) is still not handed over. + A turn with `tool_choice` none is offered every tool the client sent, as any other turn is (bbfatx on X). Anthropic caches a prompt as tools, then system, then messages, so a turn offered fewer tools (a title or summary diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index 65fdabae1..2f6a2729d 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -3127,11 +3127,21 @@ func (r *subscriptionRun) readOutput(rd io.Reader) { own[index] = true return "", false } - name, ok := strings.CutPrefix(name, "mcp__magpie__") + rest, ok := strings.CutPrefix(name, "mcp__magpie__") r.mu.Lock() - name = declaredName(r.names, name) + called := name + name = declaredName(r.names, rest) search := r.search != nil && name == r.searchName || r.answerName != "" && name == r.answerName refused := r.allowed != nil && !r.allowed[name] + // a tool the client announced but hasn't loaded yet (Claude + // Code's deferred MCP tools, told by name in the conversation + // and not in tools until a ToolSearch) is called by the + // client's own name, which is the name the run's agent called: + // the bare name cut from it is no tool the client has, and it + // answered "No such tool available" before a retry (packinggoods) + if ok && !search && name == rest && r.tools != nil && !r.tools[rest] { + name = called + } r.mu.Unlock() // magpie answers its own web search, as Claude Code does // its WebSearch, and a call of a tool the turn may not call diff --git a/internal/gateway/claude_unloaded_tool_test.go b/internal/gateway/claude_unloaded_tool_test.go new file mode 100644 index 000000000..b4afe8fa1 --- /dev/null +++ b/internal/gateway/claude_unloaded_tool_test.go @@ -0,0 +1,88 @@ +package gateway + +import ( + "encoding/json" + "net/http/httptest" + "os" + "path/filepath" + "runtime" + "strconv" + "strings" + "testing" + + "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/testenv" +) + +// Claude Code with tool search on tells its model a deferred MCP tool by +// name only ("The following deferred tools are now available via +// ToolSearch… mcp__magpie__magpie-web-search__web_search") and leaves it out +// of tools until a ToolSearch loads it. The run's model calls it as told, +// under the full name, and the client is handed that name, which it runs: +// not the bare magpie-web-search__web_search it has no tool of, which it +// answered "Error: No such tool available: magpie-web-search__web_search" +// before the model retried (packinggoods, #feedback 11-02). A tool it +// declared still comes back under its declared name. +func TestClaudeBridgeHandsOverAnUnloadedMagpieTool(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("a shell script stands in for Claude Code") + } + dir := t.TempDir() + script := `#!/bin/sh +while read -r line; do + case "$line" in + *call-search*) name=mcp__magpie__magpie-web-search__web_search ;; + *call-browser*) name=mcp__magpie__magpie-browser__browser_click ;; + *) name=mcp__magpie__Read ;; + esac + echo '{"type":"stream_event","event":{"type":"message_start","message":{"id":"m","model":"claude-sonnet-5","usage":{"input_tokens":1}}}}' + echo '{"type":"stream_event","event":{"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"toolu_'$$'","name":"'$name'"}}}' + echo '{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"{\"query\":\"magpie\"}"}}}' + echo '{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"tool_use"},"usage":{"output_tokens":3}}}' + echo '{"type":"stream_event","event":{"type":"message_stop"}}' +done +` + testenv.Program(t, filepath.Join(dir, "claude"), script) + t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) + s := New() + t.Cleanup(s.subscription.abortAll) + + // the client's tools: ToolSearch, Read and the one MCP tool it loaded; + // web_search is announced in the conversation only + tools := `[{"name":"ToolSearch","input_schema":{"type":"object"}},{"name":"Read","input_schema":{"type":"object"}},{"name":"mcp__magpie__magpie-browser__browser_click","input_schema":{"type":"object"}}]` + announced := "\nThe following deferred tools are now available via ToolSearch. Their schemas are NOT loaded — calling them directly will fail with InputValidationError. Use ToolSearch with query \"select:[,...]\" to load tool schemas before calling them:\nmcp__magpie__magpie-web-search__web_search\n\n" + p := provider.Provider{ID: "claude", Account: &provider.Account{Agent: "claude", User: "u"}} + call := func(what string) string { + t.Helper() + body := `{"model":"claude-sonnet-5","max_tokens":100,"tools":` + tools + `,"messages":[{"role":"user","content":` + strconv.Quote(announced+what) + `}]}` + rec := httptest.NewRecorder() + var u Usage + req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)) + if code, msg := s.serveClaudeSubscription(rec, req, provider.Anthropic, p, "claude-sonnet-5", []byte(body), &u); code != 200 { + t.Fatalf("%s: %d %s", what, code, msg) + } + var res struct { + Content []struct { + Type string `json:"type"` + Name string `json:"name"` + } `json:"content"` + } + json.Unmarshal(rec.Body.Bytes(), &res) + for _, c := range res.Content { + if c.Type == "tool_use" { + return c.Name + } + } + t.Fatalf("%s: no tool call in %s", what, rec.Body) + return "" + } + for what, want := range map[string]string{ + "call-search: search the web for magpie": "mcp__magpie__magpie-web-search__web_search", + "call-browser: click the button": "mcp__magpie__magpie-browser__browser_click", + "read a file": "Read", + } { + if got := call(what); got != want { + t.Errorf("%s: the client is handed a call of %q, want %q", what, got, want) + } + } +}