feat(server): type MCP access groups against SCOPES + boot gate

This commit is contained in:
jubnl
2026-07-28 20:56:43 +02:00
parent 59ea420f1d
commit 65fb00d63b
7 changed files with 159 additions and 10 deletions
+32 -2
View File
@@ -1,9 +1,11 @@
import type { McpAccessPolicy } from '@trek/nest-mcp';
import { canRead, canWrite } from './scopes';
import type { McpAccessGroup, McpAccessPolicy, McpAccessValidator } from '@trek/nest-mcp';
import { ALL_SCOPES, canRead, canWrite, type ScopeGroup } from './scopes';
/**
* TREK's context shape for @trek/nest-mcp handlers, predicates and the
* access policy — mirrors what registerTools() receives per session.
* The group registry types every decorator's `access.group` against the
* scope-derived union, so a typo'd group is a compile error in `.mcp.ts`.
*/
declare module '@trek/nest-mcp' {
interface McpContext {
@@ -11,8 +13,22 @@ declare module '@trek/nest-mcp' {
scopes: string[] | null;
isStaticToken: boolean;
}
// The augmentation pattern requires an empty single-extends interface —
// same false positive (and same disable) as McpContext in @trek/nest-mcp.
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface McpAccessGroupRegistry extends Record<ScopeGroup, true> {}
}
type AssertExact<A, B> = [A] extends [B] ? ([B] extends [A] ? true : never) : never;
/**
* Compile-time lockstep: the augmented `access.group` union must be exactly
* `ScopeGroup` — fails `npm run typecheck` if the augmentation above drifts
* (which is also the proof that unknown groups like 'budgets' are rejected).
* Exported so the policy unit test has a runtime touchpoint.
*/
export const MCP_ACCESS_GROUPS_MATCH_SCOPE_GROUPS: AssertExact<McpAccessGroup, ScopeGroup> = true;
/**
* Resolves declarative `access: { group, mode }` markers with the exact
* scopes.ts semantics the legacy registrars used at registration time:
@@ -22,3 +38,17 @@ declare module '@trek/nest-mcp' {
*/
export const trekMcpAccessPolicy: McpAccessPolicy = ({ group, mode }, ctx) =>
mode === 'write' ? canWrite(ctx.scopes, group) : canRead(ctx.scopes, group);
const VALID_GROUP_MODES: ReadonlySet<string> = new Set(ALL_SCOPES);
/**
* Boot gate run by registry.validate(): a declarative marker must resolve to
* a real scope — `{ group, mode }` requires `group:mode` ∈ ALL_SCOPES. Catches
* what the group typing alone cannot: mode drift on read-only groups (`geo`
* and `weather` have no `:write` scope, so `{ group: 'weather', mode:
* 'write' }` type-checks but would silently deny scoped tokens while passing
* `scopes: null` sessions). The offending entry is named by the registry's
* aggregated error.
*/
export const trekMcpValidateAccess: McpAccessValidator = ({ group, mode }) =>
VALID_GROUP_MODES.has(`${group}:${mode}`) ? null : `no '${group}:${mode}' scope in SCOPES`;
+5 -2
View File
@@ -36,6 +36,9 @@ export const SCOPES = {
export type Scope = typeof SCOPES[keyof typeof SCOPES];
/** 'trips' | 'places' | ... — derived from SCOPES; adding a scope extends it. */
export type ScopeGroup = Scope extends `${infer G}:${string}` ? G : never;
export const ALL_SCOPES: Scope[] = Object.values(SCOPES) as Scope[];
export interface ScopeInfo {
@@ -88,13 +91,13 @@ export function canReadTrips(scopes: string[] | null): boolean {
}
/** group:write grants write access; for trips canReadTrips handles read */
export function canWrite(scopes: string[] | null, group: string): boolean {
export function canWrite(scopes: string[] | null, group: ScopeGroup): boolean {
if (!scopes) return true;
return scopes.includes(`${group}:write`);
}
/** group:read OR group:write grant read access */
export function canRead(scopes: string[] | null, group: string): boolean {
export function canRead(scopes: string[] | null, group: ScopeGroup): boolean {
if (!scopes) return true;
return scopes.some(s => s === `${group}:read` || s === `${group}:write`);
}