Skip to content

Latest commit

Β 

History

History
373 lines (300 loc) Β· 8.42 KB

File metadata and controls

373 lines (300 loc) Β· 8.42 KB

MCP Client - Production-Grade Schema-Driven Execution

A deterministic, schema-driven execution engine for Model Context Protocol (MCP) servers. This system requires zero code changes to add new tools and uses no LLMs in the execution path.

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   User Input    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚  1. NLP Module   β”‚  Extract entities (spaCy)
    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ 2. Intent Engine β”‚  Classify intent (ML)
    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ 3. Rule Engine   β”‚  Apply business logic
    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ 4. Tool Registry β”‚  Match tool from DB
    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ 5. Schema Exec   β”‚  Build params from schema
    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ 6. MCP Client    β”‚  Execute on MCP server
    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ 7. Audit Logger  β”‚  Store full trace
    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚     Result       β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Features

  • Zero Code Changes: Add new tools by just registering MCP servers
  • No LLMs: Fully deterministic execution using schemas only
  • Schema-Driven: Uses JSON Schema for parameter extraction
  • Full Audit: Complete execution trace for every request
  • Production-Ready: PostgreSQL, async, error handling, tests

Tech Stack

Backend

  • FastAPI - High-performance async API
  • PostgreSQL - Production database with SQLAlchemy
  • spaCy - NLP for entity extraction
  • scikit-learn - Intent classification
  • json-logic-py - Rule engine
  • JSON Schema - Parameter validation

Frontend

  • Vanilla HTML/CSS/JavaScript - No build tools required
  • Modern CSS - Responsive design with flexbox/grid
  • Fetch API - REST client
  • JWT Auth - Token-based authentication

Quick Start

1. Install Dependencies

cd client_mcp_hybrid
python -m pip install -r requirements.txt
python -m spacy download en_core_web_sm

2. Configure Database

Create PostgreSQL database and .env file:

DATABASE_URL=postgresql+asyncpg://user:password@localhost/mcp_client
SECRET_KEY=your-secret-key-here
DEBUG=true

3. Configure MCP Servers

Create mcp_servers.json:

{
  "servers": [
    {
      "name": "filesystem",
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/files"]
    }
  ]
}

4. Run Application

python main.py

Access at: http://localhost:8000

Demo Credentials:

  • Username: admin / Password: admin
  • Username: user / Password: user

Frontend Pages

Dashboard (/)

  • System statistics
  • Active servers
  • Recent executions
  • Auto-refresh

Execute (/execute.html)

  • Natural language input
  • Example commands
  • Real-time results
  • Parameter display

Tools (/tools.html)

  • Browse all tools
  • Search and filter
  • View JSON schemas
  • Server grouping

Servers (/servers.html)

  • MCP server list
  • Status monitoring
  • Trigger discovery
  • Statistics

Audit (/audit.html)

  • Execution history
  • Filter by status/date
  • Detailed traces
  • Pipeline information

API Endpoints

Authentication

POST /api/v1/auth/login
POST /api/v1/auth/refresh

Execution

POST /api/v1/execute
# Body: {"input": "list files in /tmp"}

Tools

GET /api/v1/tools
GET /api/v1/tools/{tool_id}
GET /api/v1/tools/{tool_id}/schema

Servers

GET /api/v1/servers
POST /api/v1/servers/discover
GET /api/v1/servers/{server_id}/stats

Audit

GET /api/v1/audit
GET /api/v1/audit/{execution_id}

Project Structure

client_mcp_hybrid/
β”œβ”€β”€ main.py                 # FastAPI application
β”œβ”€β”€ requirements.txt        # Dependencies
β”‚
β”œβ”€β”€ config/                 # Configuration
β”œβ”€β”€ database/               # Database models
β”œβ”€β”€ nlp/                    # Entity extraction
β”œβ”€β”€ intent/                 # Intent classification
β”œβ”€β”€ rules/                  # Rule engine
β”œβ”€β”€ registry/               # Tool registry
β”œβ”€β”€ executor/               # Schema executor
β”œβ”€β”€ mcp/                    # MCP client
β”œβ”€β”€ discovery/              # Server discovery
β”œβ”€β”€ audit/                  # Audit logging
β”œβ”€β”€ api/                    # REST API
β”œβ”€β”€ pipeline/               # Pipeline orchestrator
β”‚
β”œβ”€β”€ frontend/               # Web UI (vanilla JS)
β”‚   β”œβ”€β”€ login.html
β”‚   β”œβ”€β”€ dashboard.html
β”‚   β”œβ”€β”€ execute.html
β”‚   β”œβ”€β”€ tools.html
β”‚   β”œβ”€β”€ servers.html
β”‚   β”œβ”€β”€ audit.html
β”‚   β”œβ”€β”€ css/
β”‚   β”‚   └── style.css
β”‚   └── js/
β”‚       β”œβ”€β”€ auth.js
β”‚       β”œβ”€β”€ api.js
β”‚       β”œβ”€β”€ utils.js
β”‚       β”œβ”€β”€ dashboard.js
β”‚       β”œβ”€β”€ execute.js
β”‚       β”œβ”€β”€ tools.js
β”‚       β”œβ”€β”€ servers.js
β”‚       └── audit.js
β”‚
└── tests/                  # Test suite

Usage Examples

Execute Command via API

curl -X POST http://localhost:8000/api/v1/execute \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"input": "list all files in /tmp"}'

Response

{
  "status": "success",
  "tool_name": "list_directory",
  "server_name": "filesystem",
  "result": {
    "files": ["file1.txt", "file2.txt"]
  },
  "parameters": {
    "path": "/tmp"
  },
  "duration_ms": 156,
  "execution_id": "exec_123"
}

Zero Code Changes Demo

  1. Add new MCP server to mcp_servers.json
  2. Restart application (or POST to /api/v1/servers/discover)
  3. Tools are automatically available - no code changes!
  4. Execute commands using new tools immediately

Testing

# Run all tests
pytest

# Specific test
pytest tests/test_pipeline.py

# With coverage
pytest --cov=. --cov-report=html

Production Deployment

Docker

docker build -t mcp-client .
docker run -p 8000:8000 \
  -e DATABASE_URL=postgresql://... \
  -e SECRET_KEY=... \
  mcp-client

Systemd Service

[Unit]
Description=MCP Client
After=network.target postgresql.service

[Service]
Type=simple
User=mcp
WorkingDirectory=/opt/mcp-client
Environment="DATABASE_URL=postgresql://..."
ExecStart=/opt/mcp-client/venv/bin/python main.py
Restart=always

[Install]
WantedBy=multi-user.target

Nginx Reverse Proxy

server {
    listen 80;
    server_name mcp.example.com;

    location / {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Development

Add Custom Rules

INSERT INTO business_rules (intent, conditions, actions, priority)
VALUES (
    'file_operation',
    '{"and": [{"var": "action"}, {"==": [{"var": "action"}, "read"]}]}',
    '{"require_permission": "read"}',
    100
);

Train Intent Classifier

from intent.classifier import IntentClassifier

classifier = IntentClassifier()
classifier.train(training_data)
classifier.save_model("intent_model.pkl")

Troubleshooting

Database Connection

# Check PostgreSQL
sudo systemctl status postgresql

# Test connection
psql -h localhost -U user -d mcp_client

MCP Server Discovery

# Test server manually
npx -y @modelcontextprotocol/server-filesystem /tmp

# Validate config
python -m json.tool mcp_servers.json

Frontend Issues

  • Check browser console for errors
  • Verify FastAPI is serving static files
  • Ensure all JS files are loaded (auth.js, api.js, utils.js)

Zero code changes β€’ No LLMs β€’ Fully deterministic