diff --git a/client/src/components/MCP/GrantDialog.tsx b/client/src/components/MCP/GrantDialog.tsx index 4063862103..38d9f159fd 100644 --- a/client/src/components/MCP/GrantDialog.tsx +++ b/client/src/components/MCP/GrantDialog.tsx @@ -1,4 +1,5 @@ import { useState, useId } from 'react'; +import { Tools } from 'librechat-data-provider'; import { Button, Input, @@ -43,6 +44,13 @@ const statusKeys = { unsupported: 'com_ui_schedule_consent_unsupported', } as const; +/** Built-in tools the owner knows by name. Any other, such as an action, is shown by its key. */ +const toolKeys = new Map([ + [Tools.execute_code, 'com_ui_schedule_consent_tool_code'], + [Tools.web_search, 'com_ui_schedule_consent_tool_web'], + [Tools.file_search, 'com_ui_schedule_consent_tool_files'], +]); + const messageKeys = { error: 'com_ui_schedule_consent_error', revoked: 'com_ui_schedule_consent_revoked', @@ -196,6 +204,36 @@ function Grant({ data?.asking ?? {}; const noteId = useId(); + const otherId = useId(); + /** Null until the owner changes one of the other tools, with the grant it was changed against. */ + const [editedOther, setEditedOther] = useState<{ + revision: string | null; + tools: string[]; + } | null>(null); + /** The agents' tools that are not MCP tools: each one allowed or left out, like an MCP tool. */ + const offeredOther = data?.tools.offered ?? []; + const other = ( + (editedOther?.revision === data?.revision ? editedOther?.tools : undefined) ?? + data?.tools.allowed ?? + [] + ).filter((tool) => offeredOther.includes(tool)); + const setOther = (tools: string[]) => setEditedOther({ revision: data?.revision ?? null, tools }); + /** One click for an owner who trusts the agent as it is: every server, every tool. */ + const allowEverything = () => { + if (!data) return; + setEdited({ + choice: Object.fromEntries( + data.targets.map((target) => [target.resource.serverName, offeredTools(target)]), + ), + revision: data.revision, + changed: data.changed, + }); + setOther(offeredOther); + }; + const toolLabel = (tool: string) => { + const key = toolKeys.get(tool); + return key ? localize(key) : tool; + }; const setTools = (serverName: string, tools: string[]) => setEdited({ choice: { ...choice, [serverName]: tools }, @@ -227,11 +265,14 @@ function Grant({ allow: choice, // Always sent: left out, the server keeps what the stored grant asks about. ask, + // Likewise: left out, the server keeps the other tools the stored grant allows. + tools: other, }, { onSuccess: () => { setEdited(null); setEditedAsking(null); + setEditedOther(null); setMessage('success'); }, onError: () => setMessage('error'), @@ -284,6 +325,11 @@ function Grant({ {localize('com_ui_schedule_consent_ask_unavailable')}

)} + {choosing && ( + + )} + {choosing && offeredOther.length > 0 && ( +
+ + {localize('com_ui_schedule_consent_other_tools')} + + +
+ )} + {!choosing && data.tools.allowed.length > 0 && ( +

+ {localize('com_ui_schedule_consent_other_allowed', { + tools: data.tools.allowed.map(toolLabel).join(', '), + })} +

+ )} {canConfirm && data.offer && ( <> diff --git a/client/src/components/Nav/SettingsTabs/ApiKeys/__tests__/KeyGrant.spec.tsx b/client/src/components/Nav/SettingsTabs/ApiKeys/__tests__/KeyGrant.spec.tsx index 1e48b415f4..f72f2306f0 100644 --- a/client/src/components/Nav/SettingsTabs/ApiKeys/__tests__/KeyGrant.spec.tsx +++ b/client/src/components/Nav/SettingsTabs/ApiKeys/__tests__/KeyGrant.spec.tsx @@ -50,6 +50,7 @@ const view = (tools: string[]): ScheduleMCPConsentView => ({ allowed: {}, changed: {}, asking: {}, + tools: { offered: [], allowed: [] }, approval: false, required: false, requireAfterMs: null, diff --git a/client/src/components/SidePanel/Schedules/__tests__/Consent.spec.tsx b/client/src/components/SidePanel/Schedules/__tests__/Consent.spec.tsx index d7f9e1af63..4eb8bb0dea 100644 --- a/client/src/components/SidePanel/Schedules/__tests__/Consent.spec.tsx +++ b/client/src/components/SidePanel/Schedules/__tests__/Consent.spec.tsx @@ -1,8 +1,8 @@ import { createRef } from 'react'; import userEvent from '@testing-library/user-event'; import { dataService } from 'librechat-data-provider'; -import { render, screen, waitFor } from '@testing-library/react'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { render, screen, cleanup, waitFor } from '@testing-library/react'; import type { ScheduleMCPConsentView } from 'librechat-data-provider'; import Consent from '../Consent'; @@ -47,6 +47,7 @@ const view: ScheduleMCPConsentView = { allowed: {}, changed: {}, asking: {}, + tools: { offered: [], allowed: [] }, approval: false, required: false, requireAfterMs: null, @@ -106,6 +107,7 @@ it('confirms the offer reference, lifetime and only the tools the owner ticked, lifetimeHours: 1, allow: { warehouse: ['query'] }, ask: {}, + tools: [], }), ); expect(await screen.findByText('com_ui_schedule_consent_active')).toBeVisible(); @@ -278,6 +280,76 @@ it('unticks a tool the owner accepted once it is marked again under a later gran ), ); }); +it("lists each of the agent's other tools as its own item, and sends the ones the owner ticks", async () => { + jest.mocked(dataService.getScheduleMCPConsent).mockResolvedValue({ + ...view, + tools: { offered: ['execute_code', 'send_action_mail.example', 'web_search'], allowed: [] }, + }); + jest.mocked(dataService.confirmScheduleMCPConsent).mockResolvedValue(view); + display(); + // Built-in tools go by name; anything else, such as an action, by its key. + const code = await screen.findByRole('checkbox', { name: 'com_ui_schedule_consent_tool_code' }); + const search = screen.getByRole('checkbox', { name: 'com_ui_schedule_consent_tool_web' }); + const action = screen.getByRole('checkbox', { name: 'send_action_mail.example' }); + for (const box of [code, search, action]) expect(box).not.toBeChecked(); + await userEvent.click(screen.getByRole('checkbox', { name: 'query' })); + await userEvent.click(code); + await userEvent.click(action); + await userEvent.click(action); + await userEvent.click(screen.getByRole('button', { name: 'com_ui_schedule_consent_confirm' })); + await waitFor(() => + expect(dataService.confirmScheduleMCPConsent).toHaveBeenCalledWith( + 'schedule', + expect.objectContaining({ allow: { warehouse: ['query'] }, tools: ['execute_code'] }), + ), + ); +}); +it('allows every server, tool and other tool in one click', async () => { + jest.mocked(dataService.getScheduleMCPConsent).mockResolvedValue({ + ...view, + tools: { offered: ['execute_code', 'web_search'], allowed: [] }, + }); + jest.mocked(dataService.confirmScheduleMCPConsent).mockResolvedValue(view); + display(); + await userEvent.click( + await screen.findByRole('button', { name: 'com_ui_schedule_consent_allow_all' }), + ); + for (const box of screen.getAllByRole('checkbox')) expect(box).toBeChecked(); + await userEvent.click(screen.getByRole('button', { name: 'com_ui_schedule_consent_confirm' })); + await waitFor(() => + expect(dataService.confirmScheduleMCPConsent).toHaveBeenCalledWith( + 'schedule', + expect.objectContaining({ + allow: { warehouse: ['export', 'query'] }, + tools: ['execute_code', 'web_search'], + }), + ), + ); +}); +it('starts from the other tools the stored grant allows, and names them when there is nothing to confirm', async () => { + const granted = { + ...view, + state: 'active' as const, + revision: 'grant', + expiresAtMs: Date.now() + 60_000, + allowed: { warehouse: ['query'] }, + tools: { offered: ['execute_code', 'web_search'], allowed: ['web_search'] }, + }; + jest.mocked(dataService.getScheduleMCPConsent).mockResolvedValue(granted); + const first = display(); + expect( + await screen.findByRole('checkbox', { name: 'com_ui_schedule_consent_tool_web' }), + ).toBeChecked(); + expect( + screen.getByRole('checkbox', { name: 'com_ui_schedule_consent_tool_code' }), + ).not.toBeChecked(); + first.clear(); + cleanup(); + jest.mocked(dataService.getScheduleMCPConsent).mockResolvedValue({ ...granted, offer: null }); + display(); + expect(await screen.findByText('com_ui_schedule_consent_other_allowed')).toBeVisible(); + expect(screen.queryByRole('checkbox')).not.toBeInTheDocument(); +}); it('names the changed tools when there is nothing to confirm', async () => { jest.mocked(dataService.getScheduleMCPConsent).mockResolvedValue({ ...view, diff --git a/client/src/locales/en/translation.json b/client/src/locales/en/translation.json index b323c9004a..6ae6e9fdd9 100644 --- a/client/src/locales/en/translation.json +++ b/client/src/locales/en/translation.json @@ -3936,6 +3936,12 @@ "com_ui_schedule_consent_allow_server": "Allow all tools on {{server}}", "com_ui_schedule_consent_allowed": "Allowed tools: {{tools}}", "com_ui_schedule_consent_none_selected": "Choose at least one tool to confirm.", + "com_ui_schedule_consent_allow_all": "Allow everything", + "com_ui_schedule_consent_other_tools": "Other tools", + "com_ui_schedule_consent_other_allowed": "Other tools allowed: {{tools}}", + "com_ui_schedule_consent_tool_code": "Code execution", + "com_ui_schedule_consent_tool_web": "Web search", + "com_ui_schedule_consent_tool_files": "File search", "com_ui_schedule_consent_ask_first": "Ask me first", "com_ui_schedule_consent_ask_tool": "Ask me before each use of {{tool}}", "com_ui_schedule_consent_asking": "Asks you before each use: {{tools}}", diff --git a/packages/api/src/agents/__tests__/initialize.test.ts b/packages/api/src/agents/__tests__/initialize.test.ts index 2910e5dd40..e4a3cd3c46 100644 --- a/packages/api/src/agents/__tests__/initialize.test.ts +++ b/packages/api/src/agents/__tests__/initialize.test.ts @@ -7764,6 +7764,7 @@ describe("initializeAgent: under a schedule's grant", () => { 'export_mcp_warehouse', 'purge_mcp_archive', Tools.web_search, + Tools.execute_code, ]; async function initialize(attach: boolean) { const { agent, req, res, loadTools, db } = createMocks(); @@ -7772,8 +7773,11 @@ describe("initializeAgent: under a schedule's grant", () => { loadTools.mockResolvedValue({ toolDefinitions: [] }); if (attach) { // The agents select `query` and `export` on the warehouse; the owner allowed `query`. + // Of the agent's other tools the owner allowed web search and left code execution out. const fixture = await executionFixture('invoke', readTool, undefined, { offered: ['export'], + tools: [Tools.execute_code, Tools.web_search], + allowTools: [Tools.web_search], }); await fixture.factory.attach(getMCPRequestContext(req, res)!, fixture.identity, 'invoke'); } @@ -7792,16 +7796,54 @@ describe("initializeAgent: under a schedule's grant", () => { return loadTools.mock.calls.map(([call]) => call.tools as string[]); } - it('loads only the MCP tools the owner allowed, and nothing from a server left out', async () => { + it('loads only the tools the owner allowed, MCP or not, and nothing from a server left out', async () => { const [tools, ...retries] = await initialize(true); expect(retries).toEqual([]); expect(tools).toContain('query_mcp_warehouse'); expect(tools).toContain(Tools.web_search); + expect(tools).not.toContain(Tools.execute_code); expect(tools).not.toContain('export_mcp_warehouse'); // The loader is never asked about the archive server, so it is never connected to. expect(tools).not.toContain('purge_mcp_archive'); }); + it('sets up code execution and memory for the model only where the owner allowed them', async () => { + const defined = async (allowTools: string[]) => { + const { agent, req, res, loadTools, db } = createMocks(); + agent.id = 'root'; + agent.tools = ['query_mcp_warehouse', Tools.execute_code, Tools.memory]; + loadTools.mockResolvedValue({ toolDefinitions: [] }); + const fixture = await executionFixture('invoke', readTool, undefined, { + tools: [Tools.execute_code, Tools.memory], + allowTools, + }); + await fixture.factory.attach(getMCPRequestContext(req, res)!, fixture.identity, 'invoke'); + const result = await initializeAgent( + { + req, + res, + agent, + loadTools, + endpointOption: { endpoint: EModelEndpoint.agents }, + allowedProviders: new Set([agent.provider]), + isInitialAgent: true, + codeEnvAvailable: true, + memoryAvailable: true, + }, + db, + ); + return (result.toolDefinitions ?? []).map((tool) => tool.name); + }; + // The markers on the agent's tool list set these up, not the loader: left out, they must + // not be read there either, or the model is shown a tool that is then refused. + const all = await defined([Tools.execute_code, Tools.memory]); + expect(all).toEqual(expect.arrayContaining(['bash_tool', 'set_memory', 'delete_memory'])); + const none = await defined([]); + expect(none).not.toContain('bash_tool'); + expect(none).not.toContain('set_memory'); + expect(none).not.toContain('delete_memory'); + }); + it('loads everything the agent selects for a request with no grant', async () => { const [tools] = await initialize(false); expect(tools).toEqual(expect.arrayContaining(selected)); diff --git a/packages/api/src/agents/initialize.ts b/packages/api/src/agents/initialize.ts index 613f26e09f..a5953f8016 100644 --- a/packages/api/src/agents/initialize.ts +++ b/packages/api/src/agents/initialize.ts @@ -1240,7 +1240,19 @@ export async function initializeAgent( useChatProjectContext ?? params.req?.chatProjectContextEnabled === true; assertToolOptionsWithinLimits(suppliedAgent.tool_options); const inputAgent = restoreAgentInferenceInput(suppliedAgent); - const agent = shouldUseChatProjectContext ? { ...inputAgent } : inputAgent; + /** + * Under a schedule's grant an agent has only the tools its owner allowed. Narrowed here, at + * the one list everything below reads: the loader, and the markers that set up code + * execution and memory, so a tool the owner left out is not defined for the model at all. + */ + const grantedTools = + inputAgent.tools == null + ? inputAgent.tools + : narrowScheduledMCPTools(params.req, inputAgent.id, inputAgent.tools); + const agent = + shouldUseChatProjectContext || grantedTools !== inputAgent.tools + ? { ...inputAgent, tools: grantedTools } + : inputAgent; const { user, appConfig } = runtime; const requestFileOwnerId = user?.id; const requestFileOwnerScope: FileOwnerScope | undefined = requestFileOwnerId diff --git a/packages/api/src/schedules/README.md b/packages/api/src/schedules/README.md index 328eee5106..e89b3e6870 100644 --- a/packages/api/src/schedules/README.md +++ b/packages/api/src/schedules/README.md @@ -69,6 +69,28 @@ agent still selects it. Anything else is refused with `tool_policy_denied`. The operator-declared `mcpConsent.readOnlyPolicy` list is retired. The loader warns when the key is still present and ignores it. +### Tools that are not MCP tools + +An agent's other tools (code execution, web search, file search, each action, any other tool it +carries) are offered too, each as its own item, and the dialog has one control that allows +every server and tool at once. The grant stores them per agent (`tools`). Under a grant: + +- One the owner allowed loads and runs. The code environment's tools go by several names and + are all allowed by code execution. +- One the owner left out is not loaded for the run, like a left-out MCP tool, and does not keep + the schedule from running. +- A tool that reaches the run without having been offered, or the SDK's own controls aside, is + still refused with `tool_policy_denied`. +- A grant made before these tools could be chosen lists none, so none of them loads until the + owner confirms again. +- Nothing checks these tools against the stored grant when they are called, as an MCP call is + checked. The run holds them to the grant it read when it started: they stop at the grant's + deadline, and a grant that was already revoked allows none. A revocation made while a run is + under way stops its MCP calls at once and its other tools from the next run. + +A grant still needs at least one MCP tool: a schedule whose agents use no MCP server has +nothing to authorize and runs as before. An API key's grant limits MCP tools only. + ### A tool the owner approves call by call Beside allowing a tool, the owner can set it to "ask me first". The grant stores those tools diff --git a/packages/api/src/schedules/authorization/enrollment.spec.ts b/packages/api/src/schedules/authorization/enrollment.spec.ts index f40c498537..6229fcf8f1 100644 --- a/packages/api/src/schedules/authorization/enrollment.spec.ts +++ b/packages/api/src/schedules/authorization/enrollment.spec.ts @@ -5,7 +5,7 @@ import type { IUser, AppConfig, AgentGraphAccessContext } from '@librechat/data- import type { ScheduledMCPIdentity } from 'librechat-data-provider'; import type { ScheduleMCPEnrollmentDeps } from './enrollment'; import type { ParsedServerConfig } from '~/mcp/types'; -import { createScheduleMCPEnrollmentResolver } from './enrollment'; +import { createScheduleMCPEnrollmentResolver, createScheduleMCPOffer } from './enrollment'; import { allowOffered, pinStorage } from './execution.helper'; import { createScheduleMCPConsentService } from './service'; import { createResolveAgentFireAccess } from '../access'; @@ -101,6 +101,41 @@ it('requires operator-declared recipient metadata, not a successful login or too } as AppConfig); expect(await resolve(identity, {})).toEqual([]); }); +it("offers the agents' other tools beside their MCP tools, from one walk of the agent graph", async () => { + const { deps } = setup(); + jest.mocked(deps.getNodes).mockImplementation(async (ids) => + ids.map((id) => ({ + id, + provider: 'test', + model: 'test', + tools: + id === 'root' + ? ['query_mcp_warehouse', 'web_search', 'execute_code', 'send_action_mail.example'] + : ['query_mcp_warehouse', 'web_search', 'tool_search'], + agent_ids: id === 'root' ? ['child'] : [], + })), + ); + const offer = createScheduleMCPOffer(deps); + const [targets, tools] = await Promise.all([ + offer.targets(identity, {}), + offer.tools(identity, {}), + ]); + // An agent with other tools is offered a grant like any other: they no longer rule it out. + expect(targets[0].permittedTools).toEqual([ + { agentId: 'child', tools: ['query'] }, + { agentId: 'root', tools: ['query'] }, + ]); + // Each is its own item, per agent. The SDK's own controls are not the owner's to allow. + expect(tools).toEqual([ + { agentId: 'child', tools: ['web_search'] }, + { agentId: 'root', tools: ['execute_code', 'send_action_mail.example', 'web_search'] }, + ]); + expect(deps.findUser).toHaveBeenCalledTimes(1); + expect(deps.getServers).toHaveBeenCalledTimes(1); + // A grant that limits MCP tools only, as an API key's does, offers none of them. + const limited = createScheduleMCPOffer(deps, { otherTools: 'ignore' }); + expect(await limited.tools(identity, {})).toEqual([]); +}); it('denies a changed resource URL even if the configured server name is unchanged', async () => { const { deps, resolve } = setup(); jest.mocked(deps.getServers).mockResolvedValue({ diff --git a/packages/api/src/schedules/authorization/enrollment.ts b/packages/api/src/schedules/authorization/enrollment.ts index 6ab2acb5ca..cb61cdcb03 100644 --- a/packages/api/src/schedules/authorization/enrollment.ts +++ b/packages/api/src/schedules/authorization/enrollment.ts @@ -9,6 +9,7 @@ import type { MCPGrantIdentity, ScheduledMCPTarget, ScheduledMCPIdentity, + ScheduledMCPToolSelection, ScheduledMCPResourceBinding, } from 'librechat-data-provider'; import type { @@ -17,10 +18,10 @@ import type { IUser, AppConfig, } from '@librechat/data-schemas'; -import type { ScheduleMCPEnrollmentResolver } from './service'; +import type { ScheduleMCPEnrollmentResolver, ScheduleToolsResolver } from './service'; import type { GetAppConfigOptions } from '~/app/service'; import type { ParsedServerConfig } from '~/mcp/types'; -import { getScheduledMCPPolicyRevision, isScheduledMCPCandidate } from './policy'; +import { getScheduledMCPPolicyRevision, isScheduledToolItem } from './policy'; import { getScheduledMCPConfigurationRevision } from './configuration'; import { resolveScheduledMCPRequirements } from '../requirements'; import { getAppConfigOptionsFromUser } from '~/app/service'; @@ -39,6 +40,23 @@ export interface ScheduleMCPEnrollmentDeps { ) => Promise>; } +interface OfferOptions { + /** Where the operator declared the servers this kind of grant may cover. */ + resources?: ( + appConfig: AppConfig | undefined, + ) => Record | undefined; + /** + * A schedule's grant covers the whole run, so the agents' other tools are offered too, each + * to be allowed or left out. A grant that only limits MCP use leaves them alone. + */ + otherTools?: 'offer' | 'ignore'; +} + +interface Offer { + targets: ScheduledMCPTarget[]; + tools: ScheduledMCPToolSelection[]; +} + /** * Resolves what the owner can be offered: the MCP tools the agent graph selects, on servers * whose recipient the operator declared. Does not connect or mint. @@ -47,25 +65,38 @@ export function createScheduleMCPEnrollmentResolver< Identity extends MCPGrantIdentity = ScheduledMCPIdentity, >( deps: ScheduleMCPEnrollmentDeps, - options: { - /** Where the operator declared the servers this kind of grant may cover. */ - resources?: ( - appConfig: AppConfig | undefined, - ) => Record | undefined; - /** - * A schedule's grant covers the whole run, so an agent with any other kind of tool cannot - * be offered one. A grant that only limits MCP use leaves the agent's other tools alone. - */ - otherTools?: 'refuse' | 'ignore'; - } = {}, + options: OfferOptions = {}, ): ScheduleMCPEnrollmentResolver { + return createScheduleMCPOffer(deps, options).targets; +} + +/** + * Both halves of an offer, read once: the MCP tools on declared servers, and the agents' + * other tools. Asked for together, as the consent view does, they share one walk of the + * agent graph. + */ +export function createScheduleMCPOffer( + deps: ScheduleMCPEnrollmentDeps, + options: OfferOptions = {}, +): { targets: ScheduleMCPEnrollmentResolver; tools: ScheduleToolsResolver } { + const pending = new Map }>(); + const resolve = (identity: Identity, signal?: AbortSignal): Promise => { + const key = JSON.stringify(identity); + const shared = pending.get(key); + if (shared && shared.signal === signal) return shared.offer; + const offer = load(identity, signal).finally(() => { + if (pending.get(key)?.offer === offer) pending.delete(key); + }); + pending.set(key, { signal, offer }); + return offer; + }; const declared = options.resources ?? ((appConfig: AppConfig | undefined) => { const schedules = appConfig?.interfaceConfig?.schedules; return typeof schedules === 'object' ? schedules?.mcpConsent?.resources : undefined; }); - return async (identity, { signal }) => { + const load = async (identity: Identity, signal?: AbortSignal): Promise => { signal?.throwIfAborted(); const user = await deps.findUser(identity.ownerId); if (!user || (user.tenantId ?? null) !== identity.tenantId) @@ -78,7 +109,7 @@ export function createScheduleMCPEnrollmentResolver< failClosed: true, }); const bindings = declared(appConfig); - if (!bindings || !Object.keys(bindings).length) return []; + if (!bindings || !Object.keys(bindings).length) return { targets: [], tools: [] }; const [servers, { tools, candidates: candidateTools }] = await Promise.all([ deps.getServers(user, (appConfig?.mcpConfig ?? {}) as Record), resolveScheduledMCPRequirements( @@ -96,11 +127,15 @@ export function createScheduleMCPEnrollmentResolver< const names = Object.keys(servers); const aliases = buildServerNameAliases(names); const candidates = [...names, ...aliases.keys()]; - if ( - options.otherTools !== 'ignore' && - candidateTools.some(({ name }) => !isScheduledMCPCandidate(name)) - ) - throw new ScheduleMCPConsentError('consent_unavailable'); + const others = new Map>(); + if (options.otherTools !== 'ignore') { + for (const { name, agentId } of candidateTools) { + if (!isScheduledToolItem(name)) continue; + const names_ = others.get(agentId) ?? new Set(); + names_.add(name); + others.set(agentId, names_); + } + } const selected = new Map>>(); for (const { name: key, agentId } of tools) { if ( @@ -153,6 +188,15 @@ export function createScheduleMCPEnrollmentResolver< target.permittedTools.sort((a, b) => a.agentId.localeCompare(b.agentId)); target.policyRevision = getScheduledMCPPolicyRevision(target.permittedTools); } - return [...targets.values()]; + return { + targets: [...targets.values()], + tools: [...others] + .map(([agentId, selected]) => ({ agentId, tools: [...selected].sort() })) + .sort((a, b) => a.agentId.localeCompare(b.agentId)), + }; + }; + return { + targets: async (identity, { signal }) => (await resolve(identity, signal)).targets, + tools: async (identity, { signal }) => (await resolve(identity, signal)).tools, }; } diff --git a/packages/api/src/schedules/authorization/execution.helper.ts b/packages/api/src/schedules/authorization/execution.helper.ts index a30a6770cf..50e43fde97 100644 --- a/packages/api/src/schedules/authorization/execution.helper.ts +++ b/packages/api/src/schedules/authorization/execution.helper.ts @@ -99,13 +99,18 @@ export async function executionFixture( /** * `offered` adds tools the agents select; `allow` is what the owner picks (default: `tool`); * `ask` are the allowed tools the owner approves call by call; `approvedToolCalls` are the - * calls the owner approved in the request that resumed the run. + * calls the owner approved in the request that resumed the run. `tools` are the agents' tools + * that are not MCP tools, and `allowTools` the ones of those the owner picks (default: none). + * `coversTools: false` is a grant that limits MCP tools only, as an API key's does. */ choice: { offered?: string[]; allow?: string[]; ask?: string[]; approvedToolCalls?: string[]; + tools?: string[]; + allowTools?: string[]; + coversTools?: boolean; } = {}, ) { let time = 1000; @@ -177,6 +182,10 @@ export async function executionFixture( const service = createScheduleMCPConsentService({ storage, resolveEnrollment, + resolveTools: async () => + choice.tools?.length + ? ['child', 'root'].map((agentId) => ({ agentId, tools: [...choice.tools!].sort() })) + : [], getLimits: async () => ({ enabled: true, maxLifetimeHours: 24 }), canUse: async () => allowed, canPause: async () => true, @@ -189,9 +198,15 @@ export async function executionFixture( lifetimeHours: 1, allow: { warehouse: choice.allow ?? [tool.name] }, ...(choice.ask && { ask: { warehouse: choice.ask } }), + ...(choice.allowTools && { tools: choice.allowTools }), }); const loadAuthorization = jest.fn(async () => ({ authority: service.authority })); - const factory = createScheduleMCPExecution({ storage, loadAuthorization }); + const factory = createScheduleMCPExecution({ + storage, + loadAuthorization, + coversTools: choice.coversTools ?? true, + now: () => time, + }); const execution = (await factory.resolve(identity, stage, { approvedToolCalls: choice.approvedToolCalls, }))!; diff --git a/packages/api/src/schedules/authorization/execution.spec.ts b/packages/api/src/schedules/authorization/execution.spec.ts index e7ba5e4374..8db93bdd85 100644 --- a/packages/api/src/schedules/authorization/execution.spec.ts +++ b/packages/api/src/schedules/authorization/execution.spec.ts @@ -1,6 +1,7 @@ import { Constants } from '@librechat/agents'; import { SCHEDULE_APPROVAL_REASON } from 'librechat-data-provider'; import type { Tool } from '@modelcontextprotocol/sdk/types.js'; +import type { HookCallback } from '@librechat/agents'; import { scheduleAsksEveryUse, narrowScheduledMCPTools, @@ -347,6 +348,47 @@ it('installs an independent ceiling for root, child, handoff, and lazy tools wit ).toEqual({}); }); +it('lets a run use the other tools its owner allowed, and code tools only with code execution', async () => { + const f = await executionFixture('invoke', readTool, undefined, { + tools: ['execute_code', 'send_action_mail.example', 'web_search'], + allowTools: ['execute_code', 'web_search'], + }); + const mcp = [{ name: 'query_mcp_warehouse', serverName: 'warehouse', toolType: 'mcp' }]; + const decide = (execution: typeof f.execution, toolName: string, executingAgentId = 'root') => + createScheduledMCPRunPolicy(execution, [ + { id: 'root', toolDefinitions: mcp }, + { id: 'child', toolDefinitions: mcp }, + ]).hook( + { toolName, toolInput: {}, executingAgentId } as Parameters>[0], + new AbortController().signal, + ); + const refused = { decision: 'deny', reason: expect.any(String) }; + expect(await decide(f.execution, 'web_search')).toEqual({}); + expect(await decide(f.execution, 'web_search', 'child')).toEqual({}); + // The code environment's tools go by several names; code execution allows them all. + expect(await decide(f.execution, 'execute_code')).toEqual({}); + expect(await decide(f.execution, 'bash_tool')).toEqual({}); + // Left out by the owner, or not something the owner was offered at all. + expect(await decide(f.execution, 'send_action_mail.example')).toEqual(refused); + expect(await decide(f.execution, 'memory')).toEqual(refused); + + // Memory runs as two tools of other names, both allowed by the one item. + const remembers = await executionFixture('invoke', readTool, undefined, { + tools: ['memory', 'web_search'], + allowTools: ['memory'], + }); + expect(await decide(remembers.execution, 'set_memory')).toEqual({}); + expect(await decide(remembers.execution, 'delete_memory')).toEqual({}); + expect(await decide(f.execution, 'set_memory')).toEqual(refused); + + const without = await executionFixture('invoke', readTool, undefined, { + tools: ['execute_code', 'web_search'], + allowTools: ['web_search'], + }); + expect(await decide(without.execution, 'bash_tool')).toEqual(refused); + expect(await decide(without.execution, 'web_search')).toEqual({}); +}); + it('refuses a consent deadline crossed while the final database admission is in flight', async () => { const f = await executionFixture(); jest.mocked(f.storage.admitScheduleMCPConsent).mockImplementationOnce(async () => { @@ -490,41 +532,89 @@ it.each(['consent_revoked', 'rbac_denied', 'binding_mismatch'] as const)( ); describe('the tools a run loads under a grant', () => { - it('keeps what the owner allowed and every tool that is not an MCP tool', async () => { - const fixture = await executionFixture('invoke', readTool, undefined, { offered: ['export'] }); - const keys = [ + const keys = [ + 'query_mcp_warehouse', + 'export_mcp_warehouse', + 'purge_mcp_archive', + 'web_search', + 'execute_code', + 'send_action_mail.example', + // The SDK's own controls are not the owner's to allow: they always load. + 'tool_search', + ]; + + it('keeps the MCP tools and the other tools the owner allowed, each on its own', async () => { + const fixture = await executionFixture('invoke', readTool, undefined, { + offered: ['export'], + tools: ['execute_code', 'send_action_mail.example', 'web_search'], + allowTools: ['send_action_mail.example', 'web_search'], + }); + expect(fixture.execution.narrow('root', keys)).toEqual([ 'query_mcp_warehouse', - 'export_mcp_warehouse', - 'purge_mcp_archive', 'web_search', - 'execute_code', - ]; + 'send_action_mail.example', + 'tool_search', + ]); + expect(fixture.execution.grantsTool('root', 'web_search')).toBe(true); + expect(fixture.execution.grantsTool('root', 'execute_code')).toBe(false); + // A grant is per agent: one it does not list keeps only the controls. + expect(fixture.execution.narrow('stranger', keys)).toEqual(['tool_search']); + expect(fixture.execution.grantsTool('stranger', 'web_search')).toBe(false); + }); + + it('loads none of the other tools when the owner allowed none', async () => { + const fixture = await executionFixture('invoke', readTool, undefined, { + tools: ['execute_code', 'web_search'], + }); + expect(fixture.execution.narrow('root', keys)).toEqual(['query_mcp_warehouse', 'tool_search']); + }); + + it('stops loading and allowing the other tools once the grant has run out or been revoked', async () => { + const choice = { tools: ['execute_code', 'web_search'], allowTools: ['web_search'] }; + const fixture = await executionFixture('invoke', readTool, undefined, choice); + expect(fixture.execution.narrow('root', keys)).toContain('web_search'); + // Nothing checks these tools at the call as an MCP call is checked, so the run does. + fixture.expire(); + expect(fixture.execution.grantsTool('root', 'web_search')).toBe(false); + // The MCP tool still loads: its call is refused where it is made. + expect(fixture.execution.narrow('root', keys)).toEqual(['query_mcp_warehouse', 'tool_search']); + + const revoked = await executionFixture('invoke', readTool, undefined, choice); + await revoked.revoke(); + const next = (await revoked.factory.resolve(revoked.identity, 'invoke'))!; + expect(next.grantsTool('root', 'web_search')).toBe(false); + expect(next.narrow('root', keys)).not.toContain('web_search'); + }); + + it('leaves the other tools alone under a grant that limits MCP tools only', async () => { + const fixture = await executionFixture('invoke', readTool, undefined, { coversTools: false }); expect(fixture.execution.narrow('root', keys)).toEqual([ 'query_mcp_warehouse', 'web_search', 'execute_code', + 'send_action_mail.example', + 'tool_search', ]); - // A grant is per agent: one it does not list keeps no MCP tool. - expect(fixture.execution.narrow('stranger', keys)).toEqual(['web_search', 'execute_code']); + expect(fixture.execution.grantsTool('root', 'web_search')).toBe(false); }); 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 = [ + const wildcards = [ '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', + 'tool_search', ]; // "Every tool" names none of them, so it would define what the owner left out. - expect(fixture.execution.narrow('root', keys)).toEqual([ + expect(fixture.execution.narrow('root', wildcards)).toEqual([ 'query_mcp_warehouse', 'sys__server__sys_mcp_warehouse', - 'web_search', + 'tool_search', ]); - expect(fixture.execution.narrow('stranger', keys)).toEqual(['web_search']); + expect(fixture.execution.narrow('stranger', wildcards)).toEqual(['tool_search']); }); it('leaves the tools of a schedule nobody authorized as they are', async () => { diff --git a/packages/api/src/schedules/authorization/execution.ts b/packages/api/src/schedules/authorization/execution.ts index fb3a4cee6c..d6a1c9e836 100644 --- a/packages/api/src/schedules/authorization/execution.ts +++ b/packages/api/src/schedules/authorization/execution.ts @@ -14,7 +14,11 @@ import type { ResolvedToolApprovalHook } from '~/agents/hitl/hooks'; import type { MCPToolsSnapshot } from '~/mcp/connection'; import type { ScheduledMCPAuthority } from './contract'; import type { ScheduledTokenContext } from '../context'; -import { getScheduledMCPToolDefinitionDigest, ScheduledMCPPolicyError } from './policy'; +import { + isScheduledToolItem, + ScheduledMCPPolicyError, + getScheduledMCPToolDefinitionDigest, +} from './policy'; import { getScheduledMCPConfigurationRevision } from './configuration'; import { ScheduledMCPBearerError } from '~/mcp/errors'; import { findMCPRequestContext } from '~/mcp/request'; @@ -55,10 +59,16 @@ export interface ScheduleMCPExecution boolean; /** - * The agent's tool keys the run may load: every key that is not an MCP tool, and the MCP - * tools the grant lists for the agent. A run that has no grant keeps them all. + * The agent's tool keys the run may load: the MCP tools the grant lists for the agent, and, + * where the grant covers the agent's other tools, the ones it lists of those. A run that has + * no grant keeps them all. */ readonly narrow: (agentId: string, toolKeys: readonly string[]) => string[]; + /** + * Whether the grant lists this tool, one that is not an MCP tool, for the agent. Always + * false where the grant does not cover the agents' other tools. + */ + readonly grantsTool: (agentId: string, toolKey: string) => boolean; /** Whether the grant asks the owner about any tool, so this run may pause. */ readonly mayAsk: boolean; /** Whether the owner approves each use of this tool, by the name the run calls it. */ @@ -141,6 +151,12 @@ export interface ScheduleMCPExecutionDeps< }>; /** Whether a subject with no grant may not use MCP tools. Absent, it always may. */ requiresGrant?: (identity: Identity) => Promise; + /** + * Whether a grant also decides which of the agents' other tools a run loads, so that one + * it does not list is left out. Absent, a grant limits MCP tools only. + */ + coversTools?: boolean; + now?: () => number; } export interface ScheduleMCPExecutionOptions { @@ -195,6 +211,22 @@ export function createScheduleMCPExecution< new Map(permittedTools.map(({ agentId, tools }) => [agentId, new Set(tools)])), ]), ); + /** + * Until when the grant read for this run allows anything: its earliest deadline, or not at + * all once any part of it was revoked. An MCP call is checked against the stored grant when + * it is made; the agents' other tools have no such call, so they are held to this. + */ + const consents = snapshot.enrollment?.consents ?? []; + const liveUntil = consents.some((consent) => consent.revokedAtMs != null) + ? 0 + : Math.min(...consents.map((consent) => consent.absoluteExpiresAtMs)); + const now = deps.now ?? Date.now; + /** The agents' tools that are not MCP tools, as the owner allowed them. */ + const others = new Map>( + deps.coversTools === true + ? (snapshot.enrollment?.tools ?? []).map(({ agentId, tools }) => [agentId, new Set(tools)]) + : [], + ); const grantedServers = [...granted.keys()]; const serverAliases = buildServerNameAliases(grantedServers); const serverSpellings = [...grantedServers, ...serverAliases.keys()]; @@ -227,6 +259,13 @@ export function createScheduleMCPExecution< if (keys.size > 0) asking.set(agentId, keys); } } + const allows = (agentId: string, toolKey: string): boolean => + others.get(agentId)?.has(toolKey) === true && now() < liveUntil; + /** Whether a run under the grant loads this key of the agent's. */ + const loads = (agentId: string, toolKey: string): boolean => { + if (toolKey.includes(Constants.mcp_delimiter)) return isGrantedKey(agentId, toolKey); + return deps.coversTools !== true || !isScheduledToolItem(toolKey) || allows(agentId, toolKey); + }; let fenced = false; /** Asked at the first MCP call, so a run that uses none pays nothing. Kept for the run. */ let required: Promise | undefined; @@ -266,11 +305,8 @@ export function createScheduleMCPExecution< grants: (agentId: string, serverName: string, selectionName: string) => granted.get(serverName)?.get(agentId)?.has(selectionName) === true, narrow: (agentId: string, toolKeys: readonly string[]) => - enrolled - ? toolKeys.filter( - (key) => !key.includes(Constants.mcp_delimiter) || isGrantedKey(agentId, key), - ) - : [...toolKeys], + enrolled ? toolKeys.filter((key) => loads(agentId, key)) : [...toolKeys], + grantsTool: allows, mayAsk: asking.size > 0, asksAbout: (agentId: string, toolKey: string) => asking.get(agentId)?.has(toolKey) === true, asksAboutAny: (toolKeys: readonly string[]) => diff --git a/packages/api/src/schedules/authorization/host.ts b/packages/api/src/schedules/authorization/host.ts index 3c3b22410c..a2c428cab2 100644 --- a/packages/api/src/schedules/authorization/host.ts +++ b/packages/api/src/schedules/authorization/host.ts @@ -6,7 +6,11 @@ import { } from 'librechat-data-provider'; import type { ScheduleMCPConsentStorage, AppConfig, IUser } from '@librechat/data-schemas'; import type { ScheduledMCPIdentity, TCheckpointerConfig } from 'librechat-data-provider'; -import type { ScheduleMCPEnrollmentResolver, ScheduleMCPConsentService } from './service'; +import type { + ScheduleToolsResolver, + ScheduleMCPConsentService, + ScheduleMCPEnrollmentResolver, +} from './service'; import type { ScheduleMCPConsentHandlers } from './handlers'; import type { CheckAccessParams } from '~/middleware/access'; import type { GetAppConfigOptions } from '~/app/service'; @@ -42,6 +46,7 @@ export function createScheduleMCPConsentHost(deps: { getRoleByName: CheckAccessParams['getRoleByName']; canViewAgent: (agentId: string, user: IUser) => Promise; resolveEnrollment?: ScheduleMCPEnrollmentResolver; + resolveTools?: ScheduleToolsResolver; /** Whether a scheduled run of this owner can pause for approval. Absent, it cannot. */ canPauseRuns?: (user: IUser) => Promise; }): { service: ScheduleMCPConsentService; handlers: ScheduleMCPConsentHandlers } { @@ -99,6 +104,7 @@ export function createScheduleMCPConsentHost(deps: { return permissions.every(Boolean) && (await deps.canViewAgent(identity.agentId, user)); }, resolveEnrollment: deps.resolveEnrollment, + resolveTools: deps.resolveTools, canPause: async (identity) => { const user = await loadPrincipal(identity); return user != null && (await deps.canPauseRuns?.(user)) === true; diff --git a/packages/api/src/schedules/authorization/policy.ts b/packages/api/src/schedules/authorization/policy.ts index 31b1a3714d..f390a4e569 100644 --- a/packages/api/src/schedules/authorization/policy.ts +++ b/packages/api/src/schedules/authorization/policy.ts @@ -23,6 +23,15 @@ export function isScheduledMCPCandidate(name: string): boolean { ); } +/** + * A tool an agent carries that is neither an MCP tool nor one of the SDK's own controls: + * code execution, web search, file search, an action. Each is its own item for the owner to + * allow or leave out. + */ +export function isScheduledToolItem(name: string): boolean { + return !name.includes(Constants.mcp_delimiter) && !isScheduledMCPCandidate(name); +} + /** Stable across object insertion order. Arrays retain their semantic order. */ function canonical(value: unknown): unknown { if (Array.isArray(value)) return value.map(canonical); diff --git a/packages/api/src/schedules/authorization/run.ts b/packages/api/src/schedules/authorization/run.ts index ac2728cb45..6004dc9fd2 100644 --- a/packages/api/src/schedules/authorization/run.ts +++ b/packages/api/src/schedules/authorization/run.ts @@ -1,5 +1,6 @@ import { Constants } from '@librechat/agents'; import { + Tools, normalizeServerName, SCHEDULE_APPROVAL_REASON, Constants as ProviderConstants, @@ -7,8 +8,12 @@ import { import type { HookCallback } from '@librechat/agents'; import type { ScheduleMCPExecution } from './execution'; import { ASK_USER_QUESTION_TOOL_NAME } from '~/agents/hitl/askUserQuestionTool'; +import { SET_MEMORY_TOOL_NAME, DELETE_MEMORY_TOOL_NAME } from '~/agents/memory'; +import { isStatefulCodeEnvironmentToolName } from '~/agents/hitl/byom'; import { ScheduledMCPPolicyError } from './policy'; +const MEMORY_TOOL_NAMES = new Set([SET_MEMORY_TOOL_NAME, DELETE_MEMORY_TOOL_NAME]); + interface HandoffEdge { from: string | string[]; to: string | string[]; @@ -39,7 +44,10 @@ export interface ScheduledMCPPolicyAgent { export const SCHEDULE_TOOL_NOT_ALLOWED = 'The owner of this schedule has not allowed this tool. Continue without it.'; -/** No direct actions, arbitrary code or unlisted tools in an enrolled run. */ +/** + * An enrolled run uses what its owner allowed: the MCP tools and the other tools the grant + * lists, the SDK's own controls, and nothing else. + */ export function createScheduledMCPRunPolicy( execution: ScheduleMCPExecution, agents: readonly ScheduledMCPPolicyAgent[], @@ -143,6 +151,16 @@ export function createScheduledMCPRunPolicy( Constants.TOOL_SEARCH, ASK_USER_QUESTION_TOOL_NAME, ]); + /** + * Whether the grant lists this tool of the agent's, one that is not an MCP tool. Some items + * the owner allows run as tools of other names: the code environment's tools are all allowed + * by code execution, and the two memory tools by memory. + */ + const allowsTool = (agentId: string, name: string): boolean => + execution.grantsTool(agentId, name) || + (isStatefulCodeEnvironmentToolName(name) && + execution.grantsTool(agentId, Tools.execute_code)) || + (MEMORY_TOOL_NAMES.has(name) && execution.grantsTool(agentId, Tools.memory)); const denied = (input: Parameters>[0]): boolean => { const agentId = input.executingAgentId; // Foreground delegation stays in this guarded run; detached continuations do not. @@ -158,7 +176,8 @@ export function createScheduledMCPRunPolicy( mcpTools.has(agentId) && (controls.has(input.toolName) || handoffs.get(agentId)?.has(input.toolName) === true || - mcpTools.get(agentId)!.has(input.toolName)) + mcpTools.get(agentId)!.has(input.toolName) || + allowsTool(agentId, input.toolName)) ); }; const decisions = new WeakMap>(); diff --git a/packages/api/src/schedules/authorization/runtime.spec.ts b/packages/api/src/schedules/authorization/runtime.spec.ts index fca19a7f2a..914f9bf596 100644 --- a/packages/api/src/schedules/authorization/runtime.spec.ts +++ b/packages/api/src/schedules/authorization/runtime.spec.ts @@ -199,10 +199,14 @@ it('uses the same real enrollment policy at readiness, root, and child invocatio } }); -it.each( - [[], ['execute_code'], ['query_mcp_warehouse', 'write_action_api']].map((tools) => ({ tools })), -)( - 'refuses prohibited or empty protected candidate sets before connecting: %s', +it('does not refuse a run because its agent also has tools that are not MCP tools', async () => { + const f = await setup(); + f.setCandidates(['query_mcp_warehouse', 'write_action_api', 'execute_code']); + await expect(f.check()).resolves.toBeDefined(); +}); + +it.each([[], ['execute_code']].map((tools) => ({ tools })))( + 'refuses a protected run that selects no MCP tool before connecting: %s', async ({ tools }) => { const f = await setup(); f.setCandidates(tools); diff --git a/packages/api/src/schedules/authorization/runtime.ts b/packages/api/src/schedules/authorization/runtime.ts index 6a7ab2913d..520eb28839 100644 --- a/packages/api/src/schedules/authorization/runtime.ts +++ b/packages/api/src/schedules/authorization/runtime.ts @@ -16,9 +16,9 @@ import { } from './execution'; import { getScheduleSource, toScheduledTokenContext } from '~/agents/authority/context'; import { readScheduleFireContext, isScheduleFireRequest } from '../trigger'; -import { createScheduleMCPEnrollmentResolver } from './enrollment'; import { getAppConfigOptionsFromUser } from '~/app/service'; import { createScheduleLimitsResolver } from '../service'; +import { createScheduleMCPOffer } from './enrollment'; import { createScheduleMCPConsentHost } from './host'; import { isOwnedAbortError } from '~/utils/errors'; import { ScheduledMCPPolicyError } from './policy'; @@ -30,7 +30,7 @@ type RuntimeRequest = Parameters[0] & { export function createScheduleMCPRuntimeHost( deps: Omit< Parameters[0], - 'resolveEnrollment' | 'getLimits' + 'resolveEnrollment' | 'resolveTools' | 'getLimits' > & { enrollment: ScheduleMCPEnrollmentDeps; getScheduleMCPCompletionState?: ScheduleMCPCompletionLookup; @@ -53,12 +53,13 @@ export function createScheduleMCPRuntimeHost( }) => Promise; } { function createEvaluation(enrollment: ScheduleMCPEnrollmentDeps) { - const resolveEnrollment = createScheduleMCPEnrollmentResolver(enrollment); + const offer = createScheduleMCPOffer(enrollment); const consent = createScheduleMCPConsentHost({ ...deps, findUser: enrollment.findUser, getLimits: createScheduleLimitsResolver((options = {}) => enrollment.getAppConfig(options)), - resolveEnrollment, + resolveEnrollment: offer.targets, + resolveTools: offer.tools, }); return { consent }; } @@ -76,6 +77,8 @@ export function createScheduleMCPRuntimeHost( const execution = createScheduleMCPExecution({ storage: deps.methods, requiresGrant: required, + // A schedule's grant covers the whole run: the agents' other tools load only if listed. + coversTools: true, async loadAuthorization(identity) { // Private to this attempt. No resolved principal, config or allow decision survives it. const principal = deps.findUser(identity.ownerId).then((user) => { diff --git a/packages/api/src/schedules/authorization/service.spec.ts b/packages/api/src/schedules/authorization/service.spec.ts index c13d03334a..78f7f4a543 100644 --- a/packages/api/src/schedules/authorization/service.spec.ts +++ b/packages/api/src/schedules/authorization/service.spec.ts @@ -45,6 +45,8 @@ function setup() { enrollment: null, }; let targets = [structuredClone(target)]; + /** The agents' tools that are not MCP tools. None unless a test offers some. */ + let others: { agentId: string; tools: string[] }[] = []; let allowed = true; let pausable = true; const storage: ScheduleMCPConsentStorage = { @@ -86,11 +88,13 @@ function setup() { canPause: async () => pausable, getLimits: async () => ({ enabled: true, maxLifetimeHours: 24 }), resolveEnrollment: async () => targets, + resolveTools: async () => others, now: () => time, }); const enroll = async ( allow: Record = { warehouse: ['query'] }, ask?: Record, + tools?: string[], ) => { const view = await service.view(identity); return service.confirm(identity, { @@ -99,6 +103,7 @@ function setup() { lifetimeHours: 1, allow, ...(ask && { ask }), + ...(tools && { tools }), }); }; return { @@ -112,6 +117,9 @@ function setup() { changeTarget: (...value: ScheduledMCPTarget[]) => { targets = value; }, + offerTools: (...value: { agentId: string; tools: string[] }[]) => { + others = value; + }, deny: () => { allowed = false; }, @@ -409,6 +417,84 @@ it('offers no overwrite of an incompatible stored grant', async () => { }); }); +describe("the agents' tools that are not MCP tools", () => { + const offered = [ + { agentId: 'child', tools: ['web_search'] }, + { agentId: 'root', tools: ['execute_code', 'send_action_mail.example', 'web_search'] }, + ]; + + it('offers each of them once, and nothing has been allowed before the owner chooses', async () => { + const s = setup(); + s.offerTools(...offered); + expect(await s.service.view(identity)).toMatchObject({ + state: 'missing', + tools: { offered: ['execute_code', 'send_action_mail.example', 'web_search'], allowed: [] }, + }); + }); + + it('stores the ones the owner allows for every agent that has them, and no others', async () => { + const s = setup(); + s.offerTools(...offered); + const view = await s.enroll(undefined, undefined, ['execute_code', 'web_search']); + expect(view).toMatchObject({ + state: 'active', + tools: { allowed: ['execute_code', 'web_search'] }, + }); + expect(s.snapshot.enrollment!.tools).toEqual([ + { agentId: 'child', tools: ['web_search'] }, + { agentId: 'root', tools: ['execute_code', 'web_search'] }, + ]); + }); + + it('stores nothing when the owner allows none, and still grants the MCP tools', async () => { + const s = setup(); + s.offerTools(...offered); + expect(await s.enroll(undefined, undefined, [])).toMatchObject({ + state: 'active', + tools: { allowed: [] }, + }); + expect(s.snapshot.enrollment).not.toHaveProperty('tools'); + }); + + it('rejects a tool that is not on offer without writing', async () => { + const s = setup(); + s.offerTools(...offered); + await expect(s.enroll(undefined, undefined, ['file_search'])).rejects.toMatchObject({ + code: 'consent_invalid', + }); + expect(s.storage.confirmScheduleMCPConsent).not.toHaveBeenCalled(); + }); + + it('keeps the stored choice when a confirmation does not say, for the tools still on offer', async () => { + const s = setup(); + s.offerTools(...offered); + await s.enroll(undefined, undefined, ['execute_code', 'web_search']); + // The agent drops code execution; a client that does not send `tools` renews the grant. + s.offerTools({ agentId: 'root', tools: ['web_search'] }); + expect(await s.enroll()).toMatchObject({ tools: { allowed: ['web_search'] } }); + expect(s.snapshot.enrollment!.tools).toEqual([{ agentId: 'root', tools: ['web_search'] }]); + }); + + it('binds a confirmation to the tools the owner was shown', async () => { + const s = setup(); + const before = await s.service.view(identity); + s.offerTools(...offered); + const shown = await s.service.view(identity); + // An offer with no such tools keeps the digest it had before they could be offered. + expect(shown.offer!.digest).not.toBe(before.offer!.digest); + s.offerTools({ agentId: 'root', tools: ['execute_code'] }); + await expect( + s.service.confirm(identity, { + offerDigest: shown.offer!.digest, + expectedRevision: null, + lifetimeHours: 1, + allow: { warehouse: ['query'] }, + tools: ['execute_code'], + }), + ).rejects.toMatchObject({ code: 'consent_changed' }); + }); +}); + describe('definition pins', () => { const first = 'a'.repeat(64); const second = 'b'.repeat(64); diff --git a/packages/api/src/schedules/authorization/service.ts b/packages/api/src/schedules/authorization/service.ts index 24b4cad61e..5592f3b6ea 100644 --- a/packages/api/src/schedules/authorization/service.ts +++ b/packages/api/src/schedules/authorization/service.ts @@ -8,6 +8,7 @@ import type { ScheduledMCPResource, ScheduledMCPTarget, ScheduleMCPConsentView, + ScheduledMCPToolSelection, ConfirmScheduleMCPConsent, } from 'librechat-data-provider'; import type { MCPConsentStorage } from '@librechat/data-schemas'; @@ -44,6 +45,12 @@ export type ScheduleMCPEnrollmentResolver< options: { signal?: AbortSignal }, ) => Promise; +/** The agents' tools that are not MCP tools, each of which the owner may allow or leave out. */ +export type ScheduleToolsResolver = ( + identity: Identity, + options: { signal?: AbortSignal }, +) => Promise; + /** The grant's subject is a schedule unless said otherwise: an API key and agent is the other. */ export interface ScheduleMCPConsentDeps { storage: MCPConsentStorage; @@ -52,6 +59,8 @@ export interface ScheduleMCPConsentDeps Promise; /** What the owner can be offered now: reachable agents' selected tools, per declared server. */ resolveEnrollment?: ScheduleMCPEnrollmentResolver; + /** The agents' other tools on offer. Absent, a grant covers MCP tools only. */ + resolveTools?: ScheduleToolsResolver; /** Whether a run of this subject can pause for the owner's approval here. Absent, it cannot. */ canPause?: (identity: Identity) => Promise; now?: () => number; @@ -207,6 +216,30 @@ function coversGrant( }); } +/** Every tool named in a set of per-agent selections, once. */ +function toolNames(selections: readonly ScheduledMCPToolSelection[]): string[] { + return [...new Set(selections.flatMap((selection) => selection.tools))].sort(); +} + +/** + * Of the agents' tools that are not MCP tools, the ones the owner allows, per agent that has + * them. Each must be on offer. A confirmation that does not say keeps what the stored grant + * allows, for the tools still on offer. + */ +function selectTools( + offered: readonly ScheduledMCPToolSelection[], + chosen: readonly string[] | undefined, + stored: readonly ScheduledMCPToolSelection[] = [], +): ScheduledMCPToolSelection[] { + const onOffer = new Set(toolNames(offered)); + const names = new Set(chosen ?? toolNames(stored).filter((tool) => onOffer.has(tool))); + if ([...names].some((tool) => !onOffer.has(tool))) + throw new ScheduleMCPConsentError('consent_invalid'); + return offered + .map(({ agentId, tools }) => ({ agentId, tools: tools.filter((tool) => names.has(tool)) })) + .filter((selection) => selection.tools.length > 0); +} + /** Allowed tools the grant asks the owner about before each use, by server. */ function askingTools( consents: readonly MCPGrantConsent[], @@ -257,6 +290,7 @@ function offerDigest( configRevision: number, targets: readonly ScheduledMCPTarget[], changed: Record, + tools: readonly ScheduledMCPToolSelection[], ): string { return createHash('sha256') .update( @@ -266,6 +300,8 @@ function offerDigest( targets, // Left out while nothing is marked, so an offer made before marks existed still holds. ...(Object.keys(changed).length > 0 && { changed }), + // Likewise for an offer with no other tools to choose from. + ...(tools.length > 0 && { tools }), }), ) .digest('hex'); @@ -300,6 +336,9 @@ export function createScheduleMCPConsentService< const identityMatches = (a: Identity, b: Identity): boolean => a.invocationMode === 'delegated' && fields(a) === fields(b); + /** What each view offered beyond its MCP targets, per agent, for the confirmation of it. */ + const offers = new WeakMap(); + async function view( identity: Identity, options: { signal?: AbortSignal } = {}, @@ -335,6 +374,8 @@ export function createScheduleMCPConsentService< const allowedByServer = allowedTools(granted); const changedByServer = changedTools(enrollment?.consents ?? []); const askingByServer = askingTools(enrollment?.consents ?? []); + const allowedOther = toolNames(enrollment?.tools ?? []); + let offeredOther: readonly ScheduledMCPToolSelection[] = []; const required = !enrollment && requiresGrant(limits, snapshot, now()); const requireAfterMs = !enrollment && limits.enabled && !required ? (limits.requireAfterMs ?? null) : null; @@ -350,6 +391,7 @@ export function createScheduleMCPConsentService< allowed: {}, changed: {}, asking: {}, + tools: { offered: [], allowed: [] }, approval, required: false, requireAfterMs: null, @@ -359,7 +401,10 @@ export function createScheduleMCPConsentService< if (allowed && limits.enabled && deps.resolveEnrollment) { let resolved: readonly ScheduledMCPTarget[]; try { - resolved = await deps.resolveEnrollment(identity, options); + [resolved, offeredOther] = await Promise.all([ + deps.resolveEnrollment(identity, options), + deps.resolveTools?.(identity, options) ?? [], + ]); } catch (error) { if (!(error instanceof ScheduleMCPConsentError) || !enrollment) throw error; return { @@ -370,6 +415,7 @@ export function createScheduleMCPConsentService< allowed: allowedByServer, changed: changedByServer, asking: askingByServer, + tools: { offered: [], allowed: allowedOther }, approval, required, requireAfterMs, @@ -385,18 +431,25 @@ export function createScheduleMCPConsentService< allowed: allowedByServer, changed: changedByServer, asking: askingByServer, + tools: { offered: [], allowed: allowedOther }, approval, required, requireAfterMs, offer: null, }; targets = canonicalTargets(resolved); - const digest = offerDigest(identity, snapshot.configRevision, targets, changedByServer); + const digest = offerDigest( + identity, + snapshot.configRevision, + targets, + changedByServer, + offeredOther, + ); offer = { digest, maxLifetimeHours: limits.maxLifetimeHours }; if (state === 'active' && !coversGrant(targets, granted)) state = 'changed'; } else if (!enrollment) state = 'unsupported'; options.signal?.throwIfAborted(); - return { + const result: ScheduleMCPConsentView = { state, revision: enrollment?.revision ?? null, expiresAtMs, @@ -404,11 +457,14 @@ export function createScheduleMCPConsentService< allowed: allowedByServer, changed: changedByServer, asking: askingByServer, + tools: { offered: toolNames(offeredOther), allowed: allowedOther }, approval, required, requireAfterMs, offer, }; + offers.set(result, offeredOther); + return result; } async function confirm( @@ -430,15 +486,18 @@ export function createScheduleMCPConsentService< throw new ScheduleMCPConsentError('consent_invalid'); const snapshot = await deps.storage.readScheduleMCPConsent(identity); if (!snapshot) throw new ScheduleMCPConsentError('consent_not_found'); + const offered = offers.get(current) ?? []; // Recompute the digest with the final observed row so an edit cannot reuse an old offer. const digest = offerDigest( identity, snapshot.configRevision, current.targets, changedTools(snapshot.enrollment?.consents ?? []), + offered, ); if (digest !== input.offerDigest) throw new ScheduleMCPConsentError('consent_changed'); const chosen = selectAllowed(current.targets, input.allow); + const others = selectTools(offered, input.tools, snapshot.enrollment?.tools); const stored = askingTools(snapshot.enrollment?.consents ?? []); const asking = selectAsking(chosen, input.ask, stored); // A tool nobody can approve would only ever be refused. Say so now, not at 3 a.m. One the @@ -476,6 +535,7 @@ export function createScheduleMCPConsentService< askTools: asking[target.resource.serverName], }), })), + ...(others.length > 0 && { tools: others }), }; const committed = await deps.storage.confirmScheduleMCPConsent({ identity, diff --git a/packages/api/src/schedules/mcp.ts b/packages/api/src/schedules/mcp.ts index a8715f2be8..5861ce220a 100644 --- a/packages/api/src/schedules/mcp.ts +++ b/packages/api/src/schedules/mcp.ts @@ -43,11 +43,11 @@ import { findShadowedServerNames, createDeadlineAbortSignal, } from '../mcp/utils'; -import { ScheduledMCPPolicyError, isScheduledMCPCandidate } from './authorization/policy'; import { MCPConfigInitializationCanceledError } from '../mcp/registry/MCPServersRegistry'; import { createMCPRequestContext, cleanupMCPRequestContext } from '../mcp/request'; import { attachScheduledMCPBearer, ScheduledMCPBearerError } from './bearer'; import { toScheduledTokenContext } from '~/agents/authority/context'; +import { ScheduledMCPPolicyError } from './authorization/policy'; import { resolveScheduledMCPRequirements } from './requirements'; import { getAppConfigOptionsFromUser } from '../app/service'; import { createConcurrencyLimiter } from '../utils/promise'; @@ -230,7 +230,7 @@ export function createScheduleMCPPreflight(deps: ScheduleMCPDeps): ScheduleMCPPr }); return appConfig; }; - const { tools, serverHints, candidates } = await resolveScheduledMCPRequirements( + const { tools, serverHints } = await resolveScheduledMCPRequirements( agentId, user, deps, @@ -242,17 +242,11 @@ export function createScheduleMCPPreflight(deps: ScheduleMCPDeps): ScheduleMCPPr name.includes(Constants.mcp_delimiter) && !name.startsWith(`${Constants.mcp_server}${Constants.mcp_delimiter}`), ); - if (execution?.enrolled) { - const denied = candidates.find(({ name }) => !isScheduledMCPCandidate(name)); - if (denied || selectedTools.length === 0) { - throw new ScheduleMCPError( - new ScheduledMCPPolicyError( - denied ? 'tool_policy_denied' : 'binding_mismatch', - '', - denied?.agentId ?? agentId, - ).outcomes, - ); - } + // An agent's other tools do not stop the run: the ones the grant leaves out are not loaded. + if (execution?.enrolled && selectedTools.length === 0) { + throw new ScheduleMCPError( + new ScheduledMCPPolicyError('binding_mismatch', '', agentId).outcomes, + ); } if (selectedTools.length === 0) return []; diff --git a/packages/data-provider/src/types/scheduleConsent.spec.ts b/packages/data-provider/src/types/scheduleConsent.spec.ts index 4218286b92..1cf7868ddb 100644 --- a/packages/data-provider/src/types/scheduleConsent.spec.ts +++ b/packages/data-provider/src/types/scheduleConsent.spec.ts @@ -242,6 +242,62 @@ it('lets the owner ask first about as many tools as they can allow', () => { expect(parse(MAX_SCHEDULED_MCP_SERVER_TOOLS + 1)).toBe(false); }); +it("takes the owner's choice of tools that are not MCP tools, and stores it per agent", () => { + const confirm = (tools?: unknown) => + confirmScheduleMCPConsentSchema.safeParse({ + offerDigest: 'a'.repeat(64), + expectedRevision: null, + lifetimeHours: 1, + allow: { warehouse: ['query'] }, + ...(tools !== undefined && { tools }), + }).success; + expect(confirm()).toBe(true); + expect(confirm([])).toBe(true); + expect(confirm(['execute_code', 'send_action_mail.example'])).toBe(true); + expect(confirm({ execute_code: true })).toBe(false); + expect(confirm([''])).toBe(false); + + const stored = (tools?: unknown) => + scheduledMCPEnrollmentSchema.safeParse({ + version: 1, + revision: 'grant-1', + scheduleRevision: 0, + consents: [ + { + id: 'consent-1', + revision: 'grant-1', + identity: { + scheduleId: 's', + ownerId: 'u', + tenantId: null, + agentId: 'root', + invocationMode: 'delegated', + }, + resource: { + serverName: 'warehouse', + url: 'https://warehouse.example/mcp', + configurationRevision: 'v1', + credentialMode: 'anonymous', + issuer: null, + audience: null, + scopes: [], + }, + permittedTools: [{ agentId: 'root', tools: ['query'] }], + policyRevision: 'p1', + grantedAtMs: 1, + absoluteExpiresAtMs: 2, + revokedAtMs: null, + }, + ], + ...(tools !== undefined && { tools }), + }).success; + // A grant made before these tools could be chosen has none, and reads as it did. + expect(stored()).toBe(true); + expect(stored([{ agentId: 'root', tools: ['execute_code'] }])).toBe(true); + expect(stored([{ agentId: 'root', tools: [] }])).toBe(false); + expect(stored(['execute_code'])).toBe(false); +}); + it('accepts a server the owner left with no tools beside one with a choice', () => { expect( confirmScheduleMCPConsentSchema.safeParse({ diff --git a/packages/data-provider/src/types/scheduleConsent.ts b/packages/data-provider/src/types/scheduleConsent.ts index 3cdb2d28a0..f49af567bd 100644 --- a/packages/data-provider/src/types/scheduleConsent.ts +++ b/packages/data-provider/src/types/scheduleConsent.ts @@ -167,6 +167,11 @@ function enrollmentSchema(consent: Consent) { scheduleRevision: z.number().int().nonnegative(), revision: identifier, consents: z.array(consent).min(1).max(32), + /** + * The agents' tools that are not MCP tools (code execution, web search, an action) that + * the owner allowed, per agent. Absent: none of them is allowed. + */ + tools: z.array(scheduledMCPToolSelectionSchema).max(MAX_SERVER_AGENTS).optional(), }) .strict(); } @@ -214,6 +219,11 @@ export const confirmScheduleMCPConsentSchema = z .record(identifier, z.array(identifier).max(MAX_SCHEDULED_MCP_SERVER_TOOLS)) .refine((value) => Object.keys(value).length <= 32) .optional(), + /** + * Of the offered tools that are not MCP tools, the ones the owner allows. Left out, the + * stored grant's choice is kept for the tools still on offer. + */ + tools: z.array(identifier).max(MAX_AGENT_TOOLS).optional(), }) .strict(); export type ConfirmScheduleMCPConsent = z.infer; @@ -232,6 +242,11 @@ export interface ScheduleMCPConsentView { changed: Record; /** Allowed tools the stored grant asks the owner about before each use, by server. */ asking: Record; + /** + * The agents' tools that are not MCP tools: every one the owner may choose from, and the + * ones the stored grant allows. + */ + tools: { offered: string[]; allowed: string[] }; /** Whether a scheduled run can pause for the owner's approval on this deployment. */ approval: boolean; /** With no grant: whether the schedule's MCP tools are refused until the owner confirms. */ diff --git a/packages/data-schemas/src/methods/scheduleConsent.spec.ts b/packages/data-schemas/src/methods/scheduleConsent.spec.ts index 93db954f21..ee474d494b 100644 --- a/packages/data-schemas/src/methods/scheduleConsent.spec.ts +++ b/packages/data-schemas/src/methods/scheduleConsent.spec.ts @@ -289,6 +289,16 @@ it('rejects credential-bearing or mismatched records at the persistence boundary await expect(confirm(null, secret)).rejects.toThrow(); }); +it('keeps the other tools the owner allowed on the grant, through revocation too', async () => { + const tools = [{ agentId: 'root', tools: ['execute_code', 'web_search'] }]; + expect(await confirm(null, enrollment('grant-1', { tools }))).toBe(true); + expect((await storage.readScheduleMCPConsent(identity))?.enrollment?.tools).toEqual(tools); + await storage.revokeScheduleMCPConsent(identity, 'grant-1'); + const revoked = await storage.readScheduleMCPConsent(identity); + expect(revoked?.compatible).toBe(true); + expect(revoked?.enrollment?.tools).toEqual(tools); +}); + it('uses only classic update operators for confirmation, revocation and admission', async () => { const update = jest.spyOn(mongoose.models.Schedule, 'updateOne'); await confirm();