import { Client, type ClientOptions } from "@modelcontextprotocol/sdk/client/index.js"; import { type SSEClientTransportOptions } from "@modelcontextprotocol/sdk/client/sse.js"; import type { StdioServerParameters } from "@modelcontextprotocol/sdk/client/stdio.js"; import { type StreamableHTTPClientTransportOptions } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; import type { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.js"; import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js"; import type { CallToolResult, GetPromptResult, Implementation, ReadResourceResult, Request } from "@modelcontextprotocol/sdk/types.js"; import { type ZodType, z } from "zod"; import { type PromiseOrValue } from "../utils/type-utils.js"; import { Agent, type AgentInvokeOptions, type AgentOptions, type Message } from "./agent.js"; export interface MCPAgentOptions extends AgentOptions { client: Client; prompts?: MCPPrompt[]; resources?: MCPResource[]; } export type MCPServerOptions = SSEServerParameters | StdioServerParameters; export type SSEServerParameters = { url: string; /** * Whether to use the StreamableHTTPClientTransport instead of the SSEClientTransport. * @default "sse" */ transport?: "sse" | "streamableHttp"; /** * Additional options to pass to the SSEClientTransport or StreamableHTTPClientTransport. */ opts?: SSEClientTransportOptions | StreamableHTTPClientTransportOptions; /** * The timeout for requests to the server, in milliseconds. * @default 60000 */ timeout?: number; /** * Whether to automatically reconnect to the server if the connection is lost. * @default 10 set to 0 to disable automatic reconnection */ maxReconnects?: number; /** * A function that determines whether to reconnect to the server based on the error. * default to reconnect on all errors. */ shouldReconnect?: (error: Error) => boolean; }; /** * MCPAgent is a specialized agent for interacting with MCP (Model Context Protocol) servers. * It provides the ability to connect to remote MCP servers using different transport methods, * and access their tools, prompts, and resources. * * MCPAgent serves as a bridge between your application and MCP servers, allowing you to: * - Connect to MCP servers over HTTP/SSE or stdio * - Access server tools as agent skills * - Utilize server prompts and resources * - Manage server connections with automatic reconnection * * @example * Here's an example of creating an MCPAgent with SSE transport: * {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-from-sse} */ export declare class MCPAgent extends Agent { tag: string; static schema(): z.ZodObject<{ url: ZodType; command: ZodType; args: ZodType; }, "strip", z.ZodTypeAny, { url?: string | undefined; command?: string | undefined; args?: string[] | undefined; }, { url?: string | undefined; command?: string | undefined; args?: string[] | undefined; }>; static load(options: { filepath: string; parsed: object; }): Promise>; /** * Create an MCPAgent from a connection to an SSE server. * * This overload establishes a Server-Sent Events connection to an MCP server * and automatically discovers its available tools, prompts, and resources. * * @param options SSE server connection parameters * @returns Promise resolving to a new MCPAgent instance * * @example * Here's an example of creating an MCPAgent with StreamableHTTP transport: * {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-from-streamable-http} * * @example * Here's an example of creating an MCPAgent with SSE transport: * {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-from-sse} * * @example * Here's an example of creating an MCPAgent with Stdio transport: * {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-from-stdio} */ static from(options: MCPServerOptions): Promise; /** * Create an MCPAgent from a pre-configured MCP client. * * This overload uses an existing MCP client instance and optionally * pre-defined prompts and resources. * * @param options MCPAgent configuration with client instance * @returns A new MCPAgent instance * * @example * Here's an example of creating an MCPAgent with a client instance: * {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-direct} */ static from(options: MCPAgentOptions): MCPAgent; private static fromTransport; /** * Create an MCPAgent instance directly with a configured client. * * @param options MCPAgent configuration options, including client instance * * @example * Here's an example of creating an MCPAgent with an existing client: * {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-direct} */ constructor(options: MCPAgentOptions); /** * The MCP client instance used for communication with the MCP server. * * This client manages the connection to the MCP server and provides * methods for interacting with server-provided functionality. */ client: Client; /** * Array of MCP prompts available from the connected server. * * Prompts can be accessed by index or by name. * * @example * Here's an example of accessing prompts: * {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-prompts} */ readonly prompts: MCPPrompt[] & { [key: string]: MCPPrompt; }; /** * Array of MCP resources available from the connected server. * * Resources can be accessed by index or by name. * * @example * Here's an example of accessing resources: * {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-resources} */ readonly resources: MCPResource[] & { [key: string]: MCPResource; }; /** * Check if the agent is invokable. * * MCPAgent itself is not directly invokable as it acts as a container * for tools, prompts, and resources. Always returns false. */ get isInvokable(): boolean; /** * Process method required by Agent interface. * * Since MCPAgent itself is not directly invokable, this method * throws an error if called. * * @param _input Input message (unused) * @param _options AgentInvokeOptions (unused) * @throws Error This method always throws an error since MCPAgent is not directly invokable */ process(_input: Message, _options: AgentInvokeOptions): Promise; /** * Shut down the agent and close the MCP connection. * * This method cleans up resources and closes the connection * to the MCP server. * * @example * Here's an example of shutting down an MCPAgent: * {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-shutdown} * * @example * Here's an example of shutting down an MCPAgent by using statement: * {@includeCode ../../test/agents/mcp-agent.test.ts#example-mcp-agent-shutdown-by-using} */ shutdown(): Promise; } export interface ClientWithReconnectOptions { transportCreator?: () => PromiseOrValue; timeout?: number; maxReconnects?: number; shouldReconnect?: (error: Error) => boolean; } declare class ClientWithReconnect extends Client { private reconnectOptions?; constructor(info: Implementation, options?: ClientOptions, reconnectOptions?: ClientWithReconnectOptions | undefined); private shouldReconnect; private reconnect; request>(request: Request, resultSchema: T, options?: RequestOptions): Promise>; } export interface MCPBaseOptions extends AgentOptions { client: ClientWithReconnect; } export declare abstract class MCPBase extends Agent { tag: string; constructor(options: MCPBaseOptions); protected client: ClientWithReconnect; } export declare class MCPTool extends MCPBase { tag: string; process(input: Message): Promise; } export interface MCPPromptInput extends Record { [key: string]: string; } export declare class MCPPrompt extends MCPBase { tag: string; process(input: MCPPromptInput): Promise; } export interface MCPResourceOptions extends MCPBaseOptions { uri: string; } export declare class MCPResource extends MCPBase { tag: string; constructor(options: MCPResourceOptions); uri: string; process(input: MCPPromptInput): Promise; } export {};