Part 1. Real-Time LangGraph Agent with MCP Tool Execution
About
This project demonstrates a decoupled real-time agent architecture that connects LangGraph agents to remote tools served by custom MCP (Modular Command Protocol) servers. The architecture enables a flexible and scalable multi-agent system where each tool can be hosted independent
Details
- Author
- junfanz1
- GitHub stars
- 27
- Downloads
- 590
- Categories
- Other, AI
Jump to
- Decoupled architecture for modular tool and agent scaling
- Asynchronous I/O enabling concurrent tool execution
- Seamless integration of MCP with LangGraph and LangChain
- Multi-server connectivity via SSE and STDIO transports
- Dynamic tool discovery and MCP-spec handshake protocol
- Foundation for future Agent2Agent interoperability
Deploy MCP tool servers (e.g., math_server.py, weather_server.py) using FastMCP, then connect a LangGraph ReAct agent via the MultiServerMCPClient or a raw ClientSession with STDIO transport. Configure servers with commands or URLs, and invoke the agent with user queries to orchestrate tool execution asynchronously.
Part 1. Real-Time LangGraph Agent with MCP Tool Execution
This project demonstrates a decoupled real-time agent architecture that connects LangGraph agents to remote tools served by custom MCP (Modular Command Protocol) servers. The architecture enables a flexible and scalable multi-agent system where each tool can be hosted independently (via SSE or STDIO), offering modularity and cloud-deployable execution.
- Decoupled Architecture: Engineered a modular system where LangGraph-based agents orchestrate LLM workflows while delegating tool execution to remote MCP servers via both SSE and STDIO transports.
- Advanced Asynchronous Programming: Utilized Python’s async/await for non-blocking I/O, ensuring concurrent execution of multiple tools and scalable real-time communication.
- MCP & LangGraph Integration: Demonstrated deep expertise in integrating Modular Command Protocol (MCP) with LangGraph and LangChain, enabling seamless transformation and invocation of distributed tools.
- Flexible Multi-Server Connectivity: Designed a MultiServerMCPClient that supports 1:1 bindings to various tool servers, highlighting the system’s ability to integrate diverse environments (local, cloud, containerized).
- Robust Agent-to-Tool Communication: Implemented detailed client sessions, handshake protocols, and dynamic tool discovery, ensuring reliable execution and interaction between agents and MCP servers.
- Forward-Looking Interoperability: Laid the groundwork for an Agent2Agent protocol, aiming for an ecosystem where AI agents can share capabilities, coordinate actions, and securely exchange context and data.
---
🚀 Project Purpose
This project aims to:
- Decouple LLM-based agent orchestration (LangGraph) from tool execution (via MCP servers).
- Enable real-time, multi-server, and language-agnostic tool integration using the MCP protocol.
- Showcase how to:
- Spin up LangChain-compatible MCP tool servers (e.g., math_server.py, weather_server.py)
- Integrate them with LangGraph ReAct agents
- Use async/await programming for non-blocking I/O across agents and tool servers
---
📦 Project Structure
.
├── servers/
│ ├── math_server.py # STDIO-based MCP tool server
│ └── weather_server.py # SSE-based MCP tool server
├── client/
│ ├── multiserver_client.py # LangGraph agent using MultiServer MCP client
│ └── stdio_client.py # LangGraph agent using STDIO transport
---
🔧 Technology Stack
- 🧠 LangGraph: ReAct agent orchestration
- 🔗 LangChain: LLM pipeline & tools abstraction
- 🧰 MCP (Modular Command Protocol):
- FastMCP – FastAPI-based server abstraction
- ClientSession, StdioServerParameters, MultiServerMCPClient
- 🌐 SSE & STDIO: Transport protocols
- 🔁 AsyncIO: Asynchronous concurrency
- ☁️ OpenAI: Backend LLM (via langchain_openai)
- 🧪 dotenv: API key management
---
📜 Source Code Breakdown
1. math_server.py and weather_server.py
Tool servers using FastMCP:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("Weather")
@mcp.tool()
async def get_weather(location: str) -> str:
return "Cold in Chicago"
if __name__ == "__main__":
mcp.run(transport="sse")
✅ Highlights:
- Each server defines an async tool via @mcp.tool()
- Server is transport-agnostic: can run over SSE or STDIO
- Designed for modular deployment: local, cloud, containerized
2. multiserver_client.py
Agent that talks to multiple MCP servers concurrently:
async with MultiServerMCPClient({
"math": {"command": "python", "args": ["math_server.py"]},
"weather": {"url": "http://localhost:8000/sse", "transport": "sse"}
}) as client:
agent = create_react_agent(llm, client.get_tools())
result = await agent.ainvoke({"message":"what's 1+1?"})
✅ Highlights:
- MultiServerMCPClient supports 1:1 bindings to multiple servers
- All tool invocations are async + streamed via appropriate transport
- Tools auto-transformed to LangChain-compatible format
3. stdio_client.py
Agent connects to one STDIO MCP server using raw ClientSession:
async with stdio_client(StdioServerParameters(...)) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
tools = await load_mcp_tools(session)
agent = create_react_agent(llm, tools)
result = await agent.invoke({"messages": [HumanMessage(content="What's 1+1?")]})
✅ Highlights:
- ClientSession.initialize() sets up MCP spec-compliant handshake
- Tools dynamically discovered via session.list_tools() and load_mcp_tools()
- Shows low-level control over I/O streams
🔄 How LangGraph & MCP Interact
Agent Flow:
1. Agent creation with tools from MCP client 2. Agent receives user query 3. Agent identifies required tool 4. MCP client sends tool invocation request to remote server 5. Remote server executes tool, sends result back 6. Agent consumes result and returns final responseArchitecture Diagram:
+----------------+ +----------------------+ +------------------------+
| LangGraph App |<------->| MultiServerMCPClient |<-----> | Remote MCP Servers |
| (ReAct Agent) | | (Tool Wrapper Layer)| | (math_server, weather) |
+----------------+ +----------------------+ +------------------------+
| ^
| |
+--------------------------------+
Async I/O (SSE or STDIO)
---
🧠 Async Usage & Benefits
The entire system uses async/await to:
- Avoid blocking while waiting on tool responses
- Allow concurrent execution of multiple tools
- Enable scalable, real-time tool orchestration
All I/O – whether network-based (SSE) or pipe-based (STDIO) – is handled asynchronously, maximizing responsiveness and throughput.
---
✅ Pros
- Modular & Scalable: Tool servers can scale independently
- Language-Agnostic: MCP spec supports Python, Node.js, Dockerized services
- Real-Time Execution: SSE & STDIO transports enable live interaction
- LangChain Compatible: Full support for LangChain & LangGraph workflows
---
⚠️ Challenges
- Tool Discovery Latency: Initial handshake adds slight overhead
- Transport Complexity: Managing multiple transport types (SSE vs STDIO) can be non-trivial
- Error Handling: Tool server failures or timeouts must be gracefully handled
- Deployment Strategy: Needs orchestration layer (e.g. Docker Compose, K8s) in production
---
🔮 Future Directions
- Dockerized Tool Servers with StdioServerParameters + container support
- Auth Layer for secure server access
- Observability: Logs, tracing, and real-time dashboard for tool usage
- LangGraph Parallel Nodes: Run multiple MCP calls in parallel subgraphs
- Multi-modal Tooling: Extend MCP tools to support image/audio inputs
---
🧩 Related Concepts
- MCP: Protocol for defining and invoking modular tools
- LangGraph: State-machine inspired framework for agentic reasoning
- ReAct: Reasoning and Acting paradigm for structured decision-making
- LangChain Tooling: Converts external functions/APIs to LLM-callable tools
---
📌 Conclusion
This project showcases a clean separation of concerns in LLM application development:
- LangGraph focuses on agent logic and orchestration
- MCP servers handle actual task execution
- Async clients bridge the two, providing real-time communication
> It's a future-proof architecture for building enterprise-grade LLM applications with modular, observable, and maintainable components.
✨ Agent2Agent Protocol
AI agents often isolated within specific applications or platforms, they lack common way to communicate, share info, or coordinate actions with other agents built by different vendors or using different frameworks. A2A defines standard way for agents to discover capabilities, agents can advertise their functions, so other agents know what they can do. Agents can assign and track tasks, including complex long running ones, exchanging status updates and results. Agents can securely exchange messages containing context instructions or data. Agents can agree on best format for presenting info (text, image) based on user interface capabilities. We aim to create interoperable ecosystem where AI agents can seamlessly work together across different enterprise applications.
---
Sign in to leave a review
Use Google, GitHub, or an email account so ratings stay tied to real people.
No reviews posted yet.




