Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
bridge: expose send_agent_task and get_agent_task over stdio MCP
Tomas Tormo56c22514ad8c
diff --git a/cmd/sam-a2a-bridge/go.mod b/cmd/sam-a2a-bridge/go.modindex eff75ad6..f6866057 100644--- a/cmd/sam-a2a-bridge/go.mod+++ b/cmd/sam-a2a-bridge/go.mod@@ -2,9 +2,20 @@ module github.com/google/sam/cmd/sam-a2a-bridgego 1.25.7-require github.com/a2aproject/a2a-go/v2 v2.5.0+require (+ github.com/a2aproject/a2a-go/v2 v2.5.0+ github.com/modelcontextprotocol/go-sdk v1.7.0+)require (+ github.com/google/jsonschema-go v0.4.3 // indirectgithub.com/google/uuid v1.6.0 // indirect+ github.com/segmentio/asm v1.1.3 // indirect+ github.com/segmentio/encoding v0.5.4 // indirect+ github.com/yosida95/uritemplate/v3 v3.0.2 // indirectgolang.org/x/mod v0.35.0 // indirect+ golang.org/x/oauth2 v0.35.0 // indirect+ golang.org/x/sync v0.20.0 // indirect+ golang.org/x/sys v0.45.0 // indirect+ golang.org/x/time v0.15.0 // indirect)diff --git a/cmd/sam-a2a-bridge/go.sum b/cmd/sam-a2a-bridge/go.sumindex 799e4584..f039c34b 100644--- a/cmd/sam-a2a-bridge/go.sum+++ b/cmd/sam-a2a-bridge/go.sum@@ -1,10 +1,30 @@github.com/a2aproject/a2a-go/v2 v2.5.0 h1:ZdcFoxv+nZTUV0i2ue5hES76YCANFPG9vjqd7vK8yWM=github.com/a2aproject/a2a-go/v2 v2.5.0/go.mod h1:NcRp/ZHxgMzDj12/BteIC2gOjljuEBKaGRfEdJ2lNSI=+github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=+github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=+github.com/google/jsonschema-go v0.4.3 h1:/DBOLZTfDow7pe2GmaJNhltueGTtDKICi8V8p+DQPd0=+github.com/google/jsonschema-go v0.4.3/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE=github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=+github.com/modelcontextprotocol/go-sdk v1.7.0 h1:yqjY2dsbKAC0LSuWZVBMrHgiG8ukXv6NRo0JiALay44=+github.com/modelcontextprotocol/go-sdk v1.7.0/go.mod h1:dL7u98E/zjJTGzEq+j30jQ8K2k1mb6LeAH4inEcSGts=+github.com/segmentio/asm v1.1.3 h1:WM03sfUOENvvKexOLp+pCqgb/WDjsi7EK8gIsICtzhc=+github.com/segmentio/asm v1.1.3/go.mod h1:Ld3L4ZXGNcSLRg4JBsZ3//1+f/TjYl0Mzen/DQy1EJg=+github.com/segmentio/encoding v0.5.4 h1:OW1VRern8Nw6ITAtwSZ7Idrl3MXCFwXHPgqESYfvNt0=+github.com/segmentio/encoding v0.5.4/go.mod h1:HS1ZKa3kSN32ZHVZ7ZLPLXWvOVIiZtyJnO1gPH1sKt0=+github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=+github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=golang.org/x/mod v0.35.0 h1:Ww1D637e6Pg+Zb2KrWfHQUnH2dQRLBQyAtpr/haaJeM=golang.org/x/mod v0.35.0/go.mod h1:+GwiRhIInF8wPm+4AoT6L0FA1QWAad3OMdTRx4tFYlU=+golang.org/x/oauth2 v0.35.0 h1:Mv2mzuHuZuY2+bkyWXIHMfhNdJAdwW3FuWeCPYN5GVQ=+golang.org/x/oauth2 v0.35.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=+golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY=+golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=+golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=+golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=+golang.org/x/tools v0.43.0 h1:12BdW9CeB3Z+J/I/wj34VMl8X+fEXBxVR90JeMX5E7s=+golang.org/x/tools v0.43.0/go.mod h1:uHkMso649BX2cZK6+RpuIPXS3ho2hZo4FVwfoy1vIk0=diff --git a/cmd/sam-a2a-bridge/main.go b/cmd/sam-a2a-bridge/main.gonew file mode 100644index 00000000..1821379d--- /dev/null+++ b/cmd/sam-a2a-bridge/main.go@@ -0,0 +1,95 @@+// 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"+ "errors"+ "flag"+ "log"++ "github.com/modelcontextprotocol/go-sdk/mcp"+)++func main() {+ sidecarURL := flag.String("url", "http://localhost:8080", "Sidecar API base URL")+ token := flag.String("token", "", "Authorization Bearer token for protected sidecar endpoints")+ flag.Parse()++ cfg := bridgeConfig{sidecarURL: *sidecarURL, token: *token}+ if err := newBridgeServer(cfg).Run(context.Background(), &mcp.StdioTransport{}); err != nil {+ log.Fatal(err)+ }+}++func newBridgeServer(cfg bridgeConfig) *mcp.Server {+ server := mcp.NewServer(&mcp.Implementation{+ Name: "sam-a2a-bridge",+ Version: "0.1.0",+ }, nil)++ mcp.AddTool(server, &mcp.Tool{+ Name: "send_agent_task",+ Description: "Send a text message to an A2A agent on the SAM mesh. Returns immediately with " ++ "{task_id, context_id, state, text}; poll non-terminal states with get_agent_task. " ++ "Pass context_id to continue a conversation, task_id to reply into a task waiting for input. " ++ "required_labels makes the local node refuse fail-closed unless the provider attested them.",+ }, handleSendAgentTask(cfg))++ mcp.AddTool(server, &mcp.Tool{+ Name: "get_agent_task",+ Description: "Fetch the current state and output of a task previously created with send_agent_task.",+ }, handleGetAgentTask(cfg))++ return server+}++func handleSendAgentTask(cfg bridgeConfig) func(context.Context, *mcp.CallToolRequest, sendAgentTaskParams) (*mcp.CallToolResult, any, error) {+ return func(ctx context.Context, _ *mcp.CallToolRequest, p sendAgentTaskParams) (*mcp.CallToolResult, any, error) {+ res, err := sendAgentTask(ctx, cfg, p)+ return toolResult(res, err)+ }+}++func handleGetAgentTask(cfg bridgeConfig) func(context.Context, *mcp.CallToolRequest, getAgentTaskParams) (*mcp.CallToolResult, any, error) {+ return func(ctx context.Context, _ *mcp.CallToolRequest, p getAgentTaskParams) (*mcp.CallToolResult, any, error) {+ res, err := getAgentTask(ctx, cfg, p)+ return toolResult(res, err)+ }+}++// toolResult renders errors as tool errors (not protocol errors) so the+// harness model sees refusal text like the labels-gate 403 verbatim.+func toolResult(res taskResult, err error) (*mcp.CallToolResult, any, error) {+ if err != nil {+ text := err.Error()+ var se *sidecarError+ if errors.As(err, &se) {+ text = se.Error()+ }+ return &mcp.CallToolResult{+ IsError: true,+ Content: []mcp.Content{&mcp.TextContent{Text: text}},+ }, nil, nil+ }+ out, err := json.Marshal(res)+ if err != nil {+ return nil, nil, err+ }+ return &mcp.CallToolResult{+ Content: []mcp.Content{&mcp.TextContent{Text: string(out)}},+ }, nil, nil+}diff --git a/cmd/sam-a2a-bridge/main_test.go b/cmd/sam-a2a-bridge/main_test.gonew file mode 100644index 00000000..ad00c7f2--- /dev/null+++ b/cmd/sam-a2a-bridge/main_test.go@@ -0,0 +1,103 @@+// 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"+ "net/http"+ "net/http/httptest"+ "strings"+ "testing"++ "github.com/modelcontextprotocol/go-sdk/mcp"+)++func textContent(t *testing.T, res *mcp.CallToolResult) string {+ t.Helper()+ if len(res.Content) != 1 {+ t.Fatalf("content items = %d, want 1", len(res.Content))+ }+ tc, ok := res.Content[0].(*mcp.TextContent)+ if !ok {+ t.Fatalf("content type = %T, want *mcp.TextContent", res.Content[0])+ }+ return tc.Text+}++func TestHandleSendAgentTaskSuccess(t *testing.T) {+ // SendMessage results arrive enveloped as {"task":...} per a2a.StreamResponse (a2a_test.go).+ task := `{"task":{"kind":"task","id":"t1","contextId":"c1","status":{"state":"completed",` ++ `"message":{"kind":"message","messageId":"m1","role":"agent","parts":[{"kind":"text","text":"done"}]}}}}`+ srv := fakeSidecar(t, "/sam/12D3KooWpeer/a2a/echo", task, nil)+ defer srv.Close()++ res, _, err := handleSendAgentTask(bridgeConfig{sidecarURL: srv.URL})(+ context.Background(), nil, sendAgentTaskParams{Peer: "12D3KooWpeer", Service: "echo", Message: "hi"})+ if err != nil {+ t.Fatal(err)+ }+ if res.IsError {+ t.Fatalf("unexpected tool error: %s", textContent(t, res))+ }+ var got taskResult+ if err := json.Unmarshal([]byte(textContent(t, res)), &got); err != nil {+ t.Fatalf("output is not the 4-field JSON: %v", err)+ }+ if got.TaskID != "t1" || got.Text != "done" {+ t.Errorf("got %+v", got)+ }+}++func TestHandleSendAgentTaskRefusalIsToolError(t *testing.T) {+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ http.Error(w, "Required labels not attested by provider", http.StatusForbidden)+ }))+ defer srv.Close()++ res, _, err := handleSendAgentTask(bridgeConfig{sidecarURL: srv.URL})(+ context.Background(), nil, sendAgentTaskParams{Peer: "12D3KooWpeer", Service: "echo",+ Message: "hi", RequiredLabels: "region=us-east-1"})+ if err != nil {+ t.Fatalf("refusals are tool errors, not protocol errors: %v", err)+ }+ if !res.IsError {+ t.Fatal("IsError must be true on sidecar refusal")+ }+ text := textContent(t, res)+ if !strings.HasPrefix(text, "403: Required labels not attested by provider") {+ t.Errorf("refusal must lead with status + verbatim body, got %q", text)+ }+}++func TestHandleGetAgentTask(t *testing.T) {+ task := `{"kind":"task","id":"t9","contextId":"c9","status":{"state":"completed"},` ++ `"artifacts":[{"artifactId":"a1","parts":[{"kind":"text","text":"result"}]}]}`+ srv := fakeSidecar(t, "/sam/12D3KooWpeer/a2a/echo", task, nil)+ defer srv.Close()++ res, _, err := handleGetAgentTask(bridgeConfig{sidecarURL: srv.URL})(+ context.Background(), nil, getAgentTaskParams{Peer: "12D3KooWpeer", Service: "echo", TaskID: "t9"})+ if err != nil {+ t.Fatal(err)+ }+ var got taskResult+ if err := json.Unmarshal([]byte(textContent(t, res)), &got); err != nil {+ t.Fatal(err)+ }+ if got.TaskID != "t9" || got.Text != "result" {+ t.Errorf("got %+v", got)+ }+}