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"));
|
||||
|
||||
Reference in new issue
Block a user