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({