Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(mcp): guard against duplicate serve() on streamable-http MCP servers (#8435)
Thinh Le76573848f7d1
diff --git a/src-tauri/src/core/mcp/helpers.rs b/src-tauri/src/core/mcp/helpers.rsindex 734bd8432..eb52e8a47 100644--- a/src-tauri/src/core/mcp/helpers.rs+++ b/src-tauri/src/core/mcp/helpers.rs@@ -330,6 +330,20 @@ pub async fn start_mcp_server<R: Runtime>(let app_state = app.state::<AppState>();let active_servers_state = app_state.mcp_active_servers.clone();+ // Idempotency guard: never serve() the same server twice. Two serve() calls+ // spin up two independent clients that each send an `initialize` (both with+ // request id 0); the second is rejected by streamable-http servers with a+ // 400, tearing down the connection (issue #8411). This can happen when boot+ // startup and a frontend activation fire for the same server.+ if servers_state.lock().await.contains_key(&name) {+ log::debug!("MCP server {name} already running; skipping duplicate start");+ return Ok(());+ }+ if !app_state.mcp_starting.lock().await.insert(name.clone()) {+ log::debug!("MCP server {name} start already in progress; skipping duplicate start");+ return Ok(());+ }+// Store active server config for restart purposesstore_active_server_config(&active_servers_state, &name, &config).await;@@ -343,6 +357,10 @@ pub async fn start_mcp_server<R: Runtime>().await;+ // Start attempt finished (success or failure) — clear the in-flight marker so+ // future (re)activations aren't blocked.+ app_state.mcp_starting.lock().await.remove(&name);+match first_start_result {Ok(_) => {log::info!("MCP server {name} started successfully");diff --git a/src-tauri/src/core/state.rs b/src-tauri/src/core/state.rsindex 8915e05c0..fa9061bbb 100644--- a/src-tauri/src/core/state.rs+++ b/src-tauri/src/core/state.rs@@ -1,4 +1,7 @@-use std::{collections::HashMap, sync::Arc};+use std::{+ collections::{HashMap, HashSet},+ sync::Arc,+};use crate::core::{downloads::models::DownloadManagerState,@@ -67,6 +70,10 @@ pub struct AppState {pub mcp_settings: Arc<Mutex<McpSettings>>,pub mcp_shutdown_in_progress: Arc<Mutex<bool>>,pub mcp_monitoring_tasks: Arc<Mutex<HashMap<String, tauri::async_runtime::JoinHandle<()>>>>,+ /// Names of MCP servers whose initial start is currently in flight. Guards+ /// against a server being `serve()`'d twice (e.g. boot startup racing a+ /// frontend activation), which sends duplicate `initialize` requests.+ pub mcp_starting: Arc<Mutex<HashSet<String>>>,pub background_cleanup_handle: Arc<Mutex<Option<tokio::task::JoinHandle<()>>>>,pub mcp_server_pids: Arc<Mutex<HashMap<String, u32>>>,/// Remote provider configurations (e.g., Anthropic, OpenAI, etc.)@@ -97,6 +104,7 @@ impl Default for AppState {mcp_settings: Default::default(),mcp_shutdown_in_progress: Default::default(),mcp_monitoring_tasks: Default::default(),+ mcp_starting: Default::default(),background_cleanup_handle: Default::default(),mcp_server_pids: Default::default(),provider_configs: Default::default(),diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rsindex 14c7f04fa..8b4adac6c 100644--- a/src-tauri/src/lib.rs+++ b/src-tauri/src/lib.rs@@ -12,7 +12,10 @@ use core::{#[cfg(not(feature = "cli"))]use jan_utils::generate_app_token;#[cfg(not(feature = "cli"))]-use std::{collections::HashMap, sync::Arc};+use std::{+ collections::{HashMap, HashSet},+ sync::Arc,+};#[cfg(not(feature = "cli"))]use tauri::{Emitter, Manager, RunEvent};#[cfg(not(feature = "cli"))]@@ -272,6 +275,7 @@ pub fn run() {mcp_settings: Arc::new(Mutex::new(McpSettings::default())),mcp_shutdown_in_progress: Arc::new(Mutex::new(false)),mcp_monitoring_tasks: Arc::new(Mutex::new(HashMap::new())),+ mcp_starting: Arc::new(Mutex::new(HashSet::new())),background_cleanup_handle: Arc::new(Mutex::new(None)),mcp_server_pids: Arc::new(Mutex::new(HashMap::new())),provider_configs: Arc::new(Mutex::new(HashMap::new())),