Expand description
Message dispatcher for routing JSON-RPC messages
This module implements the message routing layer that solves the bidirectional communication problem. It runs a background task that reads ALL messages from the transport and routes them appropriately:
- Responses → Routed to waiting
request()calls via oneshot channels - Requests → Routed to registered request handler (for elicitation, sampling, etc.)
- Notifications → Routed to registered notification handler
§Architecture
┌──────────────────────────────────────────────┐
│ MessageDispatcher │
│ │
│ Background Task (tokio::spawn): │
│ loop { │
│ msg = transport.receive().await │
│ match parse(msg) { │
│ Response => send to oneshot channel │
│ Request => call request_handler │
│ Notification => call notif_handler │
│ } │
│ } │
└──────────────────────────────────────────────┘This ensures that there’s only ONE consumer of transport.receive(),
eliminating race conditions by centralizing all message routing.