Langchain MCP Adapter
Popular agentic ai framework MCP support
Previously we introduced MCP and usage in software development. MCP is intended to create once, run in various environment, besides AI Apps (e.g. Claude desktop), IDEs, MCP can be tool in various agentic AI framework providing access to external information. Let’s see how it can be leveraged in popular agentic AI framework.
Langchain
Basic langchain-MCP adapter usage
use MCP server tool in langchain
MCP server
# math_server.py
from mcp.server.fastmcp import FastMCP
# MCP server name
mcp = FastMCP("Math")
# tool supported in this server
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers"""
return a + b
@mcp.tool()
def multiply(a: int, b: int) -> int:
"""Multiply two numbers"""
return a * b
if __name__ == "__main__":
mcp.run(transport="stdio")
Now we have Math MCP server and 2 tools available: add, multiply
MCP client
# Create server parameters for stdio connection
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from langchain_mcp_adapters.tools import load_mcp_tools
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-4o")
# provide method to specify how to launch MCP server
server_params = StdioServerParameters(
command="python",
# Make sure to update to the full absolute path to your math_server.py file
args=["/path/to/math_server.py"],
)
# use standard input/output (stdio) for communication, suitable for subprocess-based interactions.
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
# Initialize the connection
await session.initialize()
# Get tools
tools = await load_mcp_tools(session)
# Create and run the agent
agent = create_react_agent(model, tools)
agent_response = await agent.ainvoke({"messages": "what's (3 + 5) x 12?"})
Transport: besides stdio, MCP also supports Server-Sent Events (SSE) transport that facilitates server-to-client streaming over HTTP.
Use MultiServerMCPClient to connect to MCP server and it provides client.get_tools()
# save https://www.sec.gov/Archives/edgar/data/1015780/000102140801500016/dex106.htm as dex106.htm, as SEC blocks crawler
from langchain_mcp_adapters.client import MultiServerMCPClient
async def run():
async with MultiServerMCPClient(
{
"local_filesystem": {
"command": "npx",
"args": ["-y",
"@modelcontextprotocol/server-filesystem",
"/User/Documents/",
],
"transport": "stdio",
},
"web_fetch": {
"command": "npx",
"args": ["-y", "fetcher-mcp"],
"transport": "stdio",
},
"sqlite": {
"command": "uv",
"args": [
"--directory",
"sqlite",
"run",
"mcp-server-sqlite",
"--db-path",
"/Users/Desktop/chinook.db"
],
"transport": "stdio",
}
}
) as client:
# Check that we have tools from both servers
all_tools = client.get_tools()
# Create and run the agent
system_prompt = "You are a helpful assistant to answer questions, you need to first decide where to retrieve the answer. If user indicates a local file, search on local filesystem, if user indicate a web url, fetch from web, if sqlite, retrieve from sqlite, otherwise, use databricks vector search."
agent = create_react_agent(model, all_tools, prompt=system_prompt)
agent_responses = []
questions = ["list loan principal amount in dex106.htm",
"what is METHOD OF PAYMENT in https://contractbook.com/templates/loan-agreement",
"list tables in sqlite"]
for question in questions:
agent_response = await agent.ainvoke({"messages": question})
agent_responses.append(agent_response)
return agent_responses
results = asyncio.run(run())
for result in results:
print(result["messages"][-1].content)
Result (agent can pick based on user intent)
The document "dex106.htm" contains a Loan Agreement between the following parties:
- **Borrower**: Brigitte VanBaelen
- Address: 2031 Oakley Avenue, Menlo Park, CA 94025
- **Lender**: E*TRADE Group, Inc.
- Address: 4500 Bohannon Drive, Menlo Park, CA 94025
- Attention: Theodore J. Theophilos
### Summary of the Loan Agreement:
- **Date**: December 1, 2000
- **Loan Amount**: $500,000
- **Purpose**: The loan is intended for improvements to the Borrower's primary residence.
- **Interest Rate**: 6.10% per annum, compounded annually.
- **Maturity Date**: The entire principal balance and any accrued interest are due on December 1, 2002.
The agreement includes various terms regarding the loan, representations and warranties by the borrower, and conditions under which the loan may be accelerated.
If you need more specific details or sections from the document, please let me know!
The "Method of Payment" section in the Loan Agreement states:
**5. Method of Payment:**
Payment shall be made to the Lender in accordance with the Plan via **[Cash/Check/Money Order/Automatic Bank Withdrawal/Other]**.
5.1 The Borrower will make payment using this method unless prior written approval from the Lender allows otherwise.
The tables in the SQLite database are as follows:
1. albums
2. sqlite_sequence
3. artists
4. customers
5. employees
6. genres
7. invoices
8. invoice_items
9. media_types
10. playlists
11. playlist_track
12. tracks
13. sqlite_stat1
MCP Server SDK
Typescript-based MCP server
Python-based MCP server
Other OSS agentic ai framework supporting MCP
Llamaindex
Pydantic AI
Other
crewai (except community extension), openai agent sdk (except community extension), smolagents currently no native support
2025.3.26, openai agent sdk supports MCP