返回 AiToEarn
tool.decorator.ts
根目录 / project / aitoearn-backend / libs / nest-mcp / src / decorators / tool.decorator.ts
1 import { ToolAnnotations as SdkToolAnnotations } from '@modelcontextprotocol/sdk/types.js'
2 import { SetMetadata } from '@nestjs/common'
3 import { z } from 'zod'
4 import { MCP_TOOL_METADATA_KEY } from './constants'
5
6 export interface ToolMetadata {
7 name: string
8 description: string
9 parameters?: z.ZodObject
10 outputSchema?: z.ZodTypeAny
11 annotations?: SdkToolAnnotations
12 _meta?: Record<string, any>
13 }
14
15 export interface ToolAnnotations extends SdkToolAnnotations {}
16
17 export interface ToolOptions {
18 name?: string
19 description?: string
20 parameters?: z.ZodObject
21 outputSchema?: z.ZodTypeAny
22 annotations?: ToolAnnotations
23 _meta?: Record<string, any>
24 }
25
26 /**
27 * Decorator that marks a controller method as an MCP tool.
28 * @param {object} options - The options for the decorator
29 * @param {string} options.name - The name of the tool
30 * @param {string} options.description - The description of the tool
31 * @param {z.ZodObject} [options.parameters] - The parameters of the tool
32 * @param {z.ZodTypeAny} [options.outputSchema] - The output schema of the tool
33 * @returns {MethodDecorator} - The decorator
34 */
35 export function Tool(options: ToolOptions) {
36 if (options.parameters === undefined) {
37 options.parameters = z.object({})
38 }
39
40 return SetMetadata(MCP_TOOL_METADATA_KEY, options)
41 }
42
42 lines TYPESCRIPT