hyperinfer-server 0.1.1

High-performance LLM Gateway server built with Axum
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
//! MCP (Model Context Protocol) host implementation.
//!
//! Implements the SSE-based bidirectional transport described in the project spec:
//!
//! * `GET /mcp/sse` — establishes a long-lived SSE stream for the client session.
//! * `POST /mcp/message` — accepts JSON-RPC 2.0 messages and fans them out to the
//!   correct session via the in-memory session registry.
//!
//! Both endpoints are protected by the [`JwtAuthLayer`] tower middleware which
//! validates `Authorization: Bearer <token>` JWTs signed with HS256.
//!
//! # Session lifecycle
//!
//! 1. Client opens `GET /mcp/sse`.  The server creates a `McpSession` with a
//!    random UUID, inserts it into the `SessionRegistry`, and starts streaming
//!    SSE events.  The first event is `endpoint` which tells the client the URL
//!    it must use for `POST /mcp/message?session_id=<uuid>`.
//! 2. Client POSTs JSON-RPC commands to `POST /mcp/message?session_id=<uuid>`.
//!    The handler looks up the session, dispatches the command, and sends the
//!    reply through the session's mpsc channel so it appears on the SSE stream.
//! 3. When the client disconnects the SSE stream is dropped; the cleanup task
//!    removes the session from the registry.

use axum::{
    body::Body,
    extract::{Query, State},
    http::{HeaderMap, Request, StatusCode},
    middleware::Next,
    response::{sse::Event, IntoResponse, Response, Sse},
    Extension, Json,
};

use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{collections::HashMap, convert::Infallible, sync::Arc, time::Duration};
use tokio::sync::{mpsc, RwLock};
use tracing::warn;
use uuid::Uuid;

// ── JWT ─────────────────────────────────────────────────────────────────────

use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};

/// Claims expected inside an MCP Bearer JWT.
#[derive(Debug, Deserialize, Clone)]
pub struct McpClaims {
    /// Subject (agent / virtual key identifier).
    pub sub: String,
    /// Expiry (Unix timestamp seconds).  Validated automatically by
    /// `jsonwebtoken` when present.
    #[allow(dead_code)]
    pub exp: u64,
}

/// Axum middleware that validates `Authorization: Bearer <jwt>`.
///
/// Requires the JWT secret to be present in `McpState`.
/// On failure returns 401; on success the claims are added to request extensions
/// and the request is forwarded.
pub async fn jwt_auth_middleware(
    State(state): State<McpState>,
    mut req: Request<Body>,
    next: Next,
) -> Response {
    let token = match extract_bearer(req.headers()) {
        Some(t) => t,
        None => {
            return (StatusCode::UNAUTHORIZED, "Missing Authorization header").into_response();
        }
    };

    let claims = match validate_jwt(&token, &state.jwt_secret) {
        Ok(c) => c,
        Err(_) => {
            return (StatusCode::UNAUTHORIZED, "Invalid or expired JWT").into_response();
        }
    };

    req.extensions_mut().insert(claims);
    next.run(req).await
}

fn extract_bearer(headers: &HeaderMap) -> Option<String> {
    let value = headers.get(axum::http::header::AUTHORIZATION)?;
    let s = value.to_str().ok()?;
    let mut parts = s.splitn(2, char::is_whitespace);
    let scheme = parts.next()?;
    if scheme.eq_ignore_ascii_case("bearer") {
        return Some(parts.next()?.to_owned());
    }
    None
}

pub fn validate_jwt(token: &str, secret: &str) -> Result<McpClaims, jsonwebtoken::errors::Error> {
    let key = DecodingKey::from_secret(secret.as_bytes());
    let validation = Validation::new(Algorithm::HS256);
    let data = decode::<McpClaims>(token, &key, &validation)?;
    Ok(data.claims)
}

/// Create a signed HS256 JWT for testing / internal use.
pub fn create_jwt(sub: &str, secret: &str) -> String {
    use jsonwebtoken::{encode, EncodingKey, Header};

    #[derive(Serialize)]
    struct Claims<'a> {
        sub: &'a str,
        exp: u64,
    }

    let exp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
        + 3600;

    encode(
        &Header::default(),
        &Claims { sub, exp },
        &EncodingKey::from_secret(secret.as_bytes()),
    )
    .expect("JWT encoding should never fail for HS256")
}

// ── Session registry ─────────────────────────────────────────────────────────

const MAX_SESSIONS_GLOBAL: usize = 1000;
const MAX_SESSIONS_PER_OWNER: usize = 50;

struct SessionRemovalGuard {
    sessions: SessionRegistry,
    sid: String,
}

impl Drop for SessionRemovalGuard {
    fn drop(&mut self) {
        let sessions = self.sessions.clone();
        let sid = self.sid.clone();
        if let Ok(handle) = tokio::runtime::Handle::try_current() {
            handle.spawn(async move {
                let mut sessions = sessions.write().await;
                sessions.remove(&sid);
                tracing::debug!("MCP SSE session {} removed by Drop guard", sid);
            });
        } else {
            tracing::warn!(
                "MCP SSE session {} could not be cleaned up: no Tokio runtime",
                sid
            );
        }
    }
}

/// A single SSE event frame sent through the session channel.
#[derive(Debug, Clone)]
pub struct SseFrame {
    pub event: String,
    pub data: String,
}

/// A live MCP client session.
#[derive(Clone)]
pub struct McpSession {
    pub id: String,
    /// Owner/subject from the JWT that created this session.
    pub owner: String,
    /// Sender side of the event channel.  Cloned into the SSE stream and into
    /// the message handler so both can push frames to the client.
    pub tx: mpsc::Sender<SseFrame>,
}

/// Thread-safe map of session_id → session.
pub type SessionRegistry = Arc<RwLock<HashMap<String, McpSession>>>;

// ── Shared state ─────────────────────────────────────────────────────────────

#[derive(Clone)]
pub struct McpState {
    pub sessions: SessionRegistry,
    pub jwt_secret: Arc<String>,
}

impl McpState {
    pub fn new(jwt_secret: impl Into<String>) -> Self {
        Self {
            sessions: Arc::new(RwLock::new(HashMap::new())),
            jwt_secret: Arc::new(jwt_secret.into()),
        }
    }
}

// ── JSON-RPC types ────────────────────────────────────────────────────────────

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct JsonRpcRequest {
    pub jsonrpc: String,
    pub id: Option<Option<Value>>,
    pub method: String,
    #[serde(default)]
    pub params: Option<Value>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct JsonRpcResponse {
    pub jsonrpc: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<JsonRpcError>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct JsonRpcError {
    pub code: i32,
    pub message: String,
}

impl JsonRpcResponse {
    pub fn ok(id: Option<Value>, result: Value) -> Self {
        Self {
            jsonrpc: "2.0".into(),
            id,
            result: Some(result),
            error: None,
        }
    }

    pub fn err(id: Option<Value>, code: i32, message: impl Into<String>) -> Self {
        Self {
            jsonrpc: "2.0".into(),
            id,
            result: None,
            error: Some(JsonRpcError {
                code,
                message: message.into(),
            }),
        }
    }
}

// ── Dispatch ──────────────────────────────────────────────────────────────────

/// Dispatch a JSON-RPC method to its handler and return the response value.
///
/// Extend this function to add new MCP tool methods.
pub fn dispatch_method(req: &JsonRpcRequest) -> JsonRpcResponse {
    let id = req.id.clone().flatten();
    match req.method.as_str() {
        "ping" => JsonRpcResponse::ok(id, serde_json::json!("pong")),
        "tools/list" => JsonRpcResponse::ok(id, serde_json::json!({ "tools": [] })),
        "initialize" => JsonRpcResponse::ok(
            id,
            serde_json::json!({
                "protocolVersion": "2024-11-05",
                "capabilities": { "tools": {} },
                "serverInfo": { "name": "hyperinfer-mcp", "version": env!("CARGO_PKG_VERSION") }
            }),
        ),
        unknown => JsonRpcResponse::err(id, -32601, format!("Method not found: {}", unknown)),
    }
}

// ── Handlers ──────────────────────────────────────────────────────────────────

/// `GET /mcp/sse`
///
/// Opens a long-lived SSE connection for the calling agent.  The first event
/// sent to the client is `endpoint` containing the POST URL the client should
/// use for all subsequent JSON-RPC messages.
pub async fn mcp_sse_handler(
    State(state): State<McpState>,
    Extension(claims): Extension<McpClaims>,
) -> impl IntoResponse {
    let session_id = Uuid::new_v4().to_string();
    let owner = claims.sub.clone();
    let (tx, mut rx) = mpsc::channel::<SseFrame>(64);

    // Register session with owner and enforce limits.
    {
        let mut sessions = state.sessions.write().await;

        // Enforce global cap
        if sessions.len() >= MAX_SESSIONS_GLOBAL {
            warn!(
                "MCP SSE: Global session limit reached ({})",
                MAX_SESSIONS_GLOBAL
            );
            return (StatusCode::SERVICE_UNAVAILABLE, "Server at capacity").into_response();
        }

        // Enforce per-owner cap
        let owner_count = sessions.values().filter(|s| s.owner == owner).count();
        if owner_count >= MAX_SESSIONS_PER_OWNER {
            warn!(
                "MCP SSE: Per-owner session limit reached ({}) for owner: {}",
                MAX_SESSIONS_PER_OWNER, owner
            );
            return (
                StatusCode::TOO_MANY_REQUESTS,
                "Too many concurrent sessions",
            )
                .into_response();
        }

        sessions.insert(
            session_id.clone(),
            McpSession {
                id: session_id.clone(),
                owner,
                tx: tx.clone(),
            },
        );
    }

    // RAII guard for cleanup on stream drop.
    let _guard = SessionRemovalGuard {
        sessions: state.sessions.clone(),
        sid: session_id.clone(),
    };

    // Send the `endpoint` event immediately so the client knows where to POST.
    if tx
        .send(SseFrame {
            event: "endpoint".to_string(),
            data: format!("/mcp/message?session_id={}", session_id),
        })
        .await
        .is_err()
    {
        warn!(
            "MCP SSE: failed to send endpoint event for session {}; client disconnected immediately",
            session_id
        );
        // Requirement (1): remove the entry if initial send fails
        let mut sessions = state.sessions.write().await;
        sessions.remove(&session_id);
        // Return an empty stream or error response
        return Sse::new(futures::stream::empty::<
            Result<axum::response::sse::Event, std::convert::Infallible>,
        >())
        .keep_alive(
            axum::response::sse::KeepAlive::new()
                .interval(std::time::Duration::from_secs(15))
                .text("keep-alive"),
        )
        .into_response();
    }

    // Build the SSE stream from the channel: convert SseFrame → axum Event.
    let stream = async_stream::stream! {
        let _guard = _guard; // Move guard into stream to ensure it lives as long as the stream

        // Disconnect idle sessions after 30 minutes.
        let idle_timeout = Duration::from_secs(30 * 60);

        loop {
            match tokio::time::timeout(idle_timeout, rx.recv()).await {
                Ok(Some(frame)) => {
                    let ev: Result<Event, Infallible> = Ok(
                        Event::default().event(frame.event).data(frame.data)
                    );
                    yield ev;
                }
                Ok(None) => {
                    // Channel closed (e.g. server shutdown)
                    break;
                }
                Err(_) => {
                    // Timeout elapsed
                    tracing::info!("MCP SSE session {} timed out due to inactivity", _guard.sid);
                    break;
                }
            }
        }

        // Guard is dropped here and removes the session.
    };

    Sse::new(stream)
        .keep_alive(
            axum::response::sse::KeepAlive::new()
                .interval(Duration::from_secs(15))
                .text("keep-alive"),
        )
        .into_response()
}

/// Query parameters for `POST /mcp/message`.
#[derive(Deserialize)]
pub struct MessageQuery {
    pub session_id: String,
}

/// `POST /mcp/message?session_id=<uuid>`
///
/// Accepts a JSON-RPC 2.0 message body, dispatches it, and sends the response
/// back through the SSE channel of the identified session.
///
/// Returns 202 Accepted on success, 404 if the session does not exist,
/// 403 if the caller does not own the session.
pub async fn mcp_message_handler(
    State(state): State<McpState>,
    Extension(claims): Extension<McpClaims>,
    Query(query): Query<MessageQuery>,
    Json(rpc_req): Json<JsonRpcRequest>,
) -> impl IntoResponse {
    if rpc_req.jsonrpc != "2.0" {
        return (
            StatusCode::BAD_REQUEST,
            Json(JsonRpcResponse::err(
                None,
                -32600,
                "Invalid JSON-RPC version",
            )),
        )
            .into_response();
    }

    let sessions = state.sessions.read().await;
    let session = match sessions.get(&query.session_id) {
        Some(s) => s.clone(),
        None => {
            warn!("POST /mcp/message: unknown session_id {}", query.session_id);
            return (StatusCode::NOT_FOUND, "Session not found").into_response();
        }
    };

    if session.owner != claims.sub {
        warn!(
            "POST /mcp/message: unauthorized access attempt by {} to session owned by {}",
            claims.sub, session.owner
        );
        return (StatusCode::FORBIDDEN, "Not the session owner").into_response();
    }
    drop(sessions);

    let rpc_response = dispatch_method(&rpc_req);

    // If it's a notification (rpc_req.id is None), do not respond.
    if rpc_req.id.is_none() {
        return StatusCode::ACCEPTED.into_response();
    }

    let data = serde_json::to_string(&rpc_response).unwrap_or_else(|e| {
        warn!("Failed to serialize JSON-RPC response: {}", e);
        let id_val = rpc_req.id.flatten();
        let id_str = id_val
            .as_ref()
            .and_then(|v| serde_json::to_string(v).ok())
            .unwrap_or_else(|| "null".to_string());
        format!(
            r#"{{"jsonrpc":"2.0","id":{},"error":{{"code":-32603,"message":"Internal error"}}}}"#,
            id_str
        )
    });
    let frame = SseFrame {
        event: "message".to_string(),
        data,
    };

    match session.tx.try_send(frame) {
        Ok(()) => StatusCode::ACCEPTED.into_response(),
        Err(mpsc::error::TrySendError::Full(_)) => {
            warn!(
                "POST /mcp/message: session {} SSE channel full",
                query.session_id
            );
            (StatusCode::SERVICE_UNAVAILABLE, "Session stream full").into_response()
        }
        Err(mpsc::error::TrySendError::Closed(_)) => {
            warn!(
                "POST /mcp/message: session {} SSE channel closed",
                query.session_id
            );
            (StatusCode::GONE, "Session stream closed").into_response()
        }
    }
}

// ── Stream helper (used by tests) ─────────────────────────────────────────────

/// Drain up to `limit` frames from a session channel (for testing).
pub async fn collect_frames(rx: &mut mpsc::Receiver<SseFrame>, limit: usize) -> Vec<SseFrame> {
    let mut frames = Vec::new();
    while frames.len() < limit {
        match tokio::time::timeout(Duration::from_millis(200), rx.recv()).await {
            Ok(Some(frame)) => frames.push(frame),
            _ => break,
        }
    }
    frames
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use axum::{
        middleware,
        routing::{get, post},
        Router,
    };
    use axum_test::TestServer;
    use serde_json::json;

    /// Simple 200 GET handler used only in tests to validate JWT middleware.
    async fn health_ok() -> StatusCode {
        StatusCode::OK
    }

    fn build_app(state: McpState) -> Router {
        let mcp_routes = Router::new()
            .route("/mcp/message", post(mcp_message_handler))
            // `/mcp/health` is a test-only non-streaming endpoint so we can
            // check JWT middleware without hanging on an SSE stream.
            .route("/mcp/health", get(health_ok))
            .layer(middleware::from_fn_with_state(
                state.clone(),
                jwt_auth_middleware,
            ))
            .with_state(state.clone());

        // SSE route added separately (not tested via TestServer since SSE
        // responses are infinite streams and block axum-test's response reader).
        let sse_route = Router::new()
            .route("/mcp/sse", get(mcp_sse_handler))
            .layer(middleware::from_fn_with_state(
                state.clone(),
                jwt_auth_middleware,
            ))
            .with_state(state);

        mcp_routes.merge(sse_route)
    }

    fn make_jwt(secret: &str) -> String {
        create_jwt("test-agent", secret)
    }

    // ── JWT middleware ──────────────────────────────────────────────────────
    //
    // NOTE: We test the JWT middleware via `/mcp/health` (a plain 200 endpoint)
    // rather than `/mcp/sse` because SSE responses are infinite streams and
    // `axum-test` would block indefinitely waiting for the response body.

    #[tokio::test]
    async fn test_jwt_missing_header_returns_401() {
        let state = McpState::new("secret");
        let app = build_app(state);
        let server = TestServer::new(app);

        let resp: axum_test::TestResponse = server.get("/mcp/health").await;
        assert_eq!(resp.status_code(), StatusCode::UNAUTHORIZED);
    }

    #[tokio::test]
    async fn test_jwt_invalid_token_returns_401() {
        let state = McpState::new("secret");
        let app = build_app(state);
        let server = TestServer::new(app);

        let resp: axum_test::TestResponse = server
            .get("/mcp/health")
            .add_header(
                axum::http::header::AUTHORIZATION,
                axum::http::HeaderValue::from_static("Bearer not-a-valid-jwt"),
            )
            .await;
        assert_eq!(resp.status_code(), StatusCode::UNAUTHORIZED);
    }

    #[tokio::test]
    async fn test_jwt_wrong_secret_returns_401() {
        let state = McpState::new("correct-secret");
        let app = build_app(state);
        let server = TestServer::new(app);

        let bad_token = create_jwt("agent", "wrong-secret");
        let auth_value = format!("Bearer {}", bad_token);
        let resp: axum_test::TestResponse = server
            .get("/mcp/health")
            .add_header(
                axum::http::header::AUTHORIZATION,
                axum::http::HeaderValue::from_str(&auth_value).unwrap(),
            )
            .await;
        assert_eq!(resp.status_code(), StatusCode::UNAUTHORIZED);
    }

    #[tokio::test]
    async fn test_jwt_valid_case_insensitive_bearer_passes() {
        let secret = "test-secret";
        let state = McpState::new(secret);
        let app = build_app(state);
        let server = TestServer::new(app);

        // A valid token with a lowercase "bearer" should allow access.
        let token = make_jwt(secret);
        let auth_value = format!("bearer {}", token);
        let resp: axum_test::TestResponse = server
            .get("/mcp/health")
            .add_header(
                axum::http::header::AUTHORIZATION,
                axum::http::HeaderValue::from_str(&auth_value).unwrap(),
            )
            .await;
        assert_eq!(resp.status_code(), StatusCode::OK);
    }

    // ── validate_jwt unit tests ─────────────────────────────────────────────

    #[test]
    fn test_validate_jwt_valid() {
        let secret = "my-secret";
        let token = create_jwt("alice", secret);
        let claims = validate_jwt(&token, secret).unwrap();
        assert_eq!(claims.sub, "alice");
    }

    #[test]
    fn test_validate_jwt_wrong_secret() {
        let token = create_jwt("alice", "correct");
        assert!(validate_jwt(&token, "wrong").is_err());
    }

    #[test]
    fn test_validate_jwt_malformed() {
        assert!(validate_jwt("not.a.jwt", "secret").is_err());
    }

    #[test]
    fn test_validate_jwt_missing_exp_rejected() {
        use jsonwebtoken::{encode, EncodingKey, Header};

        #[derive(Serialize)]
        struct ClaimsNoExp<'a> {
            sub: &'a str,
        }

        let token = encode(
            &Header::default(),
            &ClaimsNoExp { sub: "alice" },
            &EncodingKey::from_secret(b"secret"),
        )
        .unwrap();

        assert!(validate_jwt(&token, "secret").is_err());
    }

    // ── dispatch_method ─────────────────────────────────────────────────────

    #[test]
    fn test_dispatch_ping() {
        let req = JsonRpcRequest {
            jsonrpc: "2.0".into(),
            id: Some(Some(json!(1))),
            method: "ping".into(),
            params: None,
        };
        let resp = dispatch_method(&req);
        assert_eq!(resp.result, Some(json!("pong")));
        assert!(resp.error.is_none());
    }

    #[test]
    fn test_dispatch_tools_list() {
        let req = JsonRpcRequest {
            jsonrpc: "2.0".into(),
            id: Some(Some(json!(2))),
            method: "tools/list".into(),
            params: None,
        };
        let resp = dispatch_method(&req);
        assert!(resp.result.is_some());
        assert!(resp.error.is_none());
        let tools = &resp.result.unwrap()["tools"];
        assert!(tools.is_array());
    }

    #[test]
    fn test_dispatch_initialize() {
        let req = JsonRpcRequest {
            jsonrpc: "2.0".into(),
            id: Some(Some(json!(3))),
            method: "initialize".into(),
            params: None,
        };
        let resp = dispatch_method(&req);
        assert!(resp.result.is_some());
        let result = resp.result.unwrap();
        assert_eq!(result["protocolVersion"], "2024-11-05");
    }

    #[test]
    fn test_dispatch_unknown_method() {
        let req = JsonRpcRequest {
            jsonrpc: "2.0".into(),
            id: Some(Some(json!(4))),
            method: "nonexistent".into(),
            params: None,
        };
        let resp = dispatch_method(&req);
        assert!(resp.result.is_none());
        let err = resp.error.unwrap();
        assert_eq!(err.code, -32601);
    }

    // ── POST /mcp/message ───────────────────────────────────────────────────

    #[tokio::test]
    async fn test_mcp_message_unknown_session() {
        let secret = "s";
        let state = McpState::new(secret);
        let app = build_app(state);
        let server = TestServer::new(app);
        let token = make_jwt(secret);
        let auth_value = format!("Bearer {}", token);

        let resp: axum_test::TestResponse = server
            .post("/mcp/message?session_id=does-not-exist")
            .add_header(
                axum::http::header::AUTHORIZATION,
                axum::http::HeaderValue::from_str(&auth_value).unwrap(),
            )
            .json(&json!({
                "jsonrpc": "2.0",
                "id": 1,
                "method": "ping"
            }))
            .await;
        assert_eq!(resp.status_code(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn test_mcp_message_routes_to_session() {
        let secret = "s2";
        let state = McpState::new(secret);

        // Inject a fake session directly into the registry.
        let (tx, mut rx) = mpsc::channel::<SseFrame>(8);
        let session_id = "fake-session-123".to_string();
        {
            let mut sessions = state.sessions.write().await;
            sessions.insert(
                session_id.clone(),
                McpSession {
                    id: session_id.clone(),
                    owner: "test-agent".to_string(),
                    tx,
                },
            );
        }

        let app = build_app(state);
        let server = TestServer::new(app);
        let token = make_jwt(secret);
        let auth_value = format!("Bearer {}", token);

        let resp: axum_test::TestResponse = server
            .post(&format!("/mcp/message?session_id={}", session_id))
            .add_header(
                axum::http::header::AUTHORIZATION,
                axum::http::HeaderValue::from_str(&auth_value).unwrap(),
            )
            .json(&json!({
                "jsonrpc": "2.0",
                "id": 1,
                "method": "ping"
            }))
            .await;

        assert_eq!(resp.status_code(), StatusCode::ACCEPTED);

        // The response should have been sent through the channel as an SseFrame.
        let frame = rx.recv().await.unwrap();
        assert_eq!(frame.event, "message");
        assert!(
            frame.data.contains("pong"),
            "Expected 'pong' in frame data: {}",
            frame.data
        );
    }
}