import { type SSETransportConfig } from './sse-transport.js'; import { type HTTPTransportConfig } from './http-transport.js'; import { type TransportType } from './base-transport.js'; import type { MCPInitializeResult, MCPTool, MCPPrompt, MCPResource, MCPResourceTemplate, MCPToolCallResult, MCPResourceReadResult, MCPPromptGetResult, MCPServerCapabilities } from './types.js'; import type { TransportErrorRecord } from '../discovery/types.js'; import { type MCPFeatureFlags } from '../protocol/index.js'; export interface MCPClientOptions { /** Request timeout in milliseconds (default: 30000) */ timeout?: number; /** Environment variables for the server process */ env?: Record; /** Delay before sending first request to allow server startup (default: 500ms) */ startupDelay?: number; /** Enable debug logging */ debug?: boolean; /** Transport type to use (default: stdio) */ transport?: TransportType; /** Configuration for SSE transport */ sseConfig?: Omit; /** Configuration for HTTP transport */ httpConfig?: Omit; /** Optional preflight check for remote transports (default: true) */ remotePreflight?: boolean; } /** * MCPClient connects to an MCP server via various transports and provides * methods to interact with the server's capabilities. * * Supported transports: * - stdio: Local subprocess communication (default) * - sse: Server-Sent Events for remote servers * - streamable-http: HTTP POST with streaming responses */ export declare class MCPClient { private process; private transport; private requestId; private pendingRequests; private serverCapabilities; private serverInstructions; private timeout; private startupDelay; private serverReady; private readyPromise; private debug; private transportType; private sseConfig?; private httpConfig?; private logger; /** Flag to prevent race condition during cleanup - ignore messages when true */ private cleaningUp; /** Flag to suppress exit warnings during intentional disconnect */ private disconnecting; /** Collected transport-level errors for reporting */ private transportErrors; /** Connection state for diagnostic error messages */ private connectionState; /** Protocol version negotiated with the server during initialization */ private negotiatedProtocolVersion; /** Whether to run an explicit preflight before remote connection */ private remotePreflight; constructor(options?: MCPClientOptions); /** * Optional remote connectivity/auth preflight (enabled by default). * Uses GET (not HEAD/OPTIONS) for broader compatibility. * * Any successful HTTP response (including 404/405/etc.) confirms network reachability. * Only auth failures and hard network/timeouts are treated as terminal preflight failures. */ private preflightRemote; /** * Classify a transport error based on its message. */ private classifyTransportError; /** * Record a transport error for later reporting. */ private recordTransportError; /** * Format a user-friendly error message based on category. */ private formatTransportErrorMessage; /** * Get all collected transport errors. */ getTransportErrors(): TransportErrorRecord[]; /** * Clear collected transport errors. */ clearTransportErrors(): void; /** * Check if a stderr message looks like a transport error worth recording. * Filters out normal debug/info output that servers commonly emit. */ private looksLikeTransportError; /** * Get the current transport type. */ getTransportType(): TransportType; private log; /** * Connect to an MCP server by spawning it as a subprocess. */ connect(command: string, args?: string[], env?: Record): Promise; /** * Connect to a remote MCP server via SSE or HTTP. * * @param url - The base URL of the MCP server * @param options - Optional configuration overrides */ connectRemote(url: string, options?: { transport?: 'sse' | 'streamable-http'; sessionId?: string; headers?: Record; }): Promise; /** * Set up event handlers for the transport. */ private setupTransportHandlers; /** * Wait for minimum startup delay before sending requests. * The actual "ready" state is confirmed by successful initialization. * This delay allows the server process to fully start before we attempt communication. */ private waitForStartup; /** * Initialize the MCP connection with the server. * This is the explicit confirmation that the server is ready. * On failure, all pending requests are cleared. */ initialize(): Promise; /** * Clear all pending requests with an error. * Used when initialization fails or connection is lost. */ private clearPendingRequests; /** * List all tools available on the server. * Handles pagination via nextCursor per MCP 2025-11-25 spec. */ listTools(options?: { signal?: AbortSignal; }): Promise; /** * List all prompts available on the server. * Handles pagination via nextCursor per MCP 2025-11-25 spec. */ listPrompts(options?: { signal?: AbortSignal; }): Promise; /** * List all resources available on the server. * Handles pagination via nextCursor per MCP 2025-11-25 spec. */ listResources(options?: { signal?: AbortSignal; }): Promise; /** * List all resource templates available on the server. * Handles pagination via nextCursor per MCP 2025-11-25 spec. */ listResourceTemplates(options?: { signal?: AbortSignal; }): Promise; /** * Read a resource from the server by URI. */ readResource(uri: string, options?: { signal?: AbortSignal; }): Promise; /** * Get a prompt from the server with the given arguments. */ getPrompt(name: string, args?: Record, options?: { signal?: AbortSignal; }): Promise; /** * Call a tool on the server. */ callTool(name: string, args?: Record, options?: { signal?: AbortSignal; }): Promise; /** * Get server capabilities. */ getCapabilities(): MCPServerCapabilities | null; /** * Get server instructions from initialization. */ getInstructions(): string | undefined; /** * Get the protocol version negotiated with the server during initialization. * Returns null if initialization hasn't completed yet. */ getNegotiatedProtocolVersion(): string | null; /** * Get feature flags based on the negotiated protocol version. * Returns null if initialization hasn't completed yet. */ getFeatureFlags(): MCPFeatureFlags | null; /** * Check if the server is ready (startup delay complete and initialized). * Note: This only indicates if startup delay has passed - true readiness * is confirmed by successful initialization. */ isServerReady(): boolean; /** * Disconnect from the server. */ disconnect(): Promise; private sendRequest; private sendNotification; private handleMessage; /** * Build a detailed error message for connection failures. * Includes command, exit code, stderr output, and suggestions. */ private buildConnectionErrorMessage; /** * Suggest a fix based on stderr output patterns. */ private suggestFix; private cleanup; } //# sourceMappingURL=mcp-client.d.ts.map