feat: restart MCP servers to reflect config updates (#4917)

This commit is contained in:
Louis 2025-04-17 15:25:24 +07:00 committed by GitHub
parent a0d8d9cb27
commit c9bef9ff35
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 36 additions and 2 deletions

View File

@ -2,8 +2,11 @@ use std::{collections::HashMap, sync::Arc};
use rmcp::{service::RunningService, transport::TokioChildProcess, RoleClient, ServiceExt};
use serde_json::Value;
use tauri::{AppHandle, State};
use tokio::{process::Command, sync::Mutex};
use super::{cmd::get_jan_data_folder_path, state::AppState};
/// Runs MCP commands by reading configuration from a JSON file and initializing servers
///
/// # Arguments
@ -77,6 +80,35 @@ fn extract_command_args(
Some((command, args, envs))
}
#[tauri::command]
pub async fn restart_mcp_servers(
app: AppHandle,
state: State<'_, AppState>,
) -> Result<(), String> {
let app_path = get_jan_data_folder_path(app.clone());
let app_path_str = app_path.to_str().unwrap().to_string();
let servers = state.mcp_servers.clone();
// Stop the servers
stop_mcp_servers(state.mcp_servers.clone()).await?;
// Restart the servers
run_mcp_commands(app_path_str, servers).await
}
pub async fn stop_mcp_servers(
servers_state: Arc<Mutex<HashMap<String, RunningService<RoleClient, ()>>>>,
) -> Result<(), String> {
let mut servers_map = servers_state.lock().await;
let keys: Vec<String> = servers_map.keys().cloned().collect();
for key in keys {
if let Some(service) = servers_map.remove(&key) {
service.cancel().await.map_err(|e| e.to_string())?;
}
}
drop(servers_map); // Release the lock after stopping
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -44,6 +44,7 @@ pub fn run() {
// MCP commands
core::cmd::get_tools,
core::cmd::call_tool,
core::mcp::restart_mcp_servers,
// Threads
core::threads::list_threads,
core::threads::create_thread,

View File

@ -46,6 +46,7 @@ const MCPConfiguration = () => {
return
}
await window.core?.api?.saveMcpConfigs({ configs: configContent })
await window.core?.api?.restartMcpServers()
setSuccess('Config saved successfully')
setIsSaving(false)

View File

@ -188,6 +188,7 @@ const MCPSearch = () => {
await window.core?.api?.saveMcpConfigs({
configs: JSON.stringify(config, null, 2),
})
await window.core?.api?.restartMcpServers()
toaster({
title: `Add ${serverName} success`,

View File

@ -154,8 +154,6 @@ const ChatInput = () => {
}
}
console.log(tools)
return (
<div className="relative p-4 pb-2">
{renderPreview(fileUpload)}

View File

@ -21,6 +21,7 @@ export const Routes = [
'modifyThreadAssistant',
'saveMcpConfigs',
'getMcpConfigs',
'restartMcpServers',
].map((r) => ({
path: `app`,
route: r,