Skip to content

Security

Jeffrie Budde edited this page Nov 25, 2025 · 1 revision

Security

Hardening guidance for FastRMCP deployments. Pair with Middleware Cookbook and Transports Guide.

Auth Patterns

  • Bearer token: AuthMiddleware::bearer_token("secret") for quick protection; extend with custom middleware to validate per-request metadata.
  • Custom middleware: Inspect request.params.metadata (or _meta) for headers/tokens; return MCPError::InvalidRequest("unauthorized") on failure.
  • Client identity: Store authenticated user in Context metadata (context.set_metadata("user", json!(user_id)).await) for downstream tools.
  • Transport auth: For web transports, add Axum middleware before the MCP router to verify headers/cookies.

Rate Limiting

  • Use RateLimitMiddleware::new(limit, window) (simplified) or custom middleware with shared counters keyed by connectionId/client ID.
  • Apply stricter limits on expensive tools/resources; consider per-URI token buckets.

CORS & Exposure

  • Restrict origins on web transports; replace permissive CorsLayer::new().allow_origin(Any) with explicit origins.
  • Expose only required transport(s) publicly; keep STDIO/internal transports behind firewalls or run on localhost.
  • Prefer TLS for SSE/WS in production; see docs/HTTP2.md for rustls setup.

Data Handling

  • Validate input types explicitly; use InvalidParams for user errors and avoid leaking internals.
  • Avoid returning sensitive metadata in tool results; strip in after_response middleware if needed.
  • For resources backed by filesystem/network, sandbox paths and timeouts; return ResourceError with generic messages on failure.

Connection IDs

  • SSE: reject POSTs without connectionId when multiple connections exist; never echo other clients’ IDs.
  • WebSocket: server-initiated requests default to current connection; set metadata.connectionId to target explicitly and avoid cross-tenant leaks.

Operational Tips

  • Rotate secrets without restart by storing them in shared State and reading inside middleware.
  • Log auth failures with minimal detail; avoid logging credentials.
  • Add health checks outside MCP if exposing HTTP servers; keep MCP methods private to trusted clients.

Related: Observability for logging failed auth/rate limits • Troubleshooting for common connection errors.

Back to Home

Clone this wiki locally