MCP Gateway
A standalone CLI gateway that aggregates multiple MCP (Model Context Protocol) servers behind a single headless HTTP/SSE endpoint with Code Mode — reducing LLM input token usage by up to 92% when using multiple MCP servers. Headless gateway (v1.4.1 GA): CLI-managed, no UI dependencies.
Features
- Multi-Server Aggregation — Connect to multiple MCP servers (HTTP, SSE, Stdio, Streamable HTTP) and expose them through a single endpoint
- Code Mode — 4 meta-tools that let LLMs discover and use tools dynamically without loading all schemas upfront
- OAuth 2.0 Support — Built-in OAuth flow with dynamic client registration (RFC 7591) and token storage
- Hermetic Sandbox — Starlark-based sandbox for safe code execution
- MCP Protocol Compliant — Works with Claude Desktop, Cursor, and any MCP-compatible client
Installation
pip install mcp-gway
Or with mise:
mise install
uv sync --all-groups # installs dev group with pre-commit
Quick Start
OpenCode Format (Primary)
OpenCode schema — remote / local with transport auto-detection. This is the recommended path.
# Remote — auto-detects transport (streamable-http → sse → http)
mcp-gway add youtube --type remote --url http://localhost:3001/mcp
# Remote with headers
mcp-gway add supabase --type remote --url https://mcp.supabase.com/mcp --header "Authorization=Bearer TOKEN"
# Remote with pre-registered OAuth
mcp-gway add supabase --type remote --url https://mcp.supabase.com/mcp --oauth-client-id ID --oauth-client-secret SECRET --oauth-scope "openid profile"
# Remote with timeout and enable toggle
mcp-gway add api --type remote --url https://api.example.com/mcp --timeout 10000 --enabled
mcp-gway add api --type remote --url https://api.example.com/mcp --timeout 10000 --no-enabled
# Local
mcp-gway add filesystem --type local --command "npx -y @anthropic/mcp-filesystem"
mcp-gway add tools --type local --command "python -m my_mcp_server" --env MY_VAR=value --cwd /path/to/workdir
mcp-gway add tools --type local --command "npx -y my-mcp" --env KEY=VALUE --env OTHER=123 --cwd /srv/mcp/workdir
# List and serve (local-first)
mcp-gway list
mcp-gway serve --port 8080 # bindea 127.0.0.1 por defecto
mcp-gway serve --host 127.0.0.1 --port 8080
curl -s http://127.0.0.1:8080/health | jq
Deprecated Format (still works)
Old --type http|stdio|sse|streamable-http syntax is kept for backward compat and internally mapped to remote/local. Prefer remote/local for new configs.
# Equivalent old syntax — prefer remote/local above
mcp-gway add youtube --type http --url http://localhost:3001/mcp
mcp-gway add filesystem --type stdio --command npx --args '["-y", "@anthropic/mcp-filesystem"]'
mcp-gway add supabase --type streamable-http --url https://mcp.supabase.com/mcp
mcp-gway add legacy --type sse --url https://example.com/sse
Management — CLI-Only
Headless gateway: all server management (add/remove/list/inspect/refresh) is CLI-only.
One Gateway(registry, host) process serves /mcp, /health, /ready, /live and /metrics on the same Starlette app. Registry (servers/*.json + servers/*.pyi) is the single source of truth.
Local-First Security
# Default seguro — solo loopback
mcp-gway serve --port 8080 # bindea 127.0.0.1
# Exponer en 0.0.0.0 requiere opt-in explícito
MCP_GWAY_ALLOW_REMOTE=1 mcp-gway serve --host 0.0.0.0 --port 8080
# └─ log warning "server exposed on non-loopback host"
# Sin opt-in → error controlado
mcp-gway serve --host 0.0.0.0
# Error: binding to non-loopback host '0.0.0.0' requires MCP_GWAY_ALLOW_REMOTE=1
# exit 2
Observability — Logs + Metrics + Health (Approach C, v1.4.1)
Zero vendor lock-in: stdlib
jsonlogs (nostructlog), vendoredMetricsRegistry(noprometheus_client), correlation viaX-Request-ID+contextvars, health probes/health|/ready|/live+ Prometheus text/metrics. Local-first + masking***preserved; if somehow bound non-loopback,/metricsreturns403+X-Warning: exposed.
Health & Metrics:
curl -s http://127.0.0.1:8080/health | jq
# {"status":"ok","version":"1.4.1","checks":{"registry":"ok","routes":"ok"},"uptime_seconds":42}
curl -s http://127.0.0.1:8080/ready | jq # 200 ready / 503 not_ready (registry/routes/event_loop checks)
curl -s http://127.0.0.1:8080/live | jq # 200 alive — no FS I/O, <5ms
curl -s http://127.0.0.1:8080/metrics | head -n 20
# # HELP mcp_gway_http_requests_total Total HTTP requests
# # TYPE mcp_gway_http_requests_total counter
# mcp_gway_http_requests_total{method="GET",path="/health",status="200"} 7
Correlation & JSON logs:
curl -s -H "X-Request-ID: demo123" http://127.0.0.1:8080/health -D - | grep -i X-Request-ID
# X-Request-ID: demo123 ← echo on every response; json log line also has "request_id":"demo123"
uv run mcp-gway serve --port 8080 2>&1 | head # each line valid JSON: timestamp, level, logger, message, request_id, method, path, status, duration_ms
X-Request-IDorX-Correlation-IDaccepted, sanitized to^[A-Za-z0-9_-]{1,64}$, truncated; autouuid4if absent.- Labels bounded:
pathcollapsed to/mcpor/mcp/messages(all other routes recorded as-is), server sanitized[^A-Za-z0-9_]→_32 chars. - Metrics:
http_requests_total,http_request_duration_seconds(buckets 0.005..5),mcp_tool_calls_total{server,tool,status},discovery_duration_seconds,sandbox_execute_total{status},registry_operations_total{op},gateway_sessions_active.
Local-first gating: /metrics never leaks secrets; if somehow bound non-loopback without MCP_GWAY_ALLOW_REMOTE=1, serve exits 2; if bypassed, /metrics returns 403 + X-Warning: exposed.
Connect from Claude Desktop
Add to your Claude Desktop config (~/Library/Application Support/Claude/claude_desktop_config.json):
{
"mcpServers": {
"gateway": {
"url": "http://localhost:8080/mcp"
}
}
}
Commands
| Command | Description |
|---|---|
mcp-gway add --type remote|local |
Add an MCP server and generate .pyi stub (OpenCode format, primary) |
mcp-gway remove |
Remove an MCP server |
mcp-gway update |
Update tools for a server |
mcp-gway list |
List all connected servers |
mcp-gway inspect |
Show tool signatures for a server |
mcp-gway refresh [<name>] [--auth] [--oauth-port <port>] |
Refresh connection and re-discover tools |
mcp-gway serve [--host 127.0.0.1] [--port <port>] |
Start gateway (MCP + health probes). Default 127.0.0.1; 0.0.0.0 necesita MCP_GWAY_ALLOW_REMOTE=1 |
Backward compat:
mcp-gway add --type http|stdio|sse|streamable-httpstill works (deprecated).http/sse/streamable-http→remote(withresolved_transportcached),stdio→local. Useremote/localgoing forward.
Options for add (OpenCode) — 12+ flags grouped by scope:
| Option | Description |
|---|---|
--type remote|local |
Server type (primary) |
--url <url> |
URL for remote |
--header "KEY=VALUE" |
HTTP header for remote (repeatable) |
--command "<cmd>" |
Command for local (e.g. "npx -y my-mcp") |
--env KEY=VALUE |
Environment variable for local (repeatable) |
--cwd <path> |
Working directory for local |
--oauth-client-id ID |
Pre-registered OAuth client ID |
--oauth-client-secret SECRET |
Pre-registered OAuth client secret |
--oauth-scope SCOPE |
OAuth scope |
--oauth-port <port> |
Local port for OAuth callback (default 8989) |
--timeout <ms> |
Connection timeout in ms (default 5000) |
--enabled / --no-enabled |
Enable/disable without removal (default enabled) |
--tools <list> |
Comma-separated tool filter (default * = all) |
--args <json> |
JSON array of extra args — deprecated compat, used with stdio/local |
--docs-url <url> |
Deprecated — accepted for compat but not persisted (legacy) |
Local Commands — Dynamic Allow-List (feat-006)
Dynamic-no-static: no hardcoded binaries. Operators allow-list once via env; see ADR-009.
Default-deny: empty MCP_GWAY_ALLOW_LOCAL_COMMANDS denies every local command.
# Allow-list (CSV basenames, `*` = invalid → deny + warn)
export MCP_GWAY_ALLOW_LOCAL_COMMANDS="npx,uvx,python3,agentmemory"
mcp-gway add mem --type local --command "agentmemory mcp local"
mcp-gway add fs --type local --command "npx -y @anthropic/mcp-filesystem" --cwd /srv/mcp/workdir
# Break-glass 72h (bootstrap only, time-boxed)
export MCP_GWAY_ALLOW_UNRESTRICTED_LOCAL=1
unset MCP_GWAY_ALLOW_UNRESTRICTED_LOCAL
- Marker
~/.config/mcp-gway/.local_unrestricted(epoch,0o600, 72h TTL) — fail-closed: missing, expired, or invalid → deny. - Any syntactically valid basename allowed while marker fresh; otherwise deny.
unsetreturns to allow-list mode.- CLI
add/refreshenforces allow-list/unrestricted plus re-validation before persist. cwdmust be absolute + real +is_dir, elsereason_code=invalid_cwd. Env denylist (PATH,LD_PRELOAD,PYTHONPATH, …) →reason_code=denied_env.- Spawn only resolved via PATH lookup (
shutil.which(basename)); nevershell=True/cmd /c/sh -c. Errors carryreason_code(not_allowlisted,binary_not_found, …).
Code Mode
When connected, the gateway exposes 4 meta-tools:
| Tool | Description |
|---|---|
listToolFiles |
List all available .pyi stub files |
readToolFile |
Read function signatures from a stub |
getToolDocs |
Get detailed documentation for a tool |
executeToolCode |
Execute code in a sandboxed Starlark interpreter |
OAuth Authentication
For servers requiring OAuth (e.g., Supabase):
# Trigger OAuth flow
mcp-gway refresh supabase --auth
# Or store token manually
mkdir -p ~/.config/mcp-gway/tokens
echo '{"access_token": "YOUR_TOKEN"}' > ~/.config/mcp-gway/tokens/supabase.json
Development
# Install dependencies
uv sync --all-groups # installs dev group with pre-commit
uv run pre-commit install # once per clone — hooks already configured in .pre-commit-config.yaml
# Run checks
uv run pre-commit run --all-files # ruff + ruff-format + hygiene (trailing-whitespace, end-of-file-fixer, check-yaml, check-added-large-files)
uv run pytest -v # 185 tests — CLI, MCP, Code Mode, OAuth, observability
uv run ruff check src/ tests/
uv run ruff format --check src/ tests/
# Verification probes (sin Node, sin build)
curl -s http://127.0.0.1:8080/health | jq .status # "ok"
curl -s http://127.0.0.1:8080/ready | jq .status # "ready"
curl -s http://127.0.0.1:8080/metrics | head -n 5 # # HELP mcp_gway_...
# Local-first check
mcp-gway serve --host 0.0.0.0 2>&1 | grep -q "requires MCP_GWAY_ALLOW_REMOTE" && echo "gate ok"
Pre-commit is already in place (.pre-commit-config.yaml — ruff v0.16.4, ruff-format, trailing-whitespace, end-of-file-fixer, check-yaml, check-added-large-files).
Architecture
┌──────────────────────────────────────────────────────────────────────┐
│ MCP Gateway v1.4.1 GA │
├──────────────────────────────────────────────────────────────────────┤
│ CLI (click) │ Gateway (Starlette + uvicorn, CSP) │
│ - add remote/local │ - POST /mcp (JSON-RPC) │
│ - remove/inspect/list │ - GET /mcp (SSE endpoint event) │
│ - refresh --auth │ - POST /mcp/messages?session_id=... │
│ - serve --host 127.0.0.1 │ - GET /health │
│ (local-first default) │ - GET /ready, /live, /metrics │
├──────────────────────────────────────────────────────────────────────┤
│ Code Mode (4 meta-tools) │ Starlark Sandbox │
│ - listToolFiles │ - Hermetic execution │
│ - readToolFile │ - Server injection │
│ - getToolDocs │ │
│ - executeToolCode │ │
├──────────────────────────────────────────────────────────────────────┤
│ Registry (única fuente) │ OAuth2 (RFC 7591, reutilizado) │
│ - servers/*.pyi = signatures │ - Dynamic registration │
│ - servers/*.json = config │ - PKCE + FileTokenStorage │
│ - last-write-wins, atómico │ - tokens/ no expuesto vía API │
└──────────────────────────────────────────────────────────────────────┘
│ │ │
┌────┴────┐ ┌────┴────┐ ┌────┴────┐
│ Server1 │ │ Server2 │ │ Server3 │
│ (remote)│ │ (local) │ │ (remote)│
└─────────┘ └─────────┘ └─────────┘
- Sin Node en runtime ni CI: sin UI ni assets vendoreados,
ruffúnico linter,uv_buildbackend. - Release híbrido (ADR-007):
push tags v*→uv build+pypi-publish(GAv1.4.1tag manual) +workflow_run Tests completed→python-semantic-release@v9parafix/perfpatches auto.concurrency: release,fetch-depth:0,[tool.semantic_release]syncpyproject.toml+__init__.py(1.4.1exacta).
License
MIT
Metadata
Release files for mcp-gway 1.5.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mcp_gway-1.5.1.tar.gz | 42.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mcp_gway-1.5.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 95.6 kB
Release files / mcp_gway-1.5.1.tar.gz
| Download URL | mcp_gway-1.5.1.tar.gz |
|---|---|
| Size | 42.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
43cbee34cb06f0938613cd91b82687db4c2f394b530ab55b748d6b09f31becbc
|
|
BLAKE2b-256 checksum How to use checksums |
9e7ea50692d69ec2a32a5b720d0f867533054d60be493912307ae84cdc4d35ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 10, 2026.
Transparency logRelease files / mcp_gway-1.5.1-py3-none-any.whl
| Download URL | mcp_gway-1.5.1-py3-none-any.whl |
|---|---|
| Size | 52.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f5a7c6465c77ba7f7282630bc624f246a38492c868ecf4e458933b0701d56507
|
|
BLAKE2b-256 checksum How to use checksums |
d1dd08168c63e799bd53d900dd2cd935b409ee4cc275b10e06fe660c0de8bd1c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 10, 2026.
Transparency log