/** * MCPToolConsumer — Connect to external MCP servers and consume their tools. * * Implements `MCPServerConnection` for the MCPAdapter, providing: * - Stdio-based client transport (spawn child process) * - SSE-based client transport (HTTP long-poll) * - Auto-discovery of remote tools with caching * - Permission-gated remote tool execution * * BYOC: No external MCP client library required. * * @module MCPToolConsumer */ import { EventEmitter } from 'events'; import type { MCPServerConnection, MCPTool } from '../adapters/mcp-adapter'; /** JSON-RPC 2.0 request */ export interface JsonRpcRequest { jsonrpc: '2.0'; id: number; method: string; params?: Record; } /** JSON-RPC 2.0 response */ export interface JsonRpcResponse { jsonrpc: '2.0'; id: number; result?: unknown; error?: { code: number; message: string; data?: unknown; }; } /** MCP tool call result */ export interface ToolCallResult { content: Array<{ type: string; text?: string; data?: unknown; }>; isError?: boolean; } /** Transport interface for MCP client communication */ export interface MCPClientTransport { send(request: JsonRpcRequest): Promise; close(): Promise; } /** Options for connecting to an external MCP server */ export interface MCPConsumerOptions { /** Transport to use for communication */ transport: MCPClientTransport; /** Cache tool list for this many ms (default 60000) */ toolCacheTtlMs?: number; /** Timeout per tool call in ms (default 30000) */ callTimeoutMs?: number; /** Optional prefix for tool names when registered as agents (default 'mcp') */ prefix?: string; } /** Status of a discovered remote tool */ export interface RemoteToolInfo { tool: MCPTool; serverPrefix: string; lastDiscovered: number; callCount: number; errorCount: number; } /** * Stdio-based MCP client transport. * Spawns a child process and communicates via stdin/stdout JSON-RPC. * * @example * ```ts * const transport = new StdioClientTransport('npx', ['some-mcp-server']); * const consumer = new MCPToolConsumer({ transport }); * ``` */ export declare class StdioClientTransport implements MCPClientTransport { private command; private args; private process; private pendingRequests; private nextId; private buffer; private spawnFn; constructor(command: string, args?: string[]); send(request: JsonRpcRequest): Promise; close(): Promise; private start; private processBuffer; } /** * HTTP-based MCP client transport. * Sends JSON-RPC over HTTP POST requests to an MCP server endpoint. */ export declare class HttpClientTransport implements MCPClientTransport { private url; private headers; private nextId; constructor(url: string, headers?: Record); send(request: JsonRpcRequest): Promise; close(): Promise; } /** * Consume tools from an external MCP server. * Implements MCPServerConnection for use with MCPAdapter. * * @example * ```ts * const transport = new HttpClientTransport('http://localhost:3001/mcp'); * const consumer = new MCPToolConsumer({ transport }); * * // Use with MCPAdapter: * const adapter = new MCPAdapter(); * adapter.initialize({ options: { serverConnection: consumer } }); * await adapter.discoverServerTools(); * ``` */ export declare class MCPToolConsumer extends EventEmitter implements MCPServerConnection { private transport; private toolCache; private toolCacheTime; private toolCacheTtlMs; private callTimeoutMs; private prefix; private toolInfo; private initialized; constructor(options: MCPConsumerOptions); /** * Initialize the MCP connection (send initialize handshake). */ initialize(clientInfo?: { name: string; version: string; }): Promise; /** * List available tools from the remote MCP server. * Results are cached for `toolCacheTtlMs`. */ listTools(): Promise; /** * Call a tool on the remote MCP server. */ callTool(name: string, args: Record): Promise; /** Close the transport connection */ close(): Promise; /** Get info about discovered tools */ getToolInfo(): RemoteToolInfo[]; /** Invalidate the tool cache to force re-discovery */ invalidateCache(): void; /** Get the prefix used for agent naming */ getPrefix(): string; } //# sourceMappingURL=mcp-tool-consumer.d.ts.map