Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
implement http datapath
Antonio Ojea24bc3ddc3827
diff --git a/api/sam.pb.go b/api/sam.pb.goindex 5d0e3185..086e8972 100644--- a/api/sam.pb.go+++ b/api/sam.pb.go@@ -35,6 +35,58 @@ const (_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20))+type ServiceType int32++const (+ ServiceType_SERVICE_TYPE_UNSPECIFIED ServiceType = 0+ ServiceType_SERVICE_TYPE_MCP ServiceType = 1+ ServiceType_SERVICE_TYPE_INFERENCE ServiceType = 2+ ServiceType_SERVICE_TYPE_A2A ServiceType = 3+)++// Enum value maps for ServiceType.+var (+ ServiceType_name = map[int32]string{+ 0: "SERVICE_TYPE_UNSPECIFIED",+ 1: "SERVICE_TYPE_MCP",+ 2: "SERVICE_TYPE_INFERENCE",+ 3: "SERVICE_TYPE_A2A",+ }+ ServiceType_value = map[string]int32{+ "SERVICE_TYPE_UNSPECIFIED": 0,+ "SERVICE_TYPE_MCP": 1,+ "SERVICE_TYPE_INFERENCE": 2,+ "SERVICE_TYPE_A2A": 3,+ }+)++func (x ServiceType) Enum() *ServiceType {+ p := new(ServiceType)+ *p = x+ return p+}++func (x ServiceType) String() string {+ return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))+}++func (ServiceType) Descriptor() protoreflect.EnumDescriptor {+ return file_api_sam_proto_enumTypes[0].Descriptor()+}++func (ServiceType) Type() protoreflect.EnumType {+ return &file_api_sam_proto_enumTypes[0]+}++func (x ServiceType) Number() protoreflect.EnumNumber {+ return protoreflect.EnumNumber(x)+}++// Deprecated: Use ServiceType.Descriptor instead.+func (ServiceType) EnumDescriptor() ([]byte, []int) {+ return file_api_sam_proto_rawDescGZIP(), []int{0}+}+type MeshEvent_Type int32const (@@ -71,11 +123,11 @@ func (x MeshEvent_Type) String() string {}func (MeshEvent_Type) Descriptor() protoreflect.EnumDescriptor {- return file_api_sam_proto_enumTypes[0].Descriptor()+ return file_api_sam_proto_enumTypes[1].Descriptor()}func (MeshEvent_Type) Type() protoreflect.EnumType {- return &file_api_sam_proto_enumTypes[0]+ return &file_api_sam_proto_enumTypes[1]}func (x MeshEvent_Type) Number() protoreflect.EnumNumber {@@ -397,7 +449,7 @@ func (x *EnrollResponse) GetKnownPeers() []string {type ServiceInfo struct {state protoimpl.MessageState `protogen:"open.v1"`- Type string `protobuf:"bytes,1,opt,name=type,proto3" json:"type,omitempty"`+ Type ServiceType `protobuf:"varint,1,opt,name=type,proto3,enum=sam.v1.ServiceType" json:"type,omitempty"`Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`Description string `protobuf:"bytes,3,opt,name=description,proto3" json:"description,omitempty"`unknownFields protoimpl.UnknownFields@@ -434,11 +486,11 @@ func (*ServiceInfo) Descriptor() ([]byte, []int) {return file_api_sam_proto_rawDescGZIP(), []int{5}}-func (x *ServiceInfo) GetType() string {+func (x *ServiceInfo) GetType() ServiceType {if x != nil {return x.Type}- return ""+ return ServiceType_SERVICE_TYPE_UNSPECIFIED}func (x *ServiceInfo) GetName() string {@@ -541,14 +593,19 @@ const file_api_sam_proto_rawDesc = "" +"expiration\x18\x05 \x01(\x03R\n" +"expiration\x12\x1f\n" +"\vknown_peers\x18\x06 \x03(\tR\n" +- "knownPeers\"W\n" +- "\vServiceInfo\x12\x12\n" +- "\x04type\x18\x01 \x01(\tR\x04type\x12\x12\n" ++ "knownPeers\"l\n" ++ "\vServiceInfo\x12'\n" ++ "\x04type\x18\x01 \x01(\x0e2\x13.sam.v1.ServiceTypeR\x04type\x12\x12\n" +"\x04name\x18\x02 \x01(\tR\x04name\x12 \n" +"\vdescription\x18\x03 \x01(\tR\vdescription\"U\n" +"\x12DiscoveredProvider\x12\x17\n" +"\apeer_id\x18\x01 \x01(\tR\x06peerId\x12&\n" +- "\x0flocal_proxy_url\x18\x02 \x01(\tR\rlocalProxyUrlB\x1bZ\x19github.com/google/sam/apib\x06proto3"+ "\x0flocal_proxy_url\x18\x02 \x01(\tR\rlocalProxyUrl*s\n" ++ "\vServiceType\x12\x1c\n" ++ "\x18SERVICE_TYPE_UNSPECIFIED\x10\x00\x12\x14\n" ++ "\x10SERVICE_TYPE_MCP\x10\x01\x12\x1a\n" ++ "\x16SERVICE_TYPE_INFERENCE\x10\x02\x12\x14\n" ++ "\x10SERVICE_TYPE_A2A\x10\x03B\x1bZ\x19github.com/google/sam/apib\x06proto3"var (file_api_sam_proto_rawDescOnce sync.Once@@ -562,25 +619,27 @@ func file_api_sam_proto_rawDescGZIP() []byte {return file_api_sam_proto_rawDescData}-var file_api_sam_proto_enumTypes = make([]protoimpl.EnumInfo, 1)+var file_api_sam_proto_enumTypes = make([]protoimpl.EnumInfo, 2)var file_api_sam_proto_msgTypes = make([]protoimpl.MessageInfo, 7)var file_api_sam_proto_goTypes = []any{- (MeshEvent_Type)(0), // 0: sam.v1.MeshEvent.Type- (*AuthFrame)(nil), // 1: sam.v1.AuthFrame- (*AuthResponse)(nil), // 2: sam.v1.AuthResponse- (*MeshEvent)(nil), // 3: sam.v1.MeshEvent- (*EnrollRequest)(nil), // 4: sam.v1.EnrollRequest- (*EnrollResponse)(nil), // 5: sam.v1.EnrollResponse- (*ServiceInfo)(nil), // 6: sam.v1.ServiceInfo- (*DiscoveredProvider)(nil), // 7: sam.v1.DiscoveredProvider+ (ServiceType)(0), // 0: sam.v1.ServiceType+ (MeshEvent_Type)(0), // 1: sam.v1.MeshEvent.Type+ (*AuthFrame)(nil), // 2: sam.v1.AuthFrame+ (*AuthResponse)(nil), // 3: sam.v1.AuthResponse+ (*MeshEvent)(nil), // 4: sam.v1.MeshEvent+ (*EnrollRequest)(nil), // 5: sam.v1.EnrollRequest+ (*EnrollResponse)(nil), // 6: sam.v1.EnrollResponse+ (*ServiceInfo)(nil), // 7: sam.v1.ServiceInfo+ (*DiscoveredProvider)(nil), // 8: sam.v1.DiscoveredProvider}var file_api_sam_proto_depIdxs = []int32{- 0, // 0: sam.v1.MeshEvent.type:type_name -> sam.v1.MeshEvent.Type- 1, // [1:1] is the sub-list for method output_type- 1, // [1:1] is the sub-list for method input_type- 1, // [1:1] is the sub-list for extension type_name- 1, // [1:1] is the sub-list for extension extendee- 0, // [0:1] is the sub-list for field type_name+ 1, // 0: sam.v1.MeshEvent.type:type_name -> sam.v1.MeshEvent.Type+ 0, // 1: sam.v1.ServiceInfo.type:type_name -> sam.v1.ServiceType+ 2, // [2:2] is the sub-list for method output_type+ 2, // [2:2] is the sub-list for method input_type+ 2, // [2:2] is the sub-list for extension type_name+ 2, // [2:2] is the sub-list for extension extendee+ 0, // [0:2] is the sub-list for field type_name}func init() { file_api_sam_proto_init() }@@ -593,7 +652,7 @@ func file_api_sam_proto_init() {File: protoimpl.DescBuilder{GoPackagePath: reflect.TypeOf(x{}).PkgPath(),RawDescriptor: unsafe.Slice(unsafe.StringData(file_api_sam_proto_rawDesc), len(file_api_sam_proto_rawDesc)),- NumEnums: 1,+ NumEnums: 2,NumMessages: 7,NumExtensions: 0,NumServices: 0,diff --git a/api/sam.proto b/api/sam.protoindex 8e4707fa..377d4e2c 100644--- a/api/sam.proto+++ b/api/sam.proto@@ -57,8 +57,15 @@ message EnrollResponse {repeated string known_peers = 6;}+enum ServiceType {+ SERVICE_TYPE_UNSPECIFIED = 0;+ SERVICE_TYPE_MCP = 1;+ SERVICE_TYPE_INFERENCE = 2;+ SERVICE_TYPE_A2A = 3;+}+message ServiceInfo {- string type = 1;+ ServiceType type = 1;string name = 2;string description = 3;}diff --git a/cmd/sam-node/mcp.go b/cmd/sam-node/mcp.goindex eac995aa..6b926fc7 100644--- a/cmd/sam-node/mcp.go+++ b/cmd/sam-node/mcp.go@@ -85,7 +85,11 @@ func NewMCPHandler(node *SamNode) http.Handler {Type string `json:"type" jsonschema:"Service type (mcp, inference, a2a)"`Name string `json:"name" jsonschema:"Service name"`}) (*mcp.CallToolResult, any, error) {- providers, err := node.DiscoverRemoteServices(ctx, params.Type, params.Name)+ serviceType, err := parseServiceType(params.Type)+ if err != nil || serviceType == api.ServiceType_SERVICE_TYPE_UNSPECIFIED {+ return nil, nil, fmt.Errorf("invalid or unspecified service type: %s", params.Type)+ }+ providers, err := node.DiscoverRemoteServices(ctx, serviceType, params.Name)if err != nil {return nil, nil, err}diff --git a/cmd/sam-node/node.go b/cmd/sam-node/node.goindex 10f4da2f..29e1d41d 100644--- a/cmd/sam-node/node.go+++ b/cmd/sam-node/node.go@@ -20,6 +20,7 @@ import ("crypto/sha256""encoding/hex""fmt"+ "net/http""os""strings""sync"@@ -32,6 +33,7 @@ import (lru "github.com/hashicorp/golang-lru/v2""github.com/ipfs/go-cid""github.com/libp2p/go-libp2p"+ gostream "github.com/libp2p/go-libp2p-gostream"dht "github.com/libp2p/go-libp2p-kad-dht"pubsub "github.com/libp2p/go-libp2p-pubsub""github.com/libp2p/go-libp2p/core/crypto"@@ -85,6 +87,7 @@ type SamNode struct {keysMu sync.RWMutexrateLimiter *PeerRateLimiterservices map[string]*api.ServiceInfo+ serviceHandlers map[string]http.HandlerservicesMu sync.RWMutexBoundHTTPAddr string}@@ -97,13 +100,14 @@ func NewSamNode(ctx context.Context, privKey crypto.PrivKey, hubPubKey ed25519.P}node := &SamNode{- Store: store,- trustedKeys: trustedKeys,- knownPeers: make(map[string]bool),- receivedMsgs: make(map[string][]string),- topics: make(map[string]*pubsub.Topic),- services: make(map[string]*api.ServiceInfo),- LocalPolicy: localPolicy,+ Store: store,+ trustedKeys: trustedKeys,+ knownPeers: make(map[string]bool),+ receivedMsgs: make(map[string][]string),+ topics: make(map[string]*pubsub.Topic),+ services: make(map[string]*api.ServiceInfo),+ serviceHandlers: make(map[string]http.Handler),+ LocalPolicy: localPolicy,}var err error@@ -235,6 +239,11 @@ func NewSamNode(ctx context.Context, privKey crypto.PrivKey, hubPubKey ed25519.P// Start key pruningnode.startKeyPruning(ctx, keyGracePeriod)+ // Start Ingress HTTP Server+ if err := node.StartIngressServer(ctx); err != nil {+ return nil, fmt.Errorf("failed to start ingress server: %w", err)+ }+return node, nil}@@ -609,8 +618,38 @@ func (n *SamNode) verifyBiscuit(biscuitData []byte, remotePeer peer.ID) (*biscuireturn nil, fmt.Errorf("no valid key found")}-func serviceNameToCID(serviceType, serviceName string) (cid.Cid, error) {- serviceKey := fmt.Sprintf("sam:service:%s:%s", serviceType, serviceName)+func serviceTypeToString(t api.ServiceType) (string, error) {+ switch t {+ case api.ServiceType_SERVICE_TYPE_MCP:+ return "mcp", nil+ case api.ServiceType_SERVICE_TYPE_INFERENCE:+ return "inference", nil+ case api.ServiceType_SERVICE_TYPE_A2A:+ return "a2a", nil+ default:+ return "", fmt.Errorf("invalid or unspecified service type")+ }+}++func parseServiceType(s string) (api.ServiceType, error) {+ switch strings.ToLower(s) {+ case "mcp":+ return api.ServiceType_SERVICE_TYPE_MCP, nil+ case "inference":+ return api.ServiceType_SERVICE_TYPE_INFERENCE, nil+ case "a2a":+ return api.ServiceType_SERVICE_TYPE_A2A, nil+ default:+ return api.ServiceType_SERVICE_TYPE_UNSPECIFIED, fmt.Errorf("invalid service type: %s", s)+ }+}++func serviceNameToCID(serviceType api.ServiceType, serviceName string) (cid.Cid, error) {+ typeStr, err := serviceTypeToString(serviceType)+ if err != nil {+ return cid.Undef, err+ }+ serviceKey := fmt.Sprintf("sam:service:%s:%s", typeStr, serviceName)hash, err := multihash.Sum([]byte(serviceKey), multihash.SHA2_256, -1)if err != nil {return cid.Undef, err@@ -619,6 +658,9 @@ func serviceNameToCID(serviceType, serviceName string) (cid.Cid, error) {}func (n *SamNode) RegisterService(ctx context.Context, info *api.ServiceInfo) error {+ if info.Type == api.ServiceType_SERVICE_TYPE_UNSPECIFIED {+ return fmt.Errorf("cannot register service with unspecified type")+ }c, err := serviceNameToCID(info.Type, info.Name)if err != nil {return err@@ -657,7 +699,7 @@ func (n *SamNode) IsServiceRegistered(serviceName string) bool {return ok}-func (n *SamNode) FindProviders(ctx context.Context, serviceType, serviceName string) ([]peer.AddrInfo, error) {+func (n *SamNode) FindProviders(ctx context.Context, serviceType api.ServiceType, serviceName string) ([]peer.AddrInfo, error) {c, err := serviceNameToCID(serviceType, serviceName)if err != nil {return nil, err@@ -675,12 +717,17 @@ func (n *SamNode) FindProviders(ctx context.Context, serviceType, serviceName streturn providerInfos, nil}-func (n *SamNode) DiscoverRemoteServices(ctx context.Context, serviceType, serviceName string) ([]*api.DiscoveredProvider, error) {+func (n *SamNode) DiscoverRemoteServices(ctx context.Context, serviceType api.ServiceType, serviceName string) ([]*api.DiscoveredProvider, error) {providers, err := n.FindProviders(ctx, serviceType, serviceName)if err != nil {return nil, err}+ typeStr, err := serviceTypeToString(serviceType)+ if err != nil {+ return nil, err+ }+var discovered []*api.DiscoveredProviderfor _, p := range providers {if p.ID == n.Host.ID() {@@ -688,12 +735,11 @@ func (n *SamNode) DiscoverRemoteServices(ctx context.Context, serviceType, servi}// Construct local proxy URL- // We need to know our own bound port. We'll assume it's set in node.BoundHTTPAddr// Format: http://localhost:<sam_port>/sam/{peer_id}/{service_type}/{service_name}localURL := fmt.Sprintf("http://%s/sam/%s/%s/%s",n.BoundHTTPAddr,p.ID.String(),- serviceType,+ typeStr,serviceName,)@@ -716,3 +762,70 @@ func (n *SamNode) ListLocalServices() []*api.ServiceInfo {}return services}++func (n *SamNode) StartIngressServer(ctx context.Context) error {+ listener, err := gostream.Listen(n.Host, "/libp2p-http")+ if err != nil {+ return err+ }++ server := &http.Server{+ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ path := r.URL.Path+ parts := strings.SplitN(strings.TrimPrefix(path, "/"), "/", 3)+ if len(parts) < 2 {+ http.Error(w, "Invalid path", http.StatusBadRequest)+ return+ }+ serviceTypeStr := parts[0]+ serviceName := parts[1]+ upstreamPath := ""+ if len(parts) > 2 {+ upstreamPath = parts[2]+ }++ serviceType, err := parseServiceType(serviceTypeStr)+ if err != nil || serviceType == api.ServiceType_SERVICE_TYPE_UNSPECIFIED {+ http.Error(w, "Invalid service type", http.StatusBadRequest)+ return+ }++ n.mu.Lock()+ handler, ok := n.serviceHandlers[serviceName]+ n.mu.Unlock()++ if !ok {+ http.Error(w, "Service not found", http.StatusNotFound)+ return+ }++ r.URL.Path = "/" + upstreamPath+ handler.ServeHTTP(w, r)+ }),+ }++ go func() {+ logger.Infof("[Ingress] Starting P2P HTTP server on protocol /libp2p-http")+ if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {+ logger.Errorf("[Ingress] Server error: %v", err)+ }+ }()++ go func() {+ <-ctx.Done()+ if err := server.Close(); err != nil {+ logger.Errorf("[Ingress] Failed to close server: %v", err)+ }+ if err := listener.Close(); err != nil {+ logger.Errorf("[Ingress] Failed to close listener: %v", err)+ }+ }()++ return nil+}++func (n *SamNode) RegisterServiceHandler(name string, handler http.Handler) {+ n.mu.Lock()+ defer n.mu.Unlock()+ n.serviceHandlers[name] = handler+}diff --git a/cmd/sam-node/proxy_test.go b/cmd/sam-node/proxy_test.gonew file mode 100644index 00000000..0e9ba4e4--- /dev/null+++ b/cmd/sam-node/proxy_test.go@@ -0,0 +1,181 @@+package main++import (+ "context"+ "fmt"+ "io"+ "net/http"+ "net/http/httptest"+ "testing"+ "time"++ "github.com/google/sam/api"+ "github.com/libp2p/go-libp2p/core/crypto"+ "github.com/libp2p/go-libp2p/core/peer"+)++func TestDatapathIntegration(t *testing.T) {+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)+ defer cancel()++ // 1. Setup: Create two in-memory nodes (Node A and Node B)+ privA, _, _ := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ privB, _, _ := crypto.GenerateKeyPair(crypto.Ed25519, -1)++ dirA := t.TempDir()+ dirB := t.TempDir()++ storeA, err := NewStore(dirA)+ if err != nil {+ t.Fatal(err)+ }+ defer func() {+ if err := storeA.Close(); err != nil {+ t.Logf("failed to close storeA: %v", err)+ }+ }()++ storeB, err := NewStore(dirB)+ if err != nil {+ t.Fatal(err)+ }+ defer func() {+ if err := storeB.Close(); err != nil {+ t.Logf("failed to close storeB: %v", err)+ }+ }()++ // Pre-populate stores with dummy keys to avoid enrollment failure if required+ // For this test, we assume we can run without full enrollment if we bypass AuthHandler++ nodeA, err := NewSamNode(ctx, privA, nil, nil, storeA, "test-mesh", "1s", []string{"/ip4/127.0.0.1/tcp/0"}, false, &CompiledLocalPolicy{}, 24*time.Hour)+ if err != nil {+ t.Fatal(err)+ }+ defer func() {+ if err := nodeA.Host.Close(); err != nil {+ t.Logf("failed to close nodeA host: %v", err)+ }+ }()++ nodeB, err := NewSamNode(ctx, privB, nil, nil, storeB, "test-mesh", "1s", []string{"/ip4/127.0.0.1/tcp/0"}, false, &CompiledLocalPolicy{}, 24*time.Hour)+ if err != nil {+ t.Fatal(err)+ }+ defer func() {+ if err := nodeB.Host.Close(); err != nil {+ t.Logf("failed to close nodeB host: %v", err)+ }+ }()++ // Connect Node B to Node A directly+ err = nodeB.Host.Connect(ctx, peer.AddrInfo{ID: nodeA.Host.ID(), Addrs: nodeA.Host.Addrs()})+ if err != nil {+ t.Fatal(err)+ }++ // 2. Target Service: On Node A, start a dummy HTTP server+ expectedHeaderValue := "test-value"+ expectedBody := `{"status":"success"}`++ dummyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.Header.Get("X-Test-Header") != expectedHeaderValue {+ t.Errorf("Expected header X-Test-Header to be %s, got %s", expectedHeaderValue, r.Header.Get("X-Test-Header"))+ }+ w.Header().Set("Content-Type", "application/json")+ w.WriteHeader(http.StatusOK)+ _, _ = w.Write([]byte(expectedBody))+ }))+ defer dummyServer.Close()++ // 3. Registration: Node A registers this dummy server in its ServiceRegistry+ serviceName := "dummy-tool"+ serviceInfo := &api.ServiceInfo{+ Type: api.ServiceType_SERVICE_TYPE_MCP,+ Name: serviceName,+ }++ // We need to make sure the node knows how to handle this service locally.+ // We use the dummy server as the handler.+ nodeA.RegisterServiceHandler(serviceName, dummyServer.Config.Handler)++ // We also register it in the DHT so Node B can find it if needed (though we use direct URL in test)+ // We ignore the error because DHT Provide might fail if routing table is empty in this isolated test.+ _ = nodeA.RegisterService(ctx, serviceInfo)++ // Manually add to services map to ensure it's registered for the test lookup+ nodeA.servicesMu.Lock()+ nodeA.services[serviceName] = serviceInfo+ nodeA.servicesMu.Unlock()++ // Start the Sidecar server for Node B to get BoundHTTPAddr populated+ // We don't need full sidecar for Node B in this test if we call createEgressProxy directly,+ // but let's populate BoundHTTPAddr to avoid nil panics if used.+ nodeB.BoundHTTPAddr = "127.0.0.1:0" // Dummy++ // 4. Execution: Node B makes a request via its local Egress Proxy++ // We need to create the egress proxy for Node B.+ // The user request says: "Implement a reverse proxy on the local `sam-node` HTTP server that intercepts requests to `/sam/`"+ // In sidecar.go we have `createEgressProxy(node)`. We can use it here.++ proxyHandler := createEgressProxy(nodeB)++ // We start a test server for Node B's proxy to simulate the local agent calling it.+ proxyServer := httptest.NewServer(proxyHandler)+ defer proxyServer.Close()++ // Construct the URL targeting Node A's service+ // http://localhost:<port>/sam/{peer_id}/{service_type}/{service_name}/{upstream_path}+ url := fmt.Sprintf("%s/sam/%s/mcp/%s/api/v1/test", proxyServer.URL, nodeA.Host.ID().String(), serviceName)++ req, err := http.NewRequest("GET", url, nil)+ if err != nil {+ t.Fatal(err)+ }+ req.Header.Set("X-Test-Header", expectedHeaderValue)++ client := &http.Client{}++ pidStr := nodeA.Host.ID().String()+ _, err = peer.Decode(pidStr)+ if err != nil {+ t.Fatalf("Failed to decode generated peer ID %s: %v", pidStr, err)+ }++ // Wait for DHT/routing to settle or just retry a few times if needed.+ // Since we connected directly, it should work immediately if the protocol is registered.++ var resp *http.Response+ for i := 0; i < 3; i++ {+ resp, err = client.Do(req)+ if err == nil && resp.StatusCode == http.StatusOK {+ break+ }+ t.Logf("Attempt %d failed: %v, status: %v", i+1, err, resp)+ time.Sleep(1 * time.Second)+ }++ if err != nil {+ t.Fatal(err)+ }+ defer func() {+ if err := resp.Body.Close(); err != nil {+ t.Logf("failed to close response body: %v", err)+ }+ }()++ // 5. Assertions+ if resp.StatusCode != http.StatusOK {+ t.Fatalf("Expected status OK, got %d", resp.StatusCode)+ }++ bodyBytes, err := io.ReadAll(resp.Body)+ if err != nil {+ t.Fatal(err)+ }++ if string(bodyBytes) != expectedBody {+ t.Fatalf("Expected body %s, got %s", expectedBody, string(bodyBytes))+ }+}diff --git a/cmd/sam-node/sidecar.go b/cmd/sam-node/sidecar.goindex 72b68373..40b7eb94 100644--- a/cmd/sam-node/sidecar.go+++ b/cmd/sam-node/sidecar.go@@ -21,10 +21,12 @@ import ("fmt""net""net/http"+ "net/http/httputil""os""strings""github.com/google/sam/api"+ libp2phttp "github.com/libp2p/go-libp2p-http")func startSidecarServer(node *SamNode, addr, token, certFile, keyFile, caFile string) {@@ -45,6 +47,9 @@ func startSidecarServer(node *SamNode, addr, token, certFile, keyFile, caFile sthandleDiscoverService(node, w, r)})))+ // Mount Egress Proxy+ mux.Handle("/sam/", withAuth(token, createEgressProxy(node)))+// Mount MCP handlermcpHandler := NewMCPHandler(node)mux.Handle("/", mcpHandler)@@ -169,7 +174,7 @@ func handleRegisterService(node *SamNode, w http.ResponseWriter, r *http.Requestreturn}- if req.Name == "" || req.Type == "" {+ if req.Name == "" || req.Type == api.ServiceType_SERVICE_TYPE_UNSPECIFIED {http.Error(w, "name and type are required", http.StatusBadRequest)return}@@ -220,12 +225,18 @@ func handleDiscoverService(node *SamNode, w http.ResponseWriter, r *http.Request}serviceName := r.URL.Query().Get("name")- serviceType := r.URL.Query().Get("type")- if serviceName == "" || serviceType == "" {+ serviceTypeStr := r.URL.Query().Get("type")+ if serviceName == "" || serviceTypeStr == "" {http.Error(w, "name and type query parameters are required", http.StatusBadRequest)return}+ serviceType, err := parseServiceType(serviceTypeStr)+ if err != nil || serviceType == api.ServiceType_SERVICE_TYPE_UNSPECIFIED {+ http.Error(w, "Invalid or unspecified service type", http.StatusBadRequest)+ return+ }+providers, err := node.DiscoverRemoteServices(r.Context(), serviceType, serviceName)if err != nil {http.Error(w, fmt.Sprintf("Failed to discover services: %v", err), http.StatusInternalServerError)@@ -237,3 +248,32 @@ func handleDiscoverService(node *SamNode, w http.ResponseWriter, r *http.Requestlogger.Errorf("Failed to encode providers: %v", err)}}++func createEgressProxy(node *SamNode) http.Handler {+ transport := libp2phttp.NewTransport(node.Host)++ proxy := &httputil.ReverseProxy{+ Director: func(req *http.Request) {+ parts := strings.SplitN(req.URL.Path, "/", 6)+ if len(parts) < 5 {+ return+ }+ peerID := parts[2]+ serviceType := parts[3]+ serviceName := parts[4]+ upstreamPath := ""+ if len(parts) > 5 {+ upstreamPath = parts[5]+ }++ req.URL.Scheme = "libp2p"+ req.URL.Host = peerID+ req.Host = peerID+ req.URL.Path = fmt.Sprintf("/%s/%s/%s", serviceType, serviceName, upstreamPath)+ logger.Infof("[Proxy] Rewriting URL to libp2p://%s%s", req.URL.Host, req.URL.Path)+ },+ Transport: transport,+ }++ return proxy+}diff --git a/cmd/sam-node/sidecar_test.go b/cmd/sam-node/sidecar_test.goindex b5bc37a4..e0166c49 100644--- a/cmd/sam-node/sidecar_test.go+++ b/cmd/sam-node/sidecar_test.go@@ -147,7 +147,7 @@ func TestHandleRegisterService(t *testing.T) {DHT: d,}- reqBody := &api.ServiceInfo{Type: "mcp", Name: "test-service", Description: "test desc"}+ reqBody := &api.ServiceInfo{Type: api.ServiceType_SERVICE_TYPE_MCP, Name: "test-service", Description: "test desc"}body, err := json.Marshal(reqBody)if err != nil {t.Fatalf("Failed to marshal request body: %v", err)@@ -229,7 +229,7 @@ func TestHandleDiscoverService(t *testing.T) {t.Fatal(err)}- serviceInfo := &api.ServiceInfo{Type: "mcp", Name: "remote-service"}+ serviceInfo := &api.ServiceInfo{Type: api.ServiceType_SERVICE_TYPE_MCP, Name: "remote-service"}c, err := serviceNameToCID(serviceInfo.Type, serviceInfo.Name)if err != nil {t.Fatal(err)@@ -268,8 +268,8 @@ func TestListLocalServices(t *testing.T) {services: make(map[string]*api.ServiceInfo),}- service1 := &api.ServiceInfo{Type: "mcp", Name: "service1"}- service2 := &api.ServiceInfo{Type: "inference", Name: "service2"}+ service1 := &api.ServiceInfo{Type: api.ServiceType_SERVICE_TYPE_MCP, Name: "service1"}+ service2 := &api.ServiceInfo{Type: api.ServiceType_SERVICE_TYPE_INFERENCE, Name: "service2"}node.services["service1"] = service1node.services["service2"] = service2diff --git a/go.mod b/go.modindex 594472ab..fa0875ee 100644--- a/go.mod+++ b/go.mod@@ -6,16 +6,22 @@ require (github.com/biscuit-auth/biscuit-go/v2 v2.2.0github.com/coreos/go-oidc/v3 v3.18.0github.com/golang-jwt/jwt/v5 v5.3.1+ github.com/hashicorp/golang-lru/v2 v2.0.7+ github.com/ipfs/go-cid v0.6.1github.com/ipfs/go-log/v2 v2.9.1github.com/libp2p/go-libp2p v0.48.0+ github.com/libp2p/go-libp2p-http v0.5.0github.com/libp2p/go-libp2p-kad-dht v0.39.1github.com/libp2p/go-libp2p-pubsub v0.16.0github.com/libp2p/go-msgio v0.3.0github.com/modelcontextprotocol/go-sdk v1.5.0github.com/multiformats/go-multiaddr v0.16.1+ github.com/multiformats/go-multihash v0.2.3+ github.com/prometheus/client_golang v1.23.2github.com/spf13/cobra v1.10.2go.etcd.io/bbolt v1.4.3golang.org/x/oauth2 v0.36.0+ golang.org/x/time v0.15.0google.golang.org/protobuf v1.36.11gopkg.in/yaml.v2 v2.4.0)@@ -41,11 +47,9 @@ require (github.com/google/uuid v1.6.0 // indirectgithub.com/gorilla/websocket v1.5.3 // indirectgithub.com/hashicorp/golang-lru v1.0.2 // indirect- github.com/hashicorp/golang-lru/v2 v2.0.7 // indirectgithub.com/huin/goupnp v1.3.0 // indirectgithub.com/inconshreveable/mousetrap v1.1.0 // indirectgithub.com/ipfs/boxo v0.39.0 // indirect- github.com/ipfs/go-cid v0.6.1 // indirectgithub.com/ipfs/go-datastore v0.9.1 // indirectgithub.com/ipld/go-ipld-prime v0.23.0 // indirectgithub.com/jackpal/go-nat-pmp v1.0.2 // indirect@@ -56,6 +60,7 @@ require (github.com/libp2p/go-cidranger v1.1.0 // indirectgithub.com/libp2p/go-flow-metrics v0.3.0 // indirectgithub.com/libp2p/go-libp2p-asn-util v0.4.1 // indirect+ github.com/libp2p/go-libp2p-gostream v0.6.0 // indirectgithub.com/libp2p/go-libp2p-kbucket v0.8.0 // indirectgithub.com/libp2p/go-libp2p-record v0.3.1 // indirectgithub.com/libp2p/go-libp2p-routing-helpers v0.7.5 // indirect@@ -74,7 +79,6 @@ require (github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirectgithub.com/multiformats/go-multibase v0.3.0 // indirectgithub.com/multiformats/go-multicodec v0.10.0 // indirect- github.com/multiformats/go-multihash v0.2.3 // indirectgithub.com/multiformats/go-multistream v0.6.1 // indirectgithub.com/multiformats/go-varint v0.1.0 // indirectgithub.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect@@ -96,7 +100,6 @@ require (github.com/pion/turn/v5 v5.0.3 // indirectgithub.com/pion/webrtc/v4 v4.2.11 // indirectgithub.com/polydawn/refmt v0.89.1-0.20231129105047-37766d95467a // indirect- github.com/prometheus/client_golang v1.23.2 // indirectgithub.com/prometheus/client_model v0.6.2 // indirectgithub.com/prometheus/common v0.67.5 // indirectgithub.com/prometheus/procfs v0.20.1 // indirect@@ -128,7 +131,6 @@ require (golang.org/x/sys v0.43.0 // indirectgolang.org/x/telemetry v0.0.0-20260423152414-329d219564b0 // indirectgolang.org/x/text v0.36.0 // indirect- golang.org/x/time v0.15.0 // indirectgolang.org/x/tools v0.44.0 // indirectgonum.org/v1/gonum v0.17.0 // indirectlukechampine.com/blake3 v1.4.1 // indirectdiff --git a/go.sum b/go.sumindex f6fec18b..5975725e 100644--- a/go.sum+++ b/go.sum@@ -259,6 +259,8 @@ github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=+github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=+github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=github.com/koron/go-ssdp v0.1.0 h1:ckl5x5H6qSNFmi+wCuROvvGUu2FQnMbQrU95IHCcv3Y=@@ -272,6 +274,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=+github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=+github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=github.com/libp2p/go-buffer-pool v0.1.0 h1:oK4mSFcQz7cTQIfqbe4MIj9gLW+mnanjyFtc6cdF0Y8=github.com/libp2p/go-buffer-pool v0.1.0/go.mod h1:N+vh8gMqimBzdKkSMVuydVDq+UV5QTWy5HSiZacSbPg=github.com/libp2p/go-cidranger v1.1.0 h1:ewPN8EZ0dd1LSnrtuwd4709PXVcITVeuwbag38yPW7c=@@ -282,6 +286,10 @@ github.com/libp2p/go-libp2p v0.48.0 h1:h2BrLAgrj7X8bEN05K7qmrjpNHYA+6tnsGRdprjTngithub.com/libp2p/go-libp2p v0.48.0/go.mod h1:Q1fBZNdmC2Hf82husCTfkKJVfHm2we5zk+NWmOGEmWk=github.com/libp2p/go-libp2p-asn-util v0.4.1 h1:xqL7++IKD9TBFMgnLPZR6/6iYhawHKHl950SO9L6n94=github.com/libp2p/go-libp2p-asn-util v0.4.1/go.mod h1:d/NI6XZ9qxw67b4e+NgpQexCIiFYJjErASrYW4PFDN8=+github.com/libp2p/go-libp2p-gostream v0.6.0 h1:QfAiWeQRce6pqnYfmIVWJFXNdDyfiR/qkCnjyaZUPYU=+github.com/libp2p/go-libp2p-gostream v0.6.0/go.mod h1:Nywu0gYZwfj7Jc91PQvbGU8dIpqbQQkjWgDuOrFaRdA=+github.com/libp2p/go-libp2p-http v0.5.0 h1:+x0AbLaUuLBArHubbbNRTsgWz0RjNTy6DJLOxQ3/QBc=+github.com/libp2p/go-libp2p-http v0.5.0/go.mod h1:glh87nZ35XCQyFsdzZps6+F4HYI6DctVFY5u1fehwSg=github.com/libp2p/go-libp2p-kad-dht v0.39.1 h1:9RzUBc7zywT4ZNamRSgEvPZmVlK3Y6xdlCYfXXvlR/Q=github.com/libp2p/go-libp2p-kad-dht v0.39.1/go.mod h1:Po2JugFEkDq9Vig/JXtc153ntOi0q58o4j7IuITCOVs=github.com/libp2p/go-libp2p-kbucket v0.8.0 h1:QAK7RzKJpYe+EuSEATAaaHYMYLkPDGC18m9jxPLnU8s=diff --git a/tests/integration/service_discovery_test.go b/tests/integration/service_discovery_test.goindex dfaa02a3..8320bfe8 100644--- a/tests/integration/service_discovery_test.go+++ b/tests/integration/service_discovery_test.go@@ -143,7 +143,7 @@ func waitForAPI(t *testing.T, addr string) {func registerService(t *testing.T, apiAddr, token, serviceName string) {t.Helper()- reqBody := map[string]string{"Type": "mcp", "Name": serviceName, "Description": "test desc"}+ reqBody := map[string]any{"Type": api.ServiceType_SERVICE_TYPE_MCP, "Name": serviceName, "Description": "test desc"}body, _ := json.Marshal(reqBody)req, _ := http.NewRequest("POST", "http://"+apiAddr+"/sam/service/register", bytes.NewBuffer(body))req.Header.Set("Authorization", "Bearer "+token)