import type { JSONRPCMessage } from '@modelcontextprotocol/sdk/types.js'; import type { SdkMcpTransport } from './sdk-mcp-transport.js'; /** * A pending response entry, used to correlate JSON-RPC requests * with their responses when communicating with in-process MCP servers. */ export type PendingMcpResponse = { resolve: (msg: JSONRPCMessage) => void; reject: (err: Error) => void; }; /** * Check if a JSON-RPC message is a request. * * A JSON-RPC request has both a `method` field (string) and an `id` field * (non-null). Notifications have a `method` but no `id`. Responses have * an `id` but no `method`. */ export declare function isJsonRpcRequest(msg: JSONRPCMessage): boolean; /** * Check if a JSON-RPC message is a response (has `id` and either `result` or `error`). */ export declare function isJsonRpcResponse(msg: JSONRPCMessage): boolean; /** * Check if a JSON-RPC message is a notification (has `method` but no `id`). */ export declare function isJsonRpcNotification(msg: JSONRPCMessage): boolean; /** * Handle an MCP control request from the CLI. * * Routes the JSON-RPC message to the appropriate in-process McpServer * by injecting it into the server's transport via `transport.onmessage()`. * * The response is tracked using a promise-based pending map keyed by * `"serverName:requestId"`. When the McpServer processes the request * and calls `transport.send()`, the SDK should call `routeMcpResponse()` * to resolve the pending promise. * * Flow: * CLI → control_request{type:'mcp_message'} → handleMcpRequest() * → transport.onmessage(msg) → McpServer processes * → transport.send(response) → routeMcpResponse() → resolves promise * → SDK sends control_response back to CLI * * @param serverName - Name of the target in-process MCP server * @param message - The JSON-RPC message from the CLI * @param transport - The SdkMcpTransport connected to the target McpServer * @param pendingResponses - Map tracking pending request → response correlation * @returns Promise that resolves with the McpServer's JSON-RPC response */ export declare function handleMcpRequest(serverName: string, message: JSONRPCMessage, transport: SdkMcpTransport, pendingResponses: Map): Promise; /** * Route an MCP response from an in-process McpServer back to the CLI. * * Called when the McpServer's transport.send() fires with a response * message. Looks up the pending request by `"serverName:id"` and resolves * the corresponding promise. * * @param serverName - Name of the MCP server that produced the response * @param message - The JSON-RPC response message from the McpServer * @param pendingResponses - Map tracking pending request → response correlation * @returns `true` if a pending request was found and resolved, `false` otherwise */ export declare function routeMcpResponse(serverName: string, message: JSONRPCMessage, pendingResponses: Map): boolean; /** * Reject all pending responses for a given server. * * Useful when an MCP server is being shut down or has errored out, * to ensure no promises are left dangling. * * @param serverName - Name of the MCP server * @param error - The error to reject pending promises with * @param pendingResponses - Map tracking pending request → response correlation * @returns Number of pending responses that were rejected */ export declare function rejectAllPending(serverName: string, error: Error, pendingResponses: Map): number;