mcp router: a tool its server no longer lists still goes to that server

A Claude Code session listed Magpie Browser's tools before magpie updated
to the one browser tool (7e201ce20). Its call to
magpie-browser__browser_tabs was answered "unknown tool …: no MCP server
switched on for claude in magpie's Library gives it", although
magpie-browser was switched on for claude and still takes the old name.
The agent read that as the server being off and went to another browser
tool. tools/call matched only the names listed at the moment of the call,
so a tool renamed since the agent listed, or a server whose list failed
just then, read as "not switched on".

Now a name that matches no listed tool goes to the server it names,
<server>__<tool> for a server switched on for the agent (Servers, which
starts nothing), and that server runs or refuses it. Two servers sharing
one name in the hub's characters, another agent's server, and a name no
server starts with are still unknown.

Run on mote at d29f02d65 plus this change:
- go test -tags nogui -run TestHubMCP ./internal/gateway/: ok.
  TestHubMCPCallsAToolNoLongerListed went red with the Servers lookup
  taken out by hand (browser_tabs and my-fs__read unknown).
- A sandbox gateway (HOME on mote, MAGPIE_ADDR 127.0.0.1:3471) with the
  real `magpie mcp browser` behind the hub: tools/list gives
  magpie-browser__browser; tools/call magpie-browser__browser_tabs now
  reaches the server and gets its own answer, the same as the new name
  (no browser connected there); magpie-browser__nope gets the server's
  "unknown tool nope".
- go test -tags nogui -timeout 45m ./internal/gateway/ ./internal/library/
  ./internal/browsermcp/: library and browsermcp ok; gateway red only on
  TestCodexPoolSaysTheClientsVersion, #4807 (another test's Codex
  identity, passes alone), not touched here.
- gofmt clean; go vet -tags nogui ./internal/gateway/ for darwin, linux
  and windows ok.
Not tried: the user's own Claude Code session against the released build
with a browser connected.
This commit is contained in:
yetone
2026-11-02 22:02:50 +08:00
parent 17c465375d
commit a3768cdd11
2 changed files with 84 additions and 8 deletions
+43 -8
View File
@@ -512,15 +512,15 @@ func (s *Server) hubMCPCall(r *http.Request, agent string, m mcpMessage) (any, m
if MCPHub == nil || !MCPHub.On() {
return toolResult(errHubOff, true), nil
}
for _, t := range hubTools(r.Context(), agent) {
if t.name == p.Name {
c := HubCaller{Session: hubSession(r, agent, p.Meta)}
if t.Roots {
c.Roots, c.Client = hubRoots(r, p.Meta), hubClient(r)
}
r = r.WithContext(WithHubCaller(r.Context(), c))
return s.hubMCPRun(r, agent, t, p.Arguments), nil
ts := hubTools(r.Context(), agent)
t, ok := hubToolNamed(ts, agent, p.Name)
if ok {
c := HubCaller{Session: hubSession(r, agent, p.Meta)}
if t.Roots {
c.Roots, c.Client = hubRoots(r, p.Meta), hubClient(r)
}
r = r.WithContext(WithHubCaller(r.Context(), c))
return s.hubMCPRun(r, agent, t, p.Arguments), nil
}
if agent == "" {
return nil, map[string]any{"code": -32602, "message": "unknown tool " + p.Name + ": no MCP server in magpie's Library gives it"}
@@ -530,6 +530,41 @@ func (s *Server) hubMCPCall(r *http.Request, agent string, m mcpMessage) (any, m
return nil, map[string]any{"code": -32601, "message": "method not found: " + m.Method}
}
// hubToolNamed is the tool name calls: one ts lists, or else a tool of a
// server switched on for agent that its name starts with, <server>__<tool>,
// which the server itself is asked for. An agent lists the tools once, and
// a server's list can change after: magpie updated, Magpie Browser's
// browser_tabs became the browser tool's tabs action, and it still takes
// the old name (7e201ce20). A server whose list failed just now is asked
// the same way, and either says why it can't run the tool.
func hubToolNamed(ts []hubTool, agent, name string) (hubTool, bool) {
for _, t := range ts {
if t.name == name {
return t, true
}
}
var found []hubTool
for _, sv := range MCPHub.Servers(agent) {
tool, ok := strings.CutPrefix(name, unsafeToolChars.ReplaceAllString(sv.Name, "-")+hubSep)
if !ok || tool == "" {
continue
}
t := hubTool{HubTool: HubTool{Server: sv.Name, Tool: tool}, name: name}
for _, l := range ts {
if l.Server == sv.Name {
t.Roots = l.Roots
break
}
}
found = append(found, t)
}
// two servers of one name in the hub's characters: which is meant isn't known
if len(found) != 1 {
return hubTool{}, false
}
return found[0], true
}
// hubMCPRun runs one tool on its server, held to the calling gateway key's
// limit. A server that is down or doesn't answer in time is an error
// result naming it, never a wait past the client's.
+41
View File
@@ -162,6 +162,47 @@ func TestHubMCPListsAndRoutes(t *testing.T) {
}
}
// A tool name the agent listed before its server's list changed (Magpie
// Browser's browser_tabs, now the browser tool's tabs action) or while
// the server's list failed still goes to that server when it is switched
// on for the agent, for the server to run or refuse; another agent's
// server, or one no server's name starts, is still unknown.
func TestHubMCPCallsAToolNoLongerListed(t *testing.T) {
fresh(t)
f := &fakeHub{on: true,
tools: map[string][]HubTool{
"claude": {{Server: "magpie-browser", Tool: "browser", Spec: hubSpec("browser")}},
},
servers: map[string][]HubServer{
"claude": {{Name: "magpie-browser"}, {Name: "my.fs"}},
"codex": {{Name: "github"}},
},
}
withHub(t, f)
h := lanGuard(New().Handler())
for _, c := range []struct{ name, ran string }{
{"magpie-browser__browser_tabs", "ran magpie-browser browser_tabs"},
{"magpie-browser__browser", "ran magpie-browser browser"},
// listed nothing just now, and its name in the hub's characters
{"my-fs__read", "ran my.fs read"},
} {
m := readMCP(t, hubPost(h, "claude", here, toolsCall(c.name, `{"action":"tabs"}`)))
if m.Error != nil || m.Result.IsError || m.Result.Content[0].Text != c.ran {
t.Errorf("%s: %+v", c.name, m)
}
}
for _, c := range []struct{ agent, name string }{{"claude", "github__create_issue"}, {"claude", "magpie-browser__"}, {"codex", "magpie-browser__browser_tabs"}, {"claude", "nope__x"}} {
m := readMCP(t, hubPost(h, c.agent, here, toolsCall(c.name, `{}`)))
if m.Error == nil || m.Error.Code != -32602 || !strings.Contains(m.Error.Message, "unknown tool "+c.name) {
t.Errorf("%s's %s: %+v", c.agent, c.name, m)
}
}
want := `claude magpie-browser browser_tabs {"action":"tabs"}|claude magpie-browser browser {"action":"tabs"}|claude my.fs read {"action":"tabs"}`
if got := strings.Join(f.ran(), "|"); got != want {
t.Errorf("ran %q, want %q", got, want)
}
}
// HubMCPAll, the one address any other app is given, is the hub for agent
// "": every server in the Library, with its own instructions and its own
// word for a tool it doesn't have; an agent's path still lists only its own.