feat(history): add unified MCP execution history

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-09-26 22:18:38 +08:00
1 parent dd82d948a9
commit 5c0ce7e4dc
39 files changed
+1261 -87

No files matched your search

+298 -5
View File
@@ -507,6 +507,157 @@ impl PendingSalesforceWrites {
}
}
// One context per dispatched request; legacy SQL helpers enrich it instead of
// writing duplicate rows. Direct internal calls retain their existing behavior.
tokio::task_local! {
static CALL_HISTORY: std::sync::Mutex<HistoryEntry>;
}
struct CallHistoryGuard {
backend: Arc<dyn DbxBackend>,
fallback: Option<HistoryEntry>,
started: Instant,
}
impl Drop for CallHistoryGuard {
fn drop(&mut self) {
if let Some(mut entry) = self.fallback.take() {
entry.execution_time_ms = self.started.elapsed().as_millis();
entry.error = Some("MCP request cancelled or interrupted".into());
let backend = self.backend.clone();
// rmcp may drop the handler future on cancellation. Persist outside
// that future so the interruption still has an audit record.
if let Ok(runtime) = tokio::runtime::Handle::try_current() {
runtime.spawn(async move {
if let Err(error) = backend.save_history_entry(&entry).await {
log::warn!("failed to save interrupted MCP history: {error}");
}
});
}
}
}
}
fn history_request(value: &serde_json::Value, depth: usize) -> serde_json::Value {
use serde_json::Value;
if depth > 8 {
return json!("[depth limit]");
}
match value {
Value::Object(fields) => Value::Object(
fields
.iter()
.take(64)
.map(|(key, value)| {
let lower = key.to_ascii_lowercase();
let secret = [
"password",
"passwd",
"secret",
"token",
"credential",
"authorization",
"private_key",
"apikey",
"api_key",
"connection_string",
"url",
"dsn",
]
.iter()
.any(|part| lower.contains(part));
(
key.chars().take(128).collect(),
if secret { json!("[redacted]") } else { history_request(value, depth + 1) },
)
})
.collect(),
),
Value::Array(values) => {
Value::Array(values.iter().take(16).map(|value| history_request(value, depth + 1)).collect())
}
Value::String(value) => json!(value.chars().take(1024).collect::<String>()),
value => value.clone(),
}
}
fn bounded_history_request(value: &serde_json::Value) -> String {
let sanitized = history_request(value, 0).to_string();
if sanitized.len() > 16 * 1024 {
json!({"truncated": true, "summary": "Request exceeds history size limit"}).to_string()
} else {
sanitized
}
}
// Response payloads need their own sanitizer: request summaries intentionally
// shorten strings and arrays, which would silently discard query results.
fn history_response(value: &serde_json::Value, depth: usize) -> serde_json::Value {
use serde_json::Value;
if depth > 32 {
return json!({"truncated": true, "reason": "depth limit"});
}
match value {
Value::Object(fields) => Value::Object(
fields
.iter()
.map(|(key, value)| {
let lower = key.to_ascii_lowercase();
let secret = [
"password",
"passwd",
"secret",
"token",
"credential",
"authorization",
"private_key",
"apikey",
"api_key",
"connection_string",
"url",
"dsn",
]
.iter()
.any(|part| lower.contains(part));
let binary = key == "data"
&& fields
.get("type")
.and_then(Value::as_str)
.is_some_and(|kind| kind == "image" || kind == "audio")
|| key == "blob";
(
key.clone(),
if secret || binary { json!("[redacted]") } else { history_response(value, depth + 1) },
)
})
.collect(),
),
Value::Array(values) => Value::Array(values.iter().map(|value| history_response(value, depth + 1)).collect()),
Value::String(text) => {
// MCP text blocks often contain JSON encoded inside a string.
// Sanitize that JSON too, while preserving the text-block shape.
if let Ok(parsed) = serde_json::from_str::<Value>(text) {
if parsed.is_object() || parsed.is_array() {
return json!(history_response(&parsed, depth + 1).to_string());
}
}
value.clone()
}
_ => value.clone(),
}
}
fn bounded_history_response<T: serde::Serialize>(value: &T) -> String {
let value = serde_json::to_value(value).unwrap_or_else(|_| json!({"serialization_error": true}));
let sanitized = history_response(&value, 0).to_string();
if sanitized.len() > 64 * 1024 {
// A short preview leaves room for JSON escaping and metadata.
json!({"truncated": true, "summary": "Response exceeds 64 KiB history size limit", "original_bytes": sanitized.len(), "preview": sanitized.chars().take(4096).collect::<String>()}).to_string()
} else {
sanitized
}
}
#[derive(Clone)]
pub struct DbxMcpServer {
backend: Arc<dyn DbxBackend>,
@@ -693,6 +844,7 @@ impl DbxMcpServer {
}
async fn save_mcp_sql_history(
&self,
tool_name: &str,
connection: &ConnectionConfig,
database: &str,
sql: &str,
@@ -717,7 +869,27 @@ impl DbxMcpServer {
affected_rows,
rollback_sql: None,
details_json: Some(r#"{"source":"mcp"}"#.to_string()),
source: "mcp".to_string(),
mcp_tool_name: Some(tool_name.to_string()),
mcp_request_json: None,
mcp_response_json: None,
mcp_session_id: None,
};
if CALL_HISTORY
.try_with(|current| {
let mut current = current.lock().unwrap();
current.connection_id = entry.connection_id.clone();
current.connection_name = entry.connection_name.clone();
current.database = entry.database.clone();
current.sql = entry.sql.clone();
current.activity_kind = entry.activity_kind.clone();
current.operation = entry.operation.clone();
current.affected_rows = entry.affected_rows;
})
.is_ok()
{
return;
}
if let Err(error) = self.backend.save_history_entry(&entry).await {
log::warn!("failed to save MCP SQL history for connection {}: {error}", connection.id);
}
@@ -1151,6 +1323,7 @@ impl DbxMcpServer {
})
.unwrap_or_else(|| format_query_result(&execution.result, max_rows as usize));
self.save_mcp_sql_history(
"dbx_execute_query",
&refreshed.connection,
&session.database,
&history_sql,
@@ -1164,6 +1337,7 @@ impl DbxMcpServer {
}
Err(error) => {
self.save_mcp_sql_history(
"dbx_execute_query",
&refreshed.connection,
&session.database,
&history_sql,
@@ -1204,7 +1378,17 @@ impl DbxMcpServer {
self.backend.execute_agent_tool(connection, &database, "execute_query", arguments, permissions).await;
let success = !result.is_error;
let error = result.is_error.then(|| result.content.trim_start_matches("Error: ").to_string());
self.save_mcp_sql_history(connection, &database, &history_sql, started_at, success, error, None).await;
self.save_mcp_sql_history(
"dbx_execute_query",
connection,
&database,
&history_sql,
started_at,
success,
error,
None,
)
.await;
agent_result(result)
}
@@ -1475,6 +1659,7 @@ impl DbxMcpServer {
}
let status = owner.status();
self.save_mcp_sql_history(
"dbx_execute_batch",
&refreshed.connection,
&session.database,
sql,
@@ -1555,6 +1740,7 @@ impl DbxMcpServer {
results.iter().map(|result| result.result.affected_rows).sum::<u64>().min(i64::MAX as u64) as i64
});
self.save_mcp_sql_history(
"dbx_execute_batch",
connection,
&database,
sql,
@@ -1573,8 +1759,17 @@ impl DbxMcpServer {
tool_result
}
Err(error) => {
self.save_mcp_sql_history(connection, &database, sql, started_at, false, Some(error.clone()), None)
.await;
self.save_mcp_sql_history(
"dbx_execute_batch",
connection,
&database,
sql,
started_at,
false,
Some(error.clone()),
None,
)
.await;
backend_tool_error("DBX_BATCH_EXECUTION_ERROR", error)
}
}
@@ -2578,8 +2773,17 @@ impl DbxMcpServer {
.await;
let success = !result.is_error;
let error = result.is_error.then(|| result.content.trim_start_matches("Error: ").to_string());
self.save_mcp_sql_history(connection, &pending.database, &pending.statement, started_at, success, error, None)
.await;
self.save_mcp_sql_history(
"dbx_salesforce_apply_write",
connection,
&pending.database,
&pending.statement,
started_at,
success,
error,
None,
)
.await;
if !success {
return agent_result(result);
}
@@ -2864,6 +3068,71 @@ impl DbxMcpServer {
#[tool_handler(router = self.tool_router)]
impl ServerHandler for DbxMcpServer {
async fn call_tool(
&self,
request: rmcp::model::CallToolRequestParams,
context: rmcp::service::RequestContext<rmcp::RoleServer>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let started = Instant::now();
let args = serde_json::Value::Object(request.arguments.clone().unwrap_or_default());
let arg = |name: &str| args.get(name).and_then(|value| value.as_str()).unwrap_or_default().to_string();
let entry = HistoryEntry {
id: Uuid::new_v4().to_string(),
connection_id: arg("connection_id"),
connection_name: arg("connection_name"),
database: arg("database"),
sql: arg("sql"),
executed_at: chrono::Utc::now().to_rfc3339(),
execution_time_ms: 0,
success: false,
error: None,
activity_kind: "mcp".into(),
operation: request.name.to_string(),
target: arg("table"),
affected_rows: None,
rollback_sql: None,
details_json: Some(r#"{"source":"mcp"}"#.into()),
source: "mcp".into(),
mcp_tool_name: Some(request.name.to_string()),
mcp_request_json: Some(bounded_history_request(&args)),
mcp_response_json: None,
mcp_session_id: args.get("session_id").and_then(|v| v.as_str()).map(str::to_owned),
};
let mut guard = CallHistoryGuard { backend: self.backend.clone(), fallback: Some(entry.clone()), started };
CALL_HISTORY.scope(std::sync::Mutex::new(entry), async {
let cancellation = context.ct.clone();
let tcc = rmcp::handler::server::tool::ToolCallContext::new(self, request, context);
let result = tokio::select! {
result = self.tool_router.call(tcc) => result,
_ = cancellation.cancelled() => Ok(tool_error("REQUEST_CANCELLED", "The MCP request was cancelled.")),
};
let mut entry = CALL_HISTORY.with(|entry| entry.lock().unwrap().clone());
entry.execution_time_ms = started.elapsed().as_millis();
entry.success = result.as_ref().is_ok_and(|result| result.is_error != Some(true));
// Keep the complete response within a bounded, redacted payload so
// the history detail view can be used for troubleshooting without
// allowing a large result to grow the local database indefinitely.
entry.mcp_response_json = Some(match &result {
Ok(result) => bounded_history_response(result),
Err(error) => bounded_history_response(error),
});
if !entry.success {
entry.error = Some(if cancellation.is_cancelled() { "MCP request cancelled" } else { "MCP tool call failed (see response summary)" }.into());
}
// Persist in a separate task so cancellation during the write cannot
// lose the record or schedule a second write from the drop guard.
guard.fallback = None;
let backend = self.backend.clone();
let write = tokio::spawn(async move {
if let Err(error) = backend.save_history_entry(&entry).await {
log::warn!("failed to save MCP call history: {error}");
}
});
let _ = write.await;
result
}).await
}
fn get_info(&self) -> ServerInfo {
ServerInfo::new(ServerCapabilities::builder().enable_tools().enable_resources().build())
.with_server_info(Implementation::new("dbx", env!("CARGO_PKG_VERSION")))
@@ -3307,6 +3576,11 @@ fn resolved_connection(
connection: dbx_core::models::connection::ConnectionConfig,
group_path: Option<&dbx_core::mcp_policy::McpConnectionGroupPath>,
) -> ResolvedConnection {
let _ = CALL_HISTORY.try_with(|entry| {
let mut entry = entry.lock().unwrap();
entry.connection_id = connection.id.clone();
entry.connection_name = connection.name.clone();
});
let database_scope = database_scope_for_connection(&policy, &connection);
let group_ids = group_path.map(|path| path.ids.clone()).unwrap_or_default();
ResolvedConnection { connection, policy, database_scope, group_ids }
@@ -4208,6 +4482,25 @@ fn format_schema_context(
#[cfg(test)]
mod tests {
#[test]
fn history_response_keeps_long_text_and_all_rows() {
let value = serde_json::json!({"content": [{"type": "text", "text": "x".repeat(4000)}], "rows": (0..100).collect::<Vec<_>>()});
let stored: serde_json::Value = serde_json::from_str(&super::bounded_history_response(&value)).unwrap();
assert_eq!(stored, value);
}
#[test]
fn history_response_redacts_nested_json_and_marks_size_limit() {
let value =
serde_json::json!({"content": [{"type": "text", "text": r#"{"password":"secret-value","rows":[1,2]}"#}]});
let stored = super::bounded_history_response(&value);
assert!(!stored.contains("secret-value"));
assert!(stored.contains("[redacted]"));
let large = super::bounded_history_response(&serde_json::json!({"text": "中".repeat(70000)}));
assert!(large.len() <= 64 * 1024);
assert_eq!(serde_json::from_str::<serde_json::Value>(&large).unwrap()["truncated"], true);
}
use super::*;
use async_trait::async_trait;
use dbx_core::models::connection::ConnectionConfig;