-
Notifications
You must be signed in to change notification settings - Fork 0
Security
Jeffrie Budde edited this page Nov 25, 2025
·
1 revision
Hardening guidance for FastRMCP deployments. Pair with Middleware Cookbook and Transports Guide.
-
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; returnMCPError::InvalidRequest("unauthorized")on failure. -
Client identity: Store authenticated user in
Contextmetadata (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.
- Use
RateLimitMiddleware::new(limit, window)(simplified) or custom middleware with shared counters keyed byconnectionId/client ID. - Apply stricter limits on expensive tools/resources; consider per-URI token buckets.
- 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.mdfor rustls setup.
- Validate input types explicitly; use
InvalidParamsfor user errors and avoid leaking internals. - Avoid returning sensitive metadata in tool results; strip in
after_responsemiddleware if needed. - For resources backed by filesystem/network, sandbox paths and timeouts; return
ResourceErrorwith generic messages on failure.
- SSE: reject POSTs without
connectionIdwhen multiple connections exist; never echo other clients’ IDs. - WebSocket: server-initiated requests default to current connection; set
metadata.connectionIdto target explicitly and avoid cross-tenant leaks.
- Rotate secrets without restart by storing them in shared
Stateand 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.