Skip to content

Agent Mesh

add mcp prompts and pagination

Antonio Ojea94b96527beab
diff --git a/cmd/sam-node/mcp.go b/cmd/sam-node/mcp.go
index 6790efa9..78e3e57f 100644
--- a/cmd/sam-node/mcp.go
+++ b/cmd/sam-node/mcp.go
@@ -143,6 +143,87 @@ func NewMCPServer(node *SamNode) *mcp.Server {
Description: "Returns the last few lines of the node's log output.",
}, node.handleGetRecentLogs)
+ // Add the find_remote_resources tool.
+ mcp.AddTool(mcpServer, &mcp.Tool{
+ Name: "find_remote_resources",
+ Description: "Discover MCP resources available on hosted services across the mesh. Returns name + description per resource.",
+ }, node.handleFindRemoteResources)
+
+ // Add the read_remote_resource tool.
+ mcp.AddTool(mcpServer, &mcp.Tool{
+ Name: "read_remote_resource",
+ Description: "Read a specific resource from a remote peer.",
+ }, node.handleReadRemoteResource)
+
+ // Add the find_remote_prompts tool.
+ mcp.AddTool(mcpServer, &mcp.Tool{
+ Name: "find_remote_prompts",
+ Description: "Discover MCP prompts available on hosted services across the mesh.",
+ }, node.handleFindRemotePrompts)
+
+ // Add the get_remote_prompt tool.
+ mcp.AddTool(mcpServer, &mcp.Tool{
+ Name: "get_remote_prompt",
+ Description: "Get a specific prompt from a remote peer.",
+ }, node.handleGetRemotePrompt)
+
+ // Add a local resource for mesh state
+ mcpServer.AddResource(&mcp.Resource{
+ URI: "mesh://state",
+ Name: "Mesh State",
+ Description: "Current state of the SAM node and mesh connections",
+ MIMEType: "application/json",
+ }, func(ctx context.Context, req *mcp.ReadResourceRequest) (*mcp.ReadResourceResult, error) {
+ peers := node.Host.Network().Peers()
+ connectedPeers := make([]string, 0, len(peers))
+ for _, p := range peers {
+ connectedPeers = append(connectedPeers, p.String())
+ }
+ resData := map[string]any{
+ "connected_peers": connectedPeers,
+ "dht_size": node.DHT.RoutingTable().Size(),
+ "hub_peer_id": node.HubPeerID.String(),
+ "peer_id": node.Host.ID().String(),
+ }
+ data, _ := json.MarshalIndent(resData, "", " ")
+ return &mcp.ReadResourceResult{
+ Contents: []*mcp.ResourceContents{
+ {
+ URI: "mesh://state",
+ MIMEType: "application/json",
+ Text: string(data),
+ },
+ },
+ }, nil
+ })
+
+ // Add a local prompt
+ mcpServer.AddPrompt(&mcp.Prompt{
+ Name: "mesh_debugging",
+ Description: "A standard prompt for debugging mesh connectivity issues",
+ Arguments: []*mcp.PromptArgument{
+ {
+ Name: "peer_id",
+ Description: "The ID of the peer you are trying to reach",
+ Required: true,
+ },
+ },
+ }, func(ctx context.Context, req *mcp.GetPromptRequest) (*mcp.GetPromptResult, error) {
+ peerID, _ := req.Params.Arguments["peer_id"]
+ text := fmt.Sprintf("I am trying to debug connectivity to peer %s. Please check the local mesh state using the mesh://state resource, and then use the check_connectivity tool to diagnose the network path. Finally, use discover_remote_services to see if they are advertising the expected service.", peerID)
+ return &mcp.GetPromptResult{
+ Description: "Debug mesh connectivity",
+ Messages: []*mcp.PromptMessage{
+ {
+ Role: "user",
+ Content: &mcp.TextContent{
+ Text: text,
+ },
+ },
+ },
+ }, nil
+ })
+
return mcpServer
}
diff --git a/cmd/sam-node/mcp_handlers.go b/cmd/sam-node/mcp_handlers.go
index e6dd5a78..49ede545 100644
--- a/cmd/sam-node/mcp_handlers.go
+++ b/cmd/sam-node/mcp_handlers.go
@@ -1,9 +1,24 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
package main
import (
"context"
"encoding/json"
"fmt"
+ "strconv"
"strings"
"sync"
"time"
@@ -242,6 +257,7 @@ type FindRemoteToolsParams struct {
Intent string `json:"intent,omitempty" jsonschema:"Natural-language description of what the caller is looking for. Reserved for future semantic ranking; currently accepted but ignored."`
PeerID string `json:"peer_id,omitempty" jsonschema:"Restrict the search to a single peer. Empty means search the whole mesh."`
ServiceName string `json:"service_name,omitempty" jsonschema:"Restrict results to tools whose name starts with this service prefix (e.g. 'code-reviewer'). Empty means no service filter."`
+ Cursor string `json:"cursor,omitempty" jsonschema:"Optional pagination cursor. Pass the nextCursor from a previous response to get the next page."`
}
// remoteToolRow is one entry in the find_remote_tools response.
@@ -311,7 +327,20 @@ func (n *SamNode) handleFindRemoteTools(ctx context.Context, req *mcp.CallToolRe
if rows == nil {
rows = []remoteToolRow{}
}
- respData, err := json.Marshal(rows)
+
+ paginatedRows, nextCursor, err := PaginateSlice(rows, params.Cursor, 50)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ respObj := map[string]any{
+ "items": paginatedRows,
+ }
+ if nextCursor != "" {
+ respObj["nextCursor"] = nextCursor
+ }
+
+ respData, err := json.Marshal(respObj)
if err != nil {
return nil, nil, err
}
@@ -609,3 +638,445 @@ func (n *SamNode) handleGetRecentLogs(ctx context.Context, req *mcp.CallToolRequ
Content: []mcp.Content{&mcp.TextContent{Text: string(data)}},
}, nil, nil
}
+
+// FindRemoteResourcesParams defines the parameters for find_remote_resources.
+type FindRemoteResourcesParams struct {
+ PeerID string `json:"peer_id,omitempty" jsonschema:"Restrict the search to a single peer. Empty means search the whole mesh."`
+ ServiceName string `json:"service_name,omitempty" jsonschema:"Restrict results to resources whose name starts with this service prefix."`
+ Cursor string `json:"cursor,omitempty" jsonschema:"Optional pagination cursor."`
+}
+
+// remoteResourceRow is one entry in the find_remote_resources response.
+type remoteResourceRow struct {
+ PeerID string `json:"peer_id"`
+ ResourceURI string `json:"resource_uri"`
+ Name string `json:"name"`
+ Description string `json:"description"`
+}
+
+func (n *SamNode) handleFindRemoteResources(ctx context.Context, req *mcp.CallToolRequest, params FindRemoteResourcesParams) (*mcp.CallToolResult, any, error) {
+ selfID := n.Host.ID().String()
+ if params.PeerID != "" && params.PeerID == selfID {
+ return nil, nil, fmt.Errorf("peer_id %q is this node; cross-mesh discovery cannot target self", params.PeerID)
+ }
+
+ var rows []remoteResourceRow
+
+ if params.PeerID != "" {
+ pid, err := peer.Decode(params.PeerID)
+ if err != nil {
+ return nil, nil, fmt.Errorf("invalid peer_id %q: %w", params.PeerID, err)
+ }
+ resources, err := n.fetchRemoteResourceCatalogue(ctx, pid)
+ if err != nil {
+ return nil, nil, err
+ }
+ rows = appendFilteredResourceRows(rows, params.PeerID, resources, params.ServiceName)
+ } else {
+ providers, err := n.DiscoverRemoteServices(ctx, api.ServiceType_SERVICE_TYPE_MCP, "")
+ if err != nil {
+ return nil, nil, fmt.Errorf("discover providers: %w", err)
+ }
+ seen := map[string]bool{}
+ var peerIDs []peer.ID
+ for _, p := range providers {
+ if p.PeerId == selfID || seen[p.PeerId] {
+ continue
+ }
+ seen[p.PeerId] = true
+ pid, err := peer.Decode(p.PeerId)
+ if err != nil {
+ continue
+ }
+ peerIDs = append(peerIDs, pid)
+ }
+
+ rows = n.fanOutFetchResources(ctx, peerIDs, params.ServiceName)
+ }
+
+ if rows == nil {
+ rows = []remoteResourceRow{}
+ }
+
+ paginatedRows, nextCursor, err := PaginateSlice(rows, params.Cursor, 50)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ respObj := map[string]any{
+ "items": paginatedRows,
+ }
+ if nextCursor != "" {
+ respObj["nextCursor"] = nextCursor
+ }
+
+ respData, err := json.Marshal(respObj)
+ if err != nil {
+ return nil, nil, err
+ }
+ return &mcp.CallToolResult{
+ Content: []mcp.Content{&mcp.TextContent{Text: string(respData)}},
+ }, nil, nil
+}
+
+func (n *SamNode) fetchRemoteResourceCatalogue(ctx context.Context, targetPeer peer.ID) ([]*mcp.Resource, error) {
+ services, err := n.fetchRemoteServiceCatalog(ctx, targetPeer, "MCP")
+ if err != nil {
+ return nil, fmt.Errorf("fetch remote service catalog: %w", err)
+ }
+
+ var allResources []*mcp.Resource
+
+ for _, svc := range services {
+ if svc.Type != api.ServiceType_SERVICE_TYPE_MCP {
+ continue
+ }
+
+ n.preparePeerAddrs(ctx, targetPeer)
+ session, cleanup, err := n.ConnectMCPSession(ctx, targetPeer, svc.Name)
+ if err != nil {
+ continue
+ }
+
+ listRes, err := session.ListResources(ctx, &mcp.ListResourcesParams{})
+ if err == nil && listRes != nil {
+ for _, r := range listRes.Resources {
+ // Namespace the URI scheme or name if we want, but resources are URIs.
+ // We'll prefix the Name to indicate origin.
+ r.Name = svc.Name + "." + r.Name
+ allResources = append(allResources, r)
+ }
+ }
+ cleanup()
+ }
+
+ return allResources, nil
+}
+
+func appendFilteredResourceRows(rows []remoteResourceRow, peerID string, resources []*mcp.Resource, serviceName string) []remoteResourceRow {
+ for _, r := range resources {
+ if serviceName != "" && !strings.HasPrefix(r.Name, serviceName+".") {
+ continue
+ }
+ rows = append(rows, remoteResourceRow{
+ PeerID: peerID,
+ ResourceURI: r.URI,
+ Name: r.Name,
+ Description: r.Description,
+ })
+ }
+ return rows
+}
+
+func (n *SamNode) fanOutFetchResources(ctx context.Context, peers []peer.ID, serviceName string) []remoteResourceRow {
+ const maxConcurrent = 8
+ sem := make(chan struct{}, maxConcurrent)
+
+ var (
+ mu sync.Mutex
+ rows []remoteResourceRow
+ )
+
+ var wg sync.WaitGroup
+ for _, pid := range peers {
+ pid := pid
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ sem <- struct{}{}
+ defer func() { <-sem }()
+
+ peerCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
+ defer cancel()
+
+ resources, err := n.fetchRemoteResourceCatalogue(peerCtx, pid)
+ if err != nil {
+ return
+ }
+ mu.Lock()
+ rows = appendFilteredResourceRows(rows, pid.String(), resources, serviceName)
+ mu.Unlock()
+ }()
+ }
+ wg.Wait()
+ return rows
+}
+
+// ReadRemoteResourceParams defines the parameters for read_remote_resource.
+type ReadRemoteResourceParams struct {
+ PeerID string `json:"peer_id" jsonschema:"The Peer ID of the target agent"`
+ URI string `json:"uri" jsonschema:"The URI of the remote resource"`
+}
+
+func (n *SamNode) handleReadRemoteResource(ctx context.Context, req *mcp.CallToolRequest, params ReadRemoteResourceParams) (*mcp.CallToolResult, any, error) {
+ targetPeer, err := peer.Decode(params.PeerID)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ // Try all MCP services on the remote peer to see which one has the resource
+ services, err := n.fetchRemoteServiceCatalog(ctx, targetPeer, "MCP")
+ if err != nil {
+ return nil, nil, err
+ }
+
+ for _, svc := range services {
+ if svc.Type != api.ServiceType_SERVICE_TYPE_MCP {
+ continue
+ }
+
+ session, cleanup, err := n.ConnectMCPSession(ctx, targetPeer, svc.Name)
+ if err != nil {
+ continue
+ }
+
+ res, err := session.ReadResource(ctx, &mcp.ReadResourceParams{
+ URI: params.URI,
+ })
+
+ if err == nil && res != nil && len(res.Contents) > 0 {
+ defer cleanup()
+
+ // Marshal the contents
+ data, _ := json.Marshal(res.Contents)
+ return &mcp.CallToolResult{
+ Content: []mcp.Content{
+ &mcp.TextContent{Text: string(data)},
+ },
+ }, nil, nil
+ }
+ cleanup()
+ }
+
+ return nil, nil, fmt.Errorf("resource %s not found on peer %s", params.URI, params.PeerID)
+}
+
+// FindRemotePromptsParams defines the parameters for find_remote_prompts.
+type FindRemotePromptsParams struct {
+ PeerID string `json:"peer_id,omitempty" jsonschema:"Restrict the search to a single peer. Empty means search the whole mesh."`
+ ServiceName string `json:"service_name,omitempty" jsonschema:"Restrict results to prompts whose name starts with this service prefix."`
+ Cursor string `json:"cursor,omitempty" jsonschema:"Optional pagination cursor."`
+}
+
+// remotePromptRow is one entry in the find_remote_prompts response.
+type remotePromptRow struct {
+ PeerID string `json:"peer_id"`
+ Name string `json:"name"`
+ Description string `json:"description"`
+ Arguments []*mcp.PromptArgument `json:"arguments"`
+}
+
+func (n *SamNode) handleFindRemotePrompts(ctx context.Context, req *mcp.CallToolRequest, params FindRemotePromptsParams) (*mcp.CallToolResult, any, error) {
+ selfID := n.Host.ID().String()
+ if params.PeerID != "" && params.PeerID == selfID {
+ return nil, nil, fmt.Errorf("peer_id %q is this node; cross-mesh discovery cannot target self", params.PeerID)
+ }
+
+ var rows []remotePromptRow
+
+ if params.PeerID != "" {
+ pid, err := peer.Decode(params.PeerID)
+ if err != nil {
+ return nil, nil, fmt.Errorf("invalid peer_id %q: %w", params.PeerID, err)
+ }
+ prompts, err := n.fetchRemotePromptCatalogue(ctx, pid)
+ if err != nil {
+ return nil, nil, err
+ }
+ rows = appendFilteredPromptRows(rows, params.PeerID, prompts, params.ServiceName)
+ } else {
+ providers, err := n.DiscoverRemoteServices(ctx, api.ServiceType_SERVICE_TYPE_MCP, "")
+ if err != nil {
+ return nil, nil, fmt.Errorf("discover providers: %w", err)
+ }
+ seen := map[string]bool{}
+ var peerIDs []peer.ID
+ for _, p := range providers {
+ if p.PeerId == selfID || seen[p.PeerId] {
+ continue
+ }
+ seen[p.PeerId] = true
+ pid, err := peer.Decode(p.PeerId)
+ if err != nil {
+ continue
+ }
+ peerIDs = append(peerIDs, pid)
+ }
+
+ rows = n.fanOutFetchPrompts(ctx, peerIDs, params.ServiceName)
+ }
+
+ if rows == nil {
+ rows = []remotePromptRow{}
+ }
+
+ paginatedRows, nextCursor, err := PaginateSlice(rows, params.Cursor, 50)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ respObj := map[string]any{
+ "items": paginatedRows,
+ }
+ if nextCursor != "" {
+ respObj["nextCursor"] = nextCursor
+ }
+
+ respData, err := json.Marshal(respObj)
+ if err != nil {
+ return nil, nil, err
+ }
+ return &mcp.CallToolResult{
+ Content: []mcp.Content{&mcp.TextContent{Text: string(respData)}},
+ }, nil, nil
+}
+
+func (n *SamNode) fetchRemotePromptCatalogue(ctx context.Context, targetPeer peer.ID) ([]*mcp.Prompt, error) {
+ services, err := n.fetchRemoteServiceCatalog(ctx, targetPeer, "MCP")
+ if err != nil {
+ return nil, fmt.Errorf("fetch remote service catalog: %w", err)
+ }
+
+ var allPrompts []*mcp.Prompt
+
+ for _, svc := range services {
+ if svc.Type != api.ServiceType_SERVICE_TYPE_MCP {
+ continue
+ }
+
+ n.preparePeerAddrs(ctx, targetPeer)
+ session, cleanup, err := n.ConnectMCPSession(ctx, targetPeer, svc.Name)
+ if err != nil {
+ continue
+ }
+
+ listRes, err := session.ListPrompts(ctx, &mcp.ListPromptsParams{})
+ if err == nil && listRes != nil {
+ for _, p := range listRes.Prompts {
+ p.Name = svc.Name + "." + p.Name
+ allPrompts = append(allPrompts, p)
+ }
+ }
+ cleanup()
+ }
+
+ return allPrompts, nil
+}
+
+func appendFilteredPromptRows(rows []remotePromptRow, peerID string, prompts []*mcp.Prompt, serviceName string) []remotePromptRow {
+ for _, p := range prompts {
+ if serviceName != "" && !strings.HasPrefix(p.Name, serviceName+".") {
+ continue
+ }
+ rows = append(rows, remotePromptRow{
+ PeerID: peerID,
+ Name: p.Name,
+ Description: p.Description,
+ Arguments: p.Arguments,
+ })
+ }
+ return rows
+}
+
+func (n *SamNode) fanOutFetchPrompts(ctx context.Context, peers []peer.ID, serviceName string) []remotePromptRow {
+ const maxConcurrent = 8
+ sem := make(chan struct{}, maxConcurrent)
+
+ var (
+ mu sync.Mutex
+ rows []remotePromptRow
+ )
+
+ var wg sync.WaitGroup
+ for _, pid := range peers {
+ pid := pid
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ sem <- struct{}{}
+ defer func() { <-sem }()
+
+ peerCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
+ defer cancel()
+
+ prompts, err := n.fetchRemotePromptCatalogue(peerCtx, pid)
+ if err != nil {
+ return
+ }
+ mu.Lock()
+ rows = appendFilteredPromptRows(rows, pid.String(), prompts, serviceName)
+ mu.Unlock()
+ }()
+ }
+ wg.Wait()
+ return rows
+}
+
+// GetRemotePromptParams defines the parameters for get_remote_prompt.
+type GetRemotePromptParams struct {
+ PeerID string `json:"peer_id" jsonschema:"The Peer ID of the target agent"`
+ Name string `json:"name" jsonschema:"The namespaced name of the remote prompt (e.g. 'service.prompt')"`
+ Arguments map[string]string `json:"arguments,omitempty" jsonschema:"Arguments to pass to the prompt"`
+}
+
+func (n *SamNode) handleGetRemotePrompt(ctx context.Context, req *mcp.CallToolRequest, params GetRemotePromptParams) (*mcp.CallToolResult, any, error) {
+ targetPeer, err := peer.Decode(params.PeerID)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ targetService := api.CatalogTarget
+ originalPromptName := params.Name
+ if parts := strings.SplitN(params.Name, ".", 2); len(parts) == 2 {
+ targetService = parts[0]
+ originalPromptName = parts[1]
+ }
+
+ session, cleanup, err := n.ConnectMCPSession(ctx, targetPeer, targetService)
+ if err != nil {
+ return nil, nil, err
+ }
+ defer cleanup()
+
+ res, err := session.GetPrompt(ctx, &mcp.GetPromptParams{
+ Name: originalPromptName,
+ Arguments: params.Arguments,
+ })
+
+ if err != nil {
+ return nil, nil, fmt.Errorf("failed to get prompt %s: %w", params.Name, err)
+ }
+
+ data, _ := json.Marshal(res)
+ return &mcp.CallToolResult{
+ Content: []mcp.Content{
+ &mcp.TextContent{Text: string(data)},
+ },
+ }, nil, nil
+}
+
+// PaginateSlice ...
+func PaginateSlice[T any](items []T, cursor string, limit int) ([]T, string, error) {
+ if limit <= 0 {
+ limit = 50
+ }
+ startIdx := 0
+ if cursor != "" {
+ idx, err := strconv.Atoi(cursor)
+ if err != nil {
+ return nil, "", err
+ }
+ startIdx = idx
+ }
+ if startIdx >= len(items) {
+ return []T{}, "", nil
+ }
+ endIdx := startIdx + limit
+ nextCursor := ""
+ if endIdx < len(items) {
+ nextCursor = strconv.Itoa(endIdx)
+ } else {
+ endIdx = len(items)
+ }
+ return items[startIdx:endIdx], nextCursor, nil
+}
diff --git a/cmd/sam-node/mcp_handlers_test.go b/cmd/sam-node/mcp_handlers_test.go
index 05db489f..8131244a 100644
--- a/cmd/sam-node/mcp_handlers_test.go
+++ b/cmd/sam-node/mcp_handlers_test.go
@@ -72,12 +72,14 @@ func TestHandleFindRemoteTools_EmptyMesh_ReturnsEmptyArray(t *testing.T) {
if !ok {
t.Fatalf("expected TextContent, got %T", res.Content[0])
}
- var rows []map[string]any
- if err := json.Unmarshal([]byte(tc.Text), &rows); err != nil {
- t.Fatalf("response not JSON array: %v (text: %q)", err, tc.Text)
+ var resp struct {
+ Items []map[string]any `json:"items"`
+ }
+ if err := json.Unmarshal([]byte(tc.Text), &resp); err != nil {
+ t.Fatalf("response not JSON object: %v (text: %q)", err, tc.Text)
}
- if len(rows) != 0 {
- t.Errorf("expected empty results for empty mesh, got %d rows", len(rows))
+ if len(resp.Items) != 0 {
+ t.Errorf("expected empty results for empty mesh, got %d rows", len(resp.Items))
}
}
@@ -154,8 +156,10 @@ func TestHandleFindRemoteTools_SinglePeer(t *testing.T) {
if !ok {
t.Fatalf("expected TextContent, got %T", res.Content[0])
}
- var rows []remoteToolRow
- if err := json.Unmarshal([]byte(tc.Text), &rows); err != nil {
+ var resp struct {
+ Items []remoteToolRow `json:"items"`
+ }
+ if err := json.Unmarshal([]byte(tc.Text), &resp); err != nil {
t.Fatalf("unmarshal: %v (text: %q)", err, tc.Text)
}
@@ -163,7 +167,7 @@ func TestHandleFindRemoteTools_SinglePeer(t *testing.T) {
"code-reviewer.review_pr": false,
"code-reviewer.add_comment": false,
}
- for _, row := range rows {
+ for _, row := range resp.Items {
if row.PeerID != nodeB.Host.ID().String() {
t.Errorf("row has peer_id %q, want %q", row.PeerID, nodeB.Host.ID().String())
}
@@ -173,7 +177,7 @@ func TestHandleFindRemoteTools_SinglePeer(t *testing.T) {
}
for name, found := range wantNames {
if !found {
- t.Errorf("expected tool %q in response, not found; rows=%+v", name, rows)
+ t.Errorf("expected tool %q in response, not found; rows=%+v", name, resp.Items)
}
}
}
diff --git a/tests/e2e/find_remote_tools.bats b/tests/e2e/find_remote_tools.bats
index 19eb0ea2..35a31731 100644
--- a/tests/e2e/find_remote_tools.bats
+++ b/tests/e2e/find_remote_tools.bats
@@ -79,7 +79,7 @@ teardown() {
local match_count
match_count=$(echo "$catalog" | jq --arg pid "${node2_peer_id}" '
- [.[] | select(.peer_id == $pid
+ [.items[] | select(.peer_id == $pid
and (.tool_name | startswith("calculator.")))] | length
')
echo "Matching calculator tool entries: ${match_count}"