microscope-memory 1.2.0

Pure binary cognitive memory engine with REST API bridge for LLM integration. Zero-JSON core, mmap-based, hierarchical, sub-microsecond search.
Documentation
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
//! Streamable HTTP MCP server for Microscope Memory.
//!
//! Implements the MCP Streamable HTTP transport (MCP spec
//! [`2026-07-28`](https://modelcontextprotocol.io/specification/2026-07-28/basic/transports),
//! backwards-compatible with `2025-11-25` and `2024-11-05`) for remote,
//! multi-client access to the cognitive memory engine.
//!
//! Security:
//! - **API key auth** — when `[server].api_key` is set, every request must
//!   carry a valid key via `x-api-key` or `Authorization: Bearer <key>`.
//! - **Origin validation** — when `[server].cors_origin` is set, cross-origin
//!   requests whose `Origin` header does not match are rejected (DNS-rebinding
//!   protection).
//! - **Session management** — `Mcp-Session-Id` header handling via
//!   `LocalSessionManager` for stateful and stateless operation.
//!
//! Not available on WASM targets.  Enable with `--features mcp_http`.

use std::net::SocketAddr;

use axum::body::Body;
use axum::extract::{Request, State};
use axum::http::header::{AUTHORIZATION, ORIGIN};
use axum::http::{HeaderMap, HeaderName, HeaderValue, Method, StatusCode};
use axum::middleware::{from_fn_with_state, Next};
use axum::response::Response;
use axum::routing::get;
use axum::Router;
use rmcp::handler::server::wrapper::Parameters;
use rmcp::transport::streamable_http_server::{
    session::local::LocalSessionManager, StreamableHttpServerConfig, StreamableHttpService,
};
use rmcp::{tool, tool_handler, tool_router, ErrorData as McpError, ServerHandler};
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::json;
use tower_http::cors::{Any, CorsLayer};
use tower_http::trace::TraceLayer;

use crate::config::Config;
use crate::reader::{MicroscopeReader, EMOTION_VECTOR_SIZE};
use crate::{auto_zoom, read_append_log, store_memory, store_memory_with_emotion, LAYER_NAMES};

// ─── Auth / security state ─────────────────────────────────────────────

/// Shared state for the auth middleware.
#[derive(Clone)]
pub struct AuthState {
    /// If set, requests must carry this key via `x-api-key` or
    /// `Authorization: Bearer <key>`.
    pub api_key: Option<String>,
    /// If set, only this origin is allowed (CORS + Origin header validation).
    pub cors_origin: Option<String>,
}

impl From<&Config> for AuthState {
    fn from(config: &Config) -> Self {
        Self {
            api_key: config.server.api_key.clone(),
            cors_origin: config.server.cors_origin.clone(),
        }
    }
}

/// Auth + Origin-validation middleware (DNS-rebinding protection).
///
/// Applied to every request before it reaches the MCP streamable-HTTP handler.
async fn auth_middleware(State(state): State<AuthState>, req: Request, next: Next) -> Response {
    let headers: &HeaderMap = req.headers();

    // ── API key check (only if configured) ──
    if let Some(ref expected) = state.api_key {
        let provided = headers
            .get("x-api-key")
            .or(headers.get(AUTHORIZATION))
            .and_then(|v| v.to_str().ok())
            .map(|s| {
                if s.starts_with("Bearer ") {
                    s[7..].to_owned()
                } else {
                    s.to_owned()
                }
            });

        if provided.as_deref() != Some(expected.as_str()) {
            let mut resp = Response::new(Body::from("Missing or invalid API key\n"));
            *resp.status_mut() = StatusCode::UNAUTHORIZED;
            return resp;
        }
    }

    // ── Origin validation (DNS-rebinding protection) ──
    // When `cors_origin` is configured we reject any cross-origin request
    // whose `Origin` does not match.  Non-browser clients that don't send
    // `Origin` are always allowed (typical for MCP clients).
    if let Some(ref allowed) = state.cors_origin {
        if let Some(origin) = headers.get(ORIGIN) {
            let origin_str = origin.to_str().unwrap_or("");
            if origin_str != allowed.as_str() {
                let mut resp = Response::new(Body::from("Origin not allowed\n"));
                *resp.status_mut() = StatusCode::FORBIDDEN;
                return resp;
            }
        }
    }

    next.run(req).await
}

// ─── Tool input schemas ──────────────────────────────────────────────────

/// Parameters for `memory_store`.
#[derive(Debug, Deserialize, JsonSchema)]
struct StoreParams {
    /// Memory text to store.
    text: String,
    /// Memory layer (long_term, short_term, session, associative, emotional,
    /// relational, reflections, echo_cache, etc.).
    #[serde(default = "default_layer")]
    layer: String,
    /// Importance level 1-10.
    #[serde(default = "default_importance", rename = "importance")]
    importance: u8,
    /// Optional 21D emotion vector
    /// `[joy, sadness, anger, fear, surprise, disgust, trust, anticipation,
    /// love, gratitude, curiosity, awe, confusion, anxiety, serenity, hope,
    /// pride, shame, guilt, empathy, excitement]`.
    emotion: Option<Vec<f32>>,
}

/// Parameters for `memory_recall`.
#[allow(dead_code)]
#[derive(Debug, Deserialize, JsonSchema)]
struct RecallParams {
    /// Natural-language query.
    query: String,
    /// Max results to return.
    #[serde(default = "default_k")]
    k: usize,
    /// Optional 21D emotion vector to bias results toward emotionally
    /// similar memories.
    emotion: Option<Vec<f32>>,
}

/// Parameters for `memory_find`.
#[derive(Debug, Deserialize, JsonSchema)]
struct FindParams {
    /// Text to search for.
    query: String,
    /// Max results.
    #[serde(default = "default_k")]
    k: usize,
}

/// Parameters for `memory_look`.
#[derive(Debug, Deserialize, JsonSchema)]
struct LookParams {
    /// X coordinate (0.0-1.0).
    x: f32,
    /// Y coordinate (0.0-1.0).
    y: f32,
    /// Z coordinate (0.0-1.0).
    z: f32,
    /// Depth level / zoom (0-8).
    zoom: u8,
    /// Max results.
    #[serde(default = "default_k")]
    k: usize,
}

/// Parameters for `memory_mql_query`.
#[derive(Debug, Deserialize, JsonSchema)]
struct MqlParams {
    /// MQL expression, e.g. `layer:long_term depth:2..5 "memory"`.
    mql: String,
}

/// Parameters for `memory_session_log`.
#[derive(Debug, Deserialize, JsonSchema)]
struct SessionLogParams {
    /// Number of recent interactions to return.
    #[serde(default = "default_n")]
    n: usize,
}

/// Parameters for `memory_build`.
#[derive(Debug, Deserialize, JsonSchema)]
struct BuildParams {
    /// Force rebuild even if unchanged.
    #[serde(default)]
    force: bool,
}

// ─── Parameter defaults ──────────────────────────────────────────────────

fn default_layer() -> String {
    "long_term".to_string()
}
fn default_importance() -> u8 {
    5
}
fn default_k() -> usize {
    10
}
fn default_n() -> usize {
    50
}

// ─── The MCP tool service ───────────────────────────────────────────────

/// Microscope Memory MCP service — exposes the cognitive memory engine's
/// core operations as MCP tools over Streamable HTTP.
#[derive(Clone)]
pub struct MicroscopeService {
    config: Config,
}

impl MicroscopeService {
    pub fn new(config: Config) -> Self {
        Self { config }
    }

    /// Convenience: open (or reopen) a `MicroscopeReader` from the stored
    /// config.  Tools that need the index call this rather than caching a
    /// reader, because the index may change between requests.
    fn open_reader(&self) -> Result<MicroscopeReader, McpError> {
        MicroscopeReader::open(&self.config).map_err(|e| McpError::internal_error(e, None))
    }

    /// Append-log path derived from config.
    fn append_path(&self) -> std::path::PathBuf {
        std::path::Path::new(&self.config.paths.output_dir).join("append.bin")
    }
}

#[tool_router]
impl MicroscopeService {
    /// Get memory index status: block counts, depths, layer sizes, append-log
    /// entry count.
    #[tool(
        description = "Get microscope memory index status: block count, depths, append log size"
    )]
    fn memory_status(&self) -> Result<String, McpError> {
        let reader = self.open_reader()?;
        let append_path = self.append_path();

        let append_count = if append_path.exists() {
            read_append_log(&append_path).len()
        } else {
            0
        };

        let depth_summary: Vec<_> = (0..9)
            .map(|z| {
                let (start, count) = reader.depth_ranges[z as usize];
                (z as u8, start as usize, count as usize)
            })
            .collect();

        let result = json!({
            "block_count": reader.block_count,
            "depths": depth_summary,
            "append_log_entries": append_count,
            "layers": LAYER_NAMES,
            "version": env!("CARGO_PKG_VERSION"),
        });

        Ok(serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string()))
    }

    /// Store a new memory into the microscope append log.
    #[tool(description = "Store a new memory into the microscope append log")]
    fn memory_store(
        &self,
        Parameters(StoreParams {
            text,
            layer,
            importance,
            emotion,
        }): Parameters<StoreParams>,
    ) -> Result<String, McpError> {
        // Clamp importance to [1, 10]
        let importance = importance.clamp(1, 10);

        if let Some(emo) = &emotion {
            if emo.len() != EMOTION_VECTOR_SIZE {
                return Err(McpError::invalid_params(
                    format!(
                        "emotion vector must have {} elements, got {}",
                        EMOTION_VECTOR_SIZE,
                        emo.len()
                    ),
                    None,
                ));
            }
            let arr: [f32; 21] = emo
                .iter()
                .cloned()
                .chain(std::iter::repeat(0.0))
                .take(21)
                .collect::<Vec<_>>()
                .try_into()
                .map_err(|_| McpError::internal_error("emotion vector conversion failed", None))?;

            store_memory_with_emotion(&self.config, &text, &layer, importance, Some(arr))
                .map_err(|e| McpError::internal_error(e, None))?;
        } else {
            store_memory(&self.config, &text, &layer, importance)
                .map_err(|e| McpError::internal_error(e, None))?;
        }

        Ok(format!(
            "Stored memory into '{}' layer (importance={})",
            layer, importance,
        ))
    }

    /// Natural-language recall with auto-zoom — searches both main index
    /// and append log.  Optional emotion vector biases results toward
    /// emotionally similar memories.
    #[tool(description = "Natural language recall with auto-zoom")]
    fn memory_recall(
        &self,
        Parameters(RecallParams {
            query,
            k,
            emotion: _,
        }): Parameters<RecallParams>,
    ) -> Result<String, McpError> {
        let mut reader = self.open_reader()?;

        // Auto-zoom: determine depth range from query complexity.
        let (min_depth, max_depth) = auto_zoom(&query);

        // Use the full-ranked search across index + append log.
        let ranked = reader.find_text_all_ranked(&self.config, &query, k);

        let mut results: Vec<serde_json::Value> = Vec::new();

        for (depth, idx, is_main, score) in ranked {
            let text = if is_main {
                reader.text(idx).to_string()
            } else {
                let append_path = self.append_path();
                let appended = read_append_log(&append_path);
                if idx < appended.len() {
                    appended[idx].text.clone()
                } else {
                    "<unknown>".to_string()
                }
            };

            let layer_id = if is_main {
                reader.header(idx).layer_id
            } else {
                let append_path = self.append_path();
                let appended = read_append_log(&append_path);
                appended.get(idx).map(|e| e.layer_id).unwrap_or(0)
            };

            let layer_name = LAYER_NAMES.get(layer_id as usize).unwrap_or(&"?");

            results.push(json!({
                "text": text,
                "depth": depth,
                "layer": layer_name,
                "score": score,
                "source": if is_main { "index" } else { "append_log" },
                "index": idx,
            }));
        }

        let result = json!({
            "query": query,
            "k": k,
            "min_depth": min_depth,
            "max_depth": max_depth,
            "results": results,
            "count": results.len(),
        });

        Ok(serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string()))
    }

    /// Brute-force text search across all depths (index + append log).
    #[tool(description = "Text search across all depths")]
    fn memory_find(
        &self,
        Parameters(FindParams { query, k }): Parameters<FindParams>,
    ) -> Result<String, McpError> {
        let reader = self.open_reader()?;

        let results = reader.find_text_all(&self.config, &query, k);

        let mut output: Vec<serde_json::Value> = Vec::new();
        for (depth, idx, is_main) in results {
            let text = if is_main {
                let t = reader.text(idx);
                t.to_string()
            } else {
                let append_path = self.append_path();
                let appended = read_append_log(&append_path);
                appended
                    .get(idx)
                    .map(|e| e.text.clone())
                    .unwrap_or_default()
            };

            let layer_id = if is_main {
                reader.header(idx).layer_id
            } else {
                let append_path = self.append_path();
                let appended = read_append_log(&append_path);
                appended.get(idx).map(|e| e.layer_id).unwrap_or(0)
            };

            let layer_name = LAYER_NAMES.get(layer_id as usize).unwrap_or(&"?");

            output.push(json!({
                "text": text,
                "depth": depth,
                "layer": layer_name,
                "source": if is_main { "index" } else { "append_log" },
                "index": idx,
            }));
        }

        let result = json!({
            "query": query,
            "k": k,
            "results": output,
            "count": output.len(),
        });

        Ok(serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string()))
    }

    /// Manual spatial look at specific 3D coordinates and zoom level.
    #[tool(description = "Manual spatial look at specific 3D coordinates and zoom level")]
    fn memory_look(
        &self,
        Parameters(LookParams { x, y, z, zoom, k }): Parameters<LookParams>,
    ) -> Result<String, McpError> {
        let reader = self.open_reader()?;
        let zoom = zoom.min(8);

        let results = reader.look(&self.config, x, y, z, zoom, k);

        let mut output: Vec<serde_json::Value> = Vec::new();
        for (dist_sq, idx, is_main) in results {
            let (text, layer_name) = if is_main {
                let t = reader.text(idx);
                let lid = reader.header(idx).layer_id;
                let name = LAYER_NAMES.get(lid as usize).unwrap_or(&"?");
                (t.to_string(), name.to_string())
            } else {
                let append_path = self.append_path();
                let appended = read_append_log(&append_path);
                let entry = appended.get(idx).unwrap();
                let name = LAYER_NAMES.get(entry.layer_id as usize).unwrap_or(&"?");
                (entry.text.clone(), name.to_string())
            };

            output.push(json!({
                "text": text,
                "dist_sq": dist_sq,
                "depth": zoom,
                "layer": layer_name,
                "source": if is_main { "index" } else { "append_log" },
            }));
        }

        let result = json!({
            "x": x,
            "y": y,
            "z": z,
            "zoom": zoom,
            "k": k,
            "results": output,
            "count": output.len(),
        });

        Ok(serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string()))
    }

    /// Execute an MQL (Microscope Query Language) query with filters.
    #[tool(description = "Execute an MQL query")]
    fn memory_mql_query(
        &self,
        Parameters(MqlParams { mql }): Parameters<MqlParams>,
    ) -> Result<String, McpError> {
        let reader = self.open_reader()?;
        let append_path = self.append_path();
        let appended = read_append_log(&append_path);

        let q = crate::query::parse(&mql);
        let qr = crate::query::execute(&q, &reader, &appended);

        let mut results: Vec<serde_json::Value> = Vec::new();
        for r in &qr {
            let text = if r.is_main {
                reader.text(r.block_idx).to_string()
            } else {
                appended
                    .get(r.block_idx)
                    .map(|e| e.text.clone())
                    .unwrap_or_default()
            };

            let layer_id = if r.is_main {
                reader.header(r.block_idx).layer_id
            } else {
                appended.get(r.block_idx).map(|e| e.layer_id).unwrap_or(0)
            };

            let layer_name = LAYER_NAMES.get(layer_id as usize).unwrap_or(&"?");

            results.push(json!({
                "text": text,
                "score": r.score,
                "layer": layer_name,
                "source": if r.is_main { "index" } else { "append_log" },
            }));
        }

        let result = json!({
            "mql": mql,
            "results": results,
            "count": results.len(),
        });

        Ok(serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string()))
    }

    /// Read last N interactions from the session memory layer.
    #[tool(description = "Read last N interactions from the session memory layer")]
    fn memory_session_log(
        &self,
        Parameters(SessionLogParams { n }): Parameters<SessionLogParams>,
    ) -> Result<String, McpError> {
        let session_path = std::path::Path::new(&self.config.paths.layers_dir).join("session.txt");

        if !session_path.exists() {
            return Ok(
                json!({ "entries": [], "count": 0, "session_path": session_path }).to_string(),
            );
        }

        let content = std::fs::read_to_string(&session_path)
            .map_err(|e| McpError::internal_error(e.to_string(), None))?;

        let lines: Vec<&str> = content.lines().rev().take(n).collect();

        let result = json!({
            "entries": lines,
            "count": lines.len(),
            "session_path": session_path,
        });

        Ok(serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string()))
    }

    /// Ping — health check that returns basic server info.
    #[tool(description = "Ping the MCP server")]
    fn memory_ping(&self) -> String {
        json!({
            "status": "ok",
            "server": "microscope-memory",
            "version": env!("CARGO_PKG_VERSION"),
            "protocol_version": "2025-11-25",
            "layers": LAYER_NAMES.len(),
            "max_depth": 8,
        })
        .to_string()
    }

    /// Rebuild the microscope index from layer source files.
    #[tool(description = "Rebuild the microscope index from layer source files")]
    fn memory_build(
        &self,
        Parameters(BuildParams { force }): Parameters<BuildParams>,
    ) -> Result<String, McpError> {
        let _ = force; // Fresh open validates the index; a real rebuild
                       // is a CLI-only operation (`microscope-mem build`).
        match self.open_reader() {
            Ok(reader) => Ok(json!({
                "status": "ok",
                "message": "Index opened and verified successfully",
                "block_count": reader.block_count,
            })
            .to_string()),
            Err(e) => Err(e),
        }
    }
}

/// Server metadata — name, version, instructions.  Capabilities (tools)
/// are enabled automatically by `#[tool_router]`.
///
/// NOTE: `version` must be a string literal (proc-macro limitation),
/// so keep it in sync with `Cargo.toml` when bumping.
#[tool_handler(
    name = "microscope-memory",
    version = "1.2.0",
    instructions = "Microscope Memory — cognitive memory engine with 12 memory layers and 9 hierarchical depths (D0-D8). Exposes tools for storing, recalling, and querying memories via natural language, MQL, and 3D spatial coordinates. Built on a binary mmap store with sub-millisecond recall."
)]
impl ServerHandler for MicroscopeService {}

// ─── HTTP server entry point ────────────────────────────────────────────

/// Start the Streamable HTTP MCP server.
///
/// # Arguments
/// * `config` — Microscope Memory configuration (API key and CORS origin
///   are read from `[server]`).
/// * `host` — Bind address (e.g. `"127.0.0.1"` or `"0.0.0.0"`).
/// * `port` — TCP port.
///
/// # Security
/// When `[server].api_key` is set, clients must send it via
/// `x-api-key` or `Authorization: Bearer <key>`.  When
/// `[server].cors_origin` is set, cross-origin requests whose `Origin`
/// doesn't match are rejected.
pub async fn serve_http(
    config: Config,
    host: &str,
    port: u16,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let auth_state = AuthState::from(&config);

    // Streamable HTTP server config — modern clients are stateless by
    // default; we keep session support for legacy clients and enable
    // JSON response mode for simple tool calls.
    let http_config = StreamableHttpServerConfig::default().with_json_response(true);

    // Factory: a fresh MicroscopeService per request (the Config is cheaply
    // cloned — it is a small struct of strings, bools, and numbers).
    let config_clone = config.clone();
    let mcp_service = StreamableHttpService::new(
        move || Ok(MicroscopeService::new(config_clone.clone())),
        LocalSessionManager::default().into(),
        http_config,
    );

    // CORS layer — expose the MCP-specific headers.
    let cors = {
        let mut builder = CorsLayer::new()
            .allow_methods([Method::GET, Method::POST, Method::DELETE])
            .allow_headers(Any)
            .expose_headers([
                HeaderName::from_static("mcp-session-id"),
                HeaderName::from_static("mcp-protocol-version"),
                HeaderName::from_static("mcp-supported-protocol-versions"),
            ])
            .allow_credentials(true);

        if let Some(ref origin) = auth_state.cors_origin {
            builder = builder.allow_origin(origin.parse::<HeaderValue>()?);
        } else {
            builder = builder.allow_origin(Any);
        }

        builder
    };

    // Build the axum router.
    let mut router = Router::new()
        .route(
            "/",
            get(|| async { "Microscope Memory MCP (Streamable HTTP) — POST to /mcp" }),
        )
        .nest_service("/mcp", mcp_service)
        .route_layer(from_fn_with_state(auth_state.clone(), auth_middleware));

    // Add CORS + tracing as outer layers.
    router = router.layer(cors).layer(TraceLayer::new_for_http());

    let addr = format!("{}:{}", host, port);
    let socket_addr: SocketAddr = addr.parse()?;

    eprintln!(
        "[mcp_http] Streamable HTTP MCP server listening on {}",
        addr
    );

    if auth_state.api_key.is_some() {
        eprintln!("[mcp_http] API key authentication enabled (x-api-key / Authorization: Bearer)");
    } else {
        eprintln!("[mcp_http] No API key configured — allowing unauthenticated access");
    }

    if let Some(ref origin) = auth_state.cors_origin {
        eprintln!("[mcp_http] Origin validation: only '{}' allowed", origin);
    }

    axum::serve(
        tokio::net::TcpListener::bind(socket_addr).await?,
        router.into_make_service(),
    )
    .await?;

    Ok(())
}