Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
fix: canonicalise the service_name filter, and reject a wrong scheme
Antonio Ojea17f620c98894
diff --git a/agents/skills/sam-mesh/SKILL.md b/agents/skills/sam-mesh/SKILL.mdindex c5f1540d..d945f7f7 100644--- a/agents/skills/sam-mesh/SKILL.md+++ b/agents/skills/sam-mesh/SKILL.md@@ -164,7 +164,8 @@ explicitly authorized.Use `call_remote_tool` with:- `peer_id`: the peer hosting the tool-- `tool_name`: the discovered namespaced tool name, such as `service.tool`+- `tool_name`: the discovered namespaced tool name, such as+ `mcp://everything/get-sum`- `arguments`: a JSON object whose keys match the described input schema`arguments` must be a JSON object, not a string containing JSON.diff --git a/internal/node/mcp_discovery_test.go b/internal/node/mcp_discovery_test.goindex 90c680fd..cf53778c 100644--- a/internal/node/mcp_discovery_test.go+++ b/internal/node/mcp_discovery_test.go@@ -142,12 +142,65 @@ func TestGossipToolRows(t *testing.T) {t.Errorf("row 1 should have no labels: %+v", rows[1])}- filtered := gossipToolRows(provs, "review_pr", "reviewer")+ filtered := gossipToolRows(provs, "review_pr", "mcp://reviewer")if len(filtered) != 1 || filtered[0].PeerID != "peerA" {t.Errorf("service filter: got %+v", filtered)}}+// The tool schema documents service_name as a bare name ("code-reviewer") while+// tool names carry the mcp:// prefix. Matching the two literally made the+// documented spelling return nothing, which is indistinguishable from a mesh+// that has no such service.+func TestNormalizeServiceFilter(t *testing.T) {+ for _, tc := range []struct {+ name string+ in string+ want string+ wantErr bool+ }{+ {name: "empty is no filter", in: "", want: ""},+ {name: "bare name gains the scheme", in: "everything", want: "mcp://everything"},+ {name: "namespaced name is already canonical", in: "mcp://everything", want: "mcp://everything"},+ {name: "a non-MCP scheme is an error, not an empty result", in: "inference://vllm-tpu", wantErr: true},+ } {+ t.Run(tc.name, func(t *testing.T) {+ got, err := normalizeServiceFilter(tc.in)+ if tc.wantErr {+ if err == nil {+ t.Fatalf("normalizeServiceFilter(%q): want error, got %q", tc.in, got)+ }+ return+ }+ if err != nil {+ t.Fatalf("normalizeServiceFilter(%q): %v", tc.in, err)+ }+ if got != tc.want {+ t.Errorf("normalizeServiceFilter(%q): got %q, want %q", tc.in, got, tc.want)+ }+ })+ }+}++// Providers announce a service either bare or namespaced; both denote the same+// service and must match one canonical filter.+func TestGossipToolRows_MatchesEitherAnnouncedForm(t *testing.T) {+ provs := []samdiscovery.Provider{+ {PeerID: "peerA", Service: "everything"},+ {PeerID: "peerB", Service: "mcp://everything"},+ {PeerID: "peerC", Service: "other"},+ }+ rows := gossipToolRows(provs, "get-sum", "mcp://everything")+ if len(rows) != 2 {+ t.Fatalf("got %d rows, want 2: %+v", len(rows), rows)+ }+ for _, row := range rows {+ if row.ToolName != "mcp://everything/get-sum" {+ t.Errorf("got %q", row.ToolName)+ }+ }+}+func TestFilterRowsByToolName(t *testing.T) {rows := []remoteToolRow{{PeerID: "p1", ToolName: "mcp://svc/review_pr"},diff --git a/internal/node/mcp_handlers.go b/internal/node/mcp_handlers.goindex 1cde1ad8..030ce60e 100644--- a/internal/node/mcp_handlers.go+++ b/internal/node/mcp_handlers.go@@ -317,6 +317,7 @@ type remoteToolRow struct {//// Filtering:// - If params.ServiceName is set, only tools from that service are returned.+// Bare ("everything") and namespaced ("mcp://everything") forms are equivalent.// - params.Intent is accepted and logged at debug level, but does not// filter or rank results in this implementation (placeholder for// future semantic search).@@ -330,6 +331,11 @@ func (n *SamNode) handleFindRemoteTools(ctx context.Context, req *mcp.CallToolRereturn nil, nil, fmt.Errorf("peer_id %q is this node; cross-mesh discovery cannot target self", params.PeerID)}+ serviceFilter, err := normalizeServiceFilter(params.ServiceName)+ if err != nil {+ return nil, nil, err+ }+var rows []remoteToolRow// Exact-name lookup: gossip fast path, avoiding the catalog fan-out@@ -337,7 +343,7 @@ func (n *SamNode) handleFindRemoteTools(ctx context.Context, req *mcp.CallToolReif params.ToolName != "" && params.PeerID == "" && n.Discovery != nil {n.Discovery.Ensure(api.ServiceType_SERVICE_TYPE_MCP, params.ToolName)if provs := n.Discovery.Providers(api.ServiceType_SERVICE_TYPE_MCP, params.ToolName); len(provs) > 0 {- candidates := gossipToolRows(provs, params.ToolName, params.ServiceName)+ candidates := gossipToolRows(provs, params.ToolName, serviceFilter)rows = n.verifyGossipToolRows(ctx, candidates)if len(rows) > 0 {return marshalToolRows(rows)@@ -350,7 +356,7 @@ func (n *SamNode) handleFindRemoteTools(ctx context.Context, req *mcp.CallToolReif err != nil {return nil, nil, fmt.Errorf("invalid peer_id %q: %w", params.PeerID, err)}- peerRows, err := n.fetchRemoteToolCatalogue(ctx, pid, params.ServiceName)+ peerRows, err := n.fetchRemoteToolCatalogue(ctx, pid, serviceFilter)if err != nil {return nil, nil, err}@@ -374,7 +380,7 @@ func (n *SamNode) handleFindRemoteTools(ctx context.Context, req *mcp.CallToolRepeerIDs = append(peerIDs, pid)}- rows = n.fanOutFetch(ctx, peerIDs, params.ServiceName)+ rows = n.fanOutFetch(ctx, peerIDs, serviceFilter)}if params.ToolName != "" {@@ -398,17 +404,38 @@ func marshalToolRows(rows []remoteToolRow) (*mcp.CallToolResult, any, error) {}, nil, nil}+// normalizeServiceFilter canonicalises the service_name filter.+//+// This tool only ever searches MCP services, so the scheme is redundant and a+// bare "everything" is accepted alongside "mcp://everything". Any other scheme+// is an error rather than a filter that matches nothing: an empty result is+// already the honest answer for a service the mesh does not have, so it cannot+// also mean the caller misspelled one.+func normalizeServiceFilter(name string) (string, error) {+ switch {+ case name == "":+ return "", nil+ case !strings.Contains(name, "://"):+ return api.MCPServicePrefix + name, nil+ case strings.HasPrefix(name, api.MCPServicePrefix):+ return name, nil+ default:+ return "", fmt.Errorf("service_name %q: find_remote_tools searches MCP services only; pass a bare name such as \"everything\" or an %s name", name, api.MCPServicePrefix)+ }+}+// gossipToolRows builds result rows from gossip-observed providers of a tool.+// serviceNameFilter must already be canonical; see normalizeServiceFilter.func gossipToolRows(provs []samdiscovery.Provider, toolName, serviceNameFilter string) []remoteToolRow {var rows []remoteToolRowfor _, p := range provs {- if serviceNameFilter != "" && p.Service != serviceNameFilter {- continue- }name := p.Serviceif !strings.Contains(name, "://") {name = api.MCPServicePrefix + name}+ if serviceNameFilter != "" && name != serviceNameFilter {+ continue+ }rows = append(rows, remoteToolRow{PeerID: p.PeerID,ToolName: name + "/" + toolName,@@ -544,6 +571,7 @@ func (n *SamNode) annotateToolLabels(rows []remoteToolRow) {// fetchRemoteToolCatalogue gets the remote node's service catalogue,// then opens a separate libp2p stream to each MCP service to fetch its tools.+// serviceNameFilter must already be canonical; see normalizeServiceFilter.func (n *SamNode) fetchRemoteToolCatalogue(ctx context.Context, targetPeer peer.ID, serviceNameFilter string) ([]remoteToolRow, error) {services, err := n.fetchRemoteServiceCatalog(ctx, targetPeer, "MCP")if err != nil {@@ -573,7 +601,7 @@ func (n *SamNode) fetchRemoteToolCatalogue(ctx context.Context, targetPeer peer.continue}logger.Debugf("Failed to connect MCP session for service %s: %v", targetService, err)- if serviceNameFilter == "" || connectService == serviceNameFilter || strings.HasPrefix(connectService, serviceNameFilter+".") {+ if serviceNameFilter == "" || connectService == serviceNameFilter {rows = append(rows, remoteToolRow{PeerID: targetPeer.String(),ToolName: connectService,@@ -600,7 +628,7 @@ func (n *SamNode) fetchRemoteToolCatalogue(ctx context.Context, targetPeer peer.})}} else {- if serviceNameFilter == "" || targetService == serviceNameFilter || strings.HasPrefix(targetService, serviceNameFilter+".") {+ if serviceNameFilter == "" || connectService == serviceNameFilter {rows = append(rows, remoteToolRow{PeerID: targetPeer.String(),ToolName: targetService,