diff --git a/src/examples/server/mcpServerOutputSchema.ts b/src/examples/server/mcpServerOutputSchema.ts index de3b363e..75bfe690 100644 --- a/src/examples/server/mcpServerOutputSchema.ts +++ b/src/examples/server/mcpServerOutputSchema.ts @@ -43,14 +43,7 @@ server.registerTool( void country; // Simulate weather API call const temp_c = Math.round((Math.random() * 35 - 5) * 10) / 10; - const conditionCandidates = [ - "sunny", - "cloudy", - "rainy", - "stormy", - "snowy", - ] as const; - const conditions = conditionCandidates[Math.floor(Math.random() * conditionCandidates.length)]; + const conditions = ["sunny", "cloudy", "rainy", "stormy", "snowy"][Math.floor(Math.random() * 5)]; const structuredContent = { temperature: { @@ -84,4 +77,4 @@ async function main() { main().catch((error) => { console.error("Server error:", error); process.exit(1); -}); +}); \ No newline at end of file diff --git a/src/server/mcp.test.ts b/src/server/mcp.test.ts index dc96a1b0..10e550df 100644 --- a/src/server/mcp.test.ts +++ b/src/server/mcp.test.ts @@ -1312,7 +1312,7 @@ describe("tool()", () => { resultType: "structured", // Missing required 'timestamp' field someExtraField: "unexpected" // Extra field not in schema - } as unknown as { processedInput: string; resultType: string; timestamp: string }, // Type assertion to bypass TypeScript validation for testing purposes + }, }) ); diff --git a/src/server/mcp.ts b/src/server/mcp.ts index a5624e15..791facef 100644 --- a/src/server/mcp.ts +++ b/src/server/mcp.ts @@ -169,7 +169,7 @@ export class McpServer { } const args = parseResult.data; - const cb = tool.callback as ToolCallback; + const cb = tool.callback as ToolCallback; try { result = await Promise.resolve(cb(args, extra)); } catch (error) { @@ -184,7 +184,7 @@ export class McpServer { }; } } else { - const cb = tool.callback as ToolCallback; + const cb = tool.callback as ToolCallback; try { result = await Promise.resolve(cb(extra)); } catch (error) { @@ -772,7 +772,7 @@ export class McpServer { inputSchema: ZodRawShape | undefined, outputSchema: ZodRawShape | undefined, annotations: ToolAnnotations | undefined, - callback: ToolCallback + callback: ToolCallback ): RegisteredTool { const registeredTool: RegisteredTool = { title, @@ -929,7 +929,7 @@ export class McpServer { outputSchema?: OutputArgs; annotations?: ToolAnnotations; }, - cb: ToolCallback + cb: ToolCallback ): RegisteredTool { if (this._registeredTools[name]) { throw new Error(`Tool ${name} is already registered`); @@ -944,7 +944,7 @@ export class McpServer { inputSchema, outputSchema, annotations, - cb as ToolCallback + cb as ToolCallback ); } @@ -1138,16 +1138,6 @@ export class ResourceTemplate { } } -/** - * Type helper to create a strongly-typed CallToolResult with structuredContent - */ -type TypedCallToolResult = - OutputArgs extends ZodRawShape - ? CallToolResult & { - structuredContent?: z.objectOutputType; - } - : CallToolResult; - /** * Callback for a tool handler registered with Server.tool(). * @@ -1158,21 +1148,13 @@ type TypedCallToolResult = * - `content` if the tool does not have an outputSchema * - Both fields are optional but typically one should be provided */ -export type ToolCallback< - InputArgs extends undefined | ZodRawShape = undefined, - OutputArgs extends undefined | ZodRawShape = undefined -> = InputArgs extends ZodRawShape +export type ToolCallback = + Args extends ZodRawShape ? ( - args: z.objectOutputType, - extra: RequestHandlerExtra - ) => - | TypedCallToolResult - | Promise> - : ( - extra: RequestHandlerExtra - ) => - | TypedCallToolResult - | Promise>; + args: z.objectOutputType, + extra: RequestHandlerExtra, + ) => CallToolResult | Promise + : (extra: RequestHandlerExtra) => CallToolResult | Promise; export type RegisteredTool = { title?: string; @@ -1180,24 +1162,22 @@ export type RegisteredTool = { inputSchema?: AnyZodObject; outputSchema?: AnyZodObject; annotations?: ToolAnnotations; - callback: ToolCallback; + callback: ToolCallback; enabled: boolean; enable(): void; disable(): void; - update< - InputArgs extends ZodRawShape, - OutputArgs extends ZodRawShape - >(updates: { - name?: string | null; - title?: string; - description?: string; - paramsSchema?: InputArgs; - outputSchema?: OutputArgs; - annotations?: ToolAnnotations; - callback?: ToolCallback - enabled?: boolean - }): void; - remove(): void; + update( + updates: { + name?: string | null, + title?: string, + description?: string, + paramsSchema?: InputArgs, + outputSchema?: OutputArgs, + annotations?: ToolAnnotations, + callback?: ToolCallback, + enabled?: boolean + }): void + remove(): void }; const EMPTY_OBJECT_JSON_SCHEMA = {