How pietrozullo/mcp-use works
Project type and purpose
• This is an open‑source Python library (“mcp_use”) that lets developers connect any LangChain‑compatible LLM to one or more MCP (Model‑Control‑Program) servers.
• It provides core classes for configuring servers, managing sessions, sending LLM tool calls, and processing server responses.
File structure
• Top‑level:
• mcp_use/ (core package)
– adapters/ (LangChain adapter)
– agents/ (MCPAgent logic, prompts, server manager)
– connectors/ (HTTP, stdio, WebSocket abstractions)
– task_managers/ (SSE, stdio, WS streaming handlers)
– client.py, config.py, session.py, logging.py
• examples/ (usage scripts & JSON configs)
• docs/ (MDX docs & snippets)
• tests/ (unit tests for each module)
• Key indicators: abstraction layers (connectors & task managers), the agent vs client separation, config via dict or file.
README for additional insights
• Describes core concepts: MCPClient, MCPAgent, multi‑server support, dynamic server selection, tool restrictions, debug/logging.
• Shows use cases: web browsing, Airbnb, Blender, HTTP connector, LangChain adapter.
a. Main components
1. External LLMs (e.g., OpenAI, Anthropic via LangChain)
2. MCPAgent (or custom agent via LangChainAdapter)
3. MCPClient
4. MCPSession instances (one per configured server)
5. Connectors (stdio, HTTP, WebSocket)
6. TaskManagers (SSE, stdio, WebSocket)
7. External MCP servers (Playwright, Airbnb, Blender, etc.)
8. Configuration source (dict, JSON file, env)
9. Logging/Debug component
b. Relationships & interactions
• Developer code → loads config → MCPClient.from_dict()/from_config_file
• MCPClient → spawns MCPSession objects per server config
• Each MCPSession → uses Connector to launch or connect to an MCP server
• TaskManager handles streaming events from the Connector
• MCPAgent receives user prompt → calls LLM → receives function/tool call → invokes MCPClient.send_request(server_name, tool_call)
• MCPClient routes to the appropriate MCPSession → Connector → MCP server → TaskManager → returns tool output
• MCPAgent aggregates outputs, invokes LLM for next step until completion
c. Architectural patterns & principles
• Layered modularity (separate adapters, connectors, task managers)
• Dependency inversion (abstract base classes for connectors & task managers)
• Single responsibility (each module handles one concern)
• Extensibility points (new adapters, connectors, servers)
d. Key technologies & libraries
• Python 3.11+, asyncio
• subprocess (stdio connector), aiohttp or httpx (HTTP connector), websockets
• SSE streaming
• LangChain for LLM integration
• Dotenv for env config
• JSON config files
Detailed component breakdown (recommend including these
• Configuration Loader (from dict or JSON)
• MCPClient
• manages MCPSession pool
• exposes send_request(), close_all_sessions()
• MCPSession
• Connector (stdio / HTTP / WebSocket)
• TaskManager (SSE / stdio / WebSocket)
• MCPAgent
• llm: external LangChain model
• tool loop: get next action, call client, parse result
• optional ServerManager for dynamic selection
• Adapters (e.g., LangChainAdapter.create_tools())
• Logging & Debug (global flags, agent.verbose)
• Example scripts (browser_use.py, airbnb_use.py, blender_use.py)