From 68d8776e8f145b1ba74fba39b8fef8cc6204d306 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Fri, 8 May 2026 12:44:16 +0800 Subject: [PATCH] feat(mcp): notify desktop UI to reload after connection add/remove Add /reload-connections bridge route that emits mcp-reload-connections event, triggering the frontend to re-read connections from SQLite. --- mcp/src/index.ts | 9 +++++++++ src-tauri/src/commands/mcp_bridge.rs | 3 +++ src/composables/useTauriEvents.ts | 3 +++ 3 files changed, 15 insertions(+) diff --git a/mcp/src/index.ts b/mcp/src/index.ts index 73f433b4b..4737eef22 100644 --- a/mcp/src/index.ts +++ b/mcp/src/index.ts @@ -159,6 +159,7 @@ server.tool( name, db_type, host, port, username, password, database, ssl, ssh_enabled: false, } as Omit); + await notifyReload(); return text(`Connection "${config.name}" added (id: ${config.id}).`); }, ); @@ -172,6 +173,7 @@ server.tool( async ({ connection_name }) => { const removed = await backend.removeConnection(connection_name); if (!removed) return text(`Connection "${connection_name}" not found.`); + await notifyReload(); return text(`Connection "${connection_name}" removed.`); }, ); @@ -200,6 +202,13 @@ async function getBridgeUrl(): Promise { return `http://127.0.0.1:${port}`; } +async function notifyReload(): Promise { + try { + const bridgeUrl = await getBridgeUrl(); + await fetch(`${bridgeUrl}/reload-connections`, { method: "POST" }); + } catch {} +} + // Desktop-only tools: open table and execute-and-show require the Tauri bridge if (!isWebMode) { server.tool( diff --git a/src-tauri/src/commands/mcp_bridge.rs b/src-tauri/src/commands/mcp_bridge.rs index efb4dccbb..e5622f668 100644 --- a/src-tauri/src/commands/mcp_bridge.rs +++ b/src-tauri/src/commands/mcp_bridge.rs @@ -74,6 +74,9 @@ pub fn start(app_handle: AppHandle, state: Arc) { handle_open_table(&app, &st, body, &mut stream).await; } else if first_line.starts_with("POST /execute-query") { handle_execute_query(&app, &st, body, &mut stream).await; + } else if first_line.starts_with("POST /reload-connections") { + let _ = app.emit("mcp-reload-connections", ()); + respond(&mut stream, "200 OK", "ok").await; } else { let _ = stream.write_all(b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n").await; } diff --git a/src/composables/useTauriEvents.ts b/src/composables/useTauriEvents.ts index 62a02bd22..9dc62c399 100644 --- a/src/composables/useTauriEvents.ts +++ b/src/composables/useTauriEvents.ts @@ -37,6 +37,9 @@ export function useTauriEvents(deps: { openTableTarget: (target: NavigationTarge ); }, ); + listen("mcp-reload-connections", async () => { + await connectionStore.initFromDisk(); + }); listen<{ connection_id: string; database: string; sql: string }>("mcp-execute-query", async (event) => { const { connection_id, database, sql } = event.payload; if (!connectionStore.connections.length) await connectionStore.initFromDisk();