Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

46.2 KB
Raw Blame
use rmcp::{
    model::{ClientCapabilities, ClientInfo, Implementation},
    transport::{
        streamable_http_client::StreamableHttpClient,
        streamable_http_client::StreamableHttpClientTransportConfig, StreamableHttpClientTransport,
        TokioChildProcess,
    },
    ServiceExt,
};
use serde_json::Value;
use std::{collections::HashMap, env, process::Stdio, sync::Arc, time::Duration};
use tauri::{AppHandle, Emitter, Manager, Runtime, State};
use tokio::{
    io::AsyncReadExt,
    process::Command,
    sync::{Mutex, Notify},
    time::{sleep, timeout},
};

use crate::core::{
    app::commands::get_jan_data_folder_path,
    mcp::models::{extract_active_status, extract_command_args, McpSettings},
    mcp::identity,
    mcp::oauth,
    mcp::progress::JanClientHandler,
    state::{AppState, RunningMcpService, SharedMcpServers},
};
use jan_utils::{can_override_npx, can_override_uvx};

#[derive(Debug, Clone, Copy)]
pub enum ShutdownContext {
    AppExit,       // User closing app - be fast
    ManualRestart, // User restarting servers - be thorough
    FactoryReset,  // Deleting data - be very thorough
}

impl ShutdownContext {
    pub fn per_server_timeout(&self) -> Duration {
        match self {
            Self::AppExit => Duration::from_millis(500),
            Self::ManualRestart => Duration::from_secs(2),
            Self::FactoryReset => Duration::from_secs(5),
        }
    }

    pub fn overall_timeout(&self) -> Duration {
        match self {
            Self::AppExit => Duration::from_millis(1500),
            Self::ManualRestart => Duration::from_secs(5),
            Self::FactoryReset => Duration::from_secs(10),
        }
    }
}

/// Runs MCP commands by reading configuration from a JSON file and initializing servers
///
/// # Arguments
/// * `app_path` - Path to the application directory containing mcp_config.json
/// * `servers_state` - Shared state containing running MCP services
///
/// # Returns
/// * `Ok(())` if servers were initialized successfully
/// * `Err(String)` if there was an error reading config or starting servers
pub async fn run_mcp_commands<R: Runtime>(
    app: &AppHandle<R>,
    servers_state: SharedMcpServers,
) -> Result<(), String> {
    let app_path = get_jan_data_folder_path(app.clone());
    let app_path_str = app_path.to_str().unwrap().to_string();
    log::trace!(
        "Load MCP configs from {}",
        app_path_str.clone() + "/mcp_config.json"
    );
    let config_content = std::fs::read_to_string(app_path_str + "/mcp_config.json")
        .map_err(|e| format!("Failed to read config file: {e}"))?;

    let mcp_servers: serde_json::Value = serde_json::from_str(&config_content)
        .map_err(|e| format!("Failed to parse config: {e}"))?;

    // Update runtime MCP settings from config
    {
        let settings = mcp_servers
            .get("mcpSettings")
            .and_then(|value| serde_json::from_value::<McpSettings>(value.clone()).ok())
            .unwrap_or_default();

        let app_state = app.state::<AppState>();
        let mut guard = app_state.mcp_settings.lock().await;
        *guard = settings;
    }

    let server_map = mcp_servers
        .get("mcpServers")
        .and_then(Value::as_object)
        .ok_or("No mcpServers found in config")?;

    log::trace!("MCP Servers: {server_map:#?}");

    // Collect handles for initial server startup
    let mut startup_handles = Vec::new();

    for (name, config) in server_map {
        if extract_active_status(config) == Some(false) {
            log::trace!("Server {name} is not active, skipping.");
            continue;
        }

        let app_clone = app.clone();
        let servers_clone = servers_state.clone();
        let name_clone = name.clone();
        let config_clone = config.clone();

        // Spawn task for initial startup attempt
        let handle = tauri::async_runtime::spawn(async move {
            // Only wait for the initial startup attempt, not the monitoring
            let result = start_mcp_server(
                app_clone.clone(),
                servers_clone.clone(),
                name_clone.clone(),
                config_clone.clone(),
            )
            .await;

            // If initial startup failed, we still want to continue with other servers
            if let Err(e) = &result {
                log::error!("Initial startup failed for MCP server {name_clone}: {e}");
            }

            (name_clone, result)
        });

        startup_handles.push(handle);
    }

    // Wait for all initial startup attempts to complete
    let mut successful_count = 0;
    let mut failed_count = 0;

    for handle in startup_handles {
        match handle.await {
            Ok((name, result)) => match result {
                Ok(_) => {
                    log::info!("MCP server {name} initialized successfully");
                    successful_count += 1;
                }
                Err(e) => {
                    log::error!("MCP server {name} failed to initialize: {e}");
                    failed_count += 1;
                }
            },
            Err(e) => {
                log::error!("Failed to join startup task: {e}");
                failed_count += 1;
            }
        }
    }

    log::info!(
        "MCP server initialization complete: {successful_count} successful, {failed_count} failed"
    );

    Ok(())
}

/// Monitor MCP server health and auto-reconnect on failure with exponential backoff
pub async fn monitor_mcp_server_handle<R: Runtime>(
    app: AppHandle<R>,
    servers_state: SharedMcpServers,
    name: String,
    config: Value,
    shutdown_flag: Arc<Mutex<bool>>,
    reconnect_notify: Arc<Notify>,
) {
    log::info!("Monitoring MCP server {name} health");
    let mut consecutive_failures: u32 = 0;

    loop {
        // 30s, not 2s: every probe forces a ListToolsRequest the server echoes to stderr.
        tokio::select! {
            _ = sleep(Duration::from_secs(30)) => {}
            _ = reconnect_notify.notified() => {
                log::info!("MCP server {name} monitor received reconnect signal");
            }
        }

        {
            let shutdown = shutdown_flag.lock().await;
            if *shutdown {
                log::info!("MCP server {name} monitor shutting down (shutdown flag)");
                return;
            }
        }

        let health_check_result = {
            let servers = servers_state.lock().await;
            if let Some(service) = servers.get(&name) {
                match timeout(Duration::from_secs(2), service.list_all_tools()).await {
                    Ok(Ok(_)) => true,
                    Ok(Err(e)) => {
                        log::warn!("MCP server {name} health check failed: {e}");
                        false
                    }
                    Err(_) => {
                        log::warn!("MCP server {name} health check timed out");
                        false
                    }
                }
            } else {
                // Entry was removed (e.g., by get_tools cleanup or deactivate).
                // Only stop monitoring if the server was deliberately deactivated.
                let still_active = {
                    let app_state = app.state::<AppState>();
                    let active_servers = app_state.mcp_active_servers.lock().await;
                    active_servers.contains_key(&name)
                };
                if still_active {
                    log::info!("MCP server {name} entry missing but still active, will reconnect");
                    false
                } else {
                    log::info!("MCP server {name} removed and deactivated, stopping monitor");
                    return;
                }
            }
        };

        if health_check_result {
            // Reset failure count on success
            consecutive_failures = 0;
            continue;
        }

        // Server failed health check — remove the dead entry and try to reconnect
        log::error!("MCP server {name} failed health check, attempting auto-reconnect");

        // Clean up the dead server entry (if still present) and its tracked PID
        {
            let mut servers = servers_state.lock().await;
            if let Some(service) = servers.remove(&name) {
                let _ = service.cancel().await;
            }
        }
        {
            let app_state = app.state::<AppState>();
            let mut pids = app_state.mcp_server_pids.lock().await;
            pids.remove(&name);
        }

        // Emit event so frontend shows accurate disconnected status
        emit_mcp_update_event(&app, &name);

        consecutive_failures += 1;

        // Calculate backoff delay
        let settings = {
            let app_state = app.state::<AppState>();
            let guard = app_state.mcp_settings.lock().await;
            guard.clone()
        };
        let base_delay_ms = settings.base_restart_delay_ms;
        let max_delay_ms = settings.max_restart_delay_ms;
        let multiplier = settings.backoff_multiplier;
        let delay_ms =
            (base_delay_ms as f64 * multiplier.powi((consecutive_failures - 1) as i32)) as u64;
        let capped_delay_ms = delay_ms.min(max_delay_ms);

        log::info!(
            "MCP server {name} reconnect attempt {} in {}ms",
            consecutive_failures,
            capped_delay_ms
        );
        sleep(Duration::from_millis(capped_delay_ms)).await;

        // Check shutdown flag before reconnect attempt
        {
            let shutdown = shutdown_flag.lock().await;
            if *shutdown {
                log::info!("MCP server {name} monitor shutting down before reconnect");
                return;
            }
        }

        // Check that server is still in active configs (user hasn't deactivated it)
        let still_active = {
            let app_state = app.state::<AppState>();
            let active_servers = app_state.mcp_active_servers.lock().await;
            active_servers.contains_key(&name)
        };

        if !still_active {
            log::info!("MCP server {name} was deactivated, stopping monitor");
            return;
        }

        // Attempt reconnect
        match schedule_mcp_start_task(
            app.clone(),
            servers_state.clone(),
            name.clone(),
            config.clone(),
        )
        .await
        {
            Ok(_) => {
                log::info!("MCP server {name} reconnected successfully");
                consecutive_failures = 0;
                // Emit event so frontend shows accurate connected status
                emit_mcp_update_event(&app, &name);
            }
            Err(e) => {
                log::error!(
                    "MCP server {name} reconnect attempt {consecutive_failures} failed: {e}"
                );
                // Loop continues — will retry with increased backoff
            }
        }
    }
}

/// Starts an MCP server
/// Returns the result of the first start attempt
pub async fn start_mcp_server<R: Runtime>(
    app: AppHandle<R>,
    servers_state: SharedMcpServers,
    name: String,
    config: Value,
) -> Result<(), String> {
    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 purposes
    store_active_server_config(&active_servers_state, &name, &config).await;

    // Try the first start attempt and return its result
    log::info!("Starting MCP server {name} (Initial attempt)");
    let first_start_result = schedule_mcp_start_task(
        app.clone(),
        servers_state.clone(),
        name.clone(),
        config.clone(),
    )
    .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");

            // Spawn health monitoring with auto-reconnect
            let monitor_app = app.clone();
            let monitor_servers = servers_state.clone();
            let monitor_name = name.clone();
            let monitor_config = config.clone();
            let shutdown_flag = app_state.mcp_shutdown_in_progress.clone();
            let reconnect_notify = app_state.mcp_reconnect_notify.clone();

            let monitor_handle = tauri::async_runtime::spawn(async move {
                monitor_mcp_server_handle(
                    monitor_app,
                    monitor_servers,
                    monitor_name,
                    monitor_config,
                    shutdown_flag,
                    reconnect_notify,
                )
                .await;
            });

            // Store the monitoring task handle so it can be aborted on shutdown
            {
                let mut monitoring_tasks = app_state.mcp_monitoring_tasks.lock().await;
                monitoring_tasks.insert(name.clone(), monitor_handle);
            }

            Ok(())
        }
        Err(e) => {
            log::error!("Failed to start MCP server {name} on first attempt: {e}");
            Err(e)
        }
    }
}

async fn schedule_mcp_start_task<R: Runtime>(
    app: tauri::AppHandle<R>,
    servers: SharedMcpServers,
    name: String,
    config: Value,
) -> Result<(), String> {
    let app_path = get_jan_data_folder_path(app.clone());
    let exe_path = env::current_exe().expect("Failed to get current exe path");
    let exe_parent_path = exe_path
        .parent()
        .expect("Executable must have a parent directory");
    let bin_path = exe_parent_path.to_path_buf();

    let config_params = extract_command_args(&config)
        .ok_or_else(|| format!("Failed to extract command args from config for {name}"))?;

    if let (Some(transport @ ("http" | "sse")), Some(url)) = (
        config_params.transport_type.as_deref(),
        config_params.url.clone(),
    ) {
        // One client for both transports, and one place the configured headers
        // are turned into a `HeaderMap` -- the http and sse arms used to carry
        // identical copies of that loop.
        let base = reqwest13::Client::builder()
            .default_headers(header_map(&config_params.headers))
            .connect_timeout(config_params.timeout.unwrap_or(Duration::MAX))
            .build()
            .map_err(|e| format!("Failed to build HTTP client for {name}: {e}"))?;

        // Stored OAuth credentials (refreshed if stale) are wrapped around the
        // base client so every request carries the bearer token. `None` means
        // there is no OAuth here -- nothing stored, or the user configured their
        // own `Authorization` header, which the base client already sends.
        let authorized =
            oauth::authorized_client(&app_path, &name, &url, &config, base.clone())
                .await
                .map_err(|detail| oauth::NEEDS_AUTH_PREFIX.to_string() + &detail)?;
        let had_credentials = authorized.is_some();

        // rmcp 3.x dropped the SSE client transport (MCP deprecated HTTP+SSE for
        // Streamable HTTP), so a legacy `sse` config is served over the
        // streamable-http client instead of a dedicated SSE one.
        if transport == "sse" {
            log::warn!(
                "MCP server '{name}' is configured with the deprecated 'sse' transport; \
                 connecting over Streamable HTTP instead"
            );
        }
        let handler = JanClientHandler::new(jan_client_info(), name.clone(), app.clone());

        let client = match authorized {
            Some(c) => serve_http(c, &url, handler).await,
            None => serve_http(base, &url, handler).await,
        };

        match client {
            Ok(client) => {
                log::info!("Connected to server: {:?}", client.peer_info());
                identity::insert_server(&mut *servers.lock().await, &name, client);

                emit_mcp_update_event(&app, &name);
            }
            Err(e) => {
                log::error!("Failed to connect to server: {e}");
                // Only probe when no token was sent: a failure *with* credentials
                // is either a real outage or tokens the provider revoked, and
                // discovery cannot tell those apart. Re-authenticating is offered
                // from the settings screen regardless.
                if !had_credentials && oauth::advertises_oauth(&url).await {
                    return Err(format!(
                        "{}no credentials are stored ({e})",
                        oauth::NEEDS_AUTH_PREFIX
                    ));
                }
                return Err(format!("Failed to connect to server: {e}"));
            }
        }
    } else {
        if name == "Jan Browser MCP" {
            if let Some(port_str) = config_params.envs.get("BRIDGE_PORT") {
                if let Some(port_str) = port_str.as_str() {
                    if let Ok(port) = port_str.parse::<u16>() {
                        if !jan_utils::network::is_port_available(port) {
                            log::warn!("Port {} occupied, attempting cleanup", port);
                            match kill_orphaned_mcp_process_with_app(&app, port).await {
                                Ok(true) => {
                                    log::info!("Cleaned up orphaned process on port {}", port);
                                }
                                Ok(false) => {
                                    return Err(format!(
                                        "Port {} is already in use. Please close the application using this port or restart Jan.",
                                        port
                                    ));
                                }
                                Err(e) => return Err(e),
                            }
                        }
                    }
                }
            }
        }

        let bun_x_path = if cfg!(windows) {
            bin_path.join("bun.exe")
        } else {
            bin_path.join("bun")
        };
        let uv_path = if cfg!(windows) {
            bin_path.join("uv.exe")
        } else {
            bin_path.join("uv")
        };

        // 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
            }
            // Make the child a process-group leader (pgid == pid) so teardown can
            // signal the whole tree; MCP bridges fork grandchildren that hold the
            // port and outlive a single-PID kill. A new session, not just a new
            // group, so the server can never take over a terminal Jan runs in.
            #[cfg(unix)]
            tauri_plugin_agent_tools::tools::proc::detach_from_terminal(&mut cmd);
            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);
                }
            });
            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 handler = JanClientHandler::new(jan_client_info(), name.clone(), app.clone());
            match handler.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;
                    }

                    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());
        identity::insert_server(&mut *servers.lock().await, &name, 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 immediately
        let verification_delay = Duration::from_millis(500);
        sleep(verification_delay).await;

        // Check if server is still running after the verification delay
        let server_still_running = {
            let servers_map = servers.lock().await;
            servers_map.contains_key(&name)
        };

        if !server_still_running {
            return Err(format!("MCP server {name} quit immediately after starting"));
        }

        // Verify tools/list is reachable before emitting the ready event.
        // Stdio servers (especially via npx mcp-remote) may need extra time
        // after serve() completes before the transport is ready for JSON-RPC.
        const MAX_TOOL_VERIFY_ATTEMPTS: u32 = 3;
        const TOOL_VERIFY_TIMEOUT_SECS: u64 = 2;
        const TOOL_VERIFY_BACKOFF_MS: u64 = 1000;

        for attempt in 1..=MAX_TOOL_VERIFY_ATTEMPTS {
            let verify_result = {
                let servers_map = servers.lock().await;
                if let Some(service) = servers_map.get(&name) {
                    Some(
                        timeout(
                            Duration::from_secs(TOOL_VERIFY_TIMEOUT_SECS),
                            service.list_all_tools(),
                        )
                        .await,
                    )
                } else {
                    log::info!(
                        "MCP server {name} was removed during tools/list verification; skipping"
                    );
                    None
                }
            };

            match verify_result {
                None => {
                    // Server was removed from state (e.g., user toggled it off).
                    // Skip emitting the event entirely — this is intentional.
                    return Ok(());
                }
                Some(Ok(Ok(_tools))) => {
                    log::info!("MCP server {name} tools/list verified on attempt {attempt}");
                    break;
                }
                Some(Ok(Err(e))) => {
                    log::warn!(
                        "MCP server {name} tools/list failed on attempt {attempt}/{MAX_TOOL_VERIFY_ATTEMPTS}: {e}"
                    );
                    if attempt < MAX_TOOL_VERIFY_ATTEMPTS {
                        sleep(Duration::from_millis(TOOL_VERIFY_BACKOFF_MS)).await;
                    }
                }
                Some(Err(_)) => {
                    log::warn!(
                        "MCP server {name} tools/list timed out on attempt {attempt}/{MAX_TOOL_VERIFY_ATTEMPTS}"
                    );
                    if attempt < MAX_TOOL_VERIFY_ATTEMPTS {
                        sleep(Duration::from_millis(TOOL_VERIFY_BACKOFF_MS)).await;
                    }
                }
            }
        }
        // If all attempts failed, we still proceed to emit the event.
        // The health monitor will handle ongoing reconnection.

        if name == "Jan Browser MCP" {
            if let Some(port_str) = config_params.envs.get("BRIDGE_PORT") {
                if let Some(port_str) = port_str.as_str() {
                    if let Ok(port) = port_str.parse::<u16>() {
                        use crate::core::mcp::lockfile::create_lock_file;
                        let child_pid = app
                            .state::<AppState>()
                            .mcp_server_pids
                            .lock()
                            .await
                            .get(&name)
                            .copied();
                        if let Some(pid) = child_pid {
                            if let Err(e) = create_lock_file(&app, port, &name, pid) {
                                log::warn!("Failed to create lock file for port {}: {}", port, e);
                            }
                        }
                    }
                }
            }
        }

        emit_mcp_update_event(&app, &name);
    }
    Ok(())
}

/// Route an MCP server's stderr line through Jan's logger at the level the
/// server itself reported, defaulting to info when no level tag is present.
fn log_mcp_stderr_line(server_name: &str, line: &str) {
    let trimmed = line.trim_start();
    let level_token = trimmed.split_whitespace().next().map(|t| {
        t.trim_matches(|c: char| !c.is_ascii_alphabetic())
            .to_ascii_uppercase()
    });
    match level_token.as_deref() {
        Some("ERROR" | "CRITICAL" | "FATAL") => {
            log::error!("[mcp-stderr:{server_name}] {line}")
        }
        Some("WARN" | "WARNING") => log::warn!("[mcp-stderr:{server_name}] {line}"),
        Some("DEBUG") => log::debug!("[mcp-stderr:{server_name}] {line}"),
        Some("TRACE") => log::trace!("[mcp-stderr:{server_name}] {line}"),
        _ => log::info!("[mcp-stderr:{server_name}] {line}"),
    }
}

/// Turn the entry's configured `headers` map into a `HeaderMap`, skipping any
/// pair that is not a valid header name/value. Uses `reqwest13` because rmcp's
/// streamable-http transport is implemented for reqwest 0.13.
fn header_map(headers: &serde_json::Map<String, Value>) -> reqwest13::header::HeaderMap {
    let mut map = reqwest13::header::HeaderMap::new();
    for (key, value) in headers.iter() {
        if let Some(v) = value.as_str() {
            if let (Ok(name), Ok(val)) = (
                reqwest13::header::HeaderName::from_bytes(key.as_bytes()),
                reqwest13::header::HeaderValue::from_str(v),
            ) {
                map.insert(name, val);
            }
        }
    }
    map
}

/// What every MCP server sees in `initialize.clientInfo`: Jan and its app
/// version. Servers use it for logs, audit and per-client policy, so leaving
/// the `rmcp` crate default would make Jan indistinguishable from any other
/// client built on it.
pub(crate) fn jan_client_info() -> ClientInfo {
    ClientInfo::new(
        ClientCapabilities::default(),
        Implementation::new("Jan", env!("CARGO_PKG_VERSION")),
    )
}

/// Serve the streamable-http transport over any client that implements it, so
/// the plain and OAuth-wrapped clients share one transport construction.
async fn serve_http<C>(
    client: C,
    url: &str,
    handler: JanClientHandler,
) -> Result<RunningMcpService, String>
where
    C: StreamableHttpClient + Send + Sync + 'static,
{
    let transport = StreamableHttpClientTransport::with_client(
        client,
        StreamableHttpClientTransportConfig::with_uri(url.to_string()),
    );
    handler.serve(transport).await.map_err(|e| e.to_string())
}

fn emit_mcp_update_event<R: Runtime>(app: &AppHandle<R>, name: &str) {
    if let Err(e) = app.emit(
        "mcp-update",
        serde_json::json!({
            "server": name
        }),
    ) {
        log::error!("Failed to emit mcp-update event: {e}");
    }
}

/// Restart only servers that were previously active (like cortex restart behavior)
pub async fn restart_active_mcp_servers<R: Runtime>(
    app: &AppHandle<R>,
    servers_state: SharedMcpServers,
) -> Result<(), String> {
    let app_state = app.state::<AppState>();
    let active_servers = app_state.mcp_active_servers.lock().await;

    log::info!(
        "Restarting {} previously active MCP servers",
        active_servers.len()
    );

    for (name, config) in active_servers.iter() {
        log::info!("Restarting MCP server: {name}");

        // Start server with restart monitoring - spawn async task
        let app_clone = app.clone();
        let servers_clone = servers_state.clone();
        let name_clone = name.clone();
        let config_clone = config.clone();

        tauri::async_runtime::spawn(async move {
            let _ = start_mcp_server(app_clone, servers_clone, name_clone, config_clone).await;
        });
    }

    Ok(())
}

pub async fn kill_orphaned_mcp_process_with_app<R: Runtime>(
    app: &AppHandle<R>,
    port: u16,
) -> Result<bool, String> {
    use crate::core::mcp::lockfile::{
        check_and_cleanup_stale_lock, is_process_alive, read_lock_file,
    };

    // Check lock file first (fast path)
    if let Some(lock) = read_lock_file(app, port) {
        log::debug!("Found lock file for port {}: PID={}", port, lock.pid);

        if !is_process_alive(lock.pid) {
            log::info!("Lock file stale, process {} is dead", lock.pid);
            check_and_cleanup_stale_lock(app, port).await?;
            // Fall through — port may still be held by a grandchild that outlived this PID.
        } else {
            // Process from lock file is alive - verify it's still the MCP process
            if let Some(process_info) = jan_utils::network::get_process_info_by_pid(lock.pid) {
                if jan_utils::network::is_orphaned_mcp_process(&process_info) {
                    log::info!(
                        "Lock file PID {} verified as MCP process, attempting kill",
                        lock.pid
                    );
                    kill_process_by_pid(lock.pid).await?;

                    use crate::core::mcp::lockfile::delete_lock_file;
                    delete_lock_file(app, port)?;

                    tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;

                    if jan_utils::network::is_port_available(port) {
                        log::info!("Cleaned up orphaned process via lock file");
                        return Ok(true);
                    }
                } else {
                    log::warn!(
                    "Lock file PID {} is alive but NOT an MCP process (name: {}, cmd: {:?}). Lock file is stale.",
                    lock.pid,
                    process_info.name,
                    process_info.cmd
                );
                    // PID reused by another process, clean up stale lock file
                    check_and_cleanup_stale_lock(app, port).await?;
                }
            } else {
                log::debug!(
                    "Could not get process info for PID {}, cleaning up lock file",
                    lock.pid
                );
                check_and_cleanup_stale_lock(app, port).await?;
            }
        }
    }

    // Fallback: Use lsof/netstat to find process on port
    let process_info = match jan_utils::network::find_process_using_port(port) {
        Some(info) => info,
        None => return Ok(false),
    };

    log::info!(
        "Found process on port {}: PID={}, name={}, cmd={:?}",
        port,
        process_info.pid,
        process_info.name,
        process_info.cmd
    );

    if !jan_utils::network::is_orphaned_mcp_process(&process_info) {
        log::warn!(
            "Port {} occupied by non-Jan process '{}' (PID {})",
            port,
            process_info.name,
            process_info.pid
        );
        return Err(format!(
            "Port {} is in use by another application '{}' (PID {}). Please close that application or use a different port.",
            port, process_info.name, process_info.pid
        ));
    }

    log::info!("Killing orphaned MCP process: PID {}", process_info.pid);
    kill_process_by_pid(process_info.pid).await?;

    if jan_utils::network::wait_for_port_free(
        port,
        Duration::from_millis(2000),
        Duration::from_millis(50),
    )
    .await
    {
        log::info!("Cleaned up orphaned process on port {}", port);
        Ok(true)
    } else {
        Err(format!("Port {} still in use after killing process", port))
    }
}

#[cfg(unix)]
async fn kill_process_by_pid(pid: u32) -> Result<(), String> {
    use nix::sys::signal::{kill, killpg, Signal};
    use nix::unistd::{getpgid, Pid};

    let nix_pid = Pid::from_raw(pid as i32);

    // Signal the whole process group so forked grandchildren (which may hold the
    // port) die with the parent. We spawn MCP children as group leaders, so this
    // targets only our subtree. Fall back to the single PID if the group can't be
    // resolved (e.g. an orphan found by port scan) or resolves to init.
    let pgid = getpgid(Some(nix_pid)).ok().filter(|g| g.as_raw() > 1);
    let send = |sig: Signal| match pgid {
        Some(g) => killpg(g, sig),
        None => kill(nix_pid, sig),
    };

    send(Signal::SIGTERM).map_err(|e| format!("Failed to send SIGTERM to PID {}: {}", pid, e))?;

    for _ in 0..30 {
        tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
        // Liveness of the leader PID is sufficient; the group dies with it.
        if kill(nix_pid, None).is_err() {
            return Ok(());
        }
    }

    log::warn!("Process group {} unresponsive, sending SIGKILL", pid);
    send(Signal::SIGKILL).map_err(|e| format!("Failed to send SIGKILL to PID {}: {}", pid, e))?;

    Ok(())
}

#[cfg(windows)]
async fn kill_process_by_pid(pid: u32) -> Result<(), String> {
    use std::process::Command;

    #[cfg(windows)]
    use std::os::windows::process::CommandExt;

    let mut cmd = Command::new(jan_utils::windows_system_tool("taskkill.exe"));
    cmd.args(&["/F", "/T", "/PID", &pid.to_string()]);

    #[cfg(windows)]
    cmd.creation_flags(0x08000000); // CREATE_NO_WINDOW

    let output = cmd
        .output()
        .map_err(|e| format!("Failed to run taskkill: {}", e))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(format!("taskkill failed: {}", stderr));
    }

    Ok(())
}

/// Tear down a Jan Browser MCP child and confirm its `port` is released.
/// Kills the recorded child's process group (Phase 1 makes children group
/// leaders, so this reaps forked grandchildren that hold the port), then polls;
/// if some straggler still binds the port it is found and group-killed too.
pub async fn terminate_browser_mcp(pid: Option<u32>, port: u16) {
    use crate::core::mcp::constants::{MCP_PORT_FREE_TIMEOUT, MCP_PORT_POLL_INTERVAL};

    if let Some(pid) = pid {
        if let Err(e) = kill_process_by_pid(pid).await {
            log::warn!("Failed to kill Browser MCP group (PID {}): {}", pid, e);
        }
    }

    if jan_utils::network::wait_for_port_free(port, MCP_PORT_FREE_TIMEOUT, MCP_PORT_POLL_INTERVAL)
        .await
    {
        return;
    }

    if let Some(info) = jan_utils::network::find_process_using_port(port) {
        log::info!(
            "Port {} still held after teardown; killing straggler PID {}",
            port,
            info.pid
        );
        if let Err(e) = kill_process_by_pid(info.pid).await {
            log::warn!(
                "Failed to kill straggler PID {} on port {}: {}",
                info.pid,
                port,
                e
            );
        }
        let _ = jan_utils::network::wait_for_port_free(
            port,
            MCP_PORT_FREE_TIMEOUT,
            MCP_PORT_POLL_INTERVAL,
        )
        .await;
    }
}

pub async fn background_cleanup_mcp_servers<R: Runtime>(
    app: &AppHandle<R>,
    state: &State<'_, AppState>,
) {
    let _ = stop_mcp_servers_with_context(app, state, ShutdownContext::AppExit).await;

    // Clear active servers and restart counts
    {
        let mut active_servers = state.mcp_active_servers.lock().await;
        active_servers.clear();
    }

    // Clean up all lock files created by this process
    use crate::core::mcp::lockfile::cleanup_own_locks;
    let _ = cleanup_own_locks(app);
}

struct ShutdownGuard {
    flag: Arc<Mutex<bool>>,
}

impl Drop for ShutdownGuard {
    fn drop(&mut self) {
        if let Ok(mut guard) = self.flag.try_lock() {
            *guard = false;
        } else {
            let flag = self.flag.clone();
            tauri::async_runtime::spawn(async move {
                let mut guard = flag.lock().await;
                *guard = false;
            });
        }
    }
}

pub async fn stop_mcp_servers_with_context<R: Runtime>(
    app: &AppHandle<R>,
    state: &State<'_, AppState>,
    context: ShutdownContext,
) -> Result<(), String> {
    {
        let mut shutdown_in_progress = state.mcp_shutdown_in_progress.lock().await;
        if *shutdown_in_progress {
            return Ok(());
        }
        *shutdown_in_progress = true;
    }

    let _guard = ShutdownGuard {
        flag: state.mcp_shutdown_in_progress.clone(),
    };

    {
        let mut monitoring_tasks = state.mcp_monitoring_tasks.lock().await;
        for (_name, handle) in monitoring_tasks.drain() {
            handle.abort();
        }
    }

    tokio::time::sleep(Duration::from_millis(50)).await;

    let pids_snapshot: std::collections::HashMap<String, u32> = {
        let pids = state.mcp_server_pids.lock().await;
        pids.clone()
    };
    let servers_to_stop: Vec<(String, RunningMcpService, Option<u16>)> = {
        let mut servers_map = state.mcp_servers.lock().await;
        let keys: Vec<String> = servers_map.keys().cloned().collect();

        let mut result = Vec::new();
        for key in keys {
            if let Some(service) = servers_map.remove(&key) {
                let port = if key == "Jan Browser MCP" {
                    let active_servers = state.mcp_active_servers.lock().await;
                    active_servers.get(&key).and_then(|config| {
                        config
                            .get("env")
                            .and_then(|e| e.get("BRIDGE_PORT"))
                            .and_then(|p| p.as_str())
                            .and_then(|s| s.parse::<u16>().ok())
                    })
                } else {
                    None
                };

                result.push((key, service, port));
            }
        }
        result
    };

    if servers_to_stop.is_empty() {
        return Ok(());
    }

    let server_names: Vec<String> = servers_to_stop
        .iter()
        .map(|(name, _, _)| name.clone())
        .collect();
    let per_server_timeout = context.per_server_timeout();
    let stop_handles: Vec<_> = servers_to_stop
        .into_iter()
        .map(|(name, service, port)| {
            let app_clone = app.clone();
            let child_pid = pids_snapshot.get(&name).copied();

            tauri::async_runtime::spawn(async move {
                let cancel_future = service.cancel();

                let success = tokio::time::timeout(per_server_timeout, cancel_future)
                    .await
                    .map(|r| r.is_ok())
                    .unwrap_or(false);

                if name == "Jan Browser MCP" {
                    if let Some(port) = port {
                        use crate::core::mcp::lockfile::delete_lock_file;
                        terminate_browser_mcp(child_pid, port).await;
                        let _ = delete_lock_file(&app_clone, port);
                    }
                }

                (name, success)
            })
        })
        .collect();

    let overall_timeout = context.overall_timeout();
    let results = tokio::time::timeout(
        overall_timeout,
        futures_util::future::join_all(stop_handles),
    )
    .await;

    let failed_servers: Vec<String> = match results {
        Ok(results) => {
            results
                .into_iter()
                .filter_map(|r| match r {
                    Ok((name, success)) if !success => Some(name),
                    Err(_) => None, // Task was cancelled/panicked
                    _ => None,
                })
                .collect()
        }
        Err(_) => {
            // Overall timeout - assume all servers need force-kill
            log::warn!("MCP shutdown timed out, will force-kill remaining processes");
            server_names.clone()
        }
    };

    // Force-kill processes that didn't stop gracefully
    for server_name in &failed_servers {
        if let Some(&pid) = pids_snapshot.get(server_name) {
            log::warn!("Force-killing MCP server {} (PID {})", server_name, pid);
            if let Err(e) = kill_process_by_pid(pid).await {
                log::error!("Failed to force-kill PID {}: {}", pid, e);
            }
        }
    }

    // Clean up PIDs from tracking
    {
        let mut pids = state.mcp_server_pids.lock().await;
        for name in &server_names {
            pids.remove(name);
        }
    }

    tokio::time::sleep(Duration::from_millis(200)).await;

    Ok(())
}

/// Store active server configuration for restart purposes
pub async fn store_active_server_config(
    active_servers_state: &Arc<Mutex<HashMap<String, Value>>>,
    name: &str,
    config: &Value,
) {
    let mut active_servers = active_servers_state.lock().await;
    active_servers.insert(name.to_string(), config.clone());
}

// Add a new server configuration to the MCP config file
pub fn add_server_config<R: Runtime>(
    app_handle: tauri::AppHandle<R>,
    server_key: String,
    server_value: Value,
) -> Result<(), String> {
    add_server_config_with_path(app_handle, server_key, server_value, None)
}

// Add a new server configuration to the MCP config file with custom path support
pub fn add_server_config_with_path<R: Runtime>(
    app_handle: tauri::AppHandle<R>,
    server_key: String,
    server_value: Value,
    config_filename: Option<&str>,
) -> Result<(), String> {
    let config_filename = config_filename.unwrap_or("mcp_config.json");
    let config_path = get_jan_data_folder_path(app_handle).join(config_filename);

    let mut config: Value = serde_json::from_str(
        &std::fs::read_to_string(&config_path)
            .map_err(|e| format!("Failed to read config file: {e}"))?,
    )
    .map_err(|e| format!("Failed to parse config: {e}"))?;

    config
        .as_object_mut()
        .ok_or("Config root is not an object")?
        .entry("mcpServers")
        .or_insert_with(|| Value::Object(serde_json::Map::new()))
        .as_object_mut()
        .ok_or("mcpServers is not an object")?
        .insert(server_key, server_value);

    std::fs::write(
        &config_path,
        serde_json::to_string_pretty(&config)
            .map_err(|e| format!("Failed to serialize config: {e}"))?,
    )
    .map_err(|e| format!("Failed to write config file: {e}"))?;

    Ok(())
}