mcpgate is a production-layer gateway for MCP servers. MCP servers are easy to write and expose, but bare servers lack authentication, rate limiting, caching, circuit breaking, and observability, and exposing them directly to a product makes them prone to crashing due to abusive clients, slow backends, or retry storms. mcpgate adds authentication, per-key rate limiting, response caching, circuit breakers, structured logging, and Prometheus metrics to MCP servers without modifying the servers themselves.
Key capabilities:
- Authentication: supports X-API-Key or Authorization: Bearer keys; configurable OIDC (validating JWTs signed by the issuer) and OAuth (validating opaque tokens via the authorization server's introspection endpoint). Missing or invalid credentials return 401, and key values and token contents are not logged.
- Rate limiting: token bucket per API key; exhaustion returns 429 with a JSON-RPC error, with support for per-key override configuration.
- Tool filtering and authorization: tool_filter hides tools and removes them from tools/list, and tools/call for filtered tools returns 403 before reaching the backend; tool_scopes gates individual tools by OAuth-style scope, where scope comes from API key configuration, OIDC scope/scp claims, or the scope claim in the OAuth introspection response.
- Schema rewriting: renames or hides tool input parameters per backend, and translates client parameter names back to backend names on tools/call; tool names themselves are not rewritten.
- Response rewriting: renames or hides result fields per backend, applying to JSON objects at any depth (including structuredContent and text content parseable as JSON); non-JSON content is passed through unchanged.
- Circuit breaker: when open, fails fast with 503; after cooldown, allows a single probe, closing on success and reopening on failure.
- Caching: tools/list is cached per backend; tools/call is cached only for tools explicitly listed in cacheable_tools, with hits returning X-Cache: HIT and the cached payload rewrapped with the current request id.
- Health checks: actively probes backends with tools/list; consecutive failures mark the backend unhealthy and fail fast with 503, and successful probes automatically recover it; /readyz and mcpgate_backend_health indicator exposure status.
- Load balancing: a single backend name can be configured with multiple endpoints, supporting strategies such as round_robin, while circuit breaking, caching, health checks, and SSE relay are still handled as one logical backend.
- Streaming and notifications: supports real-time relay of SSE events, incremental parsing of Streamable HTTP, and fire-and-forget notification responses with 202.
- Admin interfaces: POST /admin/cache/invalidate clears the cache, POST /admin/reload hot-reloads the configuration (API keys, rate limiting, tool filtering, rewriting, caching, circuit breaking, health checks, backend connections, etc.), and SIGHUP can also trigger a reload; GET /admin/status reports runtime status.
Usage: configure via a single YAML file. The demo can build the gateway and an echo backend within 30 seconds (requires Go 1.27+), demonstrating in sequence the 401 without a key, cache MISS/HIT, tools/call, rate limiting 429, /healthz, and /metrics. Your own backend can mount the configuration file: -v $PWD/config.yaml:/app/config.yaml. When using -stdio, mcpgate itself runs as a stdio MCP server, aggregating all configured backends into a single stdin/stdout stream, suitable for clients that only support stdio (such as Claude Desktop, IDE extensions, MCP inspectors) and server directories; stdio mode has no API key authentication or rate limiting, logs are written to stderr, and stdout carries only protocol traffic.
Performance: in-process tools/call overhead is about 80µs; against a mock backend at 10-way concurrency it reaches about 4,000 QPS with zero errors; see BENCHMARKS.md for the detailed methodology and data.
Limitations: stdio mode has no authentication or rate limiting; caching applies only to explicitly listed tools; hiding required backend parameters may cause calls to fail (the gateway does not inject values); some configuration items (listen address, TLS, Redis, OIDC/OAuth endpoints) require a restart to take effect.