LangChain's official LangChain.js MCP Adapters library has been released at:
- npmjs: https://www.npmjs.com/package/@langchain/mcp-adapters
- github: https://github.com/langchain-ai/langchainjs-mcp-adapters
You may want to consider using the above if you don't have specific needs for using this library...
This package is intended to simplify the use of Model Context Protocol (MCP) server tools with LangChain / TypeScript.
Model Context Protocol (MCP), an open standard announced by Anthropic, dramatically expands LLM's scope by enabling external tool and resource integration, including GitHub, Google Drive, Slack, Notion, Spotify, Docker, PostgreSQL, and more…
MCP is likely to become the de facto industry standard as OpenAI has announced its adoption.
Over 2000 functional components available as MCP servers:
- MCP Server Listing on the Official Site
- MCP.so - Find Awesome MCP Servers and Clients
- Smithery: MCP Server Registry
The goal of this utility is to make these 2000+ MCP servers readily accessible from LangChain.
It contains a utility function convertMcpToLangchainTools()
.
This async function handles parallel initialization of specified multiple MCP servers
and converts their available tools into an array of LangChain-compatible tools.
For detailed information on how to use this library, please refer to the following document:
A python equivalent of this utility is available here
- Node.js 16+
npm i @h1deya/langchain-mcp-tools
Can be found here
A minimal but complete working usage example can be found in this example in the langchain-mcp-tools-ts-usage repo
convertMcpToLangchainTools()
utility function accepts MCP server configurations
that follow the same structure as
Claude for Desktop,
but only the contents of the mcpServers
property,
and is expressed as a JS Object, e.g.:
const mcpServers: McpServersConfig = {
filesystem: {
command: "npx",
args: ["-y", "@modelcontextprotocol/server-filesystem", "."]
},
fetch: {
command: "uvx",
args: ["mcp-server-fetch"]
}
};
const { tools, cleanup } = await convertMcpToLangchainTools(mcpServers);
This utility function initializes all specified MCP servers in parallel,
and returns LangChain Tools
(tools: StructuredTool[]
)
by gathering available MCP tools from the servers,
and by wrapping them into LangChain tools.
It also returns an async callback function (cleanup: McpServerCleanupFn
)
to be invoked to close all MCP server sessions when finished.
The returned tools can be used with LangChain, e.g.:
// import { ChatAnthropic } from "@langchain/anthropic";
const llm = new ChatAnthropic({ model: "claude-3-7-sonnet-latest" });
// import { createReactAgent } from "@langchain/langgraph/prebuilt";
const agent = createReactAgent({
llm,
tools
});
For hands-on experimentation with MCP server integration, try this LangChain application built with the utility
For detailed information on how to use this library, please refer to the following document:
"Supercharging LangChain: Integrating 2000+ MCP with ReAct"
mcp_servers
configuration for SSE and Websocket servers are as follows:
"sse-server-name": {
url: `http://${sse_server_host}:${sse_server_port}/...`
},
"ws-server-name": {
url: `ws://${ws_server_host}:${ws_server_port}/...`
},
Note that the key "url"
may be changed in the future to match
the MCP server configurations used by Claude for Desktop once
it introduces remote server support.
A usage example can be found here
The library now supports authentication for SSE connections to MCP servers. This is particularly useful for accessing authenticated MCP servers that require OAuth.
To enable authentication, provide SSE options in your server configuration:
import { OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js';
// Implement your own OAuth client provider
class MyOAuthProvider implements OAuthClientProvider {
// Implementation details...
}
const mcpServers = {
"secure-server": {
url: "https://secure-mcp-server.example.com",
sseOptions: {
// Provide an OAuth client provider
authProvider: new MyOAuthProvider(),
// Optionally customize the initial SSE request
eventSourceInit: {
// Custom options
},
// Optionally customize recurring POST requests
requestInit: {
headers: {
'X-Custom-Header': 'custom-value'
}
}
}
}
};
A simple example showing how to implement an OAuth client provider can be found in sse-auth-test-client.ts of this usage examples repo.
For testing purposes, a sample MCP server with OAuth authentication support that works with the above client is provided in sse-auth-test-server.ts of this usage examples repo.
The working directory that is used when spawning a local (stdio) MCP server
can be specified with the "cwd"
key as follows:
"local-server-name": {
command: "...",
args: [...],
cwd: "/working/directory" // the working dir to be use by the server
},
The key name cwd
is derived from
TypeScript SDK's StdioServerParameters
.
A new key "stderr"
has been introduced to specify a file descriptor
to which local (stdio) MCP server's stderr is redirected.
The key name stderr
is derived from
TypeScript SDK's StdioServerParameters
.
const logPath = `mcp-server-${serverName}.log`;
const logFd = fs.openSync(logPath, "w");
mcpServers[serverName].stderr = logFd;
A usage example can be found here
- Currently, only text results of tool calls are supported.
- MCP features other than Tools are not supported.
Can be found here