From d3bd91fc25598e58d2dc72912f6f706e545ce7bd Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Wed, 18 Nov 2026 11:59:39 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=8D=92=20fix:=20Restrict=20MCP=20Server?= =?UTF-8?q?=20Selections=20to=20Schedule=20Grants=20(#18420)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Under a grant, a run loads only the MCP tools the owner allowed. The filter looked only at keys naming one tool, so a key standing for every tool on a server, or for the server alone, was loaded as it was. The run policy and the call-time check still refused the calls, but the tools were defined for the model and the server was connected to. A key for every tool on a server is no longer loaded under a grant, since the grant lists tools by name. A key for the server alone is kept only where the grant lists a tool on that server for the agent. --- .../schedules/authorization/execution.spec.ts | 19 ++++++++++++++ .../src/schedules/authorization/execution.ts | 25 +++++++++---------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/packages/api/src/schedules/authorization/execution.spec.ts b/packages/api/src/schedules/authorization/execution.spec.ts index d2812c4490..2a031288c9 100644 --- a/packages/api/src/schedules/authorization/execution.spec.ts +++ b/packages/api/src/schedules/authorization/execution.spec.ts @@ -502,6 +502,25 @@ describe('the tools a run loads under a grant', () => { expect(fixture.execution.narrow('stranger', keys)).toEqual(['web_search', 'execute_code']); }); + it('loads no key that stands for a whole server unless the grant lists a tool on it', async () => { + const fixture = await executionFixture('invoke', readTool, undefined, { offered: ['export'] }); + const keys = [ + 'query_mcp_warehouse', + 'sys__all__sys_mcp_warehouse', + 'sys__server__sys_mcp_warehouse', + 'sys__all__sys_mcp_archive', + 'sys__server__sys_mcp_archive', + 'web_search', + ]; + // "Every tool" names none of them, so it would define what the owner left out. + expect(fixture.execution.narrow('root', keys)).toEqual([ + 'query_mcp_warehouse', + 'sys__server__sys_mcp_warehouse', + 'web_search', + ]); + expect(fixture.execution.narrow('stranger', keys)).toEqual(['web_search']); + }); + it('leaves the tools of a schedule nobody authorized as they are', async () => { const fixture = await executionFixture(); fixture.snapshot.enrollment = null; diff --git a/packages/api/src/schedules/authorization/execution.ts b/packages/api/src/schedules/authorization/execution.ts index 99709f2ce0..3b56b45753 100644 --- a/packages/api/src/schedules/authorization/execution.ts +++ b/packages/api/src/schedules/authorization/execution.ts @@ -68,15 +68,6 @@ export function bindScheduledMCPInvocation( return context && executions.get(context)?.bind(agentId, selectionName); } -/** A key that names one tool on one MCP server, as an agent's tool list carries it. */ -function isMCPToolSelection(toolKey: string): boolean { - return ( - toolKey.includes(Constants.mcp_delimiter) && - !toolKey.startsWith(`${Constants.mcp_server}${Constants.mcp_delimiter}`) && - !toolKey.startsWith(`${Constants.mcp_all}${Constants.mcp_delimiter}`) - ); -} - /** * What a run loads for an agent. Under a schedule's grant that is only what the owner * allowed: a tool left out is never defined for the model, and a server with nothing allowed @@ -149,13 +140,19 @@ export function createScheduleMCPExecution(deps: ScheduleMCPExecutionDeps): { const grantedServers = [...granted.keys()]; const serverAliases = buildServerNameAliases(grantedServers); const serverSpellings = [...grantedServers, ...serverAliases.keys()]; + /** + * Whether a run under the grant loads this MCP key for the agent. A key for one tool needs + * that tool listed. A key for every tool on a server names none of them, and the grant + * lists tools by name, so it is never loaded. A key for the server alone is kept only + * where the grant lists a tool on that server for the agent. + */ const isGrantedKey = (agentId: string, toolKey: string): boolean => { const [selectionName, spelling] = splitMCPToolKey(toolKey, serverSpellings); const serverName = spelling != null && granted.has(spelling) ? spelling : serverAliases.get(spelling ?? ''); - return ( - serverName != null && granted.get(serverName)?.get(agentId)?.has(selectionName) === true - ); + const tools = serverName == null ? undefined : granted.get(serverName)?.get(agentId); + if (!tools || selectionName === Constants.mcp_all) return false; + return selectionName === Constants.mcp_server ? tools.size > 0 : tools.has(selectionName); }; let fenced = false; /** Asked at the first MCP call, so a run that uses none pays nothing. Kept for the run. */ @@ -197,7 +194,9 @@ export function createScheduleMCPExecution(deps: ScheduleMCPExecutionDeps): { granted.get(serverName)?.get(agentId)?.has(selectionName) === true, narrow: (agentId: string, toolKeys: readonly string[]) => enrolled - ? toolKeys.filter((key) => !isMCPToolSelection(key) || isGrantedKey(agentId, key)) + ? toolKeys.filter( + (key) => !key.includes(Constants.mcp_delimiter) || isGrantedKey(agentId, key), + ) : [...toolKeys], bind(agentId: string | undefined, selectionName: string): ScheduledMCPInvocation { return Object.freeze({