Files
TREK/server/src/nest-mcp/decorators.ts
jubnl 76c8c349b1 Fold nest mcp (#2017)
* refactor(server): fold @trek/nest-mcp into server/src/nest-mcp

* docs(server): point nest-mcp references at server/src/nest-mcp

* remove tracked md file
2026-08-22 12:55:24 +02:00

43 lines
1.9 KiB
TypeScript

import { addEntry, markController, type ClassRef } from './metadata';
import type { PromptOptions, ResourceOptions, ResourceTemplateOptions, ToolOptions } from './types';
import { Injectable } from '@nestjs/common';
/**
* Marks a class as an MCP controller. Implies `@Injectable()` — register the
* class in a module's `providers: []` and inject domain services through the
* constructor as usual. Discovery scans ONLY marked classes (explicit opt-in,
* deterministic registry).
*/
export function McpController(): ClassDecorator {
return (target) => {
Injectable()(target);
markController(target as unknown as ClassRef);
};
}
function mcpMethodDecorator(entry: (methodName: string) => Parameters<typeof addEntry>[1]): MethodDecorator {
return (target, propertyKey) => {
addEntry((target as { constructor: unknown }).constructor as ClassRef, entry(String(propertyKey)));
};
}
/** Registers the method as an MCP tool. Handler signature: `(args, ctx)`. */
export function Tool(options: ToolOptions): MethodDecorator {
return mcpMethodDecorator((methodName) => ({ kind: 'tool', methodName, options }));
}
/** Registers the method as a fixed-URI MCP resource. Handler signature: `(uri: URL, ctx)`. */
export function Resource(options: ResourceOptions): MethodDecorator {
return mcpMethodDecorator((methodName) => ({ kind: 'resource', methodName, options }));
}
/** Registers the method as an MCP resource template. Handler signature: `(uri: URL, variables, ctx)`. */
export function ResourceTemplate(options: ResourceTemplateOptions): MethodDecorator {
return mcpMethodDecorator((methodName) => ({ kind: 'resourceTemplate', methodName, options }));
}
/** Registers the method as an MCP prompt. Handler signature: `(args, ctx)`. */
export function Prompt(options: PromptOptions): MethodDecorator {
return mcpMethodDecorator((methodName) => ({ kind: 'prompt', methodName, options }));
}