MCPAdapter: tools from another program
MCP is a standard way for a program to offer tools to any agent. MCPAdapter connects to an MCP server, lists its tools, and hands them to create_agent as LangChain tools.
The Model Context Protocol lets one team write a tool server and any agent framework use it. MCP support is an extra of the langchain package, built on the FastMCP library, and marked beta: importing it prints a warning that the API may change.
pip install "langchain[mcp]==1.4.2"A server
from fastmcp import FastMCP
mcp = FastMCP("shop")
ORDERS = {"A17": "shipped on 3 March", "C40": "waiting for stock"}
@mcp.tool
def lookup_order(order_id: str) -> str:
"""Look up an order's shipping status by its id, such as A17."""
status = ORDERS.get(order_id)
return f"{order_id} {status}." if status else f"{order_id} is not an order we have."
if __name__ == "__main__":
mcp.run()This is the order lookup from lesson 5, served over MCP by FastMCP. Run as a script, it talks to its client over standard input and output.
Connecting
from langchain.mcp import MCPAdapter
MCPAdapter("shop_server.py")A plain string is always read as a URL, so that a string can never start a program by accident. A local server has to be asked for explicitly, with a Path.
import asyncio
from pathlib import Path
from langchain.agents import create_agent
from langchain.mcp import MCPAdapter
from shop_model import ShopModel
async def main():
async with MCPAdapter(Path("shop_server.py")) as adapter:
tools = await adapter.list_tools()
print([tool.name for tool in tools])
agent = create_agent(ShopModel(), tools)
result = await agent.ainvoke({"messages": [{"role": "user", "content": "Where is A17?"}]})
print(result["messages"][-1].text)
asyncio.run(main())MCPAdapter started the server, and list_tools turned its tool into a LangChain tool with the same name and description. The agent is the same one as lesson 7. It ran after the async with block closed, because each tool call opens its own connection to the server.
- Add a second
@mcp.toolto the server and print the tool names again. - Print
tools[0].descriptionandtools[0].args. - Ask about B22 through the MCP tool.
This is what real progress feels like.