Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(mcp,mlx): harden STDIO handshake fallback and MLX error decoding
Akarshan Biswasa5d6d2ed4b3a
diff --git a/src-tauri/src/core/mcp/helpers.rs b/src-tauri/src/core/mcp/helpers.rsindex 2f987d1cd..558619176 100644--- a/src-tauri/src/core/mcp/helpers.rs+++ b/src-tauri/src/core/mcp/helpers.rs@@ -559,120 +559,144 @@ async fn schedule_mcp_start_task<R: Runtime>(}}- let mut cmd = Command::new(config_params.command.clone());let bun_x_path = if cfg!(windows) {bin_path.join("bun.exe")} else {bin_path.join("bun")};- if config_params.command.clone() == "npx"- && can_override_npx(bun_x_path.display().to_string())- {- let mut cache_dir = app_path.clone();- cache_dir.push(".npx");- cmd = Command::new(bun_x_path.display().to_string());- cmd.arg("x");- cmd.env("BUN_INSTALL", cache_dir.to_str().unwrap());- }-let uv_path = if cfg!(windows) {bin_path.join("uv.exe")} else {bin_path.join("uv")};- if config_params.command.clone() == "uvx" && can_override_uvx(uv_path.display().to_string())- {- let mut cache_dir = app_path.clone();- cache_dir.push(".uvx");- cmd = Command::new(uv_path);- cmd.arg("tool");- cmd.arg("run");- cmd.env("UV_CACHE_DIR", cache_dir.to_str().unwrap());- }- #[cfg(windows)]- {- cmd.creation_flags(0x08000000); // CREATE_NO_WINDOW: prevents shell window on Windows- }-- cmd.kill_on_drop(true);- config_params- .args- .iter()- .filter_map(Value::as_str)- .for_each(|arg| {- cmd.arg(arg);+ // Whether the configured command would be rewritten to the bundled+ // `bun x` / `uv tool run`. If the override-based handshake fails we fall+ // back to the system `npx`/`uvx`, whose stdio plumbing differs and is+ // what most published MCP servers are tested against.+ let override_available = (config_params.command == "npx"+ && can_override_npx(bun_x_path.display().to_string()))+ || (config_params.command == "uvx"+ && can_override_uvx(uv_path.display().to_string()));++ let build_cmd = |use_override: bool| -> Command {+ let mut cmd = Command::new(config_params.command.clone());+ if use_override+ && config_params.command == "npx"+ && can_override_npx(bun_x_path.display().to_string())+ {+ let mut cache_dir = app_path.clone();+ cache_dir.push(".npx");+ cmd = Command::new(bun_x_path.display().to_string());+ cmd.arg("x");+ cmd.env("BUN_INSTALL", cache_dir.to_str().unwrap());+ }+ if use_override+ && config_params.command == "uvx"+ && can_override_uvx(uv_path.display().to_string())+ {+ let mut cache_dir = app_path.clone();+ cache_dir.push(".uvx");+ cmd = Command::new(uv_path.clone());+ cmd.arg("tool");+ cmd.arg("run");+ cmd.env("UV_CACHE_DIR", cache_dir.to_str().unwrap());+ }+ #[cfg(windows)]+ {+ cmd.creation_flags(0x08000000); // CREATE_NO_WINDOW: prevents shell window on Windows+ }+ cmd.kill_on_drop(true);+ config_params+ .args+ .iter()+ .filter_map(Value::as_str)+ .for_each(|arg| {+ cmd.arg(arg);+ });+ config_params.envs.iter().for_each(|(k, v)| {+ if let Some(v_str) = v.as_str() {+ cmd.env(k, v_str);+ }});- config_params.envs.iter().for_each(|(k, v)| {- if let Some(v_str) = v.as_str() {- cmd.env(k, v_str);+ cmd+ };++ let mut use_override = true;+ let (server, stderr) = loop {+ let (process, stderr) = TokioChildProcess::builder(build_cmd(use_override))+ .stderr(Stdio::piped())+ .spawn()+ .map_err(|e| {+ log::error!("Failed to run command {name}: {e}");+ format!("Failed to run command {name}: {e}")+ })?;++ if let Some(pid) = process.id() {+ log::info!("MCP server {name} spawned with PID {pid}");+ let app_state = app.state::<AppState>();+ let mut pids = app_state.mcp_server_pids.lock().await;+ pids.insert(name.clone(), pid);}- });- let (process, stderr) = TokioChildProcess::builder(cmd)- .stderr(Stdio::piped())- .spawn()- .map_err(|e| {- log::error!("Failed to run command {name}: {e}");- format!("Failed to run command {name}: {e}")- })?;-- let process_pid = process.id();- if let Some(pid) = process_pid {- log::info!("MCP server {name} spawned with PID {pid}");- let app_state = app.state::<AppState>();- let mut pids = app_state.mcp_server_pids.lock().await;- pids.insert(name.clone(), pid);- }+ match ().serve(process).await {+ Ok(server) => break (server, stderr),+ Err(e) => {+ // The child often crashes here with a write EPIPE while+ // replying to `initialize` once its pipes are torn down;+ // capture its stderr so we report that instead of an opaque+ // serve() error.+ let mut buffer = String::new();+ if let Some(mut stderr_stream) = stderr {+ let _ = stderr_stream.read_to_string(&mut buffer).await;+ }- let service = ()- .serve(process)- .await- .map_err(|e| format!("Failed to start MCP server {name}: {e}"));-- match service {- Ok(server) => {- // Keep the stderr pipe alive to prevent the child process from- // receiving SIGPIPE and to capture diagnostic output.- if let Some(mut stderr_stream) = stderr {- let stderr_name = name.clone();- tokio::spawn(async move {- let mut buf = [0u8; 1024];- while let Ok(n) = stderr_stream.read(&mut buf).await {- if n == 0 {- break;- }- if let Ok(text) = std::str::from_utf8(&buf[..n]) {- for line in text.lines() {- if !line.trim().is_empty() {- log_mcp_stderr_line(&stderr_name, line);- }- }+ if use_override && override_available {+ log::warn!(+ "MCP server {name} failed to start via bundled bun/uv override ({e}); retrying with system {}. stderr: {buffer}",+ config_params.command+ );+ use_override = false;+ continue;+ }++ let error = if buffer.trim().is_empty() {+ format!("Failed to start MCP server {name}: {e}")+ } else {+ format!("Failed to start MCP server {name}: {buffer}")+ };+ log::error!("{error}");+ return Err(error);+ }+ }+ };++ // Keep the stderr pipe alive to prevent the child process from+ // receiving SIGPIPE and to capture diagnostic output.+ if let Some(mut stderr_stream) = stderr {+ let stderr_name = name.clone();+ tokio::spawn(async move {+ let mut buf = [0u8; 1024];+ while let Ok(n) = stderr_stream.read(&mut buf).await {+ if n == 0 {+ break;+ }+ if let Ok(text) = std::str::from_utf8(&buf[..n]) {+ for line in text.lines() {+ if !line.trim().is_empty() {+ log_mcp_stderr_line(&stderr_name, line);}}- });+ }}- log::trace!("Connected to server: {:#?}", server.peer_info());- servers- .lock()- .await- .insert(name.clone(), RunningServiceEnum::NoInit(server));- log::info!("Server {name} started successfully.");- }- Err(_) => {- let mut buffer = String::new();- let error = match stderr- .expect("stderr must be piped")- .read_to_string(&mut buffer)- .await- {- Ok(_) => format!("Failed to start MCP server {name}: {buffer}"),- Err(_) => format!("Failed to read MCP server {name} stderr"),- };- log::error!("{error}");- return Err(error);- }+ });}+ log::trace!("Connected to server: {:#?}", server.peer_info());+ servers+ .lock()+ .await+ .insert(name.clone(), RunningServiceEnum::NoInit(server));+ log::info!("Server {name} started successfully.");// Wait a short time to verify the server is stable before marking as connected// This prevents race conditions where the server quits immediatelydiff --git a/web-app/src/lib/model-factory.ts b/web-app/src/lib/model-factory.tsindex f4709e654..635aa67c7 100644--- a/web-app/src/lib/model-factory.ts+++ b/web-app/src/lib/model-factory.ts@@ -868,28 +868,17 @@ export class ModelFactory {Origin: 'tauri://localhost',}- // Custom fetch that merges parameters and calls /cancel on abort- const customFetch: typeof httpFetch = async (+ // Share the common fetch (param normalisation + error-body cleaning that+ // rebuilds upstream errors from buffered text rather than re-decoding the+ // raw stream) with every other provider, then layer MLX's /cancel-on-abort+ // on top.+ const baseCustomFetch = createCustomFetch(httpFetch, parameters)+ const customFetch: typeof globalThis.fetch = async (input: RequestInfo | URL,init?: RequestInit): Promise<Response> => {- if (init?.method === 'POST' || !init?.method) {- let body: Record<string, unknown> = {}- if (init?.body) {- try {- body = JSON.parse(init.body as string)- } catch (e) {- throw new Error(- `Failed to parse MLX request body as JSON: ${e instanceof Error ? e.message : String(e)}`- )- }- }- const mergedBody = { ...body, ...parameters }- init = { ...init, body: JSON.stringify(mergedBody) }- }-// When the request is aborted, also call the server's /cancel endpoint- // to stop MLX inference immediately+ // to stop MLX inference immediately.if (init?.signal) {init.signal.addEventListener('abort', () => {httpFetch(`${baseUrl}/v1/cancel`, {@@ -902,7 +891,7 @@ export class ModelFactory {})}- return httpFetch(input, init)+ return baseCustomFetch(input, init)}const model = new OpenAICompatibleChatLanguageModel(modelId, {