Client for connecting to multiple MCP servers and loading LangChain-compatible tools.

Constructors

  • Create a new MultiServerMCPClient.

    Parameters

    • config: Record<string, {
          args: string[];
          command: string;
          cwd?: string;
          encoding?: string;
          env?: Record<string, string>;
          outputHandling?: "content" | "artifact" | {
              audio: undefined | "content" | "artifact";
              image: undefined | "content" | "artifact";
              resource: undefined | "content" | "artifact";
              text: undefined | "content" | "artifact";
          };
          restart?: {
              delayMs?: number;
              enabled?: boolean;
              maxAttempts?: number;
          };
          stderr?:
              | "overlapped"
              | "pipe"
              | "ignore"
              | "inherit";
          transport?: "stdio";
          type?: "stdio";
      } | {
          authProvider?: OAuthClientProvider;
          automaticSSEFallback?: boolean;
          headers?: Record<string, string>;
          outputHandling?: "content" | "artifact" | {
              audio: undefined | "content" | "artifact";
              image: undefined | "content" | "artifact";
              resource: undefined | "content" | "artifact";
              text: undefined | "content" | "artifact";
          };
          reconnect?: {
              delayMs?: number;
              enabled?: boolean;
              maxAttempts?: number;
          };
          transport?: "http" | "sse";
          type?: "http" | "sse";
          url: string;
      }> | {
          additionalToolNamePrefix?: string;
          mcpServers: Record<string, {
              args: string[];
              command: string;
              cwd?: string;
              encoding?: string;
              env?: Record<string, string>;
              outputHandling?: "content" | "artifact" | {
                  audio: undefined | "content" | "artifact";
                  image: undefined | "content" | "artifact";
                  resource: undefined | "content" | "artifact";
                  text: undefined | "content" | "artifact";
              };
              restart?: {
                  delayMs?: number;
                  enabled?: boolean;
                  maxAttempts?: number;
              };
              stderr?:
                  | "overlapped"
                  | "pipe"
                  | "ignore"
                  | "inherit";
              transport?: "stdio";
              type?: "stdio";
          } | {
              authProvider?: OAuthClientProvider;
              automaticSSEFallback?: boolean;
              headers?: Record<string, string>;
              outputHandling?: "content" | "artifact" | {
                  audio: undefined | "content" | "artifact";
                  image: undefined | "content" | "artifact";
                  resource: undefined | "content" | "artifact";
                  text: undefined | "content" | "artifact";
              };
              reconnect?: {
                  delayMs?: number;
                  enabled?: boolean;
                  maxAttempts?: number;
              };
              transport?: "http" | "sse";
              type?: "http" | "sse";
              url: string;
          }>;
          outputHandling?: "content" | "artifact" | {
              audio: undefined | "content" | "artifact";
              image: undefined | "content" | "artifact";
              resource: undefined | "content" | "artifact";
              text: undefined | "content" | "artifact";
          };
          prefixToolNameWithServerName?: boolean;
          throwOnLoadError?: boolean;
          useStandardContentBlocks?: boolean;
      }

      Configuration object

    Returns MultiServerMCPClient

Accessors

  • get config(): {
        additionalToolNamePrefix?: string;
        mcpServers: Record<string, {
            args: string[];
            command: string;
            cwd?: string;
            encoding?: string;
            env?: Record<string, string>;
            outputHandling?: "content" | "artifact" | {
                audio: undefined | "content" | "artifact";
                image: undefined | "content" | "artifact";
                resource: undefined | "content" | "artifact";
                text: undefined | "content" | "artifact";
            };
            restart?: {
                delayMs?: number;
                enabled?: boolean;
                maxAttempts?: number;
            };
            stderr?:
                | "overlapped"
                | "pipe"
                | "ignore"
                | "inherit";
            transport?: "stdio";
            type?: "stdio";
        } | {
            authProvider?: OAuthClientProvider;
            automaticSSEFallback?: boolean;
            headers?: Record<string, string>;
            outputHandling?: "content" | "artifact" | {
                audio: undefined | "content" | "artifact";
                image: undefined | "content" | "artifact";
                resource: undefined | "content" | "artifact";
                text: undefined | "content" | "artifact";
            };
            reconnect?: {
                delayMs?: number;
                enabled?: boolean;
                maxAttempts?: number;
            };
            transport?: "http" | "sse";
            type?: "http" | "sse";
            url: string;
        }>;
        outputHandling?: "content" | "artifact" | {
            audio: undefined | "content" | "artifact";
            image: undefined | "content" | "artifact";
            resource: undefined | "content" | "artifact";
            text: undefined | "content" | "artifact";
        };
        prefixToolNameWithServerName?: boolean;
        throwOnLoadError?: boolean;
        useStandardContentBlocks?: boolean;
    }
  • Returns clone of server config for inspection purposes.

    Client does not support config modifications.

    Returns {
        additionalToolNamePrefix?: string;
        mcpServers: Record<string, {
            args: string[];
            command: string;
            cwd?: string;
            encoding?: string;
            env?: Record<string, string>;
            outputHandling?: "content" | "artifact" | {
                audio: undefined | "content" | "artifact";
                image: undefined | "content" | "artifact";
                resource: undefined | "content" | "artifact";
                text: undefined | "content" | "artifact";
            };
            restart?: {
                delayMs?: number;
                enabled?: boolean;
                maxAttempts?: number;
            };
            stderr?:
                | "overlapped"
                | "pipe"
                | "ignore"
                | "inherit";
            transport?: "stdio";
            type?: "stdio";
        } | {
            authProvider?: OAuthClientProvider;
            automaticSSEFallback?: boolean;
            headers?: Record<string, string>;
            outputHandling?: "content" | "artifact" | {
                audio: undefined | "content" | "artifact";
                image: undefined | "content" | "artifact";
                resource: undefined | "content" | "artifact";
                text: undefined | "content" | "artifact";
            };
            reconnect?: {
                delayMs?: number;
                enabled?: boolean;
                maxAttempts?: number;
            };
            transport?: "http" | "sse";
            type?: "http" | "sse";
            url: string;
        }>;
        outputHandling?: "content" | "artifact" | {
            audio: undefined | "content" | "artifact";
            image: undefined | "content" | "artifact";
            resource: undefined | "content" | "artifact";
            text: undefined | "content" | "artifact";
        };
        prefixToolNameWithServerName?: boolean;
        throwOnLoadError?: boolean;
        useStandardContentBlocks?: boolean;
    }

    • OptionaladditionalToolNamePrefix?: string

      An additional prefix to add to the tool name Prefixes are separated by double underscores (example: mcp__add).

      "mcp"
      
    • mcpServers: Record<string, {
          args: string[];
          command: string;
          cwd?: string;
          encoding?: string;
          env?: Record<string, string>;
          outputHandling?: "content" | "artifact" | {
              audio: undefined | "content" | "artifact";
              image: undefined | "content" | "artifact";
              resource: undefined | "content" | "artifact";
              text: undefined | "content" | "artifact";
          };
          restart?: {
              delayMs?: number;
              enabled?: boolean;
              maxAttempts?: number;
          };
          stderr?:
              | "overlapped"
              | "pipe"
              | "ignore"
              | "inherit";
          transport?: "stdio";
          type?: "stdio";
      } | {
          authProvider?: OAuthClientProvider;
          automaticSSEFallback?: boolean;
          headers?: Record<string, string>;
          outputHandling?: "content" | "artifact" | {
              audio: undefined | "content" | "artifact";
              image: undefined | "content" | "artifact";
              resource: undefined | "content" | "artifact";
              text: undefined | "content" | "artifact";
          };
          reconnect?: {
              delayMs?: number;
              enabled?: boolean;
              maxAttempts?: number;
          };
          transport?: "http" | "sse";
          type?: "http" | "sse";
          url: string;
      }>

      A map of server names to their configuration

    • OptionaloutputHandling?: "content" | "artifact" | {
          audio: undefined | "content" | "artifact";
          image: undefined | "content" | "artifact";
          resource: undefined | "content" | "artifact";
          text: undefined | "content" | "artifact";
      }

      Defines where to place each tool output type in the LangChain ToolMessage. Can be overridden on a per-server basis.

      Can be set to content or artifact to send all tool output into the ToolMessage.content or ToolMessage.artifact array, respectively, or you can assign an object that maps each content type to content or artifact.

      Items in the content field will be used as input context for the LLM, while the artifact field is used for capturing tool output that won't be shown to the model, to be used in some later workflow step.

      For example, imagine that you have a SQL query tool that can return huge result sets. Rather than sending these large outputs directly to the model, perhaps you want the model to be able to inspect the output in a code execution environment. In this case, you would set the output handling for the resource type to artifact (its default value), and then upon initialization of your code execution environment, you would look through your message history for ToolMessages with the artifact field set to resource, and use the content field during initialization of the environment.

    • OptionalprefixToolNameWithServerName?: boolean

      Whether to prefix tool names with the server name. Prefixes are separated by double underscores (example: calculator_server_1__add).

      true
      
    • OptionalthrowOnLoadError?: boolean

      Whether to throw an error if a tool fails to load

      true
      
    • OptionaluseStandardContentBlocks?: boolean

      If true, the tool will use LangChain's standard multimodal content blocks for tools that output image or audio content, and embedded resources will be converted to StandardFileBlock objects. When false, all artifacts are left in their MCP format, but embedded resources will be converted to StandardFileBlock objects if ClientConfig#outputHandling causes embedded resources to be treated as content, as otherwise ChatModel providers will not be able to interpret them.

      false
      

Methods

  • Get a the MCP client for a specific server. Useful for fetching prompts or resources from that server.

    Parameters

    • serverName: string

      The name of the server

    Returns Promise<undefined | Client<{
        method: string;
        params?: {};
    }, {
        method: string;
        params?: {};
    }, {}>>

    The client for the server, or undefined if the server is not connected

  • Get tools from specified servers as a flattened array.

    Parameters

    • Rest...servers: string[]

      Optional array of server names to filter tools by. If not provided, returns tools from all servers.

    Returns Promise<StructuredToolInterface<ToolInputSchemaBase, any, any>[]>

    A flattened array of tools from the specified servers (or all servers)

  • Proactively initialize connections to all servers. This will be called automatically when methods requiring an active connection (like getTools or getClient) are called, but you can call it directly to ensure all connections are established before using the tools.

    Returns Promise<Record<string, StructuredToolInterface<ToolInputSchemaBase, any, any>[]>>

    A map of server names to arrays of tools

    If initialization fails