How paul60209/langchain_with_mcp works
Project Type and Purpose
• This is a microservices-style, full-stack AI application with a Chainlit web UI (“Client”) that embeds a Langchain Agent. The Agent communicates with three independent “tool servers” over the Model Context Protocol (MCP) via Server-Sent Events (SSE). Each tool server provides a specialized function (weather lookup, SQL queries, PPT translation), and they in turn call external services (OpenWeatherMap, MySQL, OpenAI, python-pptx).
High-Level Architectural Layers
a. Launch & Management Layer
• run.py, run_server.py, run_client.py – scripts that start/stop all subprocesses (client + servers).
• server_config.txt – persists dynamically chosen ports for each tool server.
b. Application Layer (Client)
• app.py – Chainlit UI + Langchain Agent + MCP Client Adapter.
• chainlit.config.json / chainlit.md – UI configuration.
c. Tool Server Layer
• MCP_Servers/weather_server.py – FastAPI/Starlette/Uvicorn serving the “query_weather” tool.
• MCP_Servers/sql_query_server.py – “query_database” tool backed by MySQL.
• MCP_Servers/ppt_translator_server.py – “translate_ppt” tool using OpenAI and python-pptx.
d. External Services
• OpenWeatherMap API, MySQL Database, OpenAI API
Main Components & Interactions
• Chainlit Web UI (browser) ←→ Chainlit Server (app.py)
• Renders chat interface; forwards user messages to the Langchain Agent.
• Langchain Agent (in app.py)
• Discovers and invokes “tools” via the MCP Client Adapter.
• MCP Client Adapter (in app.py)
• Maintains SSE connections to each tool server.
• Sends MCP-formatted JSON requests; listens to event stream responses.
• MCP Tool Servers (on ports 8001–8003)
• Each runs as an independent Python process under Uvicorn.
• Exposes an SSE endpoint (e.g., /sse) that accepts MCP requests and returns streamed events.
• External Dependencies
• Weather Server calls OpenWeatherMap via HTTP.
• SQL Server uses CLEARDB_DATABASE_URL to run SELECT queries against MySQL.
• PPT Server calls OpenAI API to translate and python-pptx to manipulate files.
Architectural Patterns & Design Principles
• Microservices / Client-Server: Each tool server is decoupled and independently deployable.
• Adapter Pattern: The MCP Client Adapter bridges Langchain’s tool interface to the standardized MCP protocol.
• Event-Driven Communication: SSE used for real-time, streaming responses.
• Configuration-as-Code: .env for secrets; server_config.txt for dynamic ports.
• Launch Orchestration: run.py offers a unified CLI menu to start/stop sub-processes.
Diagramming Conventions
• Related elements within bounded “layers” (e.g., Client Layer, Server Layer, External Services).