langchain.js
    Preparing search index...

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

    Index

    Constructors

    • Create a new MultiServerMCPClient.

      Parameters

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

        Configuration object

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

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

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

            A map of server names to their configuration

          • 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
            
          • OptionaldefaultToolTimeout?: number

            Default timeout in milliseconds for tool execution. Must be greater than 0. If not specified, tools will use their own configured timeout values.

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

            Defines where to place each tool output type in the LangChain ToolMessage.

            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.

      Returns MultiServerMCPClient

    Accessors

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

      Returns clone of server config for inspection purposes.

      Client does not support config modifications.

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

      • OptionaladditionalToolNamePrefix?: string

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

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

        A map of server names to their configuration

      • 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
        
      • OptionaldefaultToolTimeout?: number

        Default timeout in milliseconds for tool execution. Must be greater than 0. If not specified, tools will use their own configured timeout values.

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

        Defines where to place each tool output type in the LangChain ToolMessage.

        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.

    Methods

    • Close all connections.

      Returns Promise<void>

    • 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<{}, {}, { [key: string]: unknown }>>

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

    • Get tools from specified servers as a flattened array.

      Parameters

      • ...servers: string[]

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

      Returns Promise<DynamicStructuredTool[]>

      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, DynamicStructuredTool[]>>

      A map of server names to arrays of tools

      If initialization fails