LangChain.js MCP Adapters logo

LangChain.js MCP Adapters

by langchain-ai

This library provides a lightweight wrapper that makes Anthropic Model Context Protocol (MCP) tools compatible with LangChain.js and LangGraph.js. It allows you to connect to and utilize MCP servers within your LangChain applications.

View on GitHub

Last updated: N/A

What is LangChain.js MCP Adapters?

LangChain.js MCP Adapters is a library that enables integration of Anthropic's Model Context Protocol (MCP) tools with LangChain.js and LangGraph.js. It simplifies the process of connecting to MCP servers (either locally via stdio or remotely via SSE) and utilizing the tools they expose within LangChain agent workflows.

How to use LangChain.js MCP Adapters?

To use LangChain.js MCP Adapters, install the package using npm. Then, set up either stdio or SSE connections to your MCP server(s). You can configure single or multiple servers using code or a JSON configuration file. Once connected, load the MCP tools and integrate them with LangChain agents.

Key features of LangChain.js MCP Adapters

  • Transport Options (stdio, SSE)

  • Multi-Server Management

  • Agent Integration (LangChain.js, LangGraph.js)

  • Flexible Configuration (JSON, programmatic)

  • Automatic configuration loading from ./mcp.json

  • Environment variable substitution

  • Configurable reconnection strategies

Use cases of LangChain.js MCP Adapters

  • Integrating external tools (e.g., math, weather) into LangChain agents

  • Connecting to multiple MCP servers and utilizing their combined capabilities

  • Building agents that leverage context provided by MCP servers

  • Creating modular agent workflows with easily swappable tool configurations

FAQ from LangChain.js MCP Adapters

How do I connect to an MCP server using stdio?

Use client.connectToServerViaStdio('server-name', 'python', ['./server.py']). Ensure Python 3.8+ is installed.

How do I connect to an MCP server using SSE?

Use client.connectToServerViaSSE('server-name', 'http://localhost:8000/sse'). The MCP server needs to have a running SSE endpoint.

How do I pass authentication headers for SSE connections?

Pass a headers object to connectToServerViaSSE, e.g., client.connectToServerViaSSE('server-name', 'url', { Authorization: 'Bearer token' }, true). You may need to install extended-eventsource and set useNodeEventSource to true.

How do I manage connections to multiple MCP servers?

Use the MultiServerMCPClient. Connect to each server individually using connectToServerViaStdio or connectToServerViaSSE. Then get the tools by their server or as a flattened collection.

How do I configure the client using a JSON file?

Create a mcp.json file with connection details and use MultiServerMCPClient.fromConfigFile(). You can add multiple config files to load configurations from all of them.

LangChain.js MCP Adapters - MCP Client | MCP Directory