/** * McpClientManager: connects to MCP servers and wraps discovered tools * as AgentTool objects for registration with pi-agent-core. * * Handles two transport types: * - Stdio: spawns a subprocess, communicates via stdin/stdout * - HTTP: connects to an already-running Streamable HTTP server * * Tool discovery via tools/list, tool execution via tools/call. * Connections are persistent (kept alive between ticks) with health monitoring. * Reconnect on subprocess crash (3 attempts before deregistering). * * References: * - docs/cortex/mcp-integration.md * - docs/cortex/plans/phase-3-plugin-tools.md */ import type { McpTransportConfig, McpConnectionState, McpToolCallProgress, CortexLogger } from './types.js'; import type { CortexTool } from './tool-contract.js'; /** * Backward-compatible export name for Cortex's canonical tool contract. */ export type AgentTool = CortexTool; export declare class McpClientManager { private connections; /** * Callback invoked whenever the aggregate tool set changes. * CortexAgent uses this to resync live tools after connect/disconnect/reconnect. */ onToolsChanged?: () => void; /** * Callback invoked when a subprocess is spawned (for PID tracking). * The consumer (CortexAgent) uses this to track PIDs for exit cleanup. */ onSubprocessSpawned?: (pid: number) => void; /** * Callback invoked when a subprocess exits (for PID tracking). */ onSubprocessExited?: (pid: number) => void; /** * Consumer-set environment variable overrides that bypass the security blocklist. * Merged ON TOP of the sanitized environment for all stdio subprocesses. * Used for macOS dock icon suppression vars (DYLD_INSERT_LIBRARIES, etc.). */ envOverrides?: Record; /** * Optional callback fired when an MCP server emits a * `notifications/progress` during a long-running `tools/call`. The MCP * SDK's `resetTimeoutOnProgress` is enabled whenever a per-tool timeout is * configured, so a server that keeps emitting progress can stay alive past * the wall-clock window. Consumers wire this to whatever UI affordance * they have ("still waiting…" banners, log lines). * * Failures inside the callback are swallowed so a buggy consumer cannot * tear down an in-flight tool call. */ onToolCallProgress?: (progress: McpToolCallProgress) => void; /** Logger for MCP diagnostics. Set by CortexAgent after construction. */ logger: CortexLogger; /** * Connect to an MCP server and discover its tools. * * Spawns a subprocess (stdio) or connects to a URL (http), performs * the MCP handshake, calls tools/list, and wraps each discovered * tool as an AgentTool with namespaced name. * * @param serverName - Unique name for this server (used for tool namespacing) * @param config - Transport configuration * @throws Error if connection or tool discovery fails */ connect(serverName: string, config: McpTransportConfig): Promise; /** * Disconnect from a specific MCP server. * Closes the transport and removes all tools from that server. * * @param serverName - The server name to disconnect */ disconnect(serverName: string): Promise; /** * Close all MCP connections. * Kills all stdio subprocesses and closes HTTP connections. */ closeAll(): Promise; /** * Get all AgentTool objects from all connected MCP servers. * Returns tools namespaced as serverName__toolName. */ getTools(): AgentTool[]; /** * Get tool names from a specific server. */ getServerToolNames(serverName: string): string[]; /** * Get the connection state for all servers. */ getConnectionStates(): McpConnectionState[]; /** * Check if a specific server is connected. */ isConnected(serverName: string): boolean; /** * Get the number of active connections. */ get connectionCount(): number; private createTransport; private createStdioTransport; private createHttpTransport; /** * Discover tools from a connected MCP server and wrap them as AgentTools. */ private discoverTools; /** * Wrap a single MCP tool definition as an AgentTool. * * Key details: * - Name is prefixed with serverName__ for namespacing * - JSON Schema from MCP is wrapped via Type.Unsafe() for TypeBox * - execute() calls tools/call on the MCP connection using the original name * - Errors are caught and returned as error results (not thrown) */ private wrapMcpTool; /** * Handle unexpected disconnection from an MCP server. * Attempts reconnection up to MAX_RECONNECT_ATTEMPTS times. */ private handleDisconnect; /** * Attempt to reconnect to an MCP server. */ private attemptReconnect; } //# sourceMappingURL=mcp-client.d.ts.map