import type { SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse'; import type { StdioServerParameters } from '@modelcontextprotocol/sdk/client/stdio'; import type { StreamableHTTPClientTransportOptions } from '@modelcontextprotocol/sdk/client/streamableHttp'; import type { Transport } from '@modelcontextprotocol/sdk/shared/transport'; export type StdioClientConfig = { type: 'stdio'; } & StdioServerParameters; export type SseClientConfig = { type: 'sse'; url: string; } & SSEClientTransportOptions; export type StreamableHttpClientConfig = { type: 'stream'; url: string; } & StreamableHTTPClientTransportOptions; /** * 自定义传输配置。 * 允许直接传入 Transport 实例,用于测试或高级场景(如 InMemoryTransport)。 */ export type CustomTransportConfig = { type: 'custom'; /** 传输实例 */ transport: Transport; }; export type ClientConfig = StdioClientConfig | SseClientConfig | StreamableHttpClientConfig | CustomTransportConfig; export type McpTool = { name: string; title?: string; description?: string; inputSchema: Record; outputSchema?: Record; annotations?: Record; }; /** * MCP 客户端连接状态。 * * 状态转换: * - disconnected → connecting(调用 connect()) * - connecting → connected(连接成功) * - connecting → disconnected(连接失败且未启用自动重连) * - connected → reconnecting(连接断开 / 心跳超时,自动重连启用时) * - connected → disposed(主动调用 dispose()) * - reconnecting → connected(重连成功) * - reconnecting → disconnected(重连失败达到上限) * - * → disposed(任何状态下调用 dispose()) */ export type McpClientStatus = 'disconnected' | 'connecting' | 'connected' | 'reconnecting' | 'disposed'; /** * 状态变更事件。 * McpClient 在状态变化时通过 onStatusChange 回调通知外部。 */ export type McpClientStatusEvent = { /** 客户端名称 */ clientName: string; /** 当前状态 */ status: McpClientStatus; /** 前一个状态 */ previousStatus: McpClientStatus; /** 错误信息(仅在状态因错误变化时提供,如连接失败、重连失败) */ error?: unknown; /** 重连尝试次数(仅 reconnecting 状态) */ reconnectAttempt?: number; /** 最大重连次数(仅 reconnecting 状态) */ maxReconnectAttempts?: number; }; /** 状态变更监听器 */ export type McpClientStatusListener = (event: McpClientStatusEvent) => void; /** 重连配置 */ export type McpClientReconnectOptions = { /** 是否启用自动重连,默认 true */ autoReconnect?: boolean; /** 最大重连次数,默认 3 */ maxReconnectAttempts?: number; /** 重连基础延迟(ms),默认 1000。实际延迟 = baseDelay * 2^attempt * (0.5 + random() * 0.5) */ reconnectBaseDelay?: number; /** 重连最大延迟(ms),默认 30000 */ reconnectMaxDelay?: number; }; /** 心跳配置 */ export type McpClientHeartbeatOptions = { /** 是否启用心跳,默认 true(仅 SSE/StreamableHTTP) */ enabled?: boolean; /** 心跳间隔(ms),默认 30000 */ interval?: number; /** 心跳超时(ms),默认 5000 */ timeout?: number; }; /** 超时配置 */ export type McpClientTimeoutOptions = { /** 连接超时(ms),默认 10000 */ connectTimeout?: number; /** 单次工具调用超时(ms),默认 60000 */ callTimeout?: number; }; /** 熔断器配置 */ export type McpClientCircuitBreakerOptions = { /** 冷却时间(ms),熔断触发后多久允许再次尝试,默认 60000 */ cooldownPeriod?: number; }; /** 工具过滤配置 */ export type McpClientToolFilterOptions = { /** * 工具白名单。仅允许列表中的工具注入 Agent。 * 支持字符串(精确匹配)或正则表达式。 * 与 blockedTools 互斥,同时设置时 allowedTools 优先。 */ allowedTools?: Array; /** * 工具黑名单。列表中的工具不会注入 Agent。 * 支持字符串(精确匹配)或正则表达式。 */ blockedTools?: Array; }; /** * MCP 客户端配置(整合传输 + 稳定性 + 监听 + 过滤)。 */ export type McpClientOptions = { /** 客户端名称,用于工具名前缀和日志标识 */ name: string; /** 传输层配置 */ transport: ClientConfig; /** 重连配置 */ reconnect?: McpClientReconnectOptions; /** 心跳配置 */ heartbeat?: McpClientHeartbeatOptions; /** 超时配置 */ timeout?: McpClientTimeoutOptions; /** 熔断器配置 */ circuitBreaker?: McpClientCircuitBreakerOptions; /** 工具过滤配置 */ toolFilter?: McpClientToolFilterOptions; /** 状态变更监听器(构造时传入,可选) */ onStatusChange?: McpClientStatusListener; }; /** connect() 方法选项 */ export type McpClientConnectOptions = { /** * 是否立即返回,不等待重试完成。 * * - `false`(默认):等待首次连接及所有重试完成后才返回 * - `true`:仅尝试首次连接,失败后抛出异常,重试在后台异步进行 * * 使用场景:拦截器中不希望阻塞 Agent 执行流程时设置为 true */ failFast?: boolean; }; /** MCP 工具调用结果(内部使用) */ export type McpToolResult = { /** 非结构化内容(文本/图片/音频等) */ content: Array>; /** 结构化数据(JSON 对象) */ structuredContent?: Record; /** 工具执行是否出错 */ isError: boolean; }; declare module '@agforge/core' { interface ToolMetadata { mcp?: { clientName: string; toolName: string; }; } }