Skip to content

Agent Mesh

feat(mobile): expose direct MCP tool calling via Go FFI

Antonio Ojeabec5b3e3ce43
diff --git a/mobile/sam-node-ffi/ffi/ffi.go b/mobile/sam-node-ffi/ffi/ffi.go
index 91840623..6ccb4b1f 100644
--- a/mobile/sam-node-ffi/ffi/ffi.go
+++ b/mobile/sam-node-ffi/ffi/ffi.go
@@ -29,6 +29,7 @@ import (
"github.com/google/sam/internal/node"
golog "github.com/ipfs/go-log/v2"
+ "github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"google.golang.org/protobuf/encoding/protojson"
)
@@ -375,3 +376,34 @@ func GetMeshInfo() string {
}
return string(jsonBytes)
}
+
+// CallRemoteTool calls an MCP tool on a remote peer and returns the result as a JSON string.
+func CallRemoteTool(peerIDStr string, toolName string, argsJSON string) string {
+ if activeNode == nil {
+ return `{"error": "node not running"}`
+ }
+
+ targetPeer, err := peer.Decode(peerIDStr)
+ if err != nil {
+ return fmt.Sprintf(`{"error": "invalid peer ID: %s"}`, err.Error())
+ }
+
+ var params any
+ if argsJSON != "" && argsJSON != "{}" {
+ if err := json.Unmarshal([]byte(argsJSON), &params); err != nil {
+ return fmt.Sprintf(`{"error": "invalid arguments JSON: %s"}`, err.Error())
+ }
+ }
+
+ res, err := activeNode.CallMCPTool(context.Background(), targetPeer, toolName, params)
+ if err != nil {
+ return fmt.Sprintf(`{"error": "failed to call tool: %s"}`, err.Error())
+ }
+
+ jsonBytes, err := json.Marshal(res)
+ if err != nil {
+ return fmt.Sprintf(`{"error": "failed to marshal result: %s"}`, err.Error())
+ }
+
+ return string(jsonBytes)
+}
diff --git a/mobile/sam-node-ffi/main.go b/mobile/sam-node-ffi/main.go
index cec7d1b8..4fbadcb4 100644
--- a/mobile/sam-node-ffi/main.go
+++ b/mobile/sam-node-ffi/main.go
@@ -87,6 +87,16 @@ func GetMeshInfo() *C.char {
return C.CString(jsonStr)
}
+//export CallRemoteTool
+func CallRemoteTool(peerID *C.char, toolName *C.char, argsJSON *C.char) *C.char {
+ goPeerID := C.GoString(peerID)
+ goToolName := C.GoString(toolName)
+ goArgsJSON := C.GoString(argsJSON)
+
+ jsonStr := ffi.CallRemoteTool(goPeerID, goToolName, goArgsJSON)
+ return C.CString(jsonStr)
+}
+
//export FreeString
func FreeString(str *C.char) {
C.free(unsafe.Pointer(str))