/** * Adapt Web-standard MCP handlers to Node.js HTTP request and response objects. * * @packageDocumentation */ import type { AuthInfo, McpHandlerRequestOptions } from "@modelcontextprotocol/server"; /** Minimal duck-typed shape of a Node.js `IncomingMessage`. */ export interface NodeIncomingMessageLike extends AsyncIterable { /** HTTP method. Defaults to `GET` when omitted. */ method?: string | undefined; /** Request target. Defaults to `/` when omitted. */ url?: string | undefined; /** Incoming HTTP headers, including `host` or `:authority` when available. */ headers: Record; /** Verified authentication information forwarded to the Fetch handler. */ auth?: AuthInfo; } /** Minimal duck-typed shape of a Node.js `ServerResponse`. */ export interface NodeServerResponseLike { /** Writes the HTTP status and response headers. */ writeHead(statusCode: number, headers?: Record): unknown; /** Writes a response-body chunk and returns `false` when backpressure applies. */ write(chunk: string | Uint8Array): unknown; /** Completes the response, optionally with a final body chunk. */ end(chunk?: string | Uint8Array): unknown; /** Subscribes to response lifecycle events such as `close` and `drain`. */ on(event: string, listener: (...args: unknown[]) => void): unknown; /** Whether the underlying response stream has already been destroyed. */ destroyed?: boolean; } /** Web-standard fetch face accepted by {@link toNodeHandler}. */ export interface FetchLikeHandler { /** Handles a converted Web-standard request. */ fetch: (request: Request, options?: McpHandlerRequestOptions) => Promise; } /** Node `(req, res, parsedBody?)` handler produced by {@link toNodeHandler}. */ export type NodeRequestHandler = (req: NodeIncomingMessageLike, res: NodeServerResponseLike, parsedBody?: unknown) => Promise; /** Options for {@link toNodeHandler}. */ export interface ToNodeHandlerOptions { /** Observes conversion or handler errors before a JSON-RPC error is returned. */ onerror?: (error: Error) => void; } /** * Adapt a web-standard `fetch` handler to Node `(req, res, parsedBody?)`. * * Unless a `parsedBody` is passed, the handler receives the request body as * the bytes that were sent. * * @param handler - Handler whose `fetch` receives converted `Request` objects. * @param opts - Optional adapter error observer. * @returns A Node-compatible asynchronous request handler. * * @example * ```ts * import { createServer } from "node:http"; * import { MCPServer } from "mcp-use"; * import { toNodeHandler } from "mcp-use/node"; * * const server = new MCPServer({ name: "example", version: "1.0.0" }); * createServer(toNodeHandler(server)).listen(3000); * ``` */ export declare function toNodeHandler(handler: FetchLikeHandler, opts?: ToNodeHandlerOptions): NodeRequestHandler; /** Options for {@link toWebRequest}. */ export interface ToWebRequestOptions { /** Signal that aborts the constructed request. */ signal?: AbortSignal; } /** * Convert a duck-typed Node request to a web-standard `Request`. * * Without `parsedBody`, the body is read from `req` and kept as the bytes that * were received. It is not decoded or normalized as text. * * @param req - Node `IncomingMessage` (or Express `req`). * @param parsedBody - Optional pre-parsed JSON body. * @param options - Optional abort signal for the constructed request. * @returns A Web-standard request with the Node headers and body. * * @example * ```ts * import { toWebRequest } from "mcp-use/node"; * * const request = await toWebRequest(incomingMessage); * ``` */ export declare function toWebRequest(req: NodeIncomingMessageLike, parsedBody?: unknown, options?: ToWebRequestOptions): Promise;