From c812279a019345b16224a58e4e4d39da923dd03e Mon Sep 17 00:00:00 2001 From: Giselle van Dongen Date: Fri, 20 Nov 2026 15:29:21 +0200 Subject: [PATCH] Check tool limit after filtering for allowed --- packages/core/src/mcp.ts | 11 ++++++--- packages/tests/src/mcp.test.ts | 23 ++++++++++++++++++- .../skills/restate-agents/references/tools.md | 3 ++- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/packages/core/src/mcp.ts b/packages/core/src/mcp.ts index 172a381..ffdcb10 100644 --- a/packages/core/src/mcp.ts +++ b/packages/core/src/mcp.ts @@ -164,6 +164,7 @@ export function* mcpTools(options: McpOptions): restate.Operation options.allow?.(name) ?? true; const catalog = yield* authorized(`mcp-${options.namespace}-catalog`, (token, signal) => withClient(token, signal, undefined, async (client, transport) => { const listed = await client.listTools(undefined, { @@ -171,14 +172,17 @@ export function* mcpTools(options: McpOptions): restate.Operation 128) throw new TerminalError("MCP catalog exceeds 128 tools"); + // Filter before the limit and the journal: a large server with a few allowed + // tools loads, and the journal holds only the tools the agent can use. + const tools = listed.tools.filter((definition) => allowed(definition.name)); + if (tools.length > 128) throw new TerminalError("MCP catalog exceeds 128 allowed tools"); const discovery = client.getDiscoverResult(); if (options.protocol === "stateless" && !discovery) throw new TerminalError("MCP stateless discovery missing"); let prior: PriorDiscovery = {kind: "legacy"}; if (discovery) prior = {kind: "modern", discover: discovery}; return { - tools: listed.tools, + tools, session: { id: transport.sessionId, version: transport.protocolVersion, @@ -191,7 +195,8 @@ export function* mcpTools(options: McpOptions): restate.Operation x.method === "tools/list").length, 1); }); +void test("MCP applies allow before the catalog limit and journals only allowed tools", async () => { + const mock = fixture("stateless", {tooMany: true}); + const {output, journal} = await runHandler((ctx: Context) => + r.execute( + ctx, + (function* () { + const mcp = yield* mcpTools({ + url: "https://fixture.test/mcp", + namespace: "remote", + protocol: "stateless", + fetch: mock.fetch, + allow: (name) => name === "lookup", + }); + return Object.keys(mcp.tools); + })(), + ), + ); + assert.deepEqual(output, ["remote_lookup"]); + assert.ok(!Buffer.concat(journal).includes("extra0")); +}); + /** Runs one remote_lookup call and returns the result the model saw. */ async function lookupResult(mock: ReturnType) { const {output} = await runHandler((ctx: Context) => diff --git a/plugins/restate-agents/skills/restate-agents/references/tools.md b/plugins/restate-agents/skills/restate-agents/references/tools.md index 68cd92b..a1fb843 100644 --- a/plugins/restate-agents/skills/restate-agents/references/tools.md +++ b/plugins/restate-agents/skills/restate-agents/references/tools.md @@ -158,7 +158,8 @@ const result = yield* guide.run(question, {tools: docs.tools}); server that ignores it may repeat work). A credential the server echoes back is replaced with `[redacted]` before any result or error is journaled. Call `close()` when done with a stateful session. A namespace is 1 to 24 letters, -digits, `_` or `-`; anything else is a terminal error. +digits, `_` or `-`; anything else is a terminal error. At most 128 tools may +pass `allow`; more is a terminal error, so restrict a large server with `allow`. Every built-in tool (`sleepTool`, `humanApprovalTool`, `cancelOperationTool`, `searchToolsTool`, `programTool`) accepts `description` and