Files
magpie/internal/gateway/claude_unloaded_tool_test.go
T
yetone 0f8c3d8672 claude: a deferred built-in the bridge's model calls reaches the client as WebFetch, not mcp__magpie__WebFetch
Claude Code with tool search on leaves WebFetch, WebSearch, CronCreate and
the other deferred built-ins out of tools until a ToolSearch loads them.
The run's model, offered the client's tools as mcp__magpie__<name>, called
mcp__magpie__WebFetch, and toolStart handed that over whole: the 11-02
fix for an unloaded magpie MCP tool (packinggoods) kept the full name for
every cut name the client hadn't declared. The client answered "No such
tool available: mcp__magpie__WebFetch", the model retried, and
status lines such as claude-hud showed a "magpie" MCP failure.

toolStart now keeps the prefix only when the cut name has the router's
form, <server>__<tool> (magpieServerTool): the client names magpie's MCP
tools mcp__magpie__<server>__<tool>, and its built-ins (WebFetch,
WebSearch, CronCreate, CronDelete, CronList, NotebookEdit, Monitor,
TaskStop, SendMessage, EnterWorktree, ExitWorktree: the deferred list of
a real Claude Code session) have no __ and no prefix. A sibling the same
line got wrong: another MCP server's deferred tool, called as
mcp__magpie__mcp__linear__get_issue, is handed over as
mcp__linear__get_issue, the client's name (it was handed over whole too).
toolStart is the one place a called name is mapped back for the client:
streamed and whole messages and the interactive mode's early call
(r.onCall) all go through it; mcpCall's declaredName serves offered
tools only, and the history's names (bridgeName) are unchanged.

Run (on mote, at this tree):
- go test ./internal/gateway -count=1 -timeout 45m: ok
- gofmt -l: nothing; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: ok; go build: ok
Broken by hand: magpieServerTool returning true (the old behaviour)
turns TestClaudeBridgeHandsOverAnUnloadedMagpieTool red on the new
WebFetch and mcp__linear__ cases ("handed a call of
mcp__magpie__WebFetch, want WebFetch").
Not tried: a real Claude Code client with tool search on (the reporter's
Windows setup); a deferred tool the run's model calls by the bare client
name (WebFetch) is still the run's Claude Code's to answer and isn't
handed over. A client MCP server of its own named "magpie" with a tool
lacking __ would now be cut; magpie's router names every tool
<server>__<tool>.
Rebased on eadc6cdb9 (GUI routing files only); at the pushed head
go test ./internal/gateway -run 'TestClaudeBridge|TestClaudeToolChoice|
BridgeToolName': ok.
Full suite: not run at push.

Reported-by: steven_4422 (Discord)
2026-11-08 16:05:42 +08:00

98 lines
4.6 KiB
Go

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 deferred
// built-in has no prefix on the client: called as mcp__magpie__WebFetch it
// is handed over as WebFetch, which the client answered "No such tool
// available: mcp__magpie__WebFetch" before (steven_4422, #feedback 11-08).
// Another MCP server's tool is handed over as the client names it,
// mcp__linear__…, and 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 ;;
*call-fetch*) name=mcp__magpie__WebFetch ;;
*call-linear*) name=mcp__magpie__mcp__linear__get_issue ;;
*) 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 := "<system-reminder>\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:<name>[,<name>...]\" to load tool schemas before calling them:\nmcp__magpie__magpie-web-search__web_search\nWebFetch\nmcp__linear__get_issue\n</system-reminder>\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",
"call-fetch: fetch usemagpie.ai": "WebFetch",
"call-linear: read issue MAG-1": "mcp__linear__get_issue",
"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)
}
}
}