Skip to main content
Glama
MCP Gateway

MCP Gateway

by NightR71

MCP Gateway – MCP-Agent-Gateway

Einheitliches Gateway für den Zugriff und die Verwaltung mehrerer MCP-Server auf Unternehmensebene: Der übergeordnete LLM-Agent verbindet sich nur mit dem Gateway als einem einzigen Einstiegspunkt und kann dann die Tools aller dahinter liegenden MCP-Server aufrufen, wobei Authentifizierung, Ratenbegrenzung, Protokollierung und Metriken abgedeckt sind.

Architektur

                        ┌─────────────────────────────┐
                        │        LLM Agent / 应用      │
                        │  (LangChain / OpenAI 等)     │
                        └──────────────┬──────────────┘
                                       │ 统一 REST API
                        ┌──────────────▼──────────────┐
                        │       MCP Gateway (FastAPI)  │
                        │  ┌────────────────────────┐  │
                        │  │  鉴权 (API Key)         │  │
                        │  │  限流 (令牌桶)          │  │
                        │  │  日志 / 指标 (横切层)   │  │
                        │  └───────────┬────────────┘  │
                        │  ┌───────────▼────────────┐  │
                        │  │  工具注册中心 registry  │  │
                        │  │  (聚合所有 server 工具) │  │
                        │  └───────────┬────────────┘  │
                        │  ┌───────────▼────────────┐  │
                        │  │  MCP 客户端 (多传输)    │  │
                        │  └────────────────────────┘  │
                        └───────┬───────────┬───────────┘
                        stdio ──┤           ├── Streamable HTTP / SSE
                 ┌──────────────▼──┐   ┌────▼───────────────┐
                 │ MCP Server #1   │   │ MCP Server #2 ...   │
                 │ (demo_sql_server)│   │  (数据库/内部API等) │
                 └─────────────────┘   └─────────────────────┘

Related MCP server: Peta Core

Technologie-Stack

Python 3.12 · FastAPI · MCP Official SDK (stdio / SSE / Streamable HTTP) · pydantic-settings + YAML · structlog · Prometheus · SQLite (Schnittstellenabstraktion, austauschbar mit PostgreSQL) · pytest · Docker · GitHub Actions · uv

Schnellstart

uv sync                                   # 安装依赖(自动准备 Python 3.12)
uv run uvicorn app.main:app --reload      # 启动开发服务器

uv run pytest                             # 运行测试
uv run ruff check .                       # lint

docker compose up --build                 # 一键启动

Nach dem Start aufrufen:

  • GET /health — Gesundheitscheck

  • GET /metrics — Prometheus-Metriken (Tool-Aufrufanzahl/-dauer: mcp_gateway_tool_calls_total, mcp_gateway_tool_call_duration_seconds)

  • GET /docs — OpenAPI-Interaktionsdokumentation

Aufrufbeispiel (Demo-Key siehe Abschnitt auth in config/gateway.yaml):

curl -H "X-API-Key: dev-key-please-change" http://localhost:8000/tools

curl -X POST http://localhost:8000/tools/demo_sql__ask/call \
     -H "X-API-Key: dev-key-please-change" -H "Content-Type: application/json" \
     -d '{"arguments": {"question": "有多少客户?"}}'

demo_sql_server enthält eine Mini-E-Commerce-Bibliothek (customers / products / orders) und stellt 4 Tools bereit: ask (chinesische Frage → automatisch generiertes und ausgeführtes schreibgeschütztes SQL), run_sql (direktes Ausführen von schreibgeschütztem SQL), list_tables (Tabellenstruktur), echo (Link-Debugging). NL2SQL ist eine Regel-Template-Engine, offline ohne Abhängigkeiten, die Schnittstelle ist von der LLM-Implementierung entkoppelt und kann nahtlos ersetzt werden.

Konfiguration

config/gateway.yaml (Priorität: Standardwerte im Code < YAML < Umgebungsvariablen GATEWAY_*):

gateway:
  port: 8000
  log_level: INFO
auth:                 # API Key 鉴权(SQLite 存储,启动种子写入)
  db_path: data/gateway.db
  api_keys:
    - { key: dev-key-please-change, name: demo, rate_limit_per_minute: 60 }
servers:              # MCP Server 声明式接入,无需改代码
  - name: demo_sql
    transport: stdio  # stdio / sse / http
    command: python
    args: ["servers/demo_sql_server/server.py"]

Docker Compose verwendet config/gateway.docker.yaml: demo_sql läuft als separater Container mit Streamable HTTP, das Gateway verbindet sich über http://demo_sql:9001/mcp.

Projektstruktur

app/
├── main.py          # FastAPI 入口
├── config.py        # 配置中心(pydantic-settings + YAML)
├── core/            # 横切层:security / rate_limit / logging / metrics
├── mcp/             # 协议层:registry / client / transports / schemas
├── api/             # 接口层:deps.py + routes/
└── schemas/         # Pydantic 模型
servers/demo_sql_server/  # 示例 MCP Server(自然语言→SQL,阶段 2/4)
examples/                 # LLM Agent 调用示例(阶段 5)
tests/                    # 单元测试

Entwicklungs-Roadmap

  • Phase 1: Framework + CI (/health, /metrics, Konfigurationszentrum, strukturiertes Logging)

  • Phase 2: Protokollebene (stdio/SSE/HTTP-Client für drei Transporte + Tool-Registrierungszentrum + Demo-Server)

  • Phase 3: Einheitliche API (GET /tools, POST /tools/{name}/call) + API-Key-Authentifizierung + Token-Bucket-Ratenbegrenzung

  • Phase 4: Tool-Aufrufmetriken + Upgrade von demo_sql_server auf NL2SQL + docker-compose mit zwei Containern (öffentliche Bereitstellung und Demo-Bildschirmaufzeichnung noch ausstehend)

  • Phase 5: Agent-Aufrufbeispiele + Open-Source-Promotion

Erweiterungspfade für Unternehmen

Multi-Tenancy + RBAC · Modell-Routing (analog zu One-API) · Audit-Compliance · Automatische Skalierung in K8s · OpenTelemetry-Tracing · Circuit Breaker / Caching

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      D
      maintenance
      A production-ready unified entry point for AI agents that implements the Model Context Protocol (MCP). It provides a secure gateway with rate limiting, authentication, and observability for managing and proxying requests to multiple downstream APIs.
      MIT
    • F
      license
      Not graded
      quality
      C
      maintenance
      A production-ready MCP gateway and control plane that provides credential vault, policy engine, audit logging, and managed runtime for routing tool calls between AI agents and downstream MCP servers.
      60
      -