A lightweight, security-focused FastAPI gateway for Model Context Protocol (MCP) servers
Project description
MCP Gateway
A lightweight, security-focused FastAPI gateway for Model Context Protocol (MCP) servers. This gateway acts as an intermediary between MCP clients and servers, providing authentication, authorization, rate limiting, request validation, and audit logging.
Features
Security
- Authentication & Authorization: Token-based authentication with configurable client policies
- Request Validation: Validates requests against allowed tools and resources
- Response Sanitization: Removes sensitive information from responses
- Rate Limiting: Token bucket algorithm for request rate limiting
- Audit Logging: Comprehensive logging of all requests and responses
- Sandbox Mode: Isolates MCP server execution (planned enhancement)
Gateway Functionality
- Request Proxying: Forwards validated requests to appropriate MCP servers
- Policy Enforcement: Enforces client-specific security policies
- Error Handling: Graceful error handling with proper HTTP status codes
- CORS Support: Configurable CORS policies for web clients
Monitoring & Management
- Health Checks: Built-in health check endpoint
- Metrics: Request metrics and performance monitoring (planned)
- Admin API: Client registration and management endpoints
Quick Start
Installation
- Clone or create the project directory:
mkdir mcpgw && cd mcpgw
- Install dependencies:
pip install -r requirements.txt
- Run the gateway:
python main.py
The gateway will start on http://localhost:8000
Basic Usage
- Health Check:
curl http://localhost:8000/health
- Register a Client (for testing):
curl -X POST http://localhost:8000/admin/register-client \
-H "Content-Type: application/json" \
-d '{
"client_id": "my-client",
"client_secret": "my-secret",
"policy": {
"allowed_tools": ["get_weather"],
"allowed_resources": ["weather://*"],
"max_requests_per_minute": 100,
"require_auth": true,
"sandbox_mode": true
}
}'
- Make MCP Request:
curl -X POST http://localhost:8000/mcp/request \
-H "Authorization: Bearer YOUR_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{
"server_name": "example-weather",
"request": {
"jsonrpc": "2.0",
"method": "tools/list",
"id": "1"
}
}'
Configuration
Security Policies
Each client can have a custom security policy:
{
"allowed_tools": ["tool1", "tool2"], # Allowed tool names
"allowed_resources": ["resource://*"], # Resource patterns (regex)
"max_requests_per_minute": 60, # Rate limit
"max_request_size": 1048576, # Max request size (bytes)
"require_auth": true, # Require authentication
"allowed_origins": ["https://example.com"], # CORS origins
"sandbox_mode": true # Enable sandboxing
}
MCP Server Configuration
Configure MCP servers in the MCPServerManager.load_server_config() method or via configuration files:
{
"server-name": {
"command": "node",
"args": ["/path/to/server/build/index.js"],
"env": {"API_KEY": "your-key"},
"enabled": true
}
}
API Endpoints
Public Endpoints
GET /health- Health checkPOST /admin/register-client- Register new client (admin only)
Authenticated Endpoints
GET /servers- List available MCP serversPOST /mcp/request- Proxy MCP request to serverGET /audit/logs- Get audit logs
Security Features
Authentication
- Bearer token authentication
- Client-specific tokens with policies
- Token validation and expiration
Request Validation
- Method validation against allowed tools/resources
- Request size limits
- Input sanitization
- Policy enforcement
Rate Limiting
- Token bucket algorithm
- Per-client rate limits
- Configurable limits per security policy
Audit Logging
- All requests logged with timestamps
- Client identification
- Success/failure tracking
- Error details
Response Sanitization
- Removes sensitive fields (passwords, tokens, keys, secrets)
- Recursive sanitization of nested objects
- Configurable sensitive field patterns
Architecture
Client → Gateway → MCP Server
↓ ↓ ↓
Auth Validation Response
Rate Logging Sanitization
Limit Policy Error Handling
Enforcement
Components
- SecurityManager: Handles authentication, authorization, and policy enforcement
- MCPServerManager: Manages connections and communication with MCP servers
- RateLimitBucket: Implements token bucket rate limiting
- FastAPI App: HTTP server with middleware and endpoints
Future Enhancements
Security
- JWT token support
- OAuth2 integration
- Role-based access control (RBAC)
- Request signing and verification
- IP whitelisting/blacklisting
- Advanced sandboxing with containers
Functionality
- WebSocket support for real-time communication
- Request/response caching
- Load balancing across multiple MCP servers
- Circuit breaker pattern for fault tolerance
- Request transformation and middleware
Monitoring
- Prometheus metrics
- Distributed tracing
- Performance monitoring
- Alert system
- Dashboard UI
Storage
- Redis backend for rate limiting
- Database storage for audit logs
- Configuration management UI
- Client management dashboard
Development
Running in Development Mode
python main.py
The server will reload automatically on code changes.
Testing
Create test clients and policies:
# Example test client
test_policy = SecurityPolicy(
allowed_tools={"get_weather", "get_forecast"},
allowed_resources={"weather://*"},
max_requests_per_minute=100
)
test_client = ClientConfig(
client_id="test-client",
client_secret="test-secret",
policy=test_policy
)
Adding New MCP Servers
- Update the
MCPServerManager.load_server_config()method - Add server configuration with command, args, and environment
- Implement actual MCP communication in
send_request()method
Security Considerations
Threat Model
The gateway protects against:
- Malicious MCP servers: Policy enforcement prevents unauthorized access
- Compromised clients: Rate limiting and validation prevent abuse
- Data exfiltration: Response sanitization removes sensitive data
- DoS attacks: Rate limiting and request size limits
- Injection attacks: Input validation and sanitization
Best Practices
- Use strong authentication tokens
- Implement least-privilege policies
- Monitor audit logs regularly
- Keep dependencies updated
- Use HTTPS in production
- Implement proper error handling
- Regular security audits
License
This project is provided as an MVP example. Enhance and adapt according to your security requirements.
Contributing
This is an MVP implementation. Key areas for contribution:
- Real MCP server communication implementation
- Enhanced security features
- Performance optimizations
- Comprehensive testing
- Documentation improvements
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mcpgw-0.1.0.tar.gz.
File metadata
- Download URL: mcpgw-0.1.0.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78f6e0d236ada6d3448c938e86f68cc33ece92f47335d1c75881dd3fb183bdc6
|
|
| MD5 |
bbcecfd240df3dff417ab66bae987db2
|
|
| BLAKE2b-256 |
1c851f3a86541a4a6151c6ef0d53d118a0a3942234c90c19e19f99d7d2d588ac
|
File details
Details for the file mcpgw-0.1.0-py3-none-any.whl.
File metadata
- Download URL: mcpgw-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3caab27eb9fe3db3120f8bd894108e833adeedaa2769145a96a48a97834bae4f
|
|
| MD5 |
4405c21b43f5a656e1804552d3b550c7
|
|
| BLAKE2b-256 |
0e84d4c674e4548f74532d701f78091abe0836939c88fcff7d3f6a6e76de7c79
|