Skip to content

Examples

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

Examples

Each example lives in examples/ and is runnable. Use them as templates. See Getting Started for initial setup.

Basic (examples/basic)

  • What: Minimal STDIO server using FastMCP::new(...).run().
  • Run: cd examples/basic && cargo run.
  • Highlight: Logging via tracing_subscriber::fmt(); good starting point for CLI integrations.

SSE (examples/sse)

  • What: Server-Sent Events transport on http://127.0.0.1:3000.
  • Run: cd examples/sse && cargo run --features sse.
  • Highlight: Uses SseTransport::new() + run_sse_server; shows endpoints /mcp/sse (events) and /mcp/message (POST). First event returns connectionId; include it in POSTs.

WebSocket (examples/websocket)

  • What: Bidirectional WebSocket transport on ws://127.0.0.1:3001.
  • Run: cd examples/websocket && cargo run --features websocket.
  • Highlight: WebSocketTransport::new() + run_websocket_server; server-initiated messages go to current connection unless metadata.connectionId is provided.

Middleware (examples/middleware)

  • What: Logging + rate limit + bearer auth middleware chain.
  • Run: cd examples/middleware && RUST_LOG=info cargo run.
  • Highlight: Demonstrates middleware ordering and error handling; tools greet and calculate show validation with MCPError::InvalidParams.

Subscriptions (examples/subscriptions)

  • What: Subscribable resources (time://current, counter://value, file:///config.json) plus tools to mutate state.
  • Run: cd examples/subscriptions && cargo run.
  • Highlight: Shows subscription lifecycle (resources/subscribe, notifications, resources/unsubscribe); includes background ticker to simulate updates.

Complete (examples/complete)

  • What: All features in one place (tools, resources, prompts, state, lifecycle hooks).
  • Run: cd examples/complete && cargo run.
  • Highlights:
    • Tools: math (add/multiply/factorial/is_prime), strings (reverse/uppercase/count_words), complex types (greet_person, calculate_stats with validation).
    • Resources: system://info, server://status (uses shared state).
    • Prompts: code review, docs, bug analysis.
    • State: State holds AppConfig; lifecycle hooks initialize/clean up.

CLI Templates (fastrmcp-cli)

  • fastrmcp new <name> --template basic|sse|websocket|middleware|subscriptions|complete mirrors the above examples.
  • See fastrmcp-cli/README.md in the repo.

Related: Transports Guide for client snippets • Middleware Cookbook for extending the middleware example.

Back to Home

Clone this wiki locally