Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(app): remove the hosted Exa MCP default 0.8.3 wrote, as MCP schema 5 (#9010) (#9284)
Thinh Lef1dfc6c2a3f2
diff --git a/src-tauri/src/core/setup.rs b/src-tauri/src/core/setup.rsindex e82182076..44ed324f1 100644--- a/src-tauri/src/core/setup.rs+++ b/src-tauri/src/core/setup.rs@@ -22,7 +22,7 @@ use super::{mcp::helpers::run_mcp_commands, state::AppState};/// past-release stores sit below it, so bumping this constant without adding/// that release's store fixture breaks the build instead of skipping a/// migration silently.-pub const CURRENT_MCP_SCHEMA_VERSION: i64 = 4;+pub const CURRENT_MCP_SCHEMA_VERSION: i64 = 5;// Migrate MCP servers configurationpub fn migrate_mcp_servers(@@ -75,19 +75,48 @@ pub fn migrate_mcp_servers(log::error!("Failed to migrate Exa to HTTP: {e}");}}- if mcp_version < CURRENT_MCP_SCHEMA_VERSION {+ if mcp_version < 4 {log::info!(- "Migrating MCP schema version {CURRENT_MCP_SCHEMA_VERSION}: Removing default Exa MCP (native web search cutover)"+ "Migrating MCP schema version 4: Removing default Exa MCP (native web search cutover)");- if let Err(e) = remove_exa_server(app_handle) {+ if let Err(e) = remove_exa_server(app_handle.clone()) {log::error!("Failed to remove Exa MCP server: {e}");}}- store.set("mcp_version", CURRENT_MCP_SCHEMA_VERSION);+ let mut schema = CURRENT_MCP_SCHEMA_VERSION;+ if mcp_version < 5 {+ // Version 4 only reaches an entry the user had deactivated, and it ran+ // once for everyone on 0.8.4 or later, so what it missed is left to this+ // step: the default 0.8.3 wrote for every user. See `remove_default_exa`.+ log::info!(+ "Migrating MCP schema version 5: Removing the hosted Exa MCP default 0.8.3 wrote"+ );+ let removed = remove_default_exa(app_handle);+ if let Err(e) = &removed {+ log::error!("Failed to remove the default Exa MCP server: {e}");+ }+ schema = schema_after_exa_cutover(&removed);+ }+ store.set("mcp_version", schema);store.save().expect("Failed to save store");Ok(())}+/// The schema version to record once the version 5 cutover has been tried: the+/// current one when it worked, and the one before it when it did not - a config+/// that could not be read or written just then - so the next startup tries+/// again instead of skipping it for good. The steps before it have run and are+/// not repeated.+///+/// `pub` for the upgrade-compat fixture test.+pub fn schema_after_exa_cutover(removed: &Result<(), String>) -> i64 {+ if removed.is_ok() {+ CURRENT_MCP_SCHEMA_VERSION+ } else {+ CURRENT_MCP_SCHEMA_VERSION - 1+ }+}+fn migrate_exa_to_http(app_handle: tauri::AppHandle) -> Result<(), String> {let config_path = get_jan_data_folder_path(app_handle).join("mcp_config.json");@@ -121,19 +150,19 @@ fn migrate_exa_to_http(app_handle: tauri::AppHandle) -> Result<(), String> {Ok(())}-/// Whether an `exa` MCP server entry is one the web-search cutover removes:-/// not currently active, and carrying no API key.+/// Whether an `exa` MCP server entry is one the version 4 cutover removes: not+/// currently active, and carrying no API key.////// Note what `active: false` does *not* mean here. It is not evidence that the/// user never turned the server on: 0.8.3's own `migrate_exa_to_http` wrote/// the entry with `active: true` and an empty `env` for everybody, so a 0.8.3/// upgrader fails the `inactive` half and keeps the entry. The entries this-/// actually removes are the ones a user deactivated themselves. See-/// `remove_exa_server` for why that is left alone for now.+/// actually removes are the ones a user deactivated themselves; version 5+/// (`is_hosted_default_exa_server`) is what reaches the rest.////// It inspects `active` and `env` only -- not `url` or `type` -- so an inactive/// *custom* exa entry with an empty env also matches. Callers must reject-/// non-object values before calling (see `remove_exa_server`).+/// non-object values before calling (see `remove_exa_entry`).////// `pub` for the upgrade-compat fixture test, which pins the decision against/// real 0.8.3 bytes.@@ -147,28 +176,99 @@ pub fn is_default_exa_server(exa: &serde_json::Value) -> bool {inactive && no_key}+/// The hosted Exa endpoint 0.8.3 registered as an MCP server.+const HOSTED_EXA_URL: &str = "https://mcp.exa.ai/mcp";++/// Whether an `exa` entry is the default 0.8.3's `migrate_exa_to_http` wrote:+/// the hosted endpoint over http, with no command, no args, no key and no+/// headers, whether or not it is active (0.8.3 wrote it active for everyone, so+/// `active` says nothing about what the user wants).+///+/// Narrow on purpose, because a match deletes the entry and whatever the user+/// put in it. It is the six fields 0.8.3 wrote and nothing else: a key in `env`,+/// any `headers` (they carry authentication), any other field, and any other+/// url, transport, command or args keep the entry. A proxy, a self-hosted Exa,+/// or a server the user set up by hand under the name `exa` is not the default+/// and is never removed. Non-object values do not match.+///+/// `pub` for the upgrade-compat fixture test.+pub fn is_hosted_default_exa_server(exa: &serde_json::Value) -> bool {+ let Some(exa) = exa.as_object() else {+ return false;+ };+ // Whatever this migration does not know is the user's, and is kept.+ let only_known_fields = exa.keys().all(|key| {+ matches!(+ key.as_str(),+ "type" | "url" | "command" | "args" | "env" | "active" | "headers"+ )+ });+ let is = |key: &str, want: &str| exa.get(key).and_then(|v| v.as_str()) == Some(want);+ let empty_str = |key: &str| exa.get(key).is_none_or(|v| v.as_str() == Some(""));+ let empty_list = |key: &str| {+ exa.get(key)+ .is_none_or(|v| v.as_array().is_some_and(|a| a.is_empty()))+ };+ let empty_map = |key: &str| {+ exa.get(key)+ .is_none_or(|v| v.as_object().is_some_and(|m| m.is_empty()))+ };+ only_known_fields+ && is("type", "http")+ && is("url", HOSTED_EXA_URL)+ && empty_str("command")+ && empty_list("args")+ && empty_map("env")+ && empty_map("headers")+}+/// One-time cutover to native web search: drop the default Exa MCP server so the/// built-in web_search/web_fetch tools own web search.///-/// Narrower than the name suggests, and deliberately left that way in this-/// change: removal needs the entry to be inactive *and* keyless, but 0.8.3-/// shipped it `active: true` with an empty `env`, so upgraders from 0.8.3 keep-/// it and the cutover is close to a no-op for them. Widening the match to the-/// hosted endpoint regardless of `active` would delete configuration for the-/// whole installed base at next startup, so it is tracked in janhq/jan#9010-/// rather than changed in passing.+/// Version 4's match is narrow: the entry has to be inactive *and* keyless, but+/// 0.8.3 shipped it `active: true` with an empty `env`, so for upgraders the+/// cutover was close to a no-op. `remove_default_exa` (version 5) is the step+/// that reaches them.fn remove_exa_server(app_handle: tauri::AppHandle) -> Result<(), String> {let config_path = get_jan_data_folder_path(app_handle).join("mcp_config.json");+ remove_exa_entry(&config_path, is_default_exa_server).map(|_| ())+}++/// Version 5 of the cutover: remove the hosted Exa default 0.8.3 wrote.+///+/// Nothing is lost by it: the built-in web tools default to Exa, and keyless+/// Exa is the same hosted endpoint, so web search keeps working against the same+/// server without a second set of Exa tools next to the built-in ones. An entry+/// with a key, or any other url, is left alone (`is_hosted_default_exa_server`),+/// and a user who wants the MCP entry back re-adds it with `jan cli mcp add`.+fn remove_default_exa(app_handle: tauri::AppHandle) -> Result<(), String> {+ let config_path = get_jan_data_folder_path(app_handle).join("mcp_config.json");+ remove_default_exa_from_config(&config_path).map(|_| ())+}++/// Version 5's removal against a config file. `Ok(true)` when it removed the+/// entry. `pub` for the upgrade-compat fixture test.+pub fn remove_default_exa_from_config(config_path: &std::path::Path) -> Result<bool, String> {+ remove_exa_entry(config_path, is_hosted_default_exa_server)+}++/// Remove the `exa` entry of an `mcp_config.json` when `matches` says it is the+/// one to remove. A missing file, a missing `mcpServers`, or no `exa` entry is+/// nothing to do.+fn remove_exa_entry(+ config_path: &std::path::Path,+ matches: fn(&serde_json::Value) -> bool,+) -> Result<bool, String> {if !config_path.exists() {- return Ok(());+ return Ok(false);}let config_str =- fs::read_to_string(&config_path).map_err(|e| format!("Failed to read MCP config: {e}"))?;+ fs::read_to_string(config_path).map_err(|e| format!("Failed to read MCP config: {e}"))?;let mut config: serde_json::Value = serde_json::from_str(&config_str).map_err(|e| format!("Failed to parse MCP config: {e}"))?;let Some(servers) = config.get_mut("mcpServers").and_then(|s| s.as_object_mut()) else {- return Ok(());+ return Ok(false);};// `is_object()` is load-bearing, not defensive noise: serde_json's `get`@@ -180,18 +280,18 @@ fn remove_exa_server(app_handle: tauri::AppHandle) -> Result<(), String> {// skip.let is_default_exa = servers.get("exa")- .is_some_and(|exa| exa.is_object() && is_default_exa_server(exa));+ .is_some_and(|exa| exa.is_object() && matches(exa));if is_default_exa {servers.remove("exa");fs::write(- &config_path,+ config_path,serde_json::to_string_pretty(&config).map_err(|e| format!("Failed to serialize MCP config: {e}"))?,).map_err(|e| format!("Failed to write MCP config: {e}"))?;}- Ok(())+ Ok(is_default_exa)}/// Resolve when the frontend emits `app-ready`, or after `timeout` (so a windowdiff --git a/src-tauri/tests/fixtures/upgrade/0.8.5/store.json b/src-tauri/tests/fixtures/upgrade/0.8.5/store.jsonnew file mode 100644index 000000000..a7d6de58e--- /dev/null+++ b/src-tauri/tests/fixtures/upgrade/0.8.5/store.json@@ -0,0 +1,4 @@+{+ "mcp_version": 4,+ "version": "0.8.5"+}\ No newline at end of filediff --git a/src-tauri/tests/upgrade_compat.rs b/src-tauri/tests/upgrade_compat.rsindex 0ffafdf21..9c78d6ae8 100644--- a/src-tauri/tests/upgrade_compat.rs+++ b/src-tauri/tests/upgrade_compat.rs@@ -3,10 +3,12 @@//!//! Everything under `tests/fixtures/upgrade/` is real bytes written by the//! named release (0.8.3 stable on Windows for `store.json` and-//! `mcp_config.json`, 0.8.3 stable on macOS for the thread files), committed+//! `mcp_config.json`, 0.8.3 stable on macOS for the thread files, and 0.8.5+//! stable on macOS for its `store.json`), committed//! so a schema drift that would silently eat user data fails here instead of//! on a user machine. Threads are plain per-thread JSON/JSONL files; the store-//! marks the MCP schema version (3 on 0.8.3, 4 from 0.8.4) that+//! marks the MCP schema version (3 on 0.8.3, 4 from 0.8.4, 5 from the release+//! that removes the default Exa entry) that//! `core/setup.rs migrate_mcp_servers` migrates against.//!//! When a release intentionally changes one of these schemas, commit the old@@ -21,7 +23,10 @@#![cfg(not(feature = "cli"))]use app_lib::core::mcp::models::McpSettings;-use app_lib::core::setup::{is_default_exa_server, CURRENT_MCP_SCHEMA_VERSION};+use app_lib::core::setup::{+ is_default_exa_server, is_hosted_default_exa_server, remove_default_exa_from_config,+ schema_after_exa_cutover, CURRENT_MCP_SCHEMA_VERSION,+};use app_lib::core::threads::helpers::read_messages_from_file;use app_lib::core::threads::utils::{get_messages_path, get_thread_dir, get_thread_metadata_path};use serde_json::Value;@@ -248,24 +253,16 @@ fn mcp_config_written_by_0_8_3_parses_with_current_settings_schema() {// reading is wrong: 0.8.3's own `migrate_exa_to_http` wrote this entry with// `active: true` (`git show v0.8.3:src-tauri/src/core/setup.rs`), so it is// byte-for-byte 0.8.3's *default*, not evidence that a user activated- // anything. The contract pinned below is therefore "an exa entry as 0.8.3- // left it survives the 0.8.4 cutover" -- true precisely because 0.8.3's- // default is active.- //- // That is recorded as current behaviour, not endorsed: 0.8.3 wrote this- // shape for every user, so the cutover is close to a no-op for the whole- // 0.8.3 population. Widening it is a separate change against the shipping- // migration -- see janhq/jan#9010 and the note on `remove_exa_server`.- // If that widening lands, this assertion is expected to flip, and- // flipping it is the signal that the behaviour changed rather than a- // regression.+ // anything. Migration 4 therefore leaves it alone, and it ran once for+ // everyone on 0.8.4 or later; migration 5 is the step that removes it (see+ // `the_exa_default_0_8_3_wrote_is_removed_by_migration_5` below).let servers = config["mcpServers"].as_object().expect("mcpServers object");let fixture_exa = servers.get("exa").expect("0.8.3 config carried an exa entry");assert!(!is_default_exa_server(fixture_exa),- "an exa entry as 0.8.3 wrote it must not be classified as the removable default"+ "migration 4 must keep leaving an exa entry as 0.8.3 wrote it alone");let untouched_default = serde_json::json!({"type": "http",@@ -280,3 +277,161 @@ fn mcp_config_written_by_0_8_3_parses_with_current_settings_schema() {"the never-activated default exa entry is what migration 4 removes");}++/// Migration 5 (#9010): the hosted Exa default 0.8.3 wrote for everyone is+/// removed, and only that. These rows decide who loses configuration.+#[test]+fn the_exa_default_0_8_3_wrote_is_removed_by_migration_5() {+ let hosted = |active: bool| {+ serde_json::json!({+ "type": "http",+ "url": "https://mcp.exa.ai/mcp",+ "command": "",+ "args": [],+ "env": {},+ "active": active+ })+ };+ // 0.8.3's migration 3, and the same entry deactivated by the user.+ assert!(is_hosted_default_exa_server(&hosted(true)));+ assert!(is_hosted_default_exa_server(&hosted(false)));++ // A key keeps it, whatever else it looks like.+ let mut keyed = hosted(true);+ keyed["env"] = serde_json::json!({ "EXA_API_KEY": "sk-real" });+ assert!(!is_hosted_default_exa_server(&keyed));++ // Authentication headers are credentials: an entry that carries any is the+ // user's, even when it is the hosted url, active, and has no env.+ let mut headers = hosted(true);+ headers["headers"] = serde_json::json!({ "Authorization": "Bearer sk-real" });+ assert!(!is_hosted_default_exa_server(&headers));+ // An empty headers map is what the default looks like with none set.+ let mut no_headers = hosted(true);+ no_headers["headers"] = serde_json::json!({});+ assert!(is_hosted_default_exa_server(&no_headers));+ // A field this migration does not know is the user's too.+ let mut unknown = hosted(true);+ unknown["timeout"] = serde_json::json!(60);+ assert!(!is_hosted_default_exa_server(&unknown));++ // Another url, transport, command or args is not the default.+ let mut proxy = hosted(false);+ proxy["url"] = serde_json::json!("https://exa.proxy.internal/mcp");+ assert!(!is_hosted_default_exa_server(&proxy));+ let mut stdio = hosted(true);+ stdio["type"] = serde_json::json!("stdio");+ assert!(!is_hosted_default_exa_server(&stdio));+ let mut command = hosted(true);+ command["command"] = serde_json::json!("npx");+ command["args"] = serde_json::json!(["-y", "exa-mcp-server"]);+ assert!(!is_hosted_default_exa_server(&command));++ // A malformed entry is not something to delete.+ for malformed in [+ serde_json::json!("exa"),+ serde_json::json!(null),+ serde_json::json!(7),+ ] {+ assert!(!is_hosted_default_exa_server(&malformed));+ }+}++#[test]+fn migration_5_removes_the_exa_entry_from_a_real_0_8_3_config_and_nothing_else() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("mcp_config.json");+ std::fs::copy(fixture_root("0.8.3").join("mcp_config.json"), &path).unwrap();+ let before: Value = serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();++ assert!(remove_default_exa_from_config(&path).unwrap());++ let after: Value = serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();+ assert!(after["mcpServers"].get("exa").is_none());+ // Every other server, and the settings, are byte-for-byte what they were.+ let mut expected = before["mcpServers"].as_object().unwrap().clone();+ expected.remove("exa");+ assert_eq!(after["mcpServers"], Value::Object(expected));+ assert_eq!(after["mcpSettings"], before["mcpSettings"]);++ // Once it is gone there is nothing left for a second run to do.+ assert!(!remove_default_exa_from_config(&path).unwrap());+}++#[test]+fn migration_5_leaves_a_config_it_does_not_recognise_alone() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("mcp_config.json");++ // No file yet: nothing to do, and nothing created.+ assert!(!remove_default_exa_from_config(&path).unwrap());+ assert!(!path.exists());++ for config in [+ // A keyed entry.+ serde_json::json!({ "mcpServers": { "exa": {+ "type": "http", "url": "https://mcp.exa.ai/mcp", "command": "", "args": [],+ "env": { "EXA_API_KEY": "sk-real" }, "active": true } } }),+ // The hosted url, active, with authentication headers.+ serde_json::json!({ "mcpServers": { "exa": {+ "type": "http", "url": "https://mcp.exa.ai/mcp", "command": "", "args": [],+ "env": {}, "headers": { "Authorization": "Bearer sk-real" }, "active": true } } }),+ // The hosted url with a field this migration does not know.+ serde_json::json!({ "mcpServers": { "exa": {+ "type": "http", "url": "https://mcp.exa.ai/mcp", "command": "", "args": [],+ "env": {}, "timeout": 60, "active": true } } }),+ // A custom endpoint, inactive and keyless.+ serde_json::json!({ "mcpServers": { "exa": {+ "type": "http", "url": "https://exa.proxy.internal/mcp", "command": "", "args": [],+ "env": {}, "active": false } } }),+ // A malformed entry.+ serde_json::json!({ "mcpServers": { "exa": "oops" } }),+ // No exa at all.+ serde_json::json!({ "mcpServers": { "fetch": { "command": "uvx", "args": [], "env": {} } } }),+ // No servers object.+ serde_json::json!({ "mcpSettings": {} }),+ ] {+ std::fs::write(&path, serde_json::to_string_pretty(&config).unwrap()).unwrap();+ let bytes = std::fs::read(&path).unwrap();+ assert!(!remove_default_exa_from_config(&path).unwrap(), "{config}");+ assert_eq!(std::fs::read(&path).unwrap(), bytes, "{config}");+ }+}++/// A cutover that could not read or write the config just then must not be+/// recorded as done: the next startup has to try it again, or the entry stays for+/// good even after the problem is gone.+#[test]+fn a_failed_exa_cutover_is_retried_at_the_next_startup() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("mcp_config.json");++ // A config that cannot be read right now (a directory stands in for it).+ std::fs::create_dir(&path).unwrap();+ let failed = remove_default_exa_from_config(&path).map(|_| ());+ assert!(failed.is_err());+ let recorded = schema_after_exa_cutover(&failed);+ assert!(+ recorded < CURRENT_MCP_SCHEMA_VERSION,+ "a failed cutover recorded as schema {recorded} would never run again"+ );++ // The next startup: the config is readable again, so the cutover runs.+ std::fs::remove_dir(&path).unwrap();+ std::fs::copy(fixture_root("0.8.3").join("mcp_config.json"), &path).unwrap();+ let retried = remove_default_exa_from_config(&path).map(|_| ());+ assert!(retried.is_ok());+ assert_eq!(+ schema_after_exa_cutover(&retried),+ CURRENT_MCP_SCHEMA_VERSION+ );+ let config: Value = serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();+ assert!(config["mcpServers"].get("exa").is_none());++ // Nothing to remove is a success too: no reason to keep retrying.+ let nothing = remove_default_exa_from_config(&path).map(|_| ());+ assert_eq!(+ schema_after_exa_cutover(¬hing),+ CURRENT_MCP_SCHEMA_VERSION+ );+}