MCP + LangChain Integration Demo
Overview
This project demonstrates how to connect MCP (Model Context Protocol) servers to a LangChain agent, enabling the LLM to use external tools (web fetching, filesystem access, etc.) via a standardized protocol.
Setup
Install Dependencies```bash
pip install langchain langchain-openai langchain-mcp-adapters mcp python-dotenv mcp-server-fetch
### Environment Variables
Create a `.env` file:```
OPENAI_API_KEY=your-key-here
Run```bash
python mcp_langchain.py
---
## Demo Results
### MCP Server Connection & Tools Loaded```
============================================================
MCP + LangChain Integration Demo
============================================================
[1] Connecting to MCP servers...
Configured: ['fetch']
[2] Loading tools from MCP servers...
Loaded 1 tool(s):
- fetch: Fetches a URL from the internet and optionally extracts its contents as markdown
[3] Creating LangChain ReAct agent with MCP tools...
Agent ready.
Agent Responses Using MCP Capabilities
Query 1 — Web API inspection (practical use case: debugging HTTP requests)```
Query: Fetch https://httpbin.org/get and tell me what headers were sent.
Agent response:
- Accept: /
- Accept-Encoding: gzip, deflate, br, zstd
- Host: httpbin.org
- User-Agent: ModelContextProtocol/1.0
- X-Amzn-Trace-Id: Root=1-699866a7-...
**Query 2 — Web content summarization (practical use case: research assistant)**```
Query: Fetch https://httpbin.org/html and summarize the content.
Agent response:
The content is an excerpt from Herman Melville's "Moby-Dick".
It tells the mournful story of Perth, an old blacksmith...
The narrative reflects on the tragic unraveling of Perth's life.
Errors Encountered & Resolutions
| Error | Cause | Resolution |
|---|---|---|
NotImplementedError: MultiServerMCPClient cannot be used as a context manager |
API changed in langchain-mcp-adapters 0.2.x — async with removed |
Switched to client = MultiServerMCPClient(...) + await client.get_tools() |
command not found: /tmp/mcp_demo |
@modelcontextprotocol/server-filesystem npm package incompatible with npm 9.7.1 (uses npm: protocol links) |
Removed filesystem server; used mcp-server-fetch (Python) which works reliably |
create_react_agent deprecation warning |
langgraph.prebuilt moving it to langchain.agents |
Non-blocking warning; can update import when ready |
Key Takeaways
MultiServerMCPClientin v0.2.x is not a context manager — just instantiate andawait get_tools()mcp-server-fetch(Python) works out of the box as a stdio MCP server- Tool conversion is automatic —
get_tools()returns LangChain-compatibleBaseToolobjects - The agent correctly chose to invoke the
fetchtool for both queries and interpreted the results
