Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/core/src/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,25 @@ export function* mcpTools<C = unknown>(options: McpOptions): restate.Operation<M
yield* options.onAuthorizationRequired();
}
}
const allowed = (name: string) => 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, {
signal,
timeout,
cacheMode: "refresh",
});
if (listed.tools.length > 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,
Expand All @@ -191,7 +195,8 @@ export function* mcpTools<C = unknown>(options: McpOptions): restate.Operation<M
const warnings: string[] = [];
// One bad remote tool is skipped; the rest of the catalog stays usable.
for (const definition of catalog.tools) {
if (options.allow && !options.allow(definition.name)) continue;
// A journal written before filtering moved into the catalog step holds every tool.
if (!allowed(definition.name)) continue;
const name = localToolName(`${options.namespace}_${definition.name}`);
if (!TOOL_NAME.test(name) || tools.has(name)) {
warnings.push(`Ignored MCP tool with an invalid or duplicate name: ${name}`);
Expand Down
23 changes: 22 additions & 1 deletion packages/tests/src/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,31 @@ void test("MCP fails once, without retries, when its own checks reject a catalog
})(),
),
);
assert.equal(output, "MCP catalog exceeds 128 tools");
assert.equal(output, "MCP catalog exceeds 128 allowed tools");
assert.equal(mock.seen.filter((x) => 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<typeof fixture>) {
const {output} = await runHandler((ctx: Context) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down