This package provides a seamless bridge between LangChain.js and Model Context Protocol (MCP), allowing developers to easily incorporate MCP tools into their LangChain workflows. It serves as a TypeScript port of the Python-based langchain-mcp library, making MCP functionality accessible within JavaScript and TypeScript applications. With this client, you can initialize an MCP server, extract LangChain-compatible tools, and integrate them directly into agents and other LangChain constructs. This enables powerful AI applications that can leverage both LangChain's flexibility and MCP's standardized tool interfaces.
The mcp-langchain-ts-client
package allows you to use Model Context Protocol (MCP) tools within your LangChain.js applications. This client bridges the gap between these two ecosystems, enabling you to build more powerful AI applications.
You can install the package via npm:
npm install mcp-langchain-ts-client
To use the MCP client with LangChain.js, follow these steps:
MCPToolkit
class from the package:import { MCPToolkit } from "mcp-langchain-ts-client";
const serverParams = {
command: "npx",
args: [
"-y",
"@modelcontextprotocol/server-everything"
]
};
const toolkit = new MCPToolkit(serverParams);
await toolkit.initialize();
const tools = toolkit.tools;
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { ChatAnthropic } from "@langchain/anthropic";
const llm = new ChatAnthropic({ model: 'claude-3-5-sonnet-20241022' });
const agent = createReactAgent({ llm, tools });
// Now you can use the agent with MCP tools
const result = await agent.invoke({ input: "Your query here" });
You can customize the MCP server configuration by modifying the server parameters. For example, you might want to use a different MCP server package or specify additional arguments:
const serverParams = {
command: "npx",
args: [
"-y",
"@modelcontextprotocol/server-custom",
"--some-option=value"
]
};
The toolkit provides error handling for common issues. If the MCP server fails to start or respond, the initialize()
method will throw an error with details about what went wrong.
When you're done using the MCP toolkit, it's good practice to clean up resources:
await toolkit.cleanup();
This will properly shut down the MCP server process.