Skip to content

Observability

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

Observability

Logging, tracing, and metrics guidance. See Middleware Cookbook for logging middleware patterns.

Tracing Setup

use tracing_subscriber::{fmt, EnvFilter};

fn init_tracing() {
    fmt()
        .with_env_filter(EnvFilter::from_default_env().add_directive(tracing::Level::INFO.into()))
        .with_target(false)
        .init();
}
  • Recommended env vars: RUST_LOG=info for normal use, RUST_LOG=fastrmcp=debug,tower_http=debug for transport debugging.
  • Add LoggingMiddleware for structured request/response logs around handlers.

Recommended Fields

  • Request: method, id, tool/resource/prompt name, client.name, client.version, connection_id (SSE/WS), latency_ms.
  • Response: id, status (ok/error), error.code, error.message, subscription_id (for notifications).
  • Transport: connection_id, remote_addr (if available), protocol (stdio/sse/ws), close_reason.
  • Context logs: use ctx.log_info/debug/warning/error to emit MCP notifications back to clients.

Metrics (DIY)

  • Not built-in yet; recommended patterns:
    1. Add a metrics middleware (see Metrics example in docs/MIDDLEWARE.md): counters for total requests/errors and histograms for latency.
    2. Expose Prometheus via axum route in web transports.
    3. Track subscription counts via subscription_manager().subscription_count() and per-URI counts.
    4. Emit progress via ctx.report_progress(...) for long-running tools.

Debugging Tips

  • Enable request/response logging with LoggingMiddleware::requests_only() during input validation debugging to avoid noisy responses.
  • For schema issues, log the deserialized params in the tool and return InvalidParams with a concise message.
  • Web transports: set tower_http=debug and inspect SSE frames/WS frames in browser devtools.
  • Reproduce with the examples to isolate framework vs. app issues; see Examples.

Related: TroubleshootingSecurity for auth/rate limit logging.

Back to Home

Clone this wiki locally