Files
TREK/server/tests/unit/nest-mcp/tool-gate.test.ts
T
Maurice 3f38621024 feat(server): let a trip owner switch trip addons for one trip
A trip can now turn a trip addon (lists, costs, documents, collab, road
trip, tours) on or off for itself, whatever the instance switch says. No
row in the new trip_addon_overrides table means the trip follows the
instance; a row is the owner's choice. Going back to the instance setting
deletes the row, and no addon data is touched either way.

- GET/PUT /api/trips/:tripId/addons[/:addonId] (every member reads, the
  owner writes) and the MCP tools get_trip_addons and set_trip_addon, on
  the shared tripAddons contract; tripAddons:changed tells the members.
- Every trip-scoped addon surface asks the trip: REST through
  TripAddonGuard, MCP through a per-call gate on the named or pinned trip,
  plugin RPC and events, the share link, the offline bundle, reminders,
  doc sync, the assistant (tools, keep_attached_file), booking costs, the
  trip summary prompt, and the cross-trip tour reads (library, Atlas
  stats, collection save).
- Routes without a trip open while the addon is on in any of the user's
  trips. Switching an addon on that the instance has off ends the members'
  MCP sessions so they reconnect with its tools.
- A copied trip takes its owner's choices along.

Behaviour changes: an anonymous call to an addon route that is off now
gets 401 instead of 404, and with an instance switch off the packing,
todo and files plugin RPC methods, the files MCP tools, todo reminder
mails, core plugin events, plugin view contributions, the share link's
lists, costs and chat sections and the offline bundle's lists follow it.
2026-11-21 13:23:09 +01:00

185 lines
6.4 KiB
TypeScript

import {
createTestRegistry,
McpController,
McpModule,
McpRegistryService,
Resource,
Tool,
type McpDynamicTool,
type McpToolGate,
} from '../../../src/nest-mcp';
import { asCtx, createAttachHarness, type AttachHarness, type TestCtx } from './harness';
import { Injectable, Module } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import 'reflect-metadata';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { z } from 'zod';
const REFUSAL = { content: [{ type: 'text', text: 'refused' }], isError: true };
@McpController()
class GatedMcp {
calls = 0;
@Tool({ name: 'write_it', inputSchema: { n: z.number() }, annotations: { readOnlyHint: false } })
async writeIt({ n }: { n: number }, ctx: TestCtx) {
this.calls++;
return { content: [{ type: 'text', text: JSON.stringify({ n, userId: ctx.userId }) }] };
}
@Tool({ name: 'no_schema', annotations: { readOnlyHint: true } })
async noSchema(args: Record<string, never>) {
return { content: [{ type: 'text', text: JSON.stringify(args) }] };
}
@Resource({ name: 'res', uri: 'gate://res' })
async res(uri: URL) {
return { contents: [{ uri: uri.href, text: 'resource' }] };
}
}
const textOf = (result: unknown): string => (result as { content: { text: string }[] }).content[0].text;
describe('tool gate', () => {
let harness: AttachHarness | undefined;
afterEach(async () => {
await harness?.cleanup();
harness = undefined;
});
it('answers with the gate result and never runs the handler', async () => {
const mcp = new GatedMcp();
const gate = vi.fn<McpToolGate>(() => REFUSAL);
harness = await createAttachHarness(createTestRegistry([mcp], { toolGate: gate }), { userId: 3 });
const result = await harness.client.callTool({ name: 'write_it', arguments: { n: 1 } });
expect(result).toEqual(REFUSAL);
expect(mcp.calls).toBe(0);
expect(gate).toHaveBeenCalledWith(
expect.objectContaining({ name: 'write_it', annotations: { readOnlyHint: false } }),
asCtx({ userId: 3 }),
{ n: 1 },
);
});
it('runs the handler with its arguments when the gate returns undefined', async () => {
const mcp = new GatedMcp();
harness = await createAttachHarness(createTestRegistry([mcp], { toolGate: async () => undefined }), { userId: 4 });
const result = await harness.client.callTool({ name: 'write_it', arguments: { n: 2 } });
expect(JSON.parse(textOf(result))).toEqual({ n: 2, userId: 4 });
expect(mcp.calls).toBe(1);
});
it('gates a tool without an input schema too', async () => {
harness = await createAttachHarness(createTestRegistry([new GatedMcp()], { toolGate: () => REFUSAL }), {});
expect(await harness.client.callTool({ name: 'no_schema', arguments: {} })).toEqual(REFUSAL);
});
it('leaves resources alone', async () => {
const gate = vi.fn<McpToolGate>(() => REFUSAL);
harness = await createAttachHarness(createTestRegistry([new GatedMcp()], { toolGate: gate }), {});
const read = await harness.client.readResource({ uri: 'gate://res' });
expect(read.contents[0]).toMatchObject({ text: 'resource' });
expect(gate).not.toHaveBeenCalled();
});
it('leaves dynamic tools to their own source', async () => {
const gate = vi.fn<McpToolGate>(() => REFUSAL);
const dynamic: McpDynamicTool = {
options: { name: 'dyn', inputSchema: {}, access: () => true },
handler: () => ({ content: [{ type: 'text', text: 'dynamic ran' }] }),
};
harness = await createAttachHarness(
createTestRegistry([], { toolGate: gate }),
{},
{ dynamicTools: () => [dynamic] },
);
expect(textOf(await harness.client.callTool({ name: 'dyn', arguments: {} }))).toBe('dynamic ran');
expect(gate).not.toHaveBeenCalled();
});
it('runs after onInvoke and inside around, so the audit and the trace still see a refused call', async () => {
const order: string[] = [];
const registry = createTestRegistry([new GatedMcp()], {
toolGate: () => {
order.push('gate');
return REFUSAL;
},
});
harness = await createAttachHarness(
registry,
{},
{
onInvoke: () => order.push('onInvoke'),
around: async (_info, call) => {
order.push('around:in');
const out = await call();
order.push('around:out');
return out;
},
},
);
await harness.client.callTool({ name: 'write_it', arguments: { n: 1 } });
expect(order).toEqual(['onInvoke', 'around:in', 'gate', 'around:out']);
});
it('is skipped entirely without a configured gate', async () => {
const mcp = new GatedMcp();
harness = await createAttachHarness(createTestRegistry([mcp]), { userId: 1 });
await harness.client.callTool({ name: 'write_it', arguments: { n: 5 } });
expect(mcp.calls).toBe(1);
});
});
@Injectable()
class Verdicts {
refuse = true;
}
@Module({ providers: [Verdicts, GatedMcp], exports: [Verdicts] })
class GatedModule {}
describe('McpModule.forRoot toolGate', () => {
let harness: AttachHarness | undefined;
afterEach(async () => {
await harness?.cleanup();
harness = undefined;
});
it('builds the gate from the container with its injected providers', async () => {
const moduleRef = await Test.createTestingModule({
imports: [
GatedModule,
McpModule.forRoot({
toolGate: {
imports: [GatedModule],
inject: [Verdicts],
useFactory: (verdicts: Verdicts) => () => (verdicts.refuse ? REFUSAL : undefined),
},
}),
],
}).compile();
await moduleRef.init();
harness = await createAttachHarness(moduleRef.get(McpRegistryService), { userId: 1 });
expect(await harness.client.callTool({ name: 'write_it', arguments: { n: 1 } })).toEqual(REFUSAL);
moduleRef.get(Verdicts).refuse = false;
expect(JSON.parse(textOf(await harness.client.callTool({ name: 'write_it', arguments: { n: 1 } })))).toEqual({
n: 1,
userId: 1,
});
await moduleRef.close();
});
it('boots without a gate and runs every handler', async () => {
const moduleRef = await Test.createTestingModule({ imports: [GatedModule, McpModule.forRoot()] }).compile();
await moduleRef.init();
harness = await createAttachHarness(moduleRef.get(McpRegistryService), { userId: 2 });
expect(JSON.parse(textOf(await harness.client.callTool({ name: 'write_it', arguments: { n: 3 } })))).toEqual({
n: 3,
userId: 2,
});
await moduleRef.close();
});
});