Skip to main content

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 use mcp-use as a comprehensive tool provider for your LangChain-powered agents.

How it Works

The LangChainAdapter 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.
The adapter maintains a mapping of these generated functions to their actual execution logic, allowing you to easily call them when requested by the agent.

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.
You can pass a disallowed_tools list to the adapter’s constructor to prevent specific tools, resources, or prompts from being exposed to the model.
3
Use the 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.
This list will include tools generated from your MCP tools, resources, and prompts.
If you don’t want to create all tools, you can call single functions. For example, if you only want to use tools and resources, you can do the following:
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.

Complete Example

For reference, here is the complete, runnable code for integrating mcp-use with LangChain.