Skip to content

Commit d7e5c06

Browse files
committed
fix: harden mcp connectivity and install safety
1 parent e1c469e commit d7e5c06

11 files changed

Lines changed: 423 additions & 180 deletions

File tree

daemon-rs/src/hook_boot.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use serde_json::json;
2525
use std::path::PathBuf;
2626

2727
const DEFAULT_BUDGET: u32 = 600;
28-
const DEFAULT_PORT: u16 = 7437;
2928

3029
// ---- Internal types ---------------------------------------------------------
3130

@@ -45,24 +44,27 @@ struct HealthResult {
4544

4645
/// Read the auth token from ~/.cortex/cortex.token for authenticated requests.
4746
fn read_auth_token() -> Option<String> {
48-
let home = std::env::var("USERPROFILE")
49-
.or_else(|_| std::env::var("HOME"))
50-
.ok()?;
51-
let path = PathBuf::from(home).join(".cortex").join("cortex.token");
52-
std::fs::read_to_string(path)
53-
.ok()
54-
.map(|s| s.trim().to_string())
47+
let path = crate::auth::CortexPaths::resolve().token;
48+
match std::fs::read_to_string(path) {
49+
Ok(token) => {
50+
let trimmed = token.trim();
51+
if trimmed.is_empty() {
52+
None
53+
} else {
54+
Some(trimmed.to_string())
55+
}
56+
}
57+
Err(_) => None,
58+
}
5559
}
5660

5761
fn daemon_port() -> u16 {
58-
std::env::var("CORTEX_PORT")
59-
.ok()
60-
.and_then(|v| v.parse::<u16>().ok())
61-
.unwrap_or(DEFAULT_PORT)
62+
crate::auth::CortexPaths::resolve().port
6263
}
6364

6465
async fn fetch_boot(agent: &str, budget: u32, port: u16) -> Option<BootResult> {
6566
let client = reqwest::Client::builder()
67+
.connect_timeout(std::time::Duration::from_secs(3))
6668
.timeout(std::time::Duration::from_secs(7))
6769
.build()
6870
.ok()?;
@@ -90,6 +92,7 @@ async fn fetch_boot(agent: &str, budget: u32, port: u16) -> Option<BootResult> {
9092

9193
async fn fetch_health(port: u16) -> Option<HealthResult> {
9294
let client = reqwest::Client::builder()
95+
.connect_timeout(std::time::Duration::from_secs(3))
9396
.timeout(std::time::Duration::from_secs(2))
9497
.build()
9598
.ok()?;

daemon-rs/src/main.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mod hook_boot;
2929
mod indexer;
3030
mod logging;
3131
mod mcp_proxy;
32+
#[allow(dead_code)]
3233
mod mcp_stdio;
3334
mod prompt_inject;
3435
mod rate_limit;
@@ -80,24 +81,7 @@ async fn main() {
8081
let base_url = format!("http://127.0.0.1:{}", paths.port);
8182
if let Err(e) = mcp_proxy::run(&base_url, None).await {
8283
eprintln!("[cortex-mcp] {e}");
83-
84-
// Legacy fallback: standalone MCP (stdio only, no daemon pretending).
85-
eprintln!("[cortex-mcp] Running standalone -- start the daemon for shared state");
86-
let db_path = paths.db.clone();
87-
eprintln!("[cortex-mcp] DB: {}", db_path.display());
88-
89-
let (mcp_state, _shutdown_rx) =
90-
state::initialize(&db_path, false).expect("Failed to initialize state");
91-
92-
mcp_stdio::run(mcp_state.clone()).await;
93-
eprintln!("[cortex-mcp] MCP session ended.");
94-
95-
let conn = mcp_state.db.lock().await;
96-
if let Err(e) =
97-
conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE); PRAGMA optimize;")
98-
{
99-
eprintln!("[cortex-mcp] Warning: WAL checkpoint failed: {e}");
100-
}
84+
std::process::exit(1);
10185
}
10286
}
10387

@@ -674,7 +658,7 @@ fn print_usage_and_exit(code: i32) -> ! {
674658
eprintln!();
675659
eprintln!("Daemon:");
676660
eprintln!(" serve HTTP daemon on :7437");
677-
eprintln!(" mcp MCP stdio (proxy to daemon, standalone fallback)");
661+
eprintln!(" mcp MCP stdio (proxy to daemon)");
678662
eprintln!(" paths --json Print resolved Cortex paths + port as JSON");
679663
eprintln!(" plugin ensure-daemon [--agent <name>]");
680664
eprintln!(" plugin mcp [--url <base>] [--api-key <key>]");

0 commit comments

Comments
 (0)