mcp-production-mastery
by MnMansour
README.md
# Production MCP Agent Architecture
A production-grade, end-to-end implementation of an AI Agent connecting to external tools using the **Model Context Protocol (MCP)**, enforcing **Deterministic Structured Outputs with Zod**, and instrumented with **Langfuse Observability**.
---
## ποΈ Architecture Overview
```text
ββββββββββββββββββ Stdio Transport βββββββββββββββββββββ
β β βββ 1. tools/list Discovery ββββΊ β β
β MCP Client β β MCP Server β
β (Agent Loop) β βββ 2. tools/call Execution ββββΊ β (FastMCP Engine) β
βββββββββ¬βββββββββ βββββββββββ¬ββββββββββ
β β
β 3. LLM Invocations & Structured Outputs β Audit Logs
βΌ βΌ
ββββββββββββββββββ βββββββββββββββββββββ
β OpenAI API β β Target Storage β
β (gpt-4o) β β / Infrastructureβ
βββββββββ¬βββββββββ βββββββββββ¬ββββββββββ
β
β 4. Telemetry Spans, Latency & Cost Tracing
βΌ
ββββββββββββββββββ
β Langfuse β
β Observability β
ββββββββββββββββββ
```
---
## β‘ Key Features
* **Model Context Protocol (MCP)**: Implements standard Client-Server decoupled tool discovery and execution over `stdio` transport using standard JSON-RPC 2.0.
* **100% Deterministic JSON Schemas**: Uses OpenAI's `zodResponseFormat` and `zodFunction` helpers to eliminate non-deterministic markdown or missing fields at runtime.
* **Full Observability & Tracing**: Instrument traces, model generations, and tool execution spans via **Langfuse** for latency monitoring and token cost tracking.
* **Modular TypeScript Design**: Built with Modern ES Modules, strict typing, clean separation of concerns, and native execution via `tsx`.
* **Container Ready**: Includes Docker & Docker Compose setup for production deployment.
---
## π Project Structure
```text
mcp-production-mastery/
βββ .env.example
βββ Dockerfile
βββ docker-compose.yml
βββ package.json
βββ tsconfig.json
βββ src/
βββ client.ts # Main MCP Client agent loop & execution engine
βββ server.ts # MCP Server exposing standard operational tools
βββ config/
β βββ telemetry.ts # Centralized Langfuse SDK initialization
βββ schemas/
β βββ output.schemas.ts # Zod contracts for agent responses
βββ tools/
βββ database.tool.ts # Database mutation tool implementation
βββ ping.tool.ts # System infrastructure diagnostic tool
```
---
## π Getting Started
### Prerequisites
* Node.js v18+
* npm v9+
* OpenAI API Key
* Langfuse Account Key Pair (Cloud or Self-Hosted)
### Step 1: Clone & Install Dependencies
```bash
git clone [https://github.com/YOUR_USERNAME/mcp-production-mastery.git](https://github.com/YOUR_USERNAME/mcp-production-mastery.git)
cd mcp-production-mastery
npm install
```
### Step 2: Configure Environment Variables
Copy `.env.example` to `.env` and fill in your API credentials:
```bash
cp .env.example .env
```
### Step 3: Run the Agent
Execute the full client-server loop in development mode:
```bash
# Run the complete agent orchestration loop
npm run start:client
```
To run the standalone MCP server process listening on Stdio:
```bash
npm run start:server
```
---
## π³ Docker Deployment
To build and run the MCP Server container using Docker Compose:
```bash
# Build and run server image
docker-compose up --build
```
---
## π οΈ Modifying & Extending Tools
To add a new operational tool to the MCP Server:
1. Create a new Zod input schema and execution function under `src/tools/`.
2. Register the tool inside `src/server.ts` using `server.tool(...)`.
3. The MCP Client will automatically discover the new tool on launch via `tools/list` without requiring client modifications.
---
## π License
Distributed under the MIT License. See `LICENSE` for details.This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo issues