MCP to LangChain/LangGraph Adapter

by SDCalvo

312 downloads Not rated yet

About

Addapter that turns MCP server tools into langchain usable tools

Explore

- Connects to any MCP server via stdio transport.
- Automatically discovers and converts MCP tools to LangChain BaseTool.
- Supports both LangChain agents and LangGraph react agents.
- Manages conversation history with LangGraph memory checkpoints.
- Allows custom environment variables per server process.

To use this adapter, you need to have the necessary packages installed:


load_dotenv()


Alternatively, you can set the API key directly in your environment or code:

```python
import os
os.environ["OPENAI_API_KEY"] = "your_api_key_here"

config = {"configurable": {"thread_id": "example-thread"}}

Here's a complete example of how to use the adapter:

python
from mcp_langchain_adapter import MCPAdapter

name

Name of the tool

description

Description of the tool

server_script_path

Path to the MCP server script

env

Optional environment variables for the server process

args_schema

Optional Pydantic model for tool arguments

print(f"Found {len(tools)} tools:")
for tool in tools:
print(f"- {tool.name}: {tool.description}")


Once you have the tools, you can use them in LangChain applications:

python
from langchain.agents import AgentExecutor, create_react_agent
from langchain.prompts import PromptTemplate
from langchain_openai import ChatOpenAI

agent = create_react_agent(llm, tools, prompt_template)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

LangGraph provides a more modern, flexible approach to building agents. Here's how to use our MCP tools with LangGraph:

``python
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
from langgraph.prebuilt import create_react_agent
from langgraph.checkpoint.memory import MemorySaver

agent = create_react_agent(
llm,
tools,
prompt="You are a helpful AI assistant that can use tools to solve problems.",
checkpointer=memory
)

The MCPToolWrapper class extends LangChain's BaseTool to wrap MCP tools:

`python
MCPToolWrapper(
name: str,
description: str,
server_script_path: str,
env: Optional[Dict[str, str]] = None,
args_schema: Optional[Type[BaseModel]] = None
)
`

-
name: Name of the tool
-
description: Description of the tool
-
server_script_path: Path to the MCP server script
-
env: Optional environment variables for the server process
-
args_schema: Optional Pydantic model for tool arguments

print(f"Found {len(tools)} tools:")
for tool in tools:
print(f"- {tool.name}: {tool.description}")

add_tool = adapter.get_tool_by_name("add")
if add_tool:
result = add_tool.run({"a": 5, "b": 7})
print(f"Result of add(5, 7): {result}")

weather_tool = adapter.get_tool_by_name("get_weather")
if weather_tool:
result = weather_tool.run({"city": "London"})
print(f"Result of get_weather('London'): {result}")
``

This project provides an adapter that allows you to use MCP (Multi-modal Conversational Procedure) server tools in LangChain and LangGraph applications. With this adapter, you can seamlessly integrate MCP's tools into your AI application pipelines.

Table of Contents

- Introduction - Installation - Getting Started - Setting Up the MCP Server - Connecting to the MCP Server - Using MCP Tools with LangChain - Using MCP Tools with LangGraph - API Reference - MCPAdapter - MCPToolWrapper - Utility Functions - Examples - Basic Usage - Integration with LangChain Agents - Integration with LangGraph Agents - Troubleshooting - Contributing

Introduction

The MCP to LangChain/LangGraph Adapter bridges the gap between MCP servers, which provide various tools through a standardized interface, and LangChain/LangGraph, popular frameworks for building applications with large language models. This adapter enables you to: - Connect to an MCP server - Discover available tools - Convert MCP tools to LangChain-compatible tools - Use these tools in LangChain agents, chains, and LangGraph agents

Installation

To use this adapter, you need to have the necessary packages installed: ```bash
No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.