mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 18:34:09 +00:00
refactor(mcp): the demo-mode write block runs once, in the registry's tool gate
Every write tool used to open with its own demo check, 196 copies across 30 tool files, most of them injecting AuthService for nothing else. The registry now takes a toolGate (built from the container) that runs before each registered tool handler, and TREK's gate refuses a demo account on every tool not annotated read-only with the same canned result. The per-tool checks, the AuthService injections, AuthService.isDemoUser and the AuthModule imports that only fed them are gone; ESLint refuses a demo check in a tool file, and a registry-wide test calls every write tool as the demo account.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { McpAccessGroup, McpAccessMode, McpAccessPolicy, McpAccessValidator } from '../nest-mcp';
|
||||
import { demoDenied, type McpAccessGroup, type McpAccessMode, type McpAccessPolicy, type McpAccessValidator, type McpToolGate, type ToolOptions } from '../nest-mcp';
|
||||
import { ALL_SCOPES, canRead, canWrite, type Scope, type ScopeGroup } from './scopes';
|
||||
|
||||
/** The mode half of every scope: 'read' | 'write' | 'delete' | 'share'. */
|
||||
@@ -45,3 +45,26 @@ const VALID_GROUP_MODES: ReadonlySet<string> = new Set(ALL_SCOPES);
|
||||
*/
|
||||
export const trekMcpValidateAccess: McpAccessValidator = ({ group, mode }) =>
|
||||
VALID_GROUP_MODES.has(`${group}:${mode}`) ? null : `no '${group}:${mode}' scope in SCOPES`;
|
||||
|
||||
/**
|
||||
* True for a tool the demo gate holds back: every tool that does not declare
|
||||
* itself read-only. The annotation, not `access.mode`, is the signal, because
|
||||
* the share/content scopes cover reads too (get_share_link,
|
||||
* get_trip_calendar_feed, read_trip_file) and the predicate-gated tools carry
|
||||
* no mode at all. tests/unit/mcp/demo-tool-gate.test.ts holds the two in step:
|
||||
* every `mode: 'read'` tool is read-only and every `mode: 'write'` one is not.
|
||||
*/
|
||||
export function isDemoGatedTool(tool: ToolOptions): boolean {
|
||||
return tool.annotations?.readOnlyHint !== true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The demo-mode write block for every registered tool, in one place: a demo
|
||||
* account calling a tool that is not read-only gets the canned refusal, the
|
||||
* same result each tool used to return from its own first line. Given to
|
||||
* McpModule.forRoot as `toolGate` (AppModule) and to the MCP test registry.
|
||||
* Plugin tools are dynamic and keep their own gate in PluginMcpToolsService.
|
||||
*/
|
||||
export function trekDemoToolGate(isDemoUser: (userId: number) => Promise<boolean>): McpToolGate {
|
||||
return async (tool, ctx) => (isDemoGatedTool(tool) && (await isDemoUser(ctx.userId)) ? demoDenied() : undefined);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user