langchain-mcp-tools-ts
by: hideya
Package intended to simplify the use of MCP server tools within LangChain
📌Overview
Purpose: The utility aims to facilitate the accessibility of over 1500 MCP servers and their tools within the LangChain framework using TypeScript.
Overview: This package serves as a bridge between the Model Context Protocol (MCP) and LangChain, allowing developers to integrate a variety of external resources and tools efficiently. By providing a utility function, it simplifies the process of converting MCP server tools into a format compatible with LangChain.
Key Features:
-
Parallel Initialization: The
convertMcpToLangchainTools()
function enables the simultaneous initialization of multiple MCP servers, streamlining the setup process for developers. -
LangChain Compatibility: It converts available tools from MCP servers into an array of LangChain-compatible tools, allowing seamless integration with LangChain applications, which enhances their functionality.
-
Cleanup Functionality: The utility returns an async callback function to properly close all MCP server sessions, ensuring resource management and preventing memory leaks.
MCP To LangChain Tools Conversion Utility / TypeScript
This package simplifies using Model Context Protocol (MCP) server tools with LangChain and TypeScript.
MCP, an open standard announced by Anthropic, expands LLM's scope by enabling integration with various external tools and resources, including GitHub, Google Drive, Slack, Notion, Spotify, Docker, PostgreSQL, and more. MCP is expected to become the de facto industry standard, with OpenAI adopting it.
Over 2000 functional components are available as MCP servers, for example:
- MCP Server Listing on the Official Site: https://github.com/modelcontextprotocol/servers?tab=readme-ov-file#model-context-protocol-servers
- MCP.so - Find Awesome MCP Servers and Clients: https://mcp.so/
- Smithery: MCP Server Registry: https://smithery.ai/
The goal of this utility is to make these MCP servers readily accessible from LangChain.
It provides an async utility function convertMcpToLangchainTools()
that initializes multiple MCP servers in parallel and converts their tools into an array of LangChain-compatible tools.
For detailed usage, see the article:
"Supercharging LangChain: Integrating 2000+ MCP with ReAct"
A Python equivalent of this utility is available on PyPI: https://pypi.org/project/langchain-mcp-tools
Prerequisites
- Node.js 16+
Installation
npm i @h1deya/langchain-mcp-tools
Quick Start
A minimal working usage example is available in the langchain-mcp-tools-ts-usage repo.
The convertMcpToLangchainTools()
function accepts MCP server configurations similar to the mcpServers
property in Claude for Desktop, 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 initializes all specified MCP servers in parallel and returns LangChain Tools (tools: StructuredTool[]
) gathered from MCP servers, as well as a cleanup function to close all server sessions.
Example usage with LangChain:
// 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, try this LangChain application built with the utility: https://github.com/hideya/mcp-client-langchain-ts
Experimental Features
Remote MCP Server Support
MCP server configuration for SSE and Websocket servers:
"sse-server-name": {
url: `http://${sse_server_host}:${sse_server_port}/...`
},
"ws-server-name": {
url: `ws://${ws_server_host}:${ws_server_port}/...`
},
The key "url"
may change in the future to align with Claude for Desktop once it supports remote servers.
Authentication Support for SSE Connections
Supports authentication for SSE connections, useful for accessing MCP servers requiring OAuth.
Example configuration:
import { OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js';
class MyOAuthProvider implements OAuthClientProvider {
// Implementation details...
}
const mcpServers = {
"secure-server": {
url: "https://secure-mcp-server.example.com",
sseOptions: {
authProvider: new MyOAuthProvider(),
eventSourceInit: {
// Custom options
},
requestInit: {
headers: {
'X-Custom-Header': 'custom-value'
}
}
}
}
};
Working Directory Configuration for Local MCP Servers
The working directory for spawning local (stdio) MCP servers can be specified with the "cwd"
key:
"local-server-name": {
command: "...",
args: [...],
cwd: "/working/directory"
},
Configuration for Local MCP Server stderr
Redirection
Specify a file descriptor to redirect local MCP server's stderr using the "stderr"
key.
Example:
const logPath = `mcp-server-${serverName}.log`;
const logFd = fs.openSync(logPath, "w");
mcpServers[serverName].stderr = logFd;
Limitations
- Only text results of tool calls are supported.
- MCP features other than Tools are not supported.
Change Log
Available at: https://github.com/hideya/langchain-mcp-tools-ts/blob/main/CHANGELOG.md