feat(mcp): run server from Rust binary
This commit is contained in:
1 parent
c19c4b9be6
commit
8d7dc695bb
35 files changed
+3492
-100
No files matched your search
@@ -0,0 +1,25 @@
|
||||
[package]
|
||||
name = "dbx-mcp"
|
||||
version = "0.4.33"
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
|
||||
[[bin]]
|
||||
name = "dbx-mcp"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
async-trait = "0.1"
|
||||
dbx-core = { path = "../dbx-core", default-features = false }
|
||||
rmcp = { version = "2.2.0", features = ["client", "transport-io"] }
|
||||
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
|
||||
json5 = "0.4"
|
||||
schemars = "1"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
tokio = { version = "1", features = ["fs", "macros", "rt-multi-thread"] }
|
||||
uuid = { version = "1", features = ["v4"] }
|
||||
url = "2"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3"
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,7 @@
|
||||
pub mod backend;
|
||||
pub mod mongo;
|
||||
pub mod paths;
|
||||
pub mod server;
|
||||
|
||||
pub use backend::{ConnectionSummary, DbxBackend, LocalBackend, WebBackend};
|
||||
pub use server::{DbxMcpServer, McpScope};
|
||||
@@ -0,0 +1,20 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use dbx_mcp::{DbxBackend, DbxMcpServer, LocalBackend, WebBackend};
|
||||
use rmcp::ServiceExt;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let backend: Arc<dyn DbxBackend> = if let Ok(base_url) = std::env::var("DBX_WEB_URL") {
|
||||
Arc::new(
|
||||
WebBackend::new(base_url, std::env::var("DBX_WEB_PASSWORD").unwrap_or_default())
|
||||
.map_err(std::io::Error::other)?,
|
||||
)
|
||||
} else {
|
||||
let db_path = dbx_mcp::paths::storage_db_path().map_err(std::io::Error::other)?;
|
||||
Arc::new(LocalBackend::open(&db_path).await.map_err(std::io::Error::other)?)
|
||||
};
|
||||
let service = DbxMcpServer::new(backend).serve(rmcp::transport::stdio()).await?;
|
||||
service.waiting().await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,491 @@
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum MongoCommand {
|
||||
Version,
|
||||
Find { collection: String, filter: String, projection: Option<String>, sort: Option<String>, skip: u64, limit: i64 },
|
||||
Count { collection: String, filter: String, accurate: bool },
|
||||
Aggregate { collection: String, pipeline: String, options: Option<String> },
|
||||
Distinct { collection: String, field: String, filter: Option<String> },
|
||||
GetIndexes { collection: String },
|
||||
CollectionStats { collection: String, metric: String, scale: Option<serde_json::Number> },
|
||||
Insert { collection: String, documents: String },
|
||||
Update { collection: String, filter: String, update: String, options: Option<String>, many: bool },
|
||||
Delete { collection: String, filter: String, many: bool },
|
||||
CreateIndex { collection: String, keys: String, options: Option<String> },
|
||||
DropIndexes { collection: String, indexes: Option<String>, single: bool },
|
||||
DropCollection { collection: String },
|
||||
}
|
||||
|
||||
impl MongoCommand {
|
||||
pub fn is_mutating(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::Insert { .. }
|
||||
| Self::Update { .. }
|
||||
| Self::Delete { .. }
|
||||
| Self::CreateIndex { .. }
|
||||
| Self::DropIndexes { .. }
|
||||
| Self::DropCollection { .. }
|
||||
) || matches!(self, Self::Aggregate { pipeline, .. } if aggregate_writes(pipeline))
|
||||
}
|
||||
|
||||
pub fn is_dangerous(&self) -> bool {
|
||||
matches!(self, Self::DropCollection { .. })
|
||||
|| matches!(self, Self::DropIndexes { indexes: None, single: false, .. })
|
||||
|| matches!(self, Self::Aggregate { pipeline, .. } if aggregate_writes(pipeline))
|
||||
}
|
||||
|
||||
pub fn has_empty_filter(&self) -> bool {
|
||||
match self {
|
||||
Self::Update { filter, .. } | Self::Delete { filter, .. } => is_empty_object(filter),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(input: &str) -> Result<MongoCommand, String> {
|
||||
let source = input.trim().trim_end_matches(';').trim();
|
||||
if source.eq_ignore_ascii_case("db.version()") {
|
||||
return Ok(MongoCommand::Version);
|
||||
}
|
||||
let (collection, prefix_end) = parse_collection_prefix(source)?;
|
||||
|
||||
if let Some((args, tail)) = method_call(source, prefix_end, "find") {
|
||||
let filter = normalized_json(args.first().map(String::as_str).unwrap_or("{}"))?;
|
||||
let projection =
|
||||
if args.get(1).is_some_and(|arg| !arg.trim().is_empty()) { Some(normalized_json(&args[1])?) } else { None };
|
||||
if args.len() > 2 {
|
||||
return Err("MongoDB find() accepts at most filter and projection arguments.".to_string());
|
||||
}
|
||||
let mut sort = None;
|
||||
let mut skip = 0;
|
||||
let mut limit = 100;
|
||||
for (name, call_args) in chained_calls(&tail)? {
|
||||
match name.as_str() {
|
||||
"sort" => sort = Some(normalized_json(call_args.first().map(String::as_str).unwrap_or("{}"))?),
|
||||
"skip" => skip = parse_integer(&call_args, "skip")? as u64,
|
||||
"limit" => limit = parse_integer(&call_args, "limit")?,
|
||||
"count" if call_args.is_empty() => {
|
||||
return Ok(MongoCommand::Count { collection, filter, accurate: false });
|
||||
}
|
||||
_ => return Err(format!("Unsupported MongoDB find() chain: {name}()")),
|
||||
}
|
||||
}
|
||||
return Ok(MongoCommand::Find { collection, filter, projection, sort, skip, limit });
|
||||
}
|
||||
|
||||
for (method, accurate) in [("countDocuments", true), ("count", false)] {
|
||||
if let Some((args, tail)) = method_call(source, prefix_end, method) {
|
||||
if !tail.is_empty() || args.len() > 1 {
|
||||
return Err(format!("Invalid MongoDB {method}() command."));
|
||||
}
|
||||
return Ok(MongoCommand::Count {
|
||||
collection,
|
||||
filter: normalized_json(args.first().map(String::as_str).unwrap_or("{}"))?,
|
||||
accurate,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if let Some((args, tail)) = method_call(source, prefix_end, "aggregate") {
|
||||
if !tail.is_empty() || !(1..=2).contains(&args.len()) {
|
||||
return Err("Invalid MongoDB aggregate() command.".to_string());
|
||||
}
|
||||
let pipeline = normalized_json(&args[0])?;
|
||||
if !parse_json_value(&pipeline).is_some_and(|value| value.is_array()) {
|
||||
return Err("MongoDB aggregate() requires a pipeline array.".to_string());
|
||||
}
|
||||
let options = args.get(1).filter(|arg| !arg.trim().is_empty()).map(|arg| normalized_json(arg)).transpose()?;
|
||||
return Ok(MongoCommand::Aggregate { collection, pipeline, options });
|
||||
}
|
||||
|
||||
if let Some((args, tail)) = method_call(source, prefix_end, "distinct") {
|
||||
if !tail.is_empty() || !(1..=2).contains(&args.len()) {
|
||||
return Err("Invalid MongoDB distinct() command.".to_string());
|
||||
}
|
||||
let field = parse_string_arg(&args[0])?;
|
||||
let filter = args.get(1).filter(|arg| !arg.trim().is_empty()).map(|arg| normalized_json(arg)).transpose()?;
|
||||
return Ok(MongoCommand::Distinct { collection, field, filter });
|
||||
}
|
||||
|
||||
if let Some((args, tail)) = method_call(source, prefix_end, "getIndexes") {
|
||||
if !tail.is_empty() || !args.is_empty() {
|
||||
return Err("Invalid MongoDB getIndexes() command.".to_string());
|
||||
}
|
||||
return Ok(MongoCommand::GetIndexes { collection });
|
||||
}
|
||||
|
||||
for metric in ["stats", "dataSize", "storageSize", "totalIndexSize"] {
|
||||
if let Some((args, tail)) = method_call(source, prefix_end, metric) {
|
||||
if !tail.is_empty() || args.len() > 1 {
|
||||
return Err(format!("Invalid MongoDB {metric}() command."));
|
||||
}
|
||||
let scale = args
|
||||
.first()
|
||||
.filter(|arg| !arg.trim().is_empty())
|
||||
.map(|arg| {
|
||||
arg.trim()
|
||||
.parse::<f64>()
|
||||
.ok()
|
||||
.and_then(serde_json::Number::from_f64)
|
||||
.ok_or_else(|| format!("Invalid {metric} scale."))
|
||||
})
|
||||
.transpose()?;
|
||||
return Ok(MongoCommand::CollectionStats { collection, metric: metric.to_string(), scale });
|
||||
}
|
||||
}
|
||||
|
||||
if let Some((args, tail)) = method_call(source, prefix_end, "insertOne") {
|
||||
if !tail.is_empty() || args.len() != 1 {
|
||||
return Err("Invalid MongoDB insertOne() command.".to_string());
|
||||
}
|
||||
return Ok(MongoCommand::Insert { collection, documents: normalized_json(&args[0])? });
|
||||
}
|
||||
if let Some((args, tail)) = method_call(source, prefix_end, "insertMany") {
|
||||
if !tail.is_empty() || args.len() != 1 {
|
||||
return Err("Invalid MongoDB insertMany() command.".to_string());
|
||||
}
|
||||
let documents = normalized_json(&args[0])?;
|
||||
if !parse_json_value(&documents).is_some_and(|value| value.is_array()) {
|
||||
return Err("MongoDB insertMany() requires an array.".to_string());
|
||||
}
|
||||
return Ok(MongoCommand::Insert { collection, documents });
|
||||
}
|
||||
|
||||
for (method, many) in [("updateOne", false), ("updateMany", true)] {
|
||||
if let Some((args, tail)) = method_call(source, prefix_end, method) {
|
||||
if !tail.is_empty() || !(2..=3).contains(&args.len()) {
|
||||
return Err(format!("Invalid MongoDB {method}() command."));
|
||||
}
|
||||
return Ok(MongoCommand::Update {
|
||||
collection,
|
||||
filter: normalized_json(&args[0])?,
|
||||
update: normalized_json(&args[1])?,
|
||||
options: args
|
||||
.get(2)
|
||||
.filter(|arg| !arg.trim().is_empty())
|
||||
.map(|arg| normalized_json(arg))
|
||||
.transpose()?,
|
||||
many,
|
||||
});
|
||||
}
|
||||
}
|
||||
for (method, many) in [("deleteOne", false), ("deleteMany", true)] {
|
||||
if let Some((args, tail)) = method_call(source, prefix_end, method) {
|
||||
if !tail.is_empty() || args.len() != 1 {
|
||||
return Err(format!("Invalid MongoDB {method}() command."));
|
||||
}
|
||||
return Ok(MongoCommand::Delete { collection, filter: normalized_json(&args[0])?, many });
|
||||
}
|
||||
}
|
||||
if let Some((args, tail)) = method_call(source, prefix_end, "createIndex") {
|
||||
if !tail.is_empty() || !(1..=2).contains(&args.len()) {
|
||||
return Err("Invalid MongoDB createIndex() command.".to_string());
|
||||
}
|
||||
return Ok(MongoCommand::CreateIndex {
|
||||
collection,
|
||||
keys: normalized_json(&args[0])?,
|
||||
options: args.get(1).filter(|arg| !arg.trim().is_empty()).map(|arg| normalized_json(arg)).transpose()?,
|
||||
});
|
||||
}
|
||||
if let Some((args, tail)) = method_call(source, prefix_end, "dropIndex") {
|
||||
if !tail.is_empty() || args.len() != 1 {
|
||||
return Err("Invalid MongoDB dropIndex() command.".to_string());
|
||||
}
|
||||
return Ok(MongoCommand::DropIndexes { collection, indexes: Some(normalized_json(&args[0])?), single: true });
|
||||
}
|
||||
if let Some((args, tail)) = method_call(source, prefix_end, "dropIndexes") {
|
||||
if !tail.is_empty() || args.len() > 1 {
|
||||
return Err("Invalid MongoDB dropIndexes() command.".to_string());
|
||||
}
|
||||
return Ok(MongoCommand::DropIndexes {
|
||||
collection,
|
||||
indexes: args.first().filter(|arg| !arg.trim().is_empty()).map(|arg| normalized_json(arg)).transpose()?,
|
||||
single: false,
|
||||
});
|
||||
}
|
||||
if let Some((args, tail)) = method_call(source, prefix_end, "drop") {
|
||||
if !tail.is_empty() || !args.is_empty() {
|
||||
return Err("Invalid MongoDB drop() command.".to_string());
|
||||
}
|
||||
return Ok(MongoCommand::DropCollection { collection });
|
||||
}
|
||||
|
||||
Err("Unsupported MongoDB shell command.".to_string())
|
||||
}
|
||||
|
||||
fn parse_collection_prefix(source: &str) -> Result<(String, usize), String> {
|
||||
if !source.get(..3).is_some_and(|prefix| prefix.eq_ignore_ascii_case("db.")) {
|
||||
return Err("MongoDB command must start with db.<collection>.".to_string());
|
||||
}
|
||||
let rest = &source[3..];
|
||||
if rest.starts_with("getCollection") {
|
||||
let open = rest.find('(').ok_or("Invalid db.getCollection() command.")?;
|
||||
let close = matching_paren(rest, open).ok_or("Invalid db.getCollection() command.")?;
|
||||
let args = split_top_level(&rest[open + 1..close]);
|
||||
if args.len() != 1 {
|
||||
return Err("db.getCollection() requires one collection name.".to_string());
|
||||
}
|
||||
let collection = parse_string_arg(&args[0])?;
|
||||
let end = 3 + close + 1;
|
||||
let suffix = &source[end..];
|
||||
let trimmed = suffix.trim_start();
|
||||
if !trimmed.starts_with('.') {
|
||||
return Err("MongoDB collection method is required.".to_string());
|
||||
}
|
||||
return Ok((collection, end + suffix.len() - trimmed.len()));
|
||||
}
|
||||
let collection_end = rest
|
||||
.char_indices()
|
||||
.find_map(|(index, ch)| (ch == '.' || ch.is_whitespace()).then_some(index))
|
||||
.ok_or("MongoDB collection method is required.")?;
|
||||
let collection = &rest[..collection_end];
|
||||
if collection.is_empty() {
|
||||
return Err("Invalid MongoDB collection name.".to_string());
|
||||
}
|
||||
let suffix = &rest[collection_end..];
|
||||
let dot = suffix.find('.').ok_or("MongoDB collection method is required.")?;
|
||||
if !suffix[..dot].trim().is_empty() {
|
||||
return Err("Invalid MongoDB collection name.".to_string());
|
||||
}
|
||||
Ok((collection.to_string(), 3 + collection_end + dot))
|
||||
}
|
||||
|
||||
fn method_call(source: &str, prefix_end: usize, method: &str) -> Option<(Vec<String>, String)> {
|
||||
let raw_suffix = &source[prefix_end..];
|
||||
let suffix = raw_suffix.trim_start();
|
||||
let whitespace = raw_suffix.len() - suffix.len();
|
||||
let expected = format!(".{method}");
|
||||
if !suffix.starts_with(&expected) || !suffix[expected.len()..].starts_with('(') {
|
||||
return None;
|
||||
}
|
||||
let open = prefix_end + whitespace + expected.len();
|
||||
let close = matching_paren(source, open)?;
|
||||
Some((split_top_level(&source[open + 1..close]), source[close + 1..].trim().to_string()))
|
||||
}
|
||||
|
||||
fn chained_calls(chain: &str) -> Result<Vec<(String, Vec<String>)>, String> {
|
||||
let mut rest = chain.trim();
|
||||
let mut calls = Vec::new();
|
||||
while !rest.is_empty() {
|
||||
let Some(rest_after_dot) = rest.strip_prefix('.') else {
|
||||
return Err("Invalid MongoDB method chain.".to_string());
|
||||
};
|
||||
let open = rest_after_dot.find('(').ok_or("Invalid MongoDB method chain.")?;
|
||||
let name = rest_after_dot[..open].trim().to_string();
|
||||
let close = matching_paren(rest_after_dot, open).ok_or("Invalid MongoDB method chain.")?;
|
||||
calls.push((name, split_top_level(&rest_after_dot[open + 1..close])));
|
||||
rest = rest_after_dot[close + 1..].trim();
|
||||
}
|
||||
Ok(calls)
|
||||
}
|
||||
|
||||
fn parse_integer(args: &[String], name: &str) -> Result<i64, String> {
|
||||
if args.len() != 1 {
|
||||
return Err(format!("MongoDB {name}() requires one integer."));
|
||||
}
|
||||
let value =
|
||||
args[0].trim().parse::<i64>().map_err(|_| format!("MongoDB {name}() requires a non-negative integer."))?;
|
||||
if value < 0 {
|
||||
return Err(format!("MongoDB {name}() requires a non-negative integer."));
|
||||
}
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
fn parse_string_arg(arg: &str) -> Result<String, String> {
|
||||
let value = parse_json_value(&normalized_json(arg)?).ok_or("Invalid MongoDB string argument.")?;
|
||||
value.as_str().map(ToOwned::to_owned).ok_or_else(|| "MongoDB argument must be a string.".to_string())
|
||||
}
|
||||
|
||||
fn normalized_json(input: &str) -> Result<String, String> {
|
||||
let transformed = transform_shell_constructors(input.trim())?;
|
||||
let value: Value =
|
||||
json5::from_str(&transformed).map_err(|error| format!("Invalid MongoDB JSON argument: {error}"))?;
|
||||
serde_json::to_string(&value).map_err(|error| error.to_string())
|
||||
}
|
||||
|
||||
fn transform_shell_constructors(input: &str) -> Result<String, String> {
|
||||
let mut output = String::with_capacity(input.len());
|
||||
let mut index = 0;
|
||||
while index < input.len() {
|
||||
let rest = &input[index..];
|
||||
let constructor = if rest.starts_with("ObjectId(") {
|
||||
Some("ObjectId(")
|
||||
} else if rest.starts_with("ISODate(") {
|
||||
Some("ISODate(")
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let Some(constructor) = constructor else {
|
||||
let ch = rest.chars().next().ok_or("Invalid MongoDB argument.")?;
|
||||
output.push(ch);
|
||||
index += ch.len_utf8();
|
||||
continue;
|
||||
};
|
||||
let open = index + constructor.len() - 1;
|
||||
let close = matching_paren(input, open).ok_or("Unclosed MongoDB value constructor.")?;
|
||||
let inner = input[open + 1..close].trim();
|
||||
let value = parse_string_arg(inner)?;
|
||||
let key = if constructor.starts_with("ObjectId") { "$oid" } else { "$date" };
|
||||
output.push_str(&format!("{{\"{key}\":{}}}", serde_json::to_string(&value).unwrap()));
|
||||
index = close + 1;
|
||||
}
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn parse_json_value(value: &str) -> Option<Value> {
|
||||
serde_json::from_str(value).ok()
|
||||
}
|
||||
|
||||
fn is_empty_object(value: &str) -> bool {
|
||||
parse_json_value(value).is_some_and(|value| value.as_object().is_some_and(|object| object.is_empty()))
|
||||
}
|
||||
|
||||
fn aggregate_writes(pipeline: &str) -> bool {
|
||||
parse_json_value(pipeline).is_some_and(|value| {
|
||||
value.as_array().is_some_and(|stages| {
|
||||
stages.iter().any(|stage| {
|
||||
stage
|
||||
.as_object()
|
||||
.is_some_and(|object| object.keys().any(|key| matches!(key.as_str(), "$out" | "$merge")))
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn matching_paren(source: &str, open: usize) -> Option<usize> {
|
||||
let bytes = source.as_bytes();
|
||||
let mut depth = 0;
|
||||
let mut quote = None;
|
||||
let mut escape = false;
|
||||
for (index, byte) in bytes.iter().enumerate().skip(open) {
|
||||
let ch = *byte as char;
|
||||
if escape {
|
||||
escape = false;
|
||||
continue;
|
||||
}
|
||||
if quote.is_some() {
|
||||
if ch == '\\' {
|
||||
escape = true;
|
||||
} else if Some(ch) == quote {
|
||||
quote = None;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if ch == '\'' || ch == '"' || ch == '`' {
|
||||
quote = Some(ch);
|
||||
} else if ch == '(' {
|
||||
depth += 1;
|
||||
} else if ch == ')' {
|
||||
depth -= 1;
|
||||
if depth == 0 {
|
||||
return Some(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn split_top_level(source: &str) -> Vec<String> {
|
||||
if source.trim().is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
let mut result = Vec::new();
|
||||
let mut start = 0;
|
||||
let mut depth = 0;
|
||||
let mut quote = None;
|
||||
let mut escape = false;
|
||||
for (index, byte) in source.as_bytes().iter().enumerate() {
|
||||
let ch = *byte as char;
|
||||
if escape {
|
||||
escape = false;
|
||||
continue;
|
||||
}
|
||||
if quote.is_some() {
|
||||
if ch == '\\' {
|
||||
escape = true;
|
||||
} else if Some(ch) == quote {
|
||||
quote = None;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if ch == '\'' || ch == '"' || ch == '`' {
|
||||
quote = Some(ch);
|
||||
} else if matches!(ch, '(' | '[' | '{') {
|
||||
depth += 1;
|
||||
} else if matches!(ch, ')' | ']' | '}') {
|
||||
depth -= 1;
|
||||
} else if ch == ',' && depth == 0 {
|
||||
result.push(source[start..index].trim().to_string());
|
||||
start = index + 1;
|
||||
}
|
||||
}
|
||||
result.push(source[start..].trim().to_string());
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn parses_find_with_compass_syntax_and_chain() {
|
||||
assert_eq!(
|
||||
parse("db.products.find({_id: ObjectId('507f1f77bcf86cd799439011')}, {title: 1, _id: 0}).sort({title: 1}).limit(1)").unwrap(),
|
||||
MongoCommand::Find {
|
||||
collection: "products".to_string(),
|
||||
filter: r#"{"_id":{"$oid":"507f1f77bcf86cd799439011"}}"#.to_string(),
|
||||
projection: Some(r#"{"title":1,"_id":0}"#.to_string()),
|
||||
sort: Some(r#"{"title":1}"#.to_string()),
|
||||
skip: 0,
|
||||
limit: 1,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_get_collection_and_count() {
|
||||
assert_eq!(
|
||||
parse("db.getCollection('audit.logs').count()").unwrap(),
|
||||
MongoCommand::Count { collection: "audit.logs".to_string(), filter: "{}".to_string(), accurate: false }
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn identifies_dangerous_aggregate_and_empty_writes() {
|
||||
let aggregate = parse(r#"db.projects.aggregate([{"$out":"backup"}])"#).unwrap();
|
||||
assert!(aggregate.is_mutating());
|
||||
assert!(aggregate.is_dangerous());
|
||||
let update = parse("db.projects.updateMany({}, {$set: {active: false}})").unwrap();
|
||||
assert!(update.has_empty_filter());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_multiline_chains_and_update_options() {
|
||||
let command = parse(
|
||||
r#"db.getCollection("operation_logs")
|
||||
.find({_id: ObjectId("68ad51ca84c8127bc7d44cb3")})
|
||||
.sort({ts: -1})
|
||||
.skip(5)
|
||||
.limit(10)"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(matches!(command, MongoCommand::Find { skip: 5, limit: 10, .. }));
|
||||
|
||||
let update = parse(
|
||||
r#"db.orders.updateMany({status: "open"}, {$set: {"items.$[item].status": "done"}}, {arrayFilters: [{"item.id": 7}]})"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(matches!(update, MongoCommand::Update { many: true, options: Some(_), .. }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_stats_and_rejects_negative_pagination() {
|
||||
assert!(matches!(
|
||||
parse("db.users.stats(1024)").unwrap(),
|
||||
MongoCommand::CollectionStats { metric, scale: Some(_), .. } if metric == "stats"
|
||||
));
|
||||
assert!(parse("db.users.find({}).skip(-1)").is_err());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub const STORAGE_DB_FILE_NAME: &str = "dbx.db";
|
||||
|
||||
pub fn app_data_dir() -> Result<PathBuf, String> {
|
||||
if let Some(path) = std::env::var_os("DBX_DATA_DIR").filter(|value| !value.is_empty()) {
|
||||
return Ok(PathBuf::from(path));
|
||||
}
|
||||
|
||||
let home = std::env::var_os("HOME")
|
||||
.or_else(|| std::env::var_os("USERPROFILE"))
|
||||
.map(PathBuf::from)
|
||||
.ok_or_else(|| "Unable to resolve the user home directory. Set DBX_DATA_DIR explicitly.".to_string())?;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
return Ok(home.join("Library/Application Support/com.dbx.app"));
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
return Ok(std::env::var_os("APPDATA")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| home.join("AppData/Roaming"))
|
||||
.join("com.dbx.app"));
|
||||
|
||||
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
||||
return Ok(home.join(".local/share/com.dbx.app"));
|
||||
}
|
||||
|
||||
pub fn storage_db_path() -> Result<PathBuf, String> {
|
||||
Ok(app_data_dir()?.join(STORAGE_DB_FILE_NAME))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn explicit_data_dir_wins() {
|
||||
let original = std::env::var_os("DBX_DATA_DIR");
|
||||
std::env::set_var("DBX_DATA_DIR", "/tmp/dbx-mcp-data");
|
||||
assert_eq!(app_data_dir().unwrap(), PathBuf::from("/tmp/dbx-mcp-data"));
|
||||
match original {
|
||||
Some(value) => std::env::set_var("DBX_DATA_DIR", value),
|
||||
None => std::env::remove_var("DBX_DATA_DIR"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,898 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use rmcp::{
|
||||
handler::server::{router::tool::ToolRouter, wrapper::Parameters},
|
||||
model::{CallToolResult, ContentBlock, Implementation, ServerCapabilities, ServerInfo},
|
||||
schemars, tool, tool_handler, tool_router, ServerHandler,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::backend::{new_connection_config, parse_database_type, ConnectionSummary, DbxBackend};
|
||||
use crate::mongo::{self, MongoCommand};
|
||||
use dbx_core::{
|
||||
db::redis_driver::{classify_command, parse_command_argv, RedisCommandResult, RedisCommandSafety},
|
||||
models::connection::DatabaseType,
|
||||
production_safety::{is_production_database, targets_production_database},
|
||||
sql_risk::{classify_sql_risk_for_database, SqlRisk},
|
||||
};
|
||||
|
||||
#[derive(Debug, Deserialize, schemars::JsonSchema)]
|
||||
pub struct ListConnectionsRequest {}
|
||||
|
||||
#[derive(Debug, Deserialize, schemars::JsonSchema)]
|
||||
pub struct ConnectionSelector {
|
||||
#[schemars(description = "Unique ID of the DBX connection")]
|
||||
pub connection_id: Option<String>,
|
||||
#[schemars(description = "Name of the DBX connection")]
|
||||
pub connection_name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, schemars::JsonSchema)]
|
||||
pub struct ListTablesRequest {
|
||||
#[serde(flatten)]
|
||||
pub selector: ConnectionSelector,
|
||||
#[schemars(description = "Database name")]
|
||||
pub database: Option<String>,
|
||||
#[schemars(description = "Schema name")]
|
||||
pub schema: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, schemars::JsonSchema)]
|
||||
pub struct DescribeTableRequest {
|
||||
#[serde(flatten)]
|
||||
pub selector: ConnectionSelector,
|
||||
#[schemars(description = "Table name")]
|
||||
pub table: String,
|
||||
#[schemars(description = "Database name")]
|
||||
pub database: Option<String>,
|
||||
#[schemars(description = "Schema name")]
|
||||
pub schema: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, schemars::JsonSchema)]
|
||||
pub struct ExecuteQueryRequest {
|
||||
#[serde(flatten)]
|
||||
pub selector: ConnectionSelector,
|
||||
#[schemars(description = "Database name")]
|
||||
pub database: Option<String>,
|
||||
#[schemars(description = "SQL query to execute")]
|
||||
pub sql: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, schemars::JsonSchema)]
|
||||
pub struct AddConnectionRequest {
|
||||
pub name: String,
|
||||
pub db_type: String,
|
||||
pub host: String,
|
||||
pub port: Option<u16>,
|
||||
#[serde(default)]
|
||||
pub username: String,
|
||||
#[serde(default)]
|
||||
pub password: String,
|
||||
pub database: Option<String>,
|
||||
#[serde(default)]
|
||||
pub ssl: bool,
|
||||
pub driver_profile: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, schemars::JsonSchema)]
|
||||
pub struct RemoveConnectionRequest {
|
||||
pub connection_name: String,
|
||||
pub connection_id: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, schemars::JsonSchema)]
|
||||
pub struct ExecuteRedisCommandRequest {
|
||||
#[serde(flatten)]
|
||||
pub selector: ConnectionSelector,
|
||||
#[schemars(description = "Redis logical database number")]
|
||||
pub db: Option<u32>,
|
||||
#[schemars(description = "Redis command to execute, for example GET mykey or INFO")]
|
||||
pub command: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, schemars::JsonSchema)]
|
||||
pub struct SchemaContextRequest {
|
||||
#[serde(flatten)]
|
||||
pub selector: ConnectionSelector,
|
||||
pub database: Option<String>,
|
||||
pub schema: Option<String>,
|
||||
#[schemars(description = "Specific table names to include")]
|
||||
pub tables: Option<Vec<String>>,
|
||||
#[schemars(description = "Maximum number of tables to include, from 1 to 20")]
|
||||
pub max_tables: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, schemars::JsonSchema)]
|
||||
pub struct OpenTableRequest {
|
||||
#[serde(flatten)]
|
||||
pub selector: ConnectionSelector,
|
||||
pub table: String,
|
||||
pub database: Option<String>,
|
||||
pub schema: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, schemars::JsonSchema)]
|
||||
pub struct ExecuteAndShowRequest {
|
||||
#[serde(flatten)]
|
||||
pub selector: ConnectionSelector,
|
||||
pub sql: String,
|
||||
pub database: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DbxMcpServer {
|
||||
backend: Arc<dyn DbxBackend>,
|
||||
scope: McpScope,
|
||||
tool_router: ToolRouter<Self>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct McpScope {
|
||||
pub connection_id: Option<String>,
|
||||
pub connection_name: Option<String>,
|
||||
pub database: Option<String>,
|
||||
}
|
||||
|
||||
impl McpScope {
|
||||
pub fn from_env() -> Self {
|
||||
Self {
|
||||
connection_id: non_empty_env("DBX_MCP_SCOPE_CONNECTION_ID"),
|
||||
connection_name: non_empty_env("DBX_MCP_SCOPE_CONNECTION_NAME"),
|
||||
database: non_empty_env("DBX_MCP_SCOPE_DATABASE"),
|
||||
}
|
||||
}
|
||||
|
||||
fn enabled(&self) -> bool {
|
||||
self.connection_id.is_some() || self.connection_name.is_some()
|
||||
}
|
||||
|
||||
fn matches(&self, connection: &dbx_core::models::connection::ConnectionConfig) -> bool {
|
||||
self.connection_id.as_deref() == Some(connection.id.as_str())
|
||||
|| self.connection_name.as_deref() == Some(connection.name.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl DbxMcpServer {
|
||||
pub fn new(backend: Arc<dyn DbxBackend>) -> Self {
|
||||
Self::with_runtime_options(backend, McpScope::from_env(), std::env::var_os("DBX_WEB_URL").is_some())
|
||||
}
|
||||
|
||||
pub fn with_runtime_options(backend: Arc<dyn DbxBackend>, scope: McpScope, web_mode: bool) -> Self {
|
||||
let mut tool_router = Self::tool_router();
|
||||
if scope.enabled() {
|
||||
tool_router.disable_route("dbx_add_connection");
|
||||
tool_router.disable_route("dbx_remove_connection");
|
||||
}
|
||||
// Desktop UI bridge operations are intentionally unavailable remotely and in scoped AI sessions.
|
||||
if web_mode || scope.enabled() {
|
||||
tool_router.disable_route("dbx_open_table");
|
||||
tool_router.disable_route("dbx_execute_and_show");
|
||||
}
|
||||
Self { backend, scope, tool_router }
|
||||
}
|
||||
}
|
||||
|
||||
#[tool_router]
|
||||
impl DbxMcpServer {
|
||||
#[tool(
|
||||
name = "dbx_list_connections",
|
||||
description = "List database connections configured in DBX. Returns connection IDs, names, database types, endpoints, and selected databases."
|
||||
)]
|
||||
async fn list_connections(
|
||||
&self,
|
||||
Parameters(ListConnectionsRequest {}): Parameters<ListConnectionsRequest>,
|
||||
) -> CallToolResult {
|
||||
match self.load_scoped_connections().await {
|
||||
Ok(connections) if connections.is_empty() => text("No connections configured in DBX."),
|
||||
Ok(connections) => {
|
||||
let rows = connections.iter().map(ConnectionSummary::from).collect::<Vec<_>>();
|
||||
text(format_connections(&rows))
|
||||
}
|
||||
Err(error) => tool_error("CONNECTION_LOAD_ERROR", error),
|
||||
}
|
||||
}
|
||||
|
||||
#[tool(name = "dbx_list_tables", description = "List tables and views for a database connection")]
|
||||
async fn list_tables(&self, Parameters(request): Parameters<ListTablesRequest>) -> CallToolResult {
|
||||
let connection = match self.resolve_connection(&request.selector).await {
|
||||
Ok(connection) => connection,
|
||||
Err(error) => return error,
|
||||
};
|
||||
let database = self.resolve_database(request.database, &connection);
|
||||
match self.backend.list_tables(&connection, &database, &request.schema.unwrap_or_default()).await {
|
||||
Ok(tables) if tables.is_empty() => text("No tables found."),
|
||||
Ok(tables) => text(
|
||||
tables
|
||||
.into_iter()
|
||||
.map(|table| {
|
||||
let comment = table
|
||||
.comment
|
||||
.filter(|comment| !comment.is_empty())
|
||||
.map(|comment| format!(" -- {comment}"))
|
||||
.unwrap_or_default();
|
||||
format!("- {} ({}){}", table.name, table.table_type, comment)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n"),
|
||||
),
|
||||
Err(error) => tool_error("TABLE_LIST_ERROR", error),
|
||||
}
|
||||
}
|
||||
|
||||
#[tool(name = "dbx_describe_table", description = "Get column definitions for a table")]
|
||||
async fn describe_table(&self, Parameters(request): Parameters<DescribeTableRequest>) -> CallToolResult {
|
||||
let connection = match self.resolve_connection(&request.selector).await {
|
||||
Ok(connection) => connection,
|
||||
Err(error) => return error,
|
||||
};
|
||||
let database = self.resolve_database(request.database, &connection);
|
||||
match self
|
||||
.backend
|
||||
.get_columns(&connection, &database, &request.schema.unwrap_or_default(), &request.table)
|
||||
.await
|
||||
{
|
||||
Ok(columns) if columns.is_empty() => text("No columns found."),
|
||||
Ok(columns) => text(format_columns(&columns)),
|
||||
Err(error) => tool_error("TABLE_DESCRIPTION_ERROR", error),
|
||||
}
|
||||
}
|
||||
|
||||
#[tool(
|
||||
name = "dbx_execute_query",
|
||||
description = "Execute a SQL query on a database connection (max 100 rows returned)"
|
||||
)]
|
||||
async fn execute_query(&self, Parameters(request): Parameters<ExecuteQueryRequest>) -> CallToolResult {
|
||||
let connection = match self.resolve_connection(&request.selector).await {
|
||||
Ok(connection) => connection,
|
||||
Err(error) => return error,
|
||||
};
|
||||
if connection.db_type == dbx_core::models::connection::DatabaseType::Redis {
|
||||
return tool_error(
|
||||
"REDIS_COMMAND_REQUIRED",
|
||||
"Redis connections do not accept SQL through dbx_execute_query. Use dbx_execute_redis_command.",
|
||||
);
|
||||
}
|
||||
let database = self.resolve_database(request.database, &connection);
|
||||
if connection.db_type == DatabaseType::MongoDb {
|
||||
let command = match validate_mongo_command(&connection, &database, &request.sql) {
|
||||
Ok(command) => command,
|
||||
Err(error) => return error,
|
||||
};
|
||||
return match self.backend.execute_mongo_command(&connection, &database, &command).await {
|
||||
Ok(result) => text(result),
|
||||
Err(error) => tool_error("QUERY_ERROR", error),
|
||||
};
|
||||
}
|
||||
let result = self
|
||||
.backend
|
||||
.execute_agent_tool(
|
||||
&connection,
|
||||
&database,
|
||||
"execute_query",
|
||||
json!({ "sql": request.sql, "limit": 100 }),
|
||||
default_permissions(),
|
||||
)
|
||||
.await;
|
||||
agent_result(result)
|
||||
}
|
||||
|
||||
#[tool(name = "dbx_execute_redis_command", description = "Execute a Redis command on a Redis connection")]
|
||||
async fn execute_redis_command(
|
||||
&self,
|
||||
Parameters(request): Parameters<ExecuteRedisCommandRequest>,
|
||||
) -> CallToolResult {
|
||||
let connection = match self.resolve_connection(&request.selector).await {
|
||||
Ok(connection) => connection,
|
||||
Err(error) => return error,
|
||||
};
|
||||
if connection.db_type != DatabaseType::Redis {
|
||||
return tool_error("INVALID_CONNECTION_TYPE", format!("Connection \"{}\" is not Redis.", connection.name));
|
||||
}
|
||||
let argv = match parse_command_argv(&request.command) {
|
||||
Ok(argv) => argv,
|
||||
Err(error) => return tool_error("REDIS_COMMAND_BLOCKED", error),
|
||||
};
|
||||
let safety = classify_command(&argv[0]);
|
||||
let permissions = default_permissions();
|
||||
if safety == RedisCommandSafety::Blocked && !permissions.allow_dangerous {
|
||||
return tool_error(
|
||||
"REDIS_COMMAND_BLOCKED",
|
||||
format!(
|
||||
"Dangerous Redis command \"{}\" is blocked. Set DBX_MCP_ALLOW_DANGEROUS_SQL=1 to allow it.",
|
||||
argv[0].to_ascii_uppercase()
|
||||
),
|
||||
);
|
||||
}
|
||||
if safety != RedisCommandSafety::Allowed && !permissions.allow_writes {
|
||||
return tool_error(
|
||||
"REDIS_COMMAND_BLOCKED",
|
||||
"MCP Redis command execution is read-only for this session. Set DBX_MCP_ALLOW_WRITES=1 to allow write or dangerous commands.",
|
||||
);
|
||||
}
|
||||
let database = request
|
||||
.db
|
||||
.or_else(|| self.scope.database.as_deref().and_then(parse_redis_database))
|
||||
.or_else(|| redis_database(&connection))
|
||||
.unwrap_or(0);
|
||||
// Production protection is stricter than the opt-in write flags by design.
|
||||
if safety != RedisCommandSafety::Allowed && is_production_database(&connection, &database.to_string()) {
|
||||
return tool_error(
|
||||
"PRODUCTION_WRITE_BLOCKED",
|
||||
"MCP cannot execute write or dangerous Redis commands against a production database.",
|
||||
);
|
||||
}
|
||||
match self
|
||||
.backend
|
||||
.execute_redis_command(
|
||||
&connection,
|
||||
database,
|
||||
&request.command,
|
||||
safety == RedisCommandSafety::Blocked && permissions.allow_dangerous,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(result) => text(format_redis_result(&result)),
|
||||
Err(error) => tool_error("REDIS_COMMAND_ERROR", error),
|
||||
}
|
||||
}
|
||||
|
||||
#[tool(name = "dbx_get_schema_context", description = "Get compact table and column context for writing SQL")]
|
||||
async fn get_schema_context(&self, Parameters(request): Parameters<SchemaContextRequest>) -> CallToolResult {
|
||||
let connection = match self.resolve_connection(&request.selector).await {
|
||||
Ok(connection) => connection,
|
||||
Err(error) => return error,
|
||||
};
|
||||
let database = self.resolve_database(request.database, &connection);
|
||||
let schema = request.schema.unwrap_or_default();
|
||||
let max_tables = request.max_tables.unwrap_or(8).clamp(1, 20);
|
||||
let available = match self.backend.list_tables(&connection, &database, &schema).await {
|
||||
Ok(tables) => tables,
|
||||
Err(error) => return tool_error("SCHEMA_CONTEXT_ERROR", error),
|
||||
};
|
||||
let requested = request
|
||||
.tables
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|name| name.to_ascii_lowercase())
|
||||
.collect::<std::collections::HashSet<_>>();
|
||||
let mut selected = if requested.is_empty() {
|
||||
available.iter().collect::<Vec<_>>()
|
||||
} else {
|
||||
available.iter().filter(|table| requested.contains(&table.name.to_ascii_lowercase())).collect::<Vec<_>>()
|
||||
};
|
||||
let truncated = selected.len() > max_tables || (requested.is_empty() && available.len() > max_tables);
|
||||
selected.truncate(max_tables);
|
||||
if selected.is_empty() {
|
||||
return text("No matching tables found.");
|
||||
}
|
||||
let mut tables = Vec::with_capacity(selected.len());
|
||||
for table in selected {
|
||||
// Keep metadata calls sequential because some embedded drivers expose a single physical connection.
|
||||
let columns = match self.backend.get_columns(&connection, &database, &schema, &table.name).await {
|
||||
Ok(columns) => columns,
|
||||
Err(error) => return tool_error("SCHEMA_CONTEXT_ERROR", error),
|
||||
};
|
||||
tables.push((table.clone(), columns));
|
||||
}
|
||||
text(format_schema_context(&connection.name, &database, &schema, &tables, truncated))
|
||||
}
|
||||
|
||||
#[tool(name = "dbx_add_connection", description = "Add a new database connection to DBX")]
|
||||
async fn add_connection(&self, Parameters(request): Parameters<AddConnectionRequest>) -> CallToolResult {
|
||||
let mut connections = match self.backend.load_connections().await {
|
||||
Ok(connections) => connections,
|
||||
Err(error) => return tool_error("CONNECTION_LOAD_ERROR", error),
|
||||
};
|
||||
if connections.iter().any(|connection| connection.name.eq_ignore_ascii_case(&request.name)) {
|
||||
return text(format!("Connection \"{}\" already exists.", request.name));
|
||||
}
|
||||
let db_type = match parse_database_type(&request.db_type) {
|
||||
Ok(db_type) => db_type,
|
||||
Err(error) => return tool_error("INVALID_CONNECTION_TYPE", error),
|
||||
};
|
||||
let port = match request.port.or_else(|| default_port(&request.db_type)) {
|
||||
Some(port) => port,
|
||||
None => return text("Port is required for this database type."),
|
||||
};
|
||||
let config = match new_connection_config(
|
||||
Uuid::new_v4().to_string(),
|
||||
request.name,
|
||||
db_type,
|
||||
request.host,
|
||||
port,
|
||||
request.username,
|
||||
request.password,
|
||||
request.database,
|
||||
request.ssl,
|
||||
request.driver_profile,
|
||||
) {
|
||||
Ok(config) => config,
|
||||
Err(error) => return tool_error("INVALID_CONNECTION", error),
|
||||
};
|
||||
connections.push(config.clone());
|
||||
if let Err(error) = self.backend.save_connections(&connections).await {
|
||||
return tool_error("CONNECTION_SAVE_ERROR", error);
|
||||
}
|
||||
text(format!("Connection \"{}\" added (id: {}).", config.name, config.id))
|
||||
}
|
||||
|
||||
#[tool(name = "dbx_remove_connection", description = "Remove a database connection from DBX")]
|
||||
async fn remove_connection(&self, Parameters(request): Parameters<RemoveConnectionRequest>) -> CallToolResult {
|
||||
let mut connections = match self.backend.load_connections().await {
|
||||
Ok(connections) => connections,
|
||||
Err(error) => return tool_error("CONNECTION_LOAD_ERROR", error),
|
||||
};
|
||||
let target = if let Some(id) = request.connection_id.as_deref().map(str::trim).filter(|id| !id.is_empty()) {
|
||||
connections.iter().find(|connection| connection.id == id).cloned()
|
||||
} else {
|
||||
let matching = connections
|
||||
.iter()
|
||||
.filter(|connection| connection.name.eq_ignore_ascii_case(&request.connection_name))
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
if matching.len() > 1 {
|
||||
return tool_error("AMBIGUOUS_CONNECTION", ambiguous_connections(&request.connection_name, &matching));
|
||||
}
|
||||
matching.into_iter().next()
|
||||
};
|
||||
let Some(target) = target else {
|
||||
return tool_error(
|
||||
"CONNECTION_NOT_FOUND",
|
||||
format!("Connection \"{}\" not found.", request.connection_name),
|
||||
);
|
||||
};
|
||||
connections.retain(|connection| connection.id != target.id);
|
||||
if let Err(error) = self.backend.save_connections(&connections).await {
|
||||
return tool_error("CONNECTION_SAVE_ERROR", error);
|
||||
}
|
||||
text(format!("Connection \"{}\" (id: {}) removed.", target.name, target.id))
|
||||
}
|
||||
|
||||
#[tool(name = "dbx_open_table", description = "Open a table in DBX desktop app. Requires DBX to be running.")]
|
||||
async fn open_table(&self, Parameters(request): Parameters<OpenTableRequest>) -> CallToolResult {
|
||||
let connection = match self.resolve_connection(&request.selector).await {
|
||||
Ok(connection) => connection,
|
||||
Err(error) => return error,
|
||||
};
|
||||
let database = self.resolve_database(request.database, &connection);
|
||||
match self
|
||||
.backend
|
||||
.bridge_request(
|
||||
"/open-table",
|
||||
json!({
|
||||
"connection_id": connection.id,
|
||||
"connection_name": connection.name,
|
||||
"table": request.table,
|
||||
"database": database,
|
||||
"schema": request.schema,
|
||||
}),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(()) => text(format!("Opened {} in DBX", request.table)),
|
||||
Err(error) => tool_error("DBX_NOT_RUNNING", error),
|
||||
}
|
||||
}
|
||||
|
||||
#[tool(
|
||||
name = "dbx_execute_and_show",
|
||||
description = "Execute a SQL query in DBX desktop app UI and show results there. Requires DBX to be running."
|
||||
)]
|
||||
async fn execute_and_show(&self, Parameters(request): Parameters<ExecuteAndShowRequest>) -> CallToolResult {
|
||||
let connection = match self.resolve_connection(&request.selector).await {
|
||||
Ok(connection) => connection,
|
||||
Err(error) => return error,
|
||||
};
|
||||
if connection.db_type == DatabaseType::Redis {
|
||||
return tool_error("REDIS_COMMAND_REQUIRED", "Use dbx_execute_redis_command for Redis connections.");
|
||||
}
|
||||
let database = self.resolve_database(request.database, &connection);
|
||||
let permissions = default_permissions();
|
||||
if connection.db_type == DatabaseType::MongoDb {
|
||||
if let Err(error) = validate_mongo_command(&connection, &database, &request.sql) {
|
||||
return error;
|
||||
}
|
||||
} else {
|
||||
let risk = match classify_sql_risk_for_database(&request.sql, connection.db_type) {
|
||||
Ok(risk) => risk,
|
||||
Err(error) => return tool_error("SQL_BLOCKED", error),
|
||||
};
|
||||
if risk != SqlRisk::ReadOnly && targets_production_database(&connection, &database, &request.sql) {
|
||||
return tool_error(
|
||||
"PRODUCTION_WRITE_BLOCKED",
|
||||
"MCP cannot send writes against a production database to DBX.",
|
||||
);
|
||||
}
|
||||
if risk == SqlRisk::Transaction || (risk == SqlRisk::Ddl && !permissions.allow_dangerous) {
|
||||
return tool_error("SQL_BLOCKED", format!("{} statement is blocked for this session.", risk));
|
||||
}
|
||||
if risk == SqlRisk::Write && !permissions.allow_writes {
|
||||
return tool_error("SQL_BLOCKED", "MCP SQL execution is read-only for this session.");
|
||||
}
|
||||
}
|
||||
match self
|
||||
.backend
|
||||
.bridge_request(
|
||||
"/execute-query",
|
||||
json!({
|
||||
"connection_id": connection.id,
|
||||
"connection_name": connection.name,
|
||||
"sql": request.sql,
|
||||
"database": database,
|
||||
"allow_writes": permissions.allow_writes,
|
||||
"allow_dangerous": permissions.allow_dangerous,
|
||||
}),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(()) => text("Query sent to DBX"),
|
||||
Err(error) => tool_error("DBX_NOT_RUNNING", error),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DbxMcpServer {
|
||||
async fn load_scoped_connections(&self) -> Result<Vec<dbx_core::models::connection::ConnectionConfig>, String> {
|
||||
let connections = self.backend.load_connections().await?;
|
||||
if !self.scope.enabled() {
|
||||
return Ok(connections);
|
||||
}
|
||||
Ok(connections.into_iter().filter(|connection| self.scope.matches(connection)).collect())
|
||||
}
|
||||
|
||||
fn resolve_database(
|
||||
&self,
|
||||
requested: Option<String>,
|
||||
connection: &dbx_core::models::connection::ConnectionConfig,
|
||||
) -> String {
|
||||
requested.or_else(|| self.scope.database.clone()).or_else(|| connection.database.clone()).unwrap_or_default()
|
||||
}
|
||||
|
||||
async fn resolve_connection(
|
||||
&self,
|
||||
selector: &ConnectionSelector,
|
||||
) -> Result<dbx_core::models::connection::ConnectionConfig, CallToolResult> {
|
||||
let connections =
|
||||
self.backend.load_connections().await.map_err(|error| tool_error("CONNECTION_LOAD_ERROR", error))?;
|
||||
if let Some(id) = selector.connection_id.as_deref().map(str::trim).filter(|id| !id.is_empty()) {
|
||||
let connection = connections
|
||||
.into_iter()
|
||||
.find(|connection| connection.id == id)
|
||||
.ok_or_else(|| tool_error("CONNECTION_NOT_FOUND", format!("Connection with id \"{id}\" not found.")))?;
|
||||
if self.scope.enabled() && !self.scope.matches(&connection) {
|
||||
return Err(tool_error(
|
||||
"CONNECTION_OUT_OF_SCOPE",
|
||||
format!("Connection \"{id}\" is outside this DBX AI session scope."),
|
||||
));
|
||||
}
|
||||
return Ok(connection);
|
||||
}
|
||||
if self.scope.enabled() {
|
||||
let connection = connections
|
||||
.into_iter()
|
||||
.find(|connection| self.scope.matches(connection))
|
||||
.ok_or_else(|| tool_error("CONNECTION_NOT_FOUND", "Scoped DBX connection was not found."))?;
|
||||
if let Some(name) = selector.connection_name.as_deref().map(str::trim).filter(|name| !name.is_empty()) {
|
||||
if name != connection.name && name != connection.id {
|
||||
return Err(tool_error(
|
||||
"CONNECTION_OUT_OF_SCOPE",
|
||||
format!("Connection \"{name}\" is outside this DBX AI session scope."),
|
||||
));
|
||||
}
|
||||
}
|
||||
return Ok(connection);
|
||||
}
|
||||
let Some(name) = selector.connection_name.as_deref().map(str::trim).filter(|name| !name.is_empty()) else {
|
||||
return Err(tool_error("CONNECTION_NOT_FOUND", "Either connection_id or connection_name is required."));
|
||||
};
|
||||
let matching =
|
||||
connections.into_iter().filter(|connection| connection.name.eq_ignore_ascii_case(name)).collect::<Vec<_>>();
|
||||
match matching.as_slice() {
|
||||
[] => Err(tool_error("CONNECTION_NOT_FOUND", format!("Connection \"{name}\" not found."))),
|
||||
[connection] => Ok(connection.clone()),
|
||||
_ => Err(tool_error("AMBIGUOUS_CONNECTION", ambiguous_connections(name, &matching))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tool_handler(router = self.tool_router)]
|
||||
impl ServerHandler for DbxMcpServer {
|
||||
fn get_info(&self) -> ServerInfo {
|
||||
ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
|
||||
.with_server_info(Implementation::new("dbx", env!("CARGO_PKG_VERSION")))
|
||||
.with_instructions("Use DBX connections to inspect schemas and query databases safely.")
|
||||
}
|
||||
}
|
||||
|
||||
fn text(value: impl Into<String>) -> CallToolResult {
|
||||
CallToolResult::success(vec![ContentBlock::text(value)])
|
||||
}
|
||||
|
||||
fn tool_error(code: &str, message: impl Into<String>) -> CallToolResult {
|
||||
CallToolResult::error(vec![ContentBlock::text(format!("Error [{code}]: {}", message.into()))])
|
||||
}
|
||||
|
||||
fn agent_result(result: dbx_core::agent_events::ToolResult) -> CallToolResult {
|
||||
if result.is_error {
|
||||
tool_error("DBX_TOOL_ERROR", result.content.trim_start_matches("Error: "))
|
||||
} else {
|
||||
text(result.content)
|
||||
}
|
||||
}
|
||||
|
||||
fn default_permissions() -> dbx_core::agent_tools::AgentSqlPermissions {
|
||||
dbx_core::agent_tools::AgentSqlPermissions {
|
||||
allow_writes: boolean_env("DBX_MCP_ALLOW_WRITES").unwrap_or(true),
|
||||
allow_dangerous: boolean_env("DBX_MCP_ALLOW_DANGEROUS_SQL").unwrap_or(false),
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_mongo_command(
|
||||
connection: &dbx_core::models::connection::ConnectionConfig,
|
||||
database: &str,
|
||||
source: &str,
|
||||
) -> Result<MongoCommand, CallToolResult> {
|
||||
let command = mongo::parse(source).map_err(|error| {
|
||||
tool_error(
|
||||
"QUERY_ERROR",
|
||||
format!(
|
||||
"{error} Use MongoDB shell-style commands such as db.collection.find({{}}), db.collection.aggregate([]), or db.collection.countDocuments({{}})."
|
||||
),
|
||||
)
|
||||
})?;
|
||||
let permissions = default_permissions();
|
||||
if command.is_mutating() && !permissions.allow_writes {
|
||||
return Err(tool_error(
|
||||
"SQL_BLOCKED",
|
||||
"MCP MongoDB execution is read-only for this session. Set DBX_MCP_ALLOW_WRITES=1 to allow write commands.",
|
||||
));
|
||||
}
|
||||
if command.has_empty_filter() && !permissions.allow_dangerous {
|
||||
return Err(tool_error(
|
||||
"SQL_BLOCKED",
|
||||
"MongoDB update/delete commands must include a non-empty filter unless DBX_MCP_ALLOW_DANGEROUS_SQL=1 is set.",
|
||||
));
|
||||
}
|
||||
if command.is_dangerous() && !permissions.allow_dangerous {
|
||||
return Err(tool_error(
|
||||
"SQL_BLOCKED",
|
||||
"Dangerous MongoDB command is blocked. Set DBX_MCP_ALLOW_DANGEROUS_SQL=1 to allow it.",
|
||||
));
|
||||
}
|
||||
if command.is_mutating() && is_production_database(connection, database) {
|
||||
return Err(tool_error("PRODUCTION_WRITE_BLOCKED", "MCP cannot execute writes against a production database."));
|
||||
}
|
||||
Ok(command)
|
||||
}
|
||||
|
||||
fn boolean_env(name: &str) -> Option<bool> {
|
||||
match std::env::var(name).ok()?.trim().to_ascii_lowercase().as_str() {
|
||||
"1" | "true" => Some(true),
|
||||
"0" | "false" => Some(false),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn non_empty_env(name: &str) -> Option<String> {
|
||||
std::env::var(name).ok().map(|value| value.trim().to_string()).filter(|value| !value.is_empty())
|
||||
}
|
||||
|
||||
fn default_port(db_type: &str) -> Option<u16> {
|
||||
match db_type.trim().to_ascii_lowercase().as_str() {
|
||||
"mysql" | "doris" | "starrocks" | "manticoresearch" => Some(3306),
|
||||
"postgres" | "redshift" | "highgo" | "kingbase" | "opengauss" | "gaussdb" => Some(5432),
|
||||
"redis" => Some(6379),
|
||||
"mongodb" => Some(27017),
|
||||
"rqlite" => Some(4001),
|
||||
"kwdb" => Some(26257),
|
||||
"cloudflare-d1" => Some(443),
|
||||
"tdengine" => Some(6041),
|
||||
"iotdb" => Some(6667),
|
||||
"xugu" => Some(5138),
|
||||
"sqlite" | "duckdb" | "access" => Some(0),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn ambiguous_connections(name: &str, connections: &[dbx_core::models::connection::ConnectionConfig]) -> String {
|
||||
let lines = connections
|
||||
.iter()
|
||||
.map(|connection| {
|
||||
format!("- {}: {:?} @ {}:{}", connection.id, connection.db_type, connection.host, connection.port)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
format!("Multiple connections found with name \"{name}\". Please specify connection_id:\n{lines}")
|
||||
}
|
||||
|
||||
fn format_connections(connections: &[ConnectionSummary]) -> String {
|
||||
let mut output =
|
||||
String::from("| ID | Name | Type | Host | Port | Database |\n| --- | --- | --- | --- | --- | --- |");
|
||||
for connection in connections {
|
||||
output.push_str(&format!(
|
||||
"\n| {} | {} | {} | {} | {} | {} |",
|
||||
escape_cell(&connection.id),
|
||||
escape_cell(&connection.name),
|
||||
escape_cell(&connection.db_type),
|
||||
escape_cell(&connection.host),
|
||||
connection.port,
|
||||
escape_cell(&connection.database),
|
||||
));
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
fn format_columns(columns: &[dbx_core::db::ColumnInfo]) -> String {
|
||||
let rows = columns
|
||||
.iter()
|
||||
.map(|column| {
|
||||
vec![
|
||||
if column.is_primary_key { format!("{} (PK)", column.name) } else { column.name.clone() },
|
||||
column.data_type.clone(),
|
||||
if column.is_nullable { "YES".to_string() } else { "NO".to_string() },
|
||||
column.column_default.clone().unwrap_or_default(),
|
||||
column.comment.clone().unwrap_or_default(),
|
||||
]
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
markdown_table(&["Column", "Type", "Nullable", "Default", "Comment"], &rows)
|
||||
}
|
||||
|
||||
fn markdown_table(headers: &[&str], rows: &[Vec<String>]) -> String {
|
||||
let mut output = format!("| {} |\n| {} |", headers.join(" | "), vec!["---"; headers.len()].join(" | "));
|
||||
for row in rows {
|
||||
output
|
||||
.push_str(&format!("\n| {} |", row.iter().map(|value| escape_cell(value)).collect::<Vec<_>>().join(" | ")));
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
fn escape_cell(value: &str) -> String {
|
||||
value.replace('|', "\\|").replace('\n', " ")
|
||||
}
|
||||
|
||||
fn redis_database(connection: &dbx_core::models::connection::ConnectionConfig) -> Option<u32> {
|
||||
connection.database.as_deref().and_then(parse_redis_database)
|
||||
}
|
||||
|
||||
fn parse_redis_database(value: &str) -> Option<u32> {
|
||||
value.trim().parse().ok()
|
||||
}
|
||||
|
||||
fn format_redis_result(result: &RedisCommandResult) -> String {
|
||||
let value =
|
||||
result.value.as_str().map(ToOwned::to_owned).unwrap_or_else(|| {
|
||||
serde_json::to_string_pretty(&result.value).unwrap_or_else(|_| result.value.to_string())
|
||||
});
|
||||
let safety = serde_json::to_value(&result.safety)
|
||||
.ok()
|
||||
.and_then(|value| value.as_str().map(ToOwned::to_owned))
|
||||
.unwrap_or_else(|| format!("{:?}", result.safety).to_ascii_lowercase());
|
||||
format!("Command: {}\nSafety: {}\n\n{}", result.command, safety, value)
|
||||
}
|
||||
|
||||
fn format_schema_context(
|
||||
connection: &str,
|
||||
database: &str,
|
||||
schema: &str,
|
||||
tables: &[(dbx_core::db::TableInfo, Vec<dbx_core::db::ColumnInfo>)],
|
||||
truncated: bool,
|
||||
) -> String {
|
||||
let mut output = format!("Connection: {connection}");
|
||||
if !database.is_empty() {
|
||||
output.push_str(&format!("\nDatabase: {database}"));
|
||||
}
|
||||
if !schema.is_empty() {
|
||||
output.push_str(&format!("\nSchema: {schema}"));
|
||||
}
|
||||
for (table, columns) in tables {
|
||||
output.push_str(&format!("\n\n## {}\nType: {}", table.name, table.table_type));
|
||||
for column in columns {
|
||||
output.push_str(&format!(
|
||||
"\n- {} {} {}{}{}",
|
||||
column.name,
|
||||
column.data_type,
|
||||
if column.is_nullable { "NULL" } else { "NOT NULL" },
|
||||
if column.is_primary_key { " PK" } else { "" },
|
||||
column.comment.as_ref().map(|comment| format!(" -- {comment}")).unwrap_or_default(),
|
||||
));
|
||||
}
|
||||
}
|
||||
if truncated {
|
||||
output.push_str("\n\nNote: table list was truncated; request specific table names for more context.");
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use async_trait::async_trait;
|
||||
use dbx_core::models::connection::ConnectionConfig;
|
||||
|
||||
struct FakeBackend {
|
||||
connections: Vec<ConnectionConfig>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DbxBackend for FakeBackend {
|
||||
async fn load_connections(&self) -> Result<Vec<ConnectionConfig>, String> {
|
||||
Ok(self.connections.clone())
|
||||
}
|
||||
|
||||
async fn execute_agent_tool(
|
||||
&self,
|
||||
_connection: &ConnectionConfig,
|
||||
_database: &str,
|
||||
tool_name: &str,
|
||||
_arguments: serde_json::Value,
|
||||
_permissions: dbx_core::agent_tools::AgentSqlPermissions,
|
||||
) -> dbx_core::agent_events::ToolResult {
|
||||
dbx_core::agent_events::ToolResult {
|
||||
tool_call_id: "test".to_string(),
|
||||
tool_name: tool_name.to_string(),
|
||||
content: "ok".to_string(),
|
||||
is_error: false,
|
||||
explain_data: None,
|
||||
}
|
||||
}
|
||||
|
||||
async fn save_connections(&self, _connections: &[ConnectionConfig]) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connection_table_escapes_markdown_cells() {
|
||||
let output = format_connections(&[ConnectionSummary {
|
||||
id: "id|1".to_string(),
|
||||
name: "local\npg".to_string(),
|
||||
db_type: "postgres".to_string(),
|
||||
host: "127.0.0.1".to_string(),
|
||||
port: 5432,
|
||||
database: "app".to_string(),
|
||||
}]);
|
||||
assert!(output.contains("id\\|1"));
|
||||
assert!(output.contains("local pg"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_registers_list_connections_tool() {
|
||||
let server = DbxMcpServer::with_runtime_options(
|
||||
Arc::new(FakeBackend { connections: Vec::new() }),
|
||||
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(), 10);
|
||||
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_remove_connection"));
|
||||
assert!(names.contains(&"dbx_execute_redis_command"));
|
||||
assert!(names.contains(&"dbx_get_schema_context"));
|
||||
assert!(names.contains(&"dbx_open_table"));
|
||||
assert!(names.contains(&"dbx_execute_and_show"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scoped_server_hides_mutating_and_desktop_tools() {
|
||||
let server = DbxMcpServer::with_runtime_options(
|
||||
Arc::new(FakeBackend { connections: Vec::new() }),
|
||||
McpScope { connection_id: Some("scoped".to_string()), ..Default::default() },
|
||||
false,
|
||||
);
|
||||
let names = server.tool_router.list_all().into_iter().map(|tool| tool.name).collect::<Vec<_>>();
|
||||
assert_eq!(names.len(), 6);
|
||||
assert!(!names.iter().any(|name| name == "dbx_add_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"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use dbx_core::{models::connection::ConnectionConfig, storage::Storage};
|
||||
use dbx_mcp::{DbxMcpServer, LocalBackend, McpScope};
|
||||
use rmcp::{model::CallToolRequestParams, ServiceExt};
|
||||
use serde_json::{json, Map, Value};
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[tokio::test]
|
||||
async fn local_backend_reads_dbx_storage_without_desktop_process() {
|
||||
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");
|
||||
let connection: ConnectionConfig = serde_json::from_value(json!({
|
||||
"id": "local-sqlite",
|
||||
"name": "offline-sqlite",
|
||||
"db_type": "sqlite",
|
||||
"host": "",
|
||||
"port": 0,
|
||||
"username": "",
|
||||
"password": "",
|
||||
"database": directory.path().join("data.sqlite").to_string_lossy(),
|
||||
"ssl": false
|
||||
}))
|
||||
.expect("minimal connection config");
|
||||
storage.save_connections(&[connection]).await.expect("save connection");
|
||||
|
||||
let backend = Arc::new(LocalBackend::open(&db_path).await.expect("open local backend"));
|
||||
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 result = client
|
||||
.peer()
|
||||
.call_tool(CallToolRequestParams::new("dbx_list_connections"))
|
||||
.await
|
||||
.expect("list local connections");
|
||||
let text = result.content[0].as_text().expect("text response");
|
||||
assert!(text.text.contains("offline-sqlite"));
|
||||
assert!(text.text.contains("local-sqlite"));
|
||||
client.cancel().await.expect("close client");
|
||||
server_task.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires DBX_MCP_TEST_MONGO_HOST and DBX_MCP_TEST_MONGO_PASSWORD"]
|
||||
async fn executes_mongo_shell_commands_without_desktop_process() {
|
||||
let host = std::env::var("DBX_MCP_TEST_MONGO_HOST").expect("MongoDB host");
|
||||
let port = std::env::var("DBX_MCP_TEST_MONGO_PORT")
|
||||
.unwrap_or_else(|_| "27017".to_string())
|
||||
.parse::<u16>()
|
||||
.expect("MongoDB port");
|
||||
let password = std::env::var("DBX_MCP_TEST_MONGO_PASSWORD").expect("MongoDB password");
|
||||
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");
|
||||
let connection: ConnectionConfig = serde_json::from_value(json!({
|
||||
"id": "mongo-e2e",
|
||||
"name": "mongo-e2e",
|
||||
"db_type": "mongodb",
|
||||
"host": host,
|
||||
"port": port,
|
||||
"username": "root",
|
||||
"password": password,
|
||||
"database": "dbx_mcp_test",
|
||||
"url_params": "authSource=admin",
|
||||
"ssl": false
|
||||
}))
|
||||
.expect("MongoDB connection config");
|
||||
storage.save_connections(&[connection]).await.expect("save connection");
|
||||
|
||||
let backend = Arc::new(LocalBackend::open(&db_path).await.expect("open local backend"));
|
||||
let server = DbxMcpServer::with_runtime_options(backend, McpScope::default(), false);
|
||||
let (server_transport, client_transport) = tokio::io::duplex(32 * 1024);
|
||||
let server_task = tokio::spawn(async move { server.serve(server_transport).await });
|
||||
let client = ().serve(client_transport).await.expect("initialize client");
|
||||
let original_writes = std::env::var_os("DBX_MCP_ALLOW_WRITES");
|
||||
std::env::set_var("DBX_MCP_ALLOW_WRITES", "1");
|
||||
|
||||
call_query(&client, "db.items.deleteOne({_id: 'rust-mcp-e2e'})").await;
|
||||
call_query(&client, "db.items.insertOne({_id: 'rust-mcp-e2e', name: 'Ada'})").await;
|
||||
let result = call_query(&client, "db.items.find({_id: 'rust-mcp-e2e'}).limit(1)").await;
|
||||
assert!(result.contains("Ada"), "unexpected MongoDB result: {result}");
|
||||
call_query(&client, "db.items.deleteOne({_id: 'rust-mcp-e2e'})").await;
|
||||
|
||||
match original_writes {
|
||||
Some(value) => std::env::set_var("DBX_MCP_ALLOW_WRITES", value),
|
||||
None => std::env::remove_var("DBX_MCP_ALLOW_WRITES"),
|
||||
}
|
||||
client.cancel().await.expect("close client");
|
||||
server_task.abort();
|
||||
}
|
||||
|
||||
async fn call_query(client: &rmcp::service::RunningService<rmcp::RoleClient, ()>, sql: &str) -> String {
|
||||
let arguments = json!({
|
||||
"connection_id": "mongo-e2e",
|
||||
"database": "dbx_mcp_test",
|
||||
"sql": sql,
|
||||
})
|
||||
.as_object()
|
||||
.cloned()
|
||||
.unwrap_or_else(Map::<String, Value>::new);
|
||||
let result = client
|
||||
.peer()
|
||||
.call_tool(CallToolRequestParams::new("dbx_execute_query").with_arguments(arguments))
|
||||
.await
|
||||
.expect("execute MongoDB command");
|
||||
let text = result.content[0].as_text().expect("text result").text.clone();
|
||||
assert_ne!(result.is_error, Some(true), "MongoDB command failed: {text}");
|
||||
text
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use dbx_core::{agent_events::ToolResult, agent_tools::AgentSqlPermissions, models::connection::ConnectionConfig};
|
||||
use dbx_mcp::{DbxBackend, DbxMcpServer, McpScope};
|
||||
use rmcp::{model::CallToolRequestParams, ServiceExt};
|
||||
use serde_json::Value;
|
||||
|
||||
struct EmptyBackend;
|
||||
|
||||
#[async_trait]
|
||||
impl DbxBackend for EmptyBackend {
|
||||
async fn load_connections(&self) -> Result<Vec<ConnectionConfig>, String> {
|
||||
Ok(Vec::new())
|
||||
}
|
||||
|
||||
async fn execute_agent_tool(
|
||||
&self,
|
||||
_connection: &ConnectionConfig,
|
||||
_database: &str,
|
||||
tool_name: &str,
|
||||
_arguments: Value,
|
||||
_permissions: AgentSqlPermissions,
|
||||
) -> ToolResult {
|
||||
ToolResult {
|
||||
tool_call_id: "protocol-test".to_string(),
|
||||
tool_name: tool_name.to_string(),
|
||||
content: "ok".to_string(),
|
||||
is_error: false,
|
||||
explain_data: None,
|
||||
}
|
||||
}
|
||||
|
||||
async fn save_connections(&self, _connections: &[ConnectionConfig]) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn initializes_lists_tools_and_calls_a_tool() {
|
||||
let (server_transport, client_transport) = tokio::io::duplex(16 * 1024);
|
||||
let server = DbxMcpServer::with_runtime_options(Arc::new(EmptyBackend), 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 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(), 10);
|
||||
assert!(names.contains(&"dbx_list_connections"));
|
||||
assert!(names.contains(&"dbx_execute_redis_command"));
|
||||
assert!(names.contains(&"dbx_execute_and_show"));
|
||||
|
||||
let result = client.peer().call_tool(CallToolRequestParams::new("dbx_list_connections")).await.expect("call tool");
|
||||
let response = result.content[0].as_text().expect("text response");
|
||||
assert_eq!(response.text, "No connections configured in DBX.");
|
||||
|
||||
client.cancel().await.expect("close MCP client");
|
||||
server_task.abort();
|
||||
}
|
||||
Reference in new issue
Block a user