diff --git a/client/src/components/MCP/GrantTools.tsx b/client/src/components/MCP/GrantTools.tsx index 62138c88c0..0f39578711 100644 --- a/client/src/components/MCP/GrantTools.tsx +++ b/client/src/components/MCP/GrantTools.tsx @@ -63,6 +63,16 @@ function unmarked(tool: string, marked: readonly string[], seen: readonly string return marked.every((name) => seen.includes(name)); } +/** + * The whole server ticked is every tool on it ticked. One agent may select the server as a + * whole while another names a tool there, and the owner sees one list for the server: a named + * tool left out beside a ticked whole server would be allowed for the first agent and refused + * for the second, with nothing on screen to tell them apart. + */ +function spread(offered: readonly string[], ticked: string[]): string[] { + return ticked.includes(Constants.mcp_all) ? [...offered] : ticked; +} + /** * The owner's starting point: what the stored grant already allows and is still on offer. * A tool whose definition changed starts unticked, so allowing the new one is a decision. @@ -79,10 +89,13 @@ function initialChoice(data: ScheduleMCPConsentView): Record { const changed = data.changed[target.resource.serverName] ?? []; return [ target.resource.serverName, - offered.filter( - (tool) => - (allowed.includes(tool) || allowed.includes(Constants.mcp_all)) && - unmarked(tool, changed), + spread( + offered, + offered.filter( + (tool) => + (allowed.includes(tool) || allowed.includes(Constants.mcp_all)) && + unmarked(tool, changed), + ), ), ]; }), @@ -120,8 +133,11 @@ function currentChoice( const seen = edited.revision === data.revision ? (edited.changed[serverName] ?? []) : []; return [ serverName, - (edited.choice[serverName] ?? initial[serverName]).filter( - (tool) => offered.includes(tool) && unmarked(tool, marked, seen), + spread( + offered, + (edited.choice[serverName] ?? initial[serverName]).filter( + (tool) => offered.includes(tool) && unmarked(tool, marked, seen), + ), ), ]; }), @@ -561,37 +577,82 @@ function ToolChoice({ const groupId = useId(); /** * A selection of the whole server is one item of its own, listed first: every tool there, - * including ones the server gains later. It is not what ticking each named tool means, so - * the shared box below leaves it as the owner set it. + * including ones the server gains later. While it is ticked each named tool is ticked with + * it and cannot be unticked on its own; unticked, the named tools stay as they are. */ const every: string = Constants.mcp_all; const named = offered.filter((tool) => tool !== every); - const items = [...offered.filter((tool) => tool === every), ...named]; - const kept = chosen.filter((tool) => tool === every); - /** The whole-server item covers every changed tool there: they are named beside it. */ - const warns = (tool: string): boolean => - tool === every ? changed.length > 0 : changed.includes(tool); + const whole = offered.includes(every); + const covered = chosen.includes(every); /** - * Questions the grant holds about tools whose own ask box is not showing: a tool with no row - * (reached only through the whole server), or one whose row is unticked. While the whole - * server is ticked they are still allowed and still asked about, so each gets a row under - * that item, where the owner can keep asking or stop. + * Questions the grant holds about tools that have no row here: reached only through the + * whole server. Each gets a row under that item while it is ticked, so the owner can keep + * asking or stop. */ - const retained = chosen.includes(every) - ? asking.filter((tool) => tool !== every && !(named.includes(tool) && chosen.includes(tool))) - : []; + const retained = covered ? asking.filter((tool) => tool !== every && !named.includes(tool)) : []; /** Checked only when every tool is: the shared box has no partly-selected appearance. */ const all = named.length > 0 && named.every((tool) => chosen.includes(tool)); + const everyId = `${groupId}-every`; return (
+ {whole && ( +
+
+ 0 ? `${everyId}-changed` : undefined} + onCheckedChange={(checked) => + onChange( + checked === true ? [...chosen, every] : chosen.filter((name) => name !== every), + ) + } + /> + +
+ {changed.length > 0 && ( +

+ {localize('com_ui_schedule_consent_server_changed', { tools: changed.join(', ') })} +

+ )} + {retained.length > 0 && ( +
    + {retained.map((asked) => ( +
  • + { + if (checked !== true) onAskingChange(asking.filter((name) => name !== asked)); + }} + /> + +
  • + ))} +
+ )} +
+ )} {named.length > 0 && (
onChange(checked === true ? [...kept, ...named] : kept)} + aria-describedby={covered ? `${everyId}-label` : undefined} + onCheckedChange={(checked) => onChange(checked === true ? named : [])} />
)}
); diff --git a/client/src/components/SidePanel/Schedules/__tests__/Consent.spec.tsx b/client/src/components/SidePanel/Schedules/__tests__/Consent.spec.tsx index 49cd5079a8..df6557f79c 100644 --- a/client/src/components/SidePanel/Schedules/__tests__/Consent.spec.tsx +++ b/client/src/components/SidePanel/Schedules/__tests__/Consent.spec.tsx @@ -513,7 +513,7 @@ it('reads the tool and the server out of a key against the servers in use, not b expect(splitToolKey('send_action_mail.example', ['notes'])).toBeUndefined(); }); -it('offers a selection of a whole server as its own item, apart from ticking each tool', async () => { +it('offers a selection of a whole server as its own item, which ticks and locks each named tool', async () => { const whole: ScheduleMCPConsentView = { ...view, approval: true, @@ -530,29 +530,36 @@ it('offers a selection of a whole server as its own item, apart from ticking eac const every = await screen.findByRole('checkbox', { name: 'com_ui_schedule_consent_server_every', }); + const all = screen.getByRole('checkbox', { name: 'com_ui_schedule_consent_allow_server' }); + const query = screen.getByRole('checkbox', { name: 'query' }); expect(every).not.toBeChecked(); expect(screen.queryByRole('checkbox', { name: 'sys__all__sys' })).not.toBeInTheDocument(); // Ticking every named tool is not allowing whatever the server gains later. - await userEvent.click( - screen.getByRole('checkbox', { name: 'com_ui_schedule_consent_allow_server' }), - ); - expect(screen.getByRole('checkbox', { name: 'query' })).toBeChecked(); + await userEvent.click(all); + expect(query).toBeChecked(); expect(every).not.toBeChecked(); + await userEvent.click(all); + expect(query).not.toBeChecked(); + // The whole server is every tool on it: the named ones are ticked with it, and stay so. await userEvent.click(every); - // It names no tool, so there is nothing to be asked about for it. + expect(query).toBeChecked(); + expect(query).toBeDisabled(); + expect(all).toBeChecked(); + expect(all).toBeDisabled(); + // It names no tool, so there is nothing to be asked about for it: only `query` has a box. expect( screen.getAllByRole('checkbox', { name: 'com_ui_schedule_consent_ask_tool' }), ).toHaveLength(1); - // Unticking the named tools leaves it as the owner set it. - await userEvent.click( - screen.getByRole('checkbox', { name: 'com_ui_schedule_consent_allow_server' }), - ); - expect(every).toBeChecked(); + // Unticked, the named tools are the owner's to choose again, starting from ticked. + await userEvent.click(every); + expect(query).toBeChecked(); + expect(query).toBeEnabled(); + await userEvent.click(every); await userEvent.click(screen.getByRole('button', { name: 'com_ui_schedule_consent_confirm' })); await waitFor(() => expect(dataService.confirmScheduleMCPConsent).toHaveBeenCalledWith( 'schedule', - expect.objectContaining({ allow: { warehouse: ['sys__all__sys'] } }), + expect.objectContaining({ allow: { warehouse: ['query', 'sys__all__sys'] } }), ), ); }); @@ -685,32 +692,41 @@ describe('the whole-server item beside named tools', () => { ); }); - it('keeps a question in view when its tool is unticked and the whole server still covers it', async () => { - const data = stored( - { - allowed: { warehouse: ['query', 'sys__all__sys'] }, - asking: { warehouse: ['query'] }, - }, - ['query', 'sys__all__sys'], - ); + it('allows a tool another agent names along with the whole server, not only for the agent that holds it', async () => { + // The root selects the whole server and the child names `query`. The grant on file holds + // the whole server alone, so the child is refused `query` while the root may call it. + const data: ScheduleMCPConsentView = { + ...stored({ allowed: { warehouse: ['sys__all__sys'] }, asking: { warehouse: ['query'] } }, [ + 'sys__all__sys', + ]), + targets: [ + { + ...view.targets[0], + permittedTools: [ + { agentId: 'root', tools: ['sys__all__sys'] }, + { agentId: 'child', tools: ['query'] }, + ], + }, + ], + }; jest.mocked(dataService.getScheduleMCPConsent).mockResolvedValue(data); jest.mocked(dataService.confirmScheduleMCPConsent).mockResolvedValue(data); display(); expect(await every()).toBeChecked(); - // Ticked, `query` shows its own ask box. Unticked, the root still reaches it through the - // whole server and is still asked: the question moves under that item, it is not hidden. - const asks = () => - screen.getAllByRole('checkbox', { name: 'com_ui_schedule_consent_ask_tool' }); - expect(asks()).toHaveLength(1); - await userEvent.click(screen.getByRole('checkbox', { name: 'query' })); - expect(asks()).toHaveLength(1); - expect(asks()[0]).toBeChecked(); + // One list for the server: with the whole of it ticked, no tool on it is shown left out. + const query = screen.getByRole('checkbox', { name: 'query' }); + expect(query).toBeChecked(); + expect(query).toBeDisabled(); + // Its question shows once, on its own row. + const asks = screen.getAllByRole('checkbox', { name: 'com_ui_schedule_consent_ask_tool' }); + expect(asks).toHaveLength(1); + expect(asks[0]).toBeChecked(); await userEvent.click(confirm()); await waitFor(() => expect(dataService.confirmScheduleMCPConsent).toHaveBeenCalledWith( 'schedule', expect.objectContaining({ - allow: { warehouse: ['sys__all__sys'] }, + allow: { warehouse: ['query', 'sys__all__sys'] }, ask: { warehouse: ['query'] }, }), ), diff --git a/packages/api/src/schedules/README.md b/packages/api/src/schedules/README.md index b9c815a043..d9fa0a8978 100644 --- a/packages/api/src/schedules/README.md +++ b/packages/api/src/schedules/README.md @@ -155,6 +155,11 @@ refused once its definition changes. It names no tool, so it cannot be asked abo call. A grant that names tools keeps meaning those tools when the agent later selects the whole server. +The owner chooses per server, not per agent, and one agent may select the whole server while +another names a tool there. The dialog therefore sends each named tool along with the item +whenever the item is ticked, and shows them ticked and locked: otherwise a tool left out +beside a ticked whole server would be allowed for the first agent and refused for the second. + ### Tools on a server the operator did not declare A server with nothing under `mcpConsent.resources` has no recipient to hold a consent for: a