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
19 changes: 19 additions & 0 deletions packages/api/src/schedules/authorization/execution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,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;
Expand Down
25 changes: 12 additions & 13 deletions packages/api/src/schedules/authorization/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -147,13 +138,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;
const checkEnrollment = async (): Promise<void> => {
Expand Down Expand Up @@ -184,7 +181,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({
Expand Down
Loading