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): route multi-statement scripts to batch execution

zipg832e08ae1f5b
diff --git a/crates/dbx-mcp/src/server.rs b/crates/dbx-mcp/src/server.rs
index f684aeeeac..d22a5d551b 100644
--- a/crates/dbx-mcp/src/server.rs
+++ b/crates/dbx-mcp/src/server.rs
@@ -605,7 +605,7 @@ impl DbxMcpServer {
#[tool(
name = "dbx_execute_query",
- description = "Execute a SQL query on a database connection (max 100 rows returned)"
+ description = "Execute a SQL query on a database connection (max 100 rows returned). For backwards compatibility, multi-statement scripts and stored-procedure scripts are routed through the dialect-aware batch executor."
)]
async fn execute_query(&self, Parameters(request): Parameters<ExecuteQueryRequest>) -> CallToolResult {
if let Err(error) = self.ensure_tool_allowed("dbx_execute_query").await {
@@ -624,6 +624,18 @@ impl DbxMcpServer {
"Redis connections do not accept SQL through dbx_execute_query. Use dbx_execute_redis_command.",
);
}
+ if sql_requires_batch_execution(&request.sql, connection.db_type) {
+ return self
+ .execute_batch_request(ExecuteBatchQueryRequest {
+ selector: ConnectionSelector { connection_id: Some(connection.id.clone()), connection_name: None },
+ database: request.database.clone(),
+ sql: request.sql.clone(),
+ session_id: request.session_id.clone(),
+ continue_on_error: None,
+ use_transaction: None,
+ })
+ .await;
+ }
// Database discovery does not require a default database. In a
// narrowed MCP scope it must never reveal names outside the allowlist,
// so serve it directly from the scoped discovery path instead of
@@ -751,6 +763,10 @@ impl DbxMcpServer {
if let Err(error) = self.ensure_tool_allowed("dbx_execute_batch").await {
return error;
}
+ self.execute_batch_request(request).await
+ }
+
+ async fn execute_batch_request(&self, request: ExecuteBatchQueryRequest) -> CallToolResult {
let resolved = match self.resolve_connection(&request.selector).await {
Ok(resolved) => resolved,
Err(error) => return error,
@@ -1838,6 +1854,17 @@ fn backend_tool_error(default_code: &str, error: impl Into<String>) -> CallToolR
tool_error(default_code, error)
}
+/// Keep older MCP clients compatible with the original single-query tool when
+/// they send a complete SQL script. The batch executor removes client-side
+/// commands such as MySQL `DELIMITER` and preserves semicolons inside routine
+/// bodies before dispatching statements to the database.
+fn sql_requires_batch_execution(sql: &str, database_type: DatabaseType) -> bool {
+ if sql.lines().any(|line| line.trim_start().to_ascii_lowercase().starts_with("delimiter ")) {
+ return true;
+ }
+ dbx_core::sql::sql_execution_plan_for_database(sql, database_type).statements.len() > 1
+}
+
/// Maximum rows returned per statement in a `dbx_execute_batch` call, matching
/// the `dbx_execute_query` "at most 100 rows" contract so batch responses stay
/// bounded.
@@ -4053,6 +4080,17 @@ mod tests {
assert!(confirmed_batch_sql_block_reason("NOT VALID SQL ;;;", postgres_db_type, confirmed).is_some());
}
+ #[test]
+ fn execute_query_routes_scripts_to_the_dialect_aware_batch_path() {
+ assert!(sql_requires_batch_execution("SELECT 1; SELECT 2", DatabaseType::Postgres));
+ assert!(sql_requires_batch_execution(
+ "DELIMITER $$\nCREATE PROCEDURE p() BEGIN SELECT 1; END$$\nDELIMITER ;",
+ DatabaseType::Mysql
+ ));
+ assert!(!sql_requires_batch_execution("SELECT 1;", DatabaseType::Mysql));
+ assert!(!sql_requires_batch_execution("SELECT 1", DatabaseType::Mysql));
+ }
+
#[test]
fn format_batch_results_renders_failed_statement() {
let results = vec![crate::backend::BatchStatementResult {