@langchain/mcp-adapters
Version:
LangChain.js adapters for Model Context Protocol (MCP)
132 lines (131 loc) • 4.74 kB
TypeScript
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import type { DynamicStructuredTool } from "@langchain/core/tools";
import { type ClientConfig, type Connection } from "./types.js";
/**
* Error class for MCP client operations
*/
export declare class MCPClientError extends Error {
readonly serverName?: string | undefined;
constructor(message: string, serverName?: string | undefined);
}
/**
* Client for connecting to multiple MCP servers and loading LangChain-compatible tools.
*/
export declare class MultiServerMCPClient {
private _clients;
private _serverNameToTools;
private _connections?;
private _loadToolsOptions;
private _cleanupFunctions;
private _transportInstances;
private _config;
/**
* Returns clone of server config for inspection purposes.
*
* Client does not support config modifications.
*/
get config(): ClientConfig;
/**
* Create a new MultiServerMCPClient.
*
* @param config - Configuration object
*/
constructor(config: ClientConfig | Record<string, Connection>);
/**
* Proactively initialize connections to all servers. This will be called automatically when
* methods requiring an active connection (like {@link getTools} or {@link getClient}) are called,
* but you can call it directly to ensure all connections are established before using the tools.
*
* @returns A map of server names to arrays of tools
* @throws {MCPClientError} If initialization fails
*/
initializeConnections(): Promise<Record<string, DynamicStructuredTool[]>>;
/**
* Get tools from specified servers as a flattened array.
*
* @param servers - Optional array of server names to filter tools by.
* If not provided, returns tools from all servers.
* @returns A flattened array of tools from the specified servers (or all servers)
*/
getTools(...servers: string[]): Promise<DynamicStructuredTool[]>;
/**
* Get a the MCP client for a specific server. Useful for fetching prompts or resources from that server.
*
* @param serverName - The name of the server
* @returns The client for the server, or undefined if the server is not connected
*/
getClient(serverName: string): Promise<Client | undefined>;
/**
* Close all connections.
*/
close(): Promise<void>;
/**
* Initialize a stdio connection
*/
private _initializeStdioConnection;
/**
* Set up stdio restart handling
*/
private _setupStdioRestart;
private _getHttpErrorCode;
private _createAuthenticationErrorMessage;
private _toSSEConnectionURL;
/**
* Initialize a streamable HTTP connection
*/
private _initializeStreamableHTTPConnection;
/**
* Initialize an SSE connection
*
* Don't call this directly unless SSE transport is explicitly requested. Otherwise,
* use _initializeStreamableHTTPConnection and it'll fall back to SSE if needed for
* backwards compatibility.
*/
private _initializeSSEConnection;
/**
* Create an SSE transport with appropriate EventSource implementation
*
* @param serverName - The name of the server
* @param url - The URL of the server
* @param headers - The headers to send with the request
* @param authProvider - The OAuth client provider to use for authentication
* @returns The SSE transport
*/
private _createSSETransport;
private _createStreamableHTTPTransport;
/**
* Set up reconnect handling for SSE (Streamable HTTP reconnects are more complex and are handled internally by the SDK)
*/
private _setupSSEReconnect;
/**
* Load tools for a specific server
*/
private _loadToolsForServer;
/**
* Attempt to reconnect to a server after a connection failure.
*
* @param serverName - The name of the server to reconnect to
* @param connection - The connection configuration
* @param maxAttempts - Maximum number of reconnection attempts
* @param delayMs - Delay in milliseconds between reconnection attempts
* @private
*/
private _attemptReconnect;
/**
* Clean up resources for a specific server
*/
private _cleanupServerResources;
/**
* Get all tools from all servers as a flat array.
*
* @returns A flattened array of all tools
*/
private _getAllToolsAsFlatArray;
/**
* Get tools from specific servers as a flat array.
*
* @param serverNames - Names of servers to get tools from
* @returns A flattened array of tools from the specified servers
*/
private _getToolsFromServers;
}