UNPKG

xmcp

Version:

The TypeScript MCP framework

26 lines (25 loc) • 1.44 kB
import { InputRequiredResult, ReadResourceResult, ServerContext } from "@modelcontextprotocol/server"; import { ZodRawShape } from "zod/v3"; /** * Type for the original resource handler that users write */ export type UserResourceHandler = (args: ZodRawShape, extra?: ServerContext) => ReadResourceResult | InputRequiredResult | string | Promise<ReadResourceResult | InputRequiredResult | string>; /** * Type for the transformed handler that the MCP server expects (direct resources) */ export type McpResourceHandler = (uri: URL, extra: ServerContext) => ReadResourceResult | InputRequiredResult | Promise<ReadResourceResult | InputRequiredResult>; /** * Transforms a user's resource handler into an MCP-compatible handler. * * This function: * 1. Extracts parameters from the URI based on the resource file path * 2. Passes the parameters to the user's handler * 3. Transforms string responses into the required ReadResourceResult format * 4. Ensures the URI is included in the response content * * @param handler - The user's resource handler function * @param filePath - The resource file path (used to extract URI template and parameters) * @returns A transformed handler compatible with McpServer.registerResource * @throws Error if the handler returns an invalid response type */ export declare function transformResourceHandler(handler: UserResourceHandler, filePath: string, schema?: ZodRawShape): McpResourceHandler;