From 70be283d0eba88cbe7a227f7edb5ac8201cfee53 Mon Sep 17 00:00:00 2001 From: Louis Date: Mon, 14 Apr 2025 22:03:06 +0700 Subject: [PATCH] chore: add mcp write and read commands --- src-tauri/src/core/cmd.rs | 29 +++++++++++++++++++++++++ src-tauri/src/lib.rs | 2 ++ web/screens/Settings/MCP/index.tsx | 34 ++++-------------------------- web/services/tauriService.ts | 2 ++ 4 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src-tauri/src/core/cmd.rs b/src-tauri/src/core/cmd.rs index ca3d051af..3d7d921ee 100644 --- a/src-tauri/src/core/cmd.rs +++ b/src-tauri/src/core/cmd.rs @@ -7,6 +7,9 @@ use tauri::{AppHandle, Manager, Runtime, State}; use super::{server, setup, state::AppState}; const CONFIGURATION_FILE_NAME: &str = "settings.json"; +const DEFAULT_MCP_CONFIG: &str = r#"{ + "mcpServers": {} +}"#; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct AppConfiguration { @@ -352,3 +355,29 @@ pub async fn call_tool( Err(format!("Tool {} not found", tool_name)) } + +#[tauri::command] +pub async fn get_mcp_configs(app: AppHandle) -> Result { + let mut path = get_jan_data_folder_path(app); + path.push("mcp_config.json"); + log::info!("read mcp configs, path: {:?}", path); + + // Create default empty config if file doesn't exist + if !path.exists() { + log::info!("mcp_config.json not found, creating default empty config"); + fs::write(&path, DEFAULT_MCP_CONFIG) + .map_err(|e| format!("Failed to create default MCP config: {}", e))?; + } + + let contents = fs::read_to_string(path).map_err(|e| e.to_string())?; + return Ok(contents); +} + +#[tauri::command] +pub async fn save_mcp_configs(app: AppHandle, configs: String) -> Result<(), String> { + let mut path = get_jan_data_folder_path(app); + path.push("mcp_config.json"); + log::info!("save mcp configs, path: {:?}", path); + + fs::write(path, configs).map_err(|e| e.to_string()) +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index f37d97ae2..7248e15cc 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -39,6 +39,8 @@ pub fn run() { core::cmd::app_token, core::cmd::start_server, core::cmd::stop_server, + core::cmd::save_mcp_configs, + core::cmd::get_mcp_configs, // MCP commands core::cmd::get_tools, core::cmd::call_tool, diff --git a/web/screens/Settings/MCP/index.tsx b/web/screens/Settings/MCP/index.tsx index 28f26d5f0..90cb785f5 100644 --- a/web/screens/Settings/MCP/index.tsx +++ b/web/screens/Settings/MCP/index.tsx @@ -17,31 +17,9 @@ const MCP = () => { const readConfigFile = useCallback(async () => { try { - const configPath = await joinPath([janDataFolderPath, 'mcp_config.json']) - - // Check if the file exists - const fileExists = await fs.existsSync(configPath) - - if (fileExists) { - // Read the file - const content = await fs.readFileSync(configPath, 'utf-8') - setConfigContent(content) - } else { - // Create a default config if it doesn't exist - const defaultConfig = JSON.stringify( - { - servers: [], - settings: { - enabled: true, - }, - }, - null, - 2 - ) - - await fs.writeFileSync(configPath, defaultConfig) - setConfigContent(defaultConfig) - } + // Read the file + const content = await window.core?.api.getMcpConfigs() + setConfigContent(content) setError('') } catch (err) { @@ -70,11 +48,7 @@ const MCP = () => { setIsSaving(false) return } - - const configPath = await joinPath([janDataFolderPath, 'mcp_config.json']) - - // Write to the file - await fs.writeFileSync(configPath, configContent) + await window.core?.api?.saveMcpConfigs({ configs: configContent }) setSuccess('Config saved successfully') setIsSaving(false) diff --git a/web/services/tauriService.ts b/web/services/tauriService.ts index 2c592f951..666383bd1 100644 --- a/web/services/tauriService.ts +++ b/web/services/tauriService.ts @@ -19,6 +19,8 @@ export const Routes = [ 'getThreadAssistant', 'createThreadAssistant', 'modifyThreadAssistant', + 'saveMcpConfigs', + 'getMcpConfigs', ].map((r) => ({ path: `app`, route: r,