UNPKG

xmcp

Version:

The TypeScript MCP framework

31 lines (30 loc) • 1.83 kB
import { CallToolResult, InputRequiredResult, ServerContext } from "@modelcontextprotocol/server"; import { ZodRawShape } from "zod/v3"; import type { ToolExtraArguments } from "@/types/tool"; /** * Type for the original tool handler that users write */ export type UserToolResponse = CallToolResult | InputRequiredResult | string | number | Record<string, unknown>; export type UserToolHandler = (args: ZodRawShape, extra: ToolExtraArguments) => UserToolResponse | Promise<UserToolResponse>; /** * Type for the transformed handler that the MCP server expects */ export type McpToolHandler = (args: ZodRawShape, ctx: ServerContext) => CallToolResult | InputRequiredResult | Promise<CallToolResult | InputRequiredResult>; export declare function resolveToolClientInfo(ctx: ServerContext): ToolExtraArguments["clientInfo"]; /** * Transforms a user's tool handler into an MCP-compatible handler. * * This function: * 1. Passes through both args and extra parameters to the user's handler * 2. Transforms string/number responses into the required CallToolResult format * 3. Allows returning `content`, `structuredContent`, or both * 4. Allows returning only `_meta` (without `content`) when it contains widget metadata * 5. Auto-wraps HTML strings with widget metadata if provided * 6. Validates that the response is a proper CallToolResult and throws a descriptive error if not * * @param handler - The user's tool handler function * @param meta - Optional metadata to attach to responses (for MCP app widgets) * @returns A transformed handler compatible with McpServer.registerTool * @throws Error if the handler returns an invalid response type */ export declare function transformToolHandler(handler: UserToolHandler, meta?: Record<string, any>, outputSchema?: ZodRawShape, toolName?: string): McpToolHandler;