/** * mcp/client.ts — MCP Client (Model Context Protocol) * * Full MCP client implementing: * - initialize handshake (protocol version negotiation) * - tools/list (discover server-provided tools) * - tools/call (invoke a tool with arguments) * - resources/list (discover server-provided resources) * - resources/read (read a specific resource) * - prompts/list (discover server-provided prompts) * * Protocol version: MCP 2025-06-18 * Transport: stdio (subprocess) or Streamable HTTP * * Reference: Hannover src/services/mcp/client.ts * Protocol: https://modelcontextprotocol.io/specification/2025-06-18/ */ import type { McpServerConfig, McpToolDefinition, McpResource, McpPrompt, McpToolCallResult, McpInitializeResult, McpConnection, McpContent } from "./types.js"; import type { McpReturnedHttpReadPolicy } from "./types.js"; export interface McpReturnedHttpReadBinding { readonly serverName: string; readonly origin: string; readonly policy: Readonly>; readonly policyRevision: string; } export declare class McpClient { #private; private transport; private serverName; private config; private initResult; private cachedTools; private cachedResources; private notificationHandlers; /** Config reported by getConnection(); keeps ${MCP_SECRET_*} references instead of resolved credentials. */ private readonly displayConfig; constructor(serverName: string, config: McpServerConfig, displayConfig?: McpServerConfig); /** * Register a callback for server-sent JSON-RPC notifications. * Called for progress updates, log messages, list_changed events, etc. * Multiple handlers can be registered. */ onNotification(handler: (method: string, params: any) => void): void; /** Server name */ get name(): string; /** Whether the client is connected and initialized */ get connected(): boolean; /** Server capabilities (after initialize) */ get capabilities(): McpInitializeResult | null; /** * Connect to the MCP server and perform protocol handshake. */ connect(): Promise; /** * Disconnect from the MCP server. */ disconnect(): Promise; /** * List all tools provided by this MCP server. * Results are cached until disconnect. */ listTools(): Promise; /** * Call a tool on the MCP server. * * @param toolName The tool name (as returned by listTools) * @param args Tool arguments matching the tool's inputSchema * @param timeoutMs Request timeout (default: 60s for tool calls) * @param options Optional: progressToken to opt-in to progress notifications, * onProgress callback receives every notifications/progress * event whose progressToken matches. */ callTool(toolName: string, args: Record, timeoutMs?: number, options?: { progressToken?: string | number; onProgress?: (progress: { progress: number; total?: number; message?: string; }) => void; }): Promise; /** * List all resources provided by this MCP server. * Results are cached until disconnect. */ listResources(): Promise; /** * Read a specific resource by URI. */ readResource(uri: string): Promise; /** Original successful RPC object on this live connection; JSON copies never qualify. */ isOriginalReturnedHttpResult(result: unknown): boolean; getReturnedHttpReadBinding(): Readonly | undefined; isReturnedHttpUrlAllowed(url: string, binding: Readonly): boolean; /** Credential-bearing GET is available only through a current original binding. */ fetchReturnedHttpSource(url: string, binding: Readonly, signal: AbortSignal): Promise; /** * List all prompts provided by this MCP server. */ listPrompts(): Promise; /** * Get the full connection state for this server. */ getConnection(): McpConnection; private ensureConnected; } /** * Normalize a string for use as an MCP tool name component. * Replaces invalid chars with underscores, collapses multiples. */ export declare function normalizeMcpName(name: string): string; /** * Build a namespaced tool name: mcp__{server}__{tool} */ export declare function buildMcpToolName(serverName: string, toolName: string): string; /** * Parse a namespaced tool name back to server + tool. */ export declare function parseMcpToolName(fullName: string): { server: string; tool: string; } | null;