MCP代理服务

MCP Proxy是一个多服务MCP聚合代理,提供OAuth认证、令牌管理和高级路由功能,适用于需要集中管理多个MCP后端服务的场景。

  • by notfolder
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 notfolder/mcp_proxy

使用说明

MCP Proxy

MCP Proxy is a multi-service MCP (Model Context Protocol) aggregation proxy with OAuth authentication, token management, and advanced routing capabilities.

Features

  • Multi-service aggregation: Route requests to multiple MCP backends through a single proxy
  • OAuth integration: Secure OAuth flows for services like GitLab, Jira, etc.
  • Token management: Server-side token storage and injection
  • SSE and HTTP support: Handle both Server-Sent Events and HTTP-based MCP backends
  • User dashboard: Web interface for managing services and viewing audit logs
  • Admin dashboard: Administrative interface for service configuration
  • Audit logging: Comprehensive logging of all API calls
  • Monitoring: Prometheus metrics for observability

Quick Start with Docker Compose

The easiest way to get started is using Docker Compose, which will set up OpenWebUI, GitLab, GitLab MCP Server, and MCP Proxy together:

# Copy the example environment file
cp .env.example .env

# Start all services
docker compose up -d

# Wait for services to be ready (especially GitLab takes 5-10 minutes)
docker compose ps

See setup-guide-ja.md for detailed setup instructions in Japanese.

Installation

pip install -r requirements.txt

Development

pip install -r requirements-dev.txt

Running the Server

uvicorn mcp_proxy.main:app --host 0.0.0.0 --port 8000

Or with hot reload for development:

uvicorn mcp_proxy.main:app --reload

Configuration

Create a .env file in the project root with the following settings:

# Application
APP_NAME=MCP Proxy
DEBUG=false

# Database
DATABASE_URL=postgresql+asyncpg://user:password@localhost:5432/mcp_proxy

# Redis
REDIS_URL=redis://localhost:6379/0

# Security
SECRET_KEY=your-secret-key-here
ENCRYPTION_KEY=your-encryption-key-here

# OAuth
OAUTH_STATE_EXPIRE_SECONDS=600
OAUTH_CALLBACK_BASE_URL=https://proxy.example.com  # Base URL for OAuth callbacks (required for production)

# Server
HOST=0.0.0.0
PORT=8000

API Documentation

Once the server is running, visit:

  • Swagger UI: http://localhost:8000/docs
  • ReDoc: http://localhost:8000/redoc

Main Endpoints

Public Endpoints

  • GET /health - Health check
  • GET /metrics - Prometheus metrics

Admin Endpoints (requires admin role)

  • GET /admin - Admin dashboard
  • GET /admin/services - List all services
  • POST /admin/services - Create a new service
  • PUT /admin/services/{id} - Update a service
  • DELETE /admin/services/{id} - Delete a service
  • GET /admin/users - List all users
  • GET /admin/audit - View audit logs

User Endpoints (requires authentication)

  • GET /user/dashboard - User dashboard
  • GET /user/services - List available services
  • PUT /user/services/{service}/toggle - Enable/disable a service
  • PUT /user/services/{service}/config - Configure service API keys
  • GET /user/tokens - List authentication tokens
  • GET /user/audit - View personal audit logs

OAuth Endpoints

  • GET /oauth/{service}/login - Initiate OAuth flow
  • GET /oauth/{service}/callback - OAuth callback handler

MCP Proxy Endpoints (requires authentication)

  • GET /mcp/{service} - SSE endpoint for MCP connections
  • POST /mcp/{service} - HTTP endpoint for MCP requests
  • GET /mcp/{service}/openapi - Get OpenAPI spec from backend

Authentication Flow

  1. User initiates OAuth flow via /oauth/{service}/login
  2. User is redirected to OAuth provider (e.g., GitLab)
  3. After authentication, provider redirects to /oauth/{service}/callback
  4. Proxy exchanges authorization code for access token
  5. Proxy issues a server token (JWT) to the user
  6. User uses server token for all subsequent API calls

Service Configuration

Services can be configured via the Admin API:

{
  "name": "gitlab",
  "display_name": "GitLab",
  "backend_url": "http://gitlab-mcp:8001",
  "backend_type": "sse",
  "oauth_enabled": true,
  "oauth_provider_url": "https://gitlab.com",
  "oauth_client_id": "your-client-id",
  "oauth_client_secret": "your-client-secret",
  "oauth_scopes": ["api", "read_user"],
  "allowed_users": ["user@example.com"],
  "rate_limit_per_min": 60
}

Running Tests

pytest

For coverage report:

pytest --cov=mcp_proxy --cov-report=html

Documentation

See the following specification documents:

  • mcp_proxy_full_spec.md - Complete feature specification
  • mcp_proxy_test_spec.md - Comprehensive test specification

Architecture

[User Browser] 
   └→ OpenWebUI (MCP Client)
         ├ register MCP: https://proxy.example.com/mcp/gitlab
         ├ register MCP: https://proxy.example.com/mcp/jira
         └ uses tools...
               ↕ (SSE or HTTP)

[ M C P  P R O X Y ]  (single ingress)
  - /mcp/{service}  (SSE/HTTP)
  - /oauth/{service}/login
  - /oauth/{service}/callback
  - /admin/*
  - /user/*
  - /health, /metrics
    ├ route → internal GitLab-MCP (sse/http)
    └ route → external 3rd-party MCP

Security Features

  • JWT-based authentication with database verification
  • Encrypted storage of OAuth tokens and API keys
  • Service-level ACL (allowed users/groups)
  • Comprehensive audit logging
  • Rate limiting per service
  • Token expiration and refresh

License

TBD