LangChain Integration with MCP Gateway¶
LangChain is a framework for developing applications powered by language models. Integrating LangChain with the Model Context Protocol (MCP) allows agents to utilize tools defined across one or more MCP servers, enabling seamless interaction with external data sources and services.
π§° Key Features¶
- Dynamic Tool Access: Connects to MCP servers to fetch available tools in real time.
- Multi-Server Support: Interact with tools defined on multiple MCP servers simultaneously.
- Standardized Communication: Utilizes the open MCP standard for consistent tool integration.
π Installation¶
To use MCP tools in LangChain, install the langchain-mcp-adapters
package:
π Connecting to MCP Gateway¶
Here's how to set up a connection to your MCP Gateway:
from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.prebuilt import create_react_agent
client = MultiServerMCPClient(
{
"gateway": {
"url": "http://localhost:4444/mcp",
"transport": "streamable_http",
}
}
)
Replace "http://localhost:4444/mcp"
with the URL of your MCP Gateway.
π€ Creating an Agent¶
After setting up the client, you can create a LangChain agent:
Replace your_language_model
with your configured language model instance.
π§ͺ Using the Agent¶
Once the agent is created, you can use it to perform tasks: