Skip to content

Agent Mesh

sdk: only a node passes the MCP gate too

Tomas Tormo7b0debbb0f3d
diff --git a/sdk/js/src/mcp.test.ts b/sdk/js/src/mcp.test.ts
index aec677fa..b0fca7bd 100644
--- a/sdk/js/src/mcp.test.ts
+++ b/sdk/js/src/mcp.test.ts
@@ -31,7 +31,7 @@ import { readFileSync } from "node:fs";
import { after, before, test } from "node:test";
import { z } from "zod";
import { AuthRejectedError, MAX_AUTH_FRAME_BYTES, MCP_PROTOCOL } from "./auth.ts";
-import { loadBiscuit, verifyPeerBiscuit } from "./biscuit.ts";
+import { ROLE_ROUTER, loadBiscuit, verifyPeerBiscuit } from "./biscuit.ts";
import { ROLE_NODE } from "./controlplane.ts";
import { parseServiceTarget, serviceCID } from "./discovery.ts";
import { AuthFrameSchema, AuthResponseSchema } from "./gen/sam_pb.ts";
@@ -228,3 +228,15 @@ test("a service the provider does not have ends the session before MCP starts",
const conn = await caller.dial(provider.getMultiaddrs()[0] as Parameters<typeof caller.dial>[0]);
await assert.rejects(openMCPSession(conn, frame("mcp://no-such-service"), [cpKey], { signal: AbortSignal.timeout(3000) }));
});
+
+test("only a node is a provider, as sam-node's checkPeerLabels requires", async () => {
+ // The provider answers with the credential it holds at the time; a router's, attesting the floor, is not a provider's.
+ const nodeBiscuit = providerBiscuit;
+ providerBiscuit = mint(provider.peerId.toString(), ROLE_ROUTER, { region: "eu" });
+ try {
+ const conn = await caller.dial(provider.getMultiaddrs()[0] as Parameters<typeof caller.dial>[0]);
+ await assert.rejects(openMCPSession(conn, frame("mcp://calc"), [cpKey], {}, { region: "eu" }), /lacks expected role "sam:role:node"/);
+ } finally {
+ providerBiscuit = nodeBiscuit;
+ }
+});
diff --git a/sdk/js/src/mcp.ts b/sdk/js/src/mcp.ts
index 7d75768f..fb92d6b1 100644
--- a/sdk/js/src/mcp.ts
+++ b/sdk/js/src/mcp.ts
@@ -22,7 +22,8 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
import { JSONRPCMessageSchema, type JSONRPCMessage } from "@modelcontextprotocol/sdk/types.js";
import { AUTH_HANDSHAKE_TIMEOUT_MS, AuthRejectedError, MAX_AUTH_FRAME_BYTES, MCP_PROTOCOL } from "./auth.ts";
-import { BiscuitVerificationError, verifyPeerBiscuit, type VerifiedBiscuit } from "./biscuit.ts";
+import { BiscuitVerificationError, requireRole, verifyPeerBiscuit, type VerifiedBiscuit } from "./biscuit.ts";
+import { ROLE_NODE } from "./controlplane.ts";
import { decodeAuthResponse } from "./credential.ts";
/** go-msgio's default message cap, which sam-node's StreamTransport uses. */
@@ -178,6 +179,8 @@ export async function openMCPSession(
throw new AuthRejectedError(conn.remotePeer.toString(), resp.error || "no reason given");
}
provider = await verifyPeerBiscuit(resp.biscuit, conn.remotePeer.toString(), trustedKeys);
+ // Only nodes host services; a router's or an admin's credential is a member, not a provider.
+ requireRole(provider, ROLE_NODE);
requireLabels(provider, options.requiredLabels);
requireEgressLabels(provider, egressRequireLabels);
} catch (err) {
diff --git a/sdk/python/src/agent_mesh/mcp_client.py b/sdk/python/src/agent_mesh/mcp_client.py
index 72ee3cc2..61e8574b 100644
--- a/sdk/python/src/agent_mesh/mcp_client.py
+++ b/sdk/python/src/agent_mesh/mcp_client.py
@@ -34,7 +34,8 @@ from mcp.shared.message import SessionMessage
from ._proto import sam_pb2 as pb
from .auth import AUTH_HANDSHAKE_TIMEOUT, MAX_AUTH_FRAME_BYTES, MCP_PROTOCOL, AuthRejectedError
-from .biscuit import BiscuitVerificationError, VerifiedBiscuit, verify_peer_biscuit
+from .biscuit import BiscuitVerificationError, VerifiedBiscuit, require_role, verify_peer_biscuit
+from .controlplane import ROLE_NODE
from .host import open_stream
logger = logging.getLogger("agent_mesh")
@@ -126,6 +127,8 @@ async def open_mcp_session(
raise AuthRejectedError(str(peer_id), resp.error or "no reason given")
try:
provider = verify_peer_biscuit(resp.biscuit, str(peer_id), trusted_keys)
+ # Only nodes host services; a router's or an admin's credential is a member, not a provider.
+ require_role(provider, ROLE_NODE)
except BiscuitVerificationError as err:
raise AuthRejectedError(str(peer_id), f"provider credential rejected: {err}") from err
require_labels(provider, required_labels)
diff --git a/sdk/python/tests/test_mcp.py b/sdk/python/tests/test_mcp.py
index 6d049b68..d5f4002f 100644
--- a/sdk/python/tests/test_mcp.py
+++ b/sdk/python/tests/test_mcp.py
@@ -33,7 +33,7 @@ from mcp.shared.message import SessionMessage
from agent_mesh._proto import sam_pb2 as pb
from agent_mesh.auth import MCP_PROTOCOL, AuthRejectedError
-from agent_mesh.biscuit import VerifiedBiscuit, verify_peer_biscuit
+from agent_mesh.biscuit import ROLE_ROUTER, VerifiedBiscuit, verify_peer_biscuit
from agent_mesh.controlplane import ROLE_NODE
from agent_mesh.discovery import parse_service_target, service_key
from agent_mesh.identity import Identity
@@ -129,10 +129,10 @@ def mcp_stream_handler(provider_biscuit: bytes, served: list[str]):
return handle
-async def start_provider(nursery, labels=None):
+async def start_provider(nursery, labels=None, role=ROLE_NODE):
identity = Identity.generate()
host = libp2p_host(identity)
- biscuit = mint(identity.peer_id, ROLE_NODE, labels)
+ biscuit = mint(identity.peer_id, role, labels)
served: list[str] = []
host.set_stream_handler(MCP_PROTOCOL, mcp_stream_handler(biscuit, served))
started = trio.Event()
@@ -227,6 +227,13 @@ def test_tools_over_the_mesh_stream():
with trio.fail_after(5):
async with open_mcp_session(caller, pid, frame(caller_biscuit, "mcp://no-such-service"), [CP_KEY]):
pass
+
+ # Only a node is a provider, as sam-node's checkPeerLabels requires: a router attesting the floor is not.
+ router, router_addr, _ = await start_provider(nursery, labels={"region": "eu"}, role=ROLE_ROUTER)
+ await caller.connect(info_from_p2p_addr(router_addr))
+ with pytest.raises(AuthRejectedError, match="lacks expected role 'sam:role:node'"):
+ async with open_mcp_session(caller, router.get_id(), frame(caller_biscuit, "mcp://calc"), [CP_KEY], egress_require_labels={"region": "eu"}):
+ pass
nursery.cancel_scope.cancel()
async def with_timeout():