Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(mcp): sync runtime config cache so new desktop connections work without reload

t8y2ebb9b35c106f
diff --git a/crates/dbx-mcp/src/backend.rs b/crates/dbx-mcp/src/backend.rs
index 9e5fe8b2f0..239559d84c 100644
--- a/crates/dbx-mcp/src/backend.rs
+++ b/crates/dbx-mcp/src/backend.rs
@@ -308,6 +308,30 @@ impl LocalBackend {
pub fn state(&self) -> &Arc<AppState> {
&self.state
}
+
+ /// Sync the latest connection list from storage into the `AppState.configs` in-memory cache:
+ /// upsert new/changed entries and remove connections deleted from storage. Only LocalBackend
+ /// needs this — WebBackend talks HTTP and holds no local AppState, and the desktop mcp_bridge
+ /// shares the DBX process so it is unaffected by this cache desync.
+ async fn sync_runtime_configs(&self, configs: &[ConnectionConfig]) {
+ let mut runtime = self.state.configs.write().await;
+ for config in configs {
+ match runtime.get(&config.id) {
+ Some(existing) if existing == config => {}
+ _ => {
+ runtime.insert(config.id.clone(), config.clone());
+ }
+ }
+ }
+ let stale_ids: Vec<String> = runtime
+ .keys()
+ .filter(|id| !configs.iter().any(|config| &config.id == *id))
+ .cloned()
+ .collect();
+ for id in stale_ids {
+ runtime.remove(&id);
+ }
+ }
}
fn local_plugin_dir(settings: &DesktopSettings, data_dir: &Path) -> PathBuf {
@@ -329,7 +353,15 @@ impl DbxBackend for LocalBackend {
}
async fn load_connections(&self) -> Result<Vec<ConnectionConfig>, String> {
- self.state.storage.load_connections().await
+ let configs = self.state.storage.load_connections().await?;
+ // Connections created/modified/deleted in the DBX desktop UI after this process started
+ // only update the shared SQLite storage; the AppState.configs in-memory cache is not kept
+ // in sync. Sync the latest config into the runtime cache after each read, otherwise DB
+ // operations that look up the pool by id via get_or_create_pool fail with
+ // "Connection config not found" (the agent can list the connection but cannot use it,
+ // and a manual MCP reload is needed to recover).
+ self.sync_runtime_configs(&configs).await;
+ Ok(configs)
}
async fn execute_agent_tool(
diff --git a/crates/dbx-mcp/tests/local.rs b/crates/dbx-mcp/tests/local.rs
index cde4c1bfe5..15f605afa9 100644
--- a/crates/dbx-mcp/tests/local.rs
+++ b/crates/dbx-mcp/tests/local.rs
@@ -1,7 +1,7 @@
use std::{ffi::OsString, sync::Arc};
use dbx_core::{models::connection::ConnectionConfig, storage::Storage};
-use dbx_mcp::{DbxBackend, DbxMcpServer, LocalBackend, McpScope};
+use dbx_mcp::{DbxMcpServer, LocalBackend, McpScope};
use rmcp::{model::CallToolRequestParams, ServiceExt};
use serde_json::{json, Map, Value};
use tempfile::tempdir;
@@ -65,8 +65,86 @@ async fn local_backend_reads_dbx_storage_without_desktop_process() {
server_task.abort();
}
-#[cfg(feature = "duckdb-sidecar")]
#[tokio::test]
+async fn local_backend_picks_up_connections_added_after_startup_without_reload() {
+ // Regression for issue #5428: after an agent connects to MCP, a connection created in the DBX
+ // desktop UI is not reflected in the MCP server's AppState.configs in-memory cache, so the
+ // agent can list the new connection but executing an operation fails with
+ // "Connection config not found". After the fix the new connection is usable without reload.
+ let directory = tempdir().expect("temporary data directory");
+ let db_path = directory.path().join("dbx.db");
+ let storage = Storage::open(&db_path).await.expect("open storage");
+ let initial: ConnectionConfig = serde_json::from_value(json!({
+ "id": "startup-sqlite",
+ "name": "startup-sqlite",
+ "db_type": "sqlite",
+ "host": ":memory:",
+ "port": 0,
+ "username": "",
+ "password": "",
+ "database": "",
+ "ssl": false
+ }))
+ .expect("initial connection config");
+ storage.save_connections(std::slice::from_ref(&initial)).await.expect("save initial connection");
+
+ let backend = Arc::new(LocalBackend::open(&db_path).await.expect("open local backend"));
+ let server = DbxMcpServer::with_runtime_options(backend.clone(), McpScope::default(), false);
+ let (server_transport, client_transport) = tokio::io::duplex(16 * 1024);
+ let server_task = tokio::spawn(async move { server.serve(server_transport).await });
+ let client = ().serve(client_transport).await.expect("initialize client");
+
+ // Simulate a connection created in the DBX desktop UI: write directly to the shared storage,
+ // bypassing the MCP server's in-process cache.
+ let added: ConnectionConfig = serde_json::from_value(json!({
+ "id": "added-sqlite",
+ "name": "added-sqlite",
+ "db_type": "sqlite",
+ "host": ":memory:",
+ "port": 0,
+ "username": "",
+ "password": "",
+ "database": "",
+ "ssl": false
+ }))
+ .expect("added connection config");
+ storage.save_connections(&[initial, added.clone()]).await.expect("save added connection");
+
+ // list_connections reads storage live, so the new connection is already visible.
+ let list_result = client
+ .peer()
+ .call_tool(CallToolRequestParams::new("dbx_list_connections"))
+ .await
+ .expect("list connections");
+ let list_text = list_result.content[0].as_text().expect("text response").text.clone();
+ assert!(list_text.contains("added-sqlite"), "list should include added connection: {list_text}");
+
+ // execute_query resolves through the AppState.configs cache: before the fix this failed with
+ // "Connection config not found"; after the fix load_connections syncs the cache and the query
+ // succeeds.
+ let arguments = json!({
+ "connection_id": "added-sqlite",
+ "sql": "SELECT 1",
+ })
+ .as_object()
+ .cloned()
+ .unwrap_or_else(Map::<String, Value>::new);
+ let result = client
+ .peer()
+ .call_tool(CallToolRequestParams::new("dbx_execute_query").with_arguments(arguments))
+ .await
+ .expect("execute query on added connection");
+ let text = result.content[0].as_text().expect("text result").text.clone();
+ assert_ne!(result.is_error, Some(true), "query on added connection failed: {text}");
+ assert!(text.contains('1'), "unexpected query result: {text}");
+
+ client.cancel().await.expect("close client");
+ server_task.abort();
+ drop(backend);
+}
+
+#[tokio::test]
+#[cfg(feature = "duckdb-sidecar")]
async fn local_backend_uses_the_installed_duckdb_sidecar() {
let directory = tempdir().expect("temporary data directory");
let missing_driver = directory.path().join("missing-duckdb-driver");