diff --git a/internal/gateway/hub_mcp.go b/internal/gateway/hub_mcp.go index ce38b74f8..4c174ce19 100644 --- a/internal/gateway/hub_mcp.go +++ b/internal/gateway/hub_mcp.go @@ -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, __, +// 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. diff --git a/internal/gateway/hub_mcp_test.go b/internal/gateway/hub_mcp_test.go index f43715c78..71f953afe 100644 --- a/internal/gateway/hub_mcp_test.go +++ b/internal/gateway/hub_mcp_test.go @@ -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.