$git clone https://github.com/langchain-ai/langchain-mcp-adaptersThis library provides a lightweight wrapper that makes Anthropic Model Context Protocol (MCP) tools compatible with LangChain and LangGraph.
| 1 | # LangChain MCP Adapters |
| 2 | |
| 3 | This library provides a lightweight wrapper that makes [Anthropic Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) tools compatible with [LangChain](https://github.com/langchain-ai/langchain) and [LangGraph](https://github.com/langchain-ai/langgraph). |
| 4 | |
| 5 |  |
| 6 | |
| 7 | > [!note] |
| 8 | > A JavaScript/TypeScript version of this library is also available at [langchainjs](https://github.com/langchain-ai/langchainjs/tree/main/libs/langchain-mcp-adapters/). |
| 9 | |
| 10 | ## Features |
| 11 | |
| 12 | - 🛠️ Convert MCP tools into [LangChain tools](https://python.langchain.com/docs/concepts/tools/) that can be used with [LangGraph](https://github.com/langchain-ai/langgraph) agents |
| 13 | - 📦 A client implementation that allows you to connect to multiple MCP servers and load tools from them |
| 14 | |
| 15 | ## Installation |
| 16 | |
| 17 | ```bash |
| 18 | pip install langchain-mcp-adapters |
| 19 | ``` |
| 20 | |
| 21 | ## Quickstart |
| 22 | |
| 23 | Here is a simple example of using the MCP tools with a LangGraph agent. |
| 24 | |
| 25 | ```bash |
| 26 | pip install langchain-mcp-adapters langgraph "langchain[openai]" |
| 27 | |
| 28 | export OPENAI_API_KEY=<your_api_key> |
| 29 | ``` |
| 30 | |
| 31 | ### Server |
| 32 | |
| 33 | First, let's create an MCP server that can add and multiply numbers. |
| 34 | |
| 35 | ```python |
| 36 | # math_server.py |
| 37 | from mcp.server.fastmcp import FastMCP |
| 38 | |
| 39 | mcp = FastMCP("Math") |
| 40 | |
| 41 | @mcp.tool() |
| 42 | def add(a: int, b: int) -> int: |
| 43 | """Add two numbers""" |
| 44 | return a + b |
| 45 | |
| 46 | @mcp.tool() |
| 47 | def multiply(a: int, b: int) -> int: |
| 48 | """Multiply two numbers""" |
| 49 | return a * b |
| 50 | |
| 51 | if __name__ == "__main__": |
| 52 | mcp.run(transport="stdio") |
| 53 | ``` |
| 54 | |
| 55 | ### Client |
| 56 | |
| 57 | ```python |
| 58 | # Create server parameters for stdio connection |
| 59 | from mcp import ClientSession, StdioServerParameters |
| 60 | from mcp.client.stdio import stdio_client |
| 61 | |
| 62 | from langchain_mcp_adapters.tools import load_mcp_tools |
| 63 | from langchain.agents import create_agent |
| 64 | |
| 65 | server_params = StdioServerParameters( |
| 66 | command="python", |
| 67 | # Make sure to update to the full absolute path to your math_server.py file |
| 68 | args=["/path/to/math_server.py"], |
| 69 | ) |
| 70 | |
| 71 | async with stdio_client(server_params) as (read, write): |
| 72 | async with ClientSession(read, write) as session: |
| 73 | # Initialize the connection |
| 74 | await session.initialize() |
| 75 | |
| 76 | # Get tools |
| 77 | tools = await load_mcp_tools(session) |
| 78 | |
| 79 | # Create and run the agent |
| 80 | agent = create_agent("openai:gpt-4.1", tools) |
| 81 | agent_response = await agent.ainvoke({"messages": "what's (3 + 5) x 12?"}) |
| 82 | ``` |
| 83 | |
| 84 | ## Multiple MCP Servers |
| 85 | |
| 86 | The library also allows you to connect to multiple MCP servers and load tools from them: |
| 87 | |
| 88 | ### Server |
| 89 | |
| 90 | ```python |
| 91 | # math_server.py |
| 92 | ... |
| 93 | |
| 94 | # weather_server.py |
| 95 | from typing import List |
| 96 | from mcp.server.fastmcp import FastMCP |
| 97 | |
| 98 | mcp = FastMCP("Weather") |
| 99 | |
| 100 | @mcp.tool() |
| 101 | async def get_weather(location: str) -> str: |
| 102 | """Get weather for location.""" |
| 103 | return "It's always sunny in New York" |
| 104 | |
| 105 | if __name__ == "__main__": |
| 106 | mcp.run(transport="http") |
| 107 | ``` |
| 108 | |
| 109 | ```bash |
| 110 | python weather_server.py |
| 111 | ``` |
| 112 | |
| 113 | ### Client |
| 114 | |
| 115 | ```python |
| 116 | from langchain_mcp_adapters.client import MultiServerMCPClient |
| 117 | from langchain.agents import create_agent |
| 118 | |
| 119 | client = MultiServerMCPClient( |
| 120 | { |
| 121 | "math": { |
| 122 | "command": "python", |
| 123 | # Make sure to update to the full absolute path to your math_server.py file |
| 124 | "args": ["/path/to/math_server.py"], |
| 125 | "transport": "stdio", |
| 126 | }, |
| 127 | "weather": { |
| 128 | # Make sure you start your weather server on port 8000 |
| 129 | "url": "http://localhost:8000/mcp", |
| 130 | "transport": "http", |
| 131 | } |
| 132 | } |
| 133 | ) |
| 134 | tools = await client.get_tools() |
| 135 | agent = create_agent("openai:gpt-4.1", tools) |
| 136 | math_response = await agent.ainvoke({"messages": "what's (3 + 5) x 12?"}) |
| 137 | weather_response = await agent.ainvoke({"messages": "what is the weather in nyc?"}) |
| 138 | ``` |
| 139 | |
| 140 | > [!note] |
| 141 | > Example above will start a new MCP `ClientSession` for each tool invocation. If you would like to explicitly start a session for a given server, you can do: |
| 142 | > |
| 143 | > ```python |
| 144 | > from langchain_mcp_adapters.tools import load_mcp_tools |
| 145 | > |
| 146 | > client = MultiServerMCPClient({...}) |
| 147 | > asy |