1 parent
5cfb457903
commit
396ebf8393
7 files changed
+535
-6
No files matched your search
@@ -184,6 +184,12 @@ pub trait DbxBackend: Send + Sync {
|
||||
Err("SQL queries are not supported by this backend.".to_string())
|
||||
}
|
||||
async fn add_connection_for_mcp(&self, config: ConnectionConfig) -> Result<ConnectionConfig, String>;
|
||||
async fn duplicate_connection_for_mcp(
|
||||
&self,
|
||||
source_id: &str,
|
||||
copy_id: &str,
|
||||
copy_name: &str,
|
||||
) -> Result<ConnectionConfig, String>;
|
||||
async fn remove_connection_for_mcp(&self, connection_id: &str) -> Result<bool, String>;
|
||||
async fn list_tables(
|
||||
&self,
|
||||
@@ -532,6 +538,17 @@ impl DbxBackend for LocalBackend {
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
async fn duplicate_connection_for_mcp(
|
||||
&self,
|
||||
source_id: &str,
|
||||
copy_id: &str,
|
||||
copy_name: &str,
|
||||
) -> Result<ConnectionConfig, String> {
|
||||
let config = self.state.storage.duplicate_connection_for_mcp(source_id, copy_id, copy_name).await?;
|
||||
self.state.configs.write().await.insert(config.id.clone(), config.clone());
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
async fn remove_connection_for_mcp(&self, connection_id: &str) -> Result<bool, String> {
|
||||
let removed = self.state.storage.remove_connection_for_mcp(connection_id).await?;
|
||||
if removed {
|
||||
@@ -784,6 +801,23 @@ impl DbxBackend for WebBackend {
|
||||
.map_err(|error| format!("Invalid MCP connection response: {error}"))
|
||||
}
|
||||
|
||||
async fn duplicate_connection_for_mcp(
|
||||
&self,
|
||||
source_id: &str,
|
||||
copy_id: &str,
|
||||
copy_name: &str,
|
||||
) -> Result<ConnectionConfig, String> {
|
||||
self.request(
|
||||
reqwest::Method::POST,
|
||||
"/api/connection/mcp/duplicate",
|
||||
Some(json!({ "sourceId": source_id, "copyId": copy_id, "copyName": copy_name })),
|
||||
)
|
||||
.await?
|
||||
.json()
|
||||
.await
|
||||
.map_err(|error| format!("Invalid MCP connection response: {error}"))
|
||||
}
|
||||
|
||||
async fn remove_connection_for_mcp(&self, connection_id: &str) -> Result<bool, String> {
|
||||
let removed = self
|
||||
.request(
|
||||
@@ -2018,6 +2052,14 @@ mod tests {
|
||||
async fn add_connection_for_mcp(&self, config: ConnectionConfig) -> Result<ConnectionConfig, String> {
|
||||
Ok(config)
|
||||
}
|
||||
async fn duplicate_connection_for_mcp(
|
||||
&self,
|
||||
_source_id: &str,
|
||||
_copy_id: &str,
|
||||
_copy_name: &str,
|
||||
) -> Result<ConnectionConfig, String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
async fn remove_connection_for_mcp(&self, _connection_id: &str) -> Result<bool, String> {
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
@@ -111,6 +111,14 @@ pub struct AddConnectionRequest {
|
||||
pub driver_profile: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, schemars::JsonSchema)]
|
||||
pub struct DuplicateConnectionRequest {
|
||||
#[serde(flatten)]
|
||||
pub selector: ConnectionSelector,
|
||||
#[schemars(description = "Name for the copied connection")]
|
||||
pub new_name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, schemars::JsonSchema)]
|
||||
pub struct RemoveConnectionRequest {
|
||||
pub connection_name: String,
|
||||
@@ -218,6 +226,7 @@ impl DbxMcpServer {
|
||||
let mut tool_router = Self::tool_router();
|
||||
if scope.enabled() {
|
||||
tool_router.disable_route("dbx_add_connection");
|
||||
tool_router.disable_route("dbx_duplicate_connection");
|
||||
tool_router.disable_route("dbx_remove_connection");
|
||||
}
|
||||
// Desktop UI bridge operations are intentionally unavailable remotely and in scoped AI sessions.
|
||||
@@ -648,6 +657,67 @@ impl DbxMcpServer {
|
||||
}
|
||||
}
|
||||
|
||||
#[tool(
|
||||
name = "dbx_duplicate_connection",
|
||||
description = "Duplicate a DBX connection with its complete settings, credentials, tunnels, and sidebar group"
|
||||
)]
|
||||
async fn duplicate_connection(
|
||||
&self,
|
||||
Parameters(request): Parameters<DuplicateConnectionRequest>,
|
||||
) -> CallToolResult {
|
||||
let policy = match self.load_policy().await {
|
||||
Ok(policy) => policy,
|
||||
Err(error) => return error,
|
||||
};
|
||||
if policy.read_only {
|
||||
return tool_error(
|
||||
"MCP_READ_ONLY",
|
||||
"DBX global MCP read-only mode is enabled. Connection management is not allowed.",
|
||||
);
|
||||
}
|
||||
let connections = match self.backend.load_connections().await {
|
||||
Ok(connections) => connections,
|
||||
Err(error) => return tool_error("CONNECTION_LOAD_ERROR", error),
|
||||
};
|
||||
let allowed = connections
|
||||
.iter()
|
||||
.filter(|connection| policy_allows_connection(&policy, connection))
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
let source =
|
||||
if let Some(id) = request.selector.connection_id.as_deref().map(str::trim).filter(|id| !id.is_empty()) {
|
||||
allowed.iter().find(|connection| connection.id == id).cloned()
|
||||
} else if let Some(name) =
|
||||
request.selector.connection_name.as_deref().map(str::trim).filter(|name| !name.is_empty())
|
||||
{
|
||||
let matching = allowed
|
||||
.iter()
|
||||
.filter(|connection| connection.name.eq_ignore_ascii_case(name))
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
if matching.len() > 1 {
|
||||
return tool_error("AMBIGUOUS_CONNECTION", ambiguous_connections(name, &matching));
|
||||
}
|
||||
matching.into_iter().next()
|
||||
} else {
|
||||
return tool_error("CONNECTION_NOT_FOUND", "Either connection_id or connection_name is required.");
|
||||
};
|
||||
let Some(source) = source else {
|
||||
return tool_error("CONNECTION_NOT_FOUND", "The source connection was not found or is outside MCP scope.");
|
||||
};
|
||||
let new_name = request.new_name.trim();
|
||||
if new_name.is_empty() {
|
||||
return tool_error("INVALID_CONNECTION", "The copied connection name must not be empty.");
|
||||
}
|
||||
if connections.iter().any(|connection| connection.name.eq_ignore_ascii_case(new_name)) {
|
||||
return tool_error("CONNECTION_ALREADY_EXISTS", format!("Connection \"{new_name}\" already exists."));
|
||||
}
|
||||
match self.backend.duplicate_connection_for_mcp(&source.id, &Uuid::new_v4().to_string(), new_name).await {
|
||||
Ok(copy) => text(format!("Connection \"{}\" duplicated (id: {}).", copy.name, copy.id)),
|
||||
Err(error) => backend_tool_error("CONNECTION_SAVE_ERROR", error),
|
||||
}
|
||||
}
|
||||
|
||||
#[tool(name = "dbx_remove_connection", description = "Remove a database connection from DBX")]
|
||||
async fn remove_connection(&self, Parameters(request): Parameters<RemoveConnectionRequest>) -> CallToolResult {
|
||||
let policy = match self.load_policy().await {
|
||||
@@ -1348,6 +1418,23 @@ mod tests {
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
async fn duplicate_connection_for_mcp(
|
||||
&self,
|
||||
source_id: &str,
|
||||
copy_id: &str,
|
||||
copy_name: &str,
|
||||
) -> Result<ConnectionConfig, String> {
|
||||
let mut copy = self
|
||||
.connections
|
||||
.iter()
|
||||
.find(|connection| connection.id == source_id)
|
||||
.cloned()
|
||||
.ok_or_else(|| "source not found".to_string())?;
|
||||
copy.id = copy_id.to_string();
|
||||
copy.name = copy_name.to_string();
|
||||
Ok(copy)
|
||||
}
|
||||
|
||||
async fn remove_connection_for_mcp(&self, _connection_id: &str) -> Result<bool, String> {
|
||||
Ok(true)
|
||||
}
|
||||
@@ -1374,12 +1461,13 @@ mod tests {
|
||||
let server = DbxMcpServer::with_runtime_options(Arc::new(FakeBackend::default()), McpScope::default(), false);
|
||||
let tools = server.tool_router.list_all();
|
||||
let names = tools.iter().map(|tool| tool.name.as_ref()).collect::<Vec<_>>();
|
||||
assert_eq!(tools.len(), 12);
|
||||
assert_eq!(tools.len(), 13);
|
||||
assert!(names.contains(&"dbx_list_connections"));
|
||||
assert!(names.contains(&"dbx_list_tables"));
|
||||
assert!(names.contains(&"dbx_describe_table"));
|
||||
assert!(names.contains(&"dbx_execute_query"));
|
||||
assert!(names.contains(&"dbx_add_connection"));
|
||||
assert!(names.contains(&"dbx_duplicate_connection"));
|
||||
assert!(names.contains(&"dbx_remove_connection"));
|
||||
assert!(names.contains(&"dbx_execute_redis_command"));
|
||||
assert!(names.contains(&"dbx_get_schema_context"));
|
||||
@@ -1399,6 +1487,7 @@ mod tests {
|
||||
let names = server.tool_router.list_all().into_iter().map(|tool| tool.name).collect::<Vec<_>>();
|
||||
assert_eq!(names.len(), 8);
|
||||
assert!(!names.iter().any(|name| name == "dbx_add_connection"));
|
||||
assert!(!names.iter().any(|name| name == "dbx_duplicate_connection"));
|
||||
assert!(!names.iter().any(|name| name == "dbx_remove_connection"));
|
||||
assert!(!names.iter().any(|name| name == "dbx_open_table"));
|
||||
assert!(!names.iter().any(|name| name == "dbx_execute_and_show"));
|
||||
|
||||
@@ -84,6 +84,192 @@ async fn local_backend_reads_dbx_storage_without_desktop_process() {
|
||||
server_task.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn duplicate_connection_preserves_secrets_ssh_and_sidebar_group() {
|
||||
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");
|
||||
storage
|
||||
.save_mcp_global_policy(&dbx_core::storage::McpGlobalPolicy {
|
||||
read_only: false,
|
||||
allow_dangerous_sql: false,
|
||||
allowed_connection_ids: None,
|
||||
})
|
||||
.await
|
||||
.expect("enable MCP connection management");
|
||||
let source: ConnectionConfig = serde_json::from_value(json!({
|
||||
"id": "source",
|
||||
"name": "production-through-bastion",
|
||||
"note": "full configuration must survive",
|
||||
"db_type": "postgres",
|
||||
"driver_profile": "postgres-42.7",
|
||||
"host": "db.internal",
|
||||
"port": 5432,
|
||||
"username": "app",
|
||||
"password": "database-secret",
|
||||
"database": "app",
|
||||
"default_schema": "private",
|
||||
"url_params": "application_name=dbx",
|
||||
"connection_string": "postgres://app:database-secret@db.internal/app",
|
||||
"init_script": "SET application_name = 'dbx-secret-script'",
|
||||
"save_password": true,
|
||||
"ssl": true,
|
||||
"transport_layers": [{
|
||||
"type": "ssh",
|
||||
"id": "bastion",
|
||||
"name": "Bastion",
|
||||
"enabled": true,
|
||||
"host": "bastion.internal",
|
||||
"port": 22,
|
||||
"user": "deploy",
|
||||
"password": "ssh-secret",
|
||||
"key_path": "/keys/deploy",
|
||||
"key_passphrase": "key-secret",
|
||||
"auth_method": "key+password"
|
||||
}]
|
||||
}))
|
||||
.expect("full source connection");
|
||||
let unrelated: ConnectionConfig = serde_json::from_value(json!({
|
||||
"id": "unrelated",
|
||||
"name": "unrelated",
|
||||
"db_type": "sqlite",
|
||||
"host": ":memory:",
|
||||
"port": 0,
|
||||
"username": "",
|
||||
"password": "",
|
||||
"ssl": false
|
||||
}))
|
||||
.expect("unrelated connection");
|
||||
storage.save_connections(&[source.clone(), unrelated]).await.expect("save source connections");
|
||||
storage
|
||||
.save_sidebar_layout(&json!({
|
||||
"groups": [
|
||||
{ "id": "project", "name": "Project", "collapsed": false },
|
||||
{ "id": "production", "name": "Production", "collapsed": false }
|
||||
],
|
||||
"order": [{
|
||||
"type": "group",
|
||||
"id": "project",
|
||||
"children": [{
|
||||
"type": "group",
|
||||
"id": "production",
|
||||
"children": [{ "type": "connection", "id": "source" }]
|
||||
}]
|
||||
}, { "type": "connection", "id": "unrelated" }],
|
||||
"futureField": { "preserved": true }
|
||||
}))
|
||||
.await
|
||||
.expect("save sidebar layout");
|
||||
|
||||
let backend = Arc::new(LocalBackend::open(&db_path).await.expect("open local backend"));
|
||||
let policy = backend.load_mcp_global_policy().await.expect("load configured policy");
|
||||
assert!(!policy.read_only);
|
||||
let server = DbxMcpServer::with_runtime_options(backend, 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");
|
||||
let arguments = json!({
|
||||
"connection_id": "source",
|
||||
"new_name": "production-through-bastion-copy"
|
||||
})
|
||||
.as_object()
|
||||
.cloned()
|
||||
.unwrap_or_else(Map::new);
|
||||
let result = client
|
||||
.peer()
|
||||
.call_tool(CallToolRequestParams::new("dbx_duplicate_connection").with_arguments(arguments.clone()))
|
||||
.await
|
||||
.expect("duplicate connection");
|
||||
assert_ne!(result.is_error, Some(true), "unexpected duplicate error: {:?}", result.content);
|
||||
|
||||
let duplicate_again = client
|
||||
.peer()
|
||||
.call_tool(CallToolRequestParams::new("dbx_duplicate_connection").with_arguments(arguments))
|
||||
.await
|
||||
.expect("reject duplicate target name");
|
||||
assert_eq!(duplicate_again.is_error, Some(true));
|
||||
assert!(duplicate_again.content[0]
|
||||
.as_text()
|
||||
.expect("duplicate error text")
|
||||
.text
|
||||
.contains("CONNECTION_ALREADY_EXISTS"));
|
||||
|
||||
let missing_arguments = json!({
|
||||
"connection_id": "missing",
|
||||
"new_name": "must-not-be-created"
|
||||
})
|
||||
.as_object()
|
||||
.cloned()
|
||||
.unwrap_or_else(Map::new);
|
||||
let missing = client
|
||||
.peer()
|
||||
.call_tool(CallToolRequestParams::new("dbx_duplicate_connection").with_arguments(missing_arguments))
|
||||
.await
|
||||
.expect("reject missing source");
|
||||
assert_eq!(missing.is_error, Some(true));
|
||||
assert!(missing.content[0].as_text().expect("missing error text").text.contains("CONNECTION_NOT_FOUND"));
|
||||
|
||||
let root_arguments = json!({
|
||||
"connection_id": "unrelated",
|
||||
"new_name": "unrelated-copy"
|
||||
})
|
||||
.as_object()
|
||||
.cloned()
|
||||
.unwrap_or_else(Map::new);
|
||||
let root_copy = client
|
||||
.peer()
|
||||
.call_tool(CallToolRequestParams::new("dbx_duplicate_connection").with_arguments(root_arguments))
|
||||
.await
|
||||
.expect("duplicate root connection");
|
||||
assert_ne!(root_copy.is_error, Some(true));
|
||||
|
||||
storage
|
||||
.save_mcp_global_policy(&dbx_core::storage::McpGlobalPolicy {
|
||||
read_only: true,
|
||||
allow_dangerous_sql: false,
|
||||
allowed_connection_ids: None,
|
||||
})
|
||||
.await
|
||||
.expect("enable MCP read-only policy");
|
||||
let read_only_arguments = json!({
|
||||
"connection_id": "source",
|
||||
"new_name": "must-not-pass-read-only"
|
||||
})
|
||||
.as_object()
|
||||
.cloned()
|
||||
.unwrap_or_else(Map::new);
|
||||
let read_only = client
|
||||
.peer()
|
||||
.call_tool(CallToolRequestParams::new("dbx_duplicate_connection").with_arguments(read_only_arguments))
|
||||
.await
|
||||
.expect("reject read-only duplicate");
|
||||
assert_eq!(read_only.is_error, Some(true));
|
||||
assert!(read_only.content[0].as_text().expect("read-only error text").text.contains("MCP_READ_ONLY"));
|
||||
client.cancel().await.expect("close client");
|
||||
server_task.abort();
|
||||
|
||||
let reopened = Storage::open(&db_path).await.expect("reopen storage");
|
||||
let connections = reopened.load_connections().await.expect("reload connections");
|
||||
assert_eq!(connections.len(), 4, "failed duplicates must not add connections");
|
||||
let copied = connections
|
||||
.iter()
|
||||
.find(|connection| connection.name == "production-through-bastion-copy")
|
||||
.expect("persisted copied connection");
|
||||
let mut expected = source.clone();
|
||||
expected.id = copied.id.clone();
|
||||
expected.name = copied.name.clone();
|
||||
assert_ne!(copied.id, source.id);
|
||||
assert_eq!(copied, &expected);
|
||||
let root_copy = connections.iter().find(|connection| connection.name == "unrelated-copy").expect("root copy");
|
||||
|
||||
let layout = reopened.load_sidebar_layout().await.expect("load copied layout").expect("sidebar layout");
|
||||
assert_eq!(layout["futureField"]["preserved"], true);
|
||||
assert_eq!(layout["order"][0]["children"][0]["children"][0]["id"], "source");
|
||||
assert_eq!(layout["order"][0]["children"][0]["children"][1]["id"], copied.id);
|
||||
assert_eq!(layout["order"][1]["id"], "unrelated");
|
||||
assert_eq!(layout["order"][2]["id"], root_copy.id);
|
||||
}
|
||||
|
||||
#[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
|
||||
@@ -192,7 +378,6 @@ async fn local_backend_uses_the_installed_duckdb_sidecar() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn legacy_read_only_config_applies_before_settings_are_opened() {
|
||||
let _allow_writes = EnvVarGuard::set("DBX_MCP_ALLOW_WRITES", "0");
|
||||
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");
|
||||
@@ -210,6 +395,7 @@ async fn legacy_read_only_config_applies_before_settings_are_opened() {
|
||||
}))
|
||||
.expect("minimal connection config");
|
||||
storage.save_connections(&[connection]).await.expect("save connection");
|
||||
let _allow_writes = EnvVarGuard::set("DBX_MCP_ALLOW_WRITES", "0");
|
||||
|
||||
let backend = Arc::new(LocalBackend::open(&db_path).await.expect("open local backend"));
|
||||
let server = DbxMcpServer::with_runtime_options(backend, McpScope::default(), false);
|
||||
|
||||
@@ -42,6 +42,15 @@ impl DbxBackend for EmptyBackend {
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
async fn duplicate_connection_for_mcp(
|
||||
&self,
|
||||
_source_id: &str,
|
||||
_copy_id: &str,
|
||||
_copy_name: &str,
|
||||
) -> Result<ConnectionConfig, String> {
|
||||
Err("not exercised".to_string())
|
||||
}
|
||||
|
||||
async fn remove_connection_for_mcp(&self, _connection_id: &str) -> Result<bool, String> {
|
||||
Ok(true)
|
||||
}
|
||||
@@ -88,6 +97,15 @@ impl DbxBackend for PolicyBackend {
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
async fn duplicate_connection_for_mcp(
|
||||
&self,
|
||||
_source_id: &str,
|
||||
_copy_id: &str,
|
||||
_copy_name: &str,
|
||||
) -> Result<ConnectionConfig, String> {
|
||||
Err("not exercised".to_string())
|
||||
}
|
||||
|
||||
async fn remove_connection_for_mcp(&self, _connection_id: &str) -> Result<bool, String> {
|
||||
Ok(true)
|
||||
}
|
||||
@@ -117,8 +135,9 @@ async fn initializes_lists_tools_and_calls_a_tool() {
|
||||
|
||||
let tools = client.peer().list_tools(None).await.expect("list tools");
|
||||
let names = tools.tools.iter().map(|tool| tool.name.as_ref()).collect::<Vec<_>>();
|
||||
assert_eq!(names.len(), 12);
|
||||
assert_eq!(names.len(), 13);
|
||||
assert!(names.contains(&"dbx_list_connections"));
|
||||
assert!(names.contains(&"dbx_duplicate_connection"));
|
||||
assert!(names.contains(&"dbx_execute_redis_command"));
|
||||
assert!(names.contains(&"dbx_execute_and_show"));
|
||||
assert!(names.contains(&"dbx_open_session"));
|
||||
@@ -194,6 +213,30 @@ async fn enforces_global_connection_scope_and_read_only_policy() {
|
||||
server_task.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn duplicate_connection_rejects_ambiguous_source_names() {
|
||||
let backend = PolicyBackend {
|
||||
policy: McpGlobalPolicy::default(),
|
||||
connections: vec![test_connection("first", "shared"), test_connection("second", "shared")],
|
||||
group_paths: Ok(HashMap::new()),
|
||||
};
|
||||
let (server_transport, client_transport) = tokio::io::duplex(16 * 1024);
|
||||
let server = DbxMcpServer::with_runtime_options(Arc::new(backend), McpScope::default(), false);
|
||||
let server_task = tokio::spawn(async move { server.serve(server_transport).await });
|
||||
let client = ().serve(client_transport).await.expect("initialize MCP client");
|
||||
let result = client
|
||||
.peer()
|
||||
.call_tool(CallToolRequestParams::new("dbx_duplicate_connection").with_arguments(
|
||||
json!({ "connection_name": "shared", "new_name": "copy" }).as_object().cloned().unwrap_or_else(Map::new),
|
||||
))
|
||||
.await
|
||||
.expect("call duplicate connection");
|
||||
assert_eq!(result.is_error, Some(true));
|
||||
assert!(result.content[0].as_text().expect("ambiguous result").text.contains("AMBIGUOUS_CONNECTION"));
|
||||
client.cancel().await.expect("close MCP client");
|
||||
server_task.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn connection_group_path_failure_preserves_connection_listing() {
|
||||
let backend = PolicyBackend {
|
||||
|
||||
Reference in new issue
Block a user