feat(mcp): add stateful pinned query sessions

This commit is contained in:
lizujie authored and GitHub committed 2026-07-30 00:58:40 +08:00
1 parent d347c77c3d
commit f5daba54b6
9 files changed
+704 -57

No files matched your search

+2 -1
View File
@@ -344,6 +344,7 @@ pub async fn ensure_sql(
connection_id: &str,
database: &str,
sql: &str,
allow_database_switch: bool,
) -> Result<(), AppError> {
if !is_mcp_request(headers) {
return Ok(());
@@ -351,7 +352,7 @@ pub async fn ensure_sql(
let policy = load_policy(state).await?;
ensure_allowed(&policy, connection_id)?;
let config = load_connection(state, connection_id).await?;
if dbx_core::sql_risk::mcp_sql_has_forbidden_database_switch(sql, config.db_type) {
if !allow_database_switch && dbx_core::sql_risk::mcp_sql_has_forbidden_database_switch(sql, config.db_type) {
return Err(AppError::from(
"SQL_BLOCKED: MCP does not allow USE or persistent database switching.".to_string(),
));
+7 -3
View File
@@ -331,7 +331,9 @@ pub async fn execute_query(
headers: HeaderMap,
Json(req): Json<ExecuteQueryRequest>,
) -> Result<Json<dbx_core::db::QueryResult>, AppError> {
super::mcp_policy::ensure_sql(&state, &headers, &req.connection_id, &req.database, &req.sql).await?;
let allow_database_switch = req.client_session_id.as_deref().is_some_and(|id| !id.trim().is_empty());
super::mcp_policy::ensure_sql(&state, &headers, &req.connection_id, &req.database, &req.sql, allow_database_switch)
.await?;
let execution_id = req.execution_id.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
let registered = state.app.running_queries.register_task(
@@ -375,7 +377,9 @@ pub async fn execute_multi(
headers: HeaderMap,
Json(req): Json<ExecuteQueryRequest>,
) -> Result<Json<Vec<dbx_core::query::ExecuteMultiResult>>, AppError> {
super::mcp_policy::ensure_sql(&state, &headers, &req.connection_id, &req.database, &req.sql).await?;
let allow_database_switch = req.client_session_id.as_deref().is_some_and(|id| !id.trim().is_empty());
super::mcp_policy::ensure_sql(&state, &headers, &req.connection_id, &req.database, &req.sql, allow_database_switch)
.await?;
let execution_id = req.execution_id.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
let registered = state.app.running_queries.register_task(
@@ -420,7 +424,7 @@ pub async fn execute_batch(
Json(req): Json<ExecuteBatchRequest>,
) -> Result<Json<dbx_core::db::QueryResult>, AppError> {
for statement in &req.statements {
super::mcp_policy::ensure_sql(&state, &headers, &req.connection_id, &req.database, statement).await?;
super::mcp_policy::ensure_sql(&state, &headers, &req.connection_id, &req.database, statement, false).await?;
}
tracing::debug!(connection_id = %req.connection_id, "execute_batch");
let result = dbx_core::query::execute_statements(