MCP Server - LangChain
LangChain 1.4 and later supports MCP servers through its first-party MCPAdapter. Install LangChain with the MCP extra and the Anthropic integration:
pip install "langchain[mcp]>=1.4.0" langchain-anthropicCreate an authenticated FastMCP client, discover the Xweather tools, and pass them to a LangChain agent:
import asyncio
import os
from fastmcp.client import Client
from langchain.agents import create_agent
from langchain.mcp import MCPAdapter
from langchain_anthropic import ChatAnthropic
async def main():
url = (
"https://mcp.api.xweather.com/mcp"
"?include_tools=xweather_get_current_weather,"
"xweather_get_forecast_weather,"
"xweather_get_current_weather_impacts"
)
client = Client(
url, auth=os.environ["XWEATHER_API_KEY"]
)
async with MCPAdapter(client) as adapter:
tools = await adapter.list_tools()
agent = create_agent(
model=ChatAnthropic(
model="claude-sonnet-5", api_key=os.environ["ANTHROPIC_API_KEY"]
),
tools=tools,
system_prompt=(
"You are a weather analyst. Use Xweather tools when needed "
"and identify the time period and location in your answer."
),
)
response = await agent.ainvoke(
{
"messages": [
{
"role": "user",
"content": "Draft an operations brief for ORD tonight.",
}
]
}
)
print(response["messages"][-1].content)
asyncio.run(main())Tips
- Keep discovery and agent execution inside the
MCPAdaptercontext so the connection remains open. - Restrict exposed tools with Xweather’s
include_toolsorinclude_tagsquery parameters. - The
langchain.mcpnamespace is currently beta and may change in later LangChain releases.