Using mcp-use with LangChain
The LangChain adapter allows you to seamlessly integrate tools, resources, and prompts from any MCP server with LangChain agents. This enables you to usemcp-use as a comprehensive tool provider for your LangChain-powered agents.
How it Works
TheLangChainAdapter converts not only tools but also resources and prompts from your active MCP servers into a format compatible with LangChain’s tool-calling feature. It maps each of these MCP constructs to a callable function that the LangChain agent can request.
- Tools are converted directly to LangChain tools.
- Resources are converted into functions that take no arguments and read the resource’s content.
- Prompts are converted into functions that accept the prompt’s arguments.
Step-by-Step Guide
Here’s how to use the adapter to provide MCP tools, resources, and prompts to a LangChain agent.Before starting, install the LangChain SDK:
1
First, set up your
MCPClient with the desired MCP servers. This part of the process is the same as any other mcp-use application.2
Next, instantiate the
LangChainAdapter. This adapter will be responsible for converting MCP constructs into a format LangChain can understand.3
Use the This list will include tools generated from your MCP tools, resources, and prompts.
create_all method on the adapter to inspect all connected MCP servers and generate a list of tools, resources and prompts in the LangChain tool format.4
Now, you can use the generated
langchain_tools to create a LangChain agent. The agent will use the descriptions of these tools to decide if it needs to call any of them to answer the user’s query.