feat(mcp): add native installs and resource discovery
This commit is contained in:
36 files changed
+1934
-103
No files matched your search
@@ -8,6 +8,10 @@ use rmcp::ServiceExt;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if std::env::args_os().nth(1).is_some_and(|argument| argument == "--version") {
|
||||
println!("dbx-mcp {}", env!("CARGO_PKG_VERSION"));
|
||||
return Ok(());
|
||||
}
|
||||
let runtime = RuntimeConfig::from_environment_and_args()?;
|
||||
let backend: Arc<dyn DbxBackend> = if let Ok(base_url) = std::env::var("DBX_WEB_URL") {
|
||||
Arc::new(
|
||||
|
||||
@@ -2,11 +2,16 @@ use std::{collections::HashMap, sync::Arc, time::Duration, time::Instant};
|
||||
|
||||
use rmcp::{
|
||||
handler::server::{router::tool::ToolRouter, wrapper::Parameters},
|
||||
model::{CallToolResult, ContentBlock, Implementation, ServerCapabilities, ServerInfo},
|
||||
model::{
|
||||
CallToolResult, ContentBlock, ErrorData, Implementation, ListResourceTemplatesResult, ListResourcesResult,
|
||||
ReadResourceRequestParams, ReadResourceResult, Resource, ResourceContents, ResourceTemplate,
|
||||
ServerCapabilities, ServerInfo,
|
||||
},
|
||||
schemars, tool, tool_handler, tool_router, ServerHandler,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
use url::Url;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::backend::{format_query_result, new_connection_config, parse_database_type, ConnectionSummary, DbxBackend};
|
||||
@@ -36,6 +41,19 @@ use dbx_core::{
|
||||
storage::{McpDatabaseScope, McpGlobalPolicy},
|
||||
};
|
||||
|
||||
const CONNECTIONS_RESOURCE_URI: &str = "dbx://connections";
|
||||
const DATABASES_RESOURCE_TEMPLATE: &str = "dbx://connections/{connection_id}/databases";
|
||||
const TABLES_RESOURCE_TEMPLATE: &str = "dbx://connections/{connection_id}/tables{?database,schema}";
|
||||
const TABLE_SCHEMA_RESOURCE_TEMPLATE: &str = "dbx://connections/{connection_id}/table-schema{?database,schema,table}";
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
enum DbxResourceRequest {
|
||||
Connections,
|
||||
Databases { connection_id: String },
|
||||
Tables { connection_id: String, database: Option<String>, schema: Option<String> },
|
||||
TableSchema { connection_id: String, database: Option<String>, schema: Option<String>, table: String },
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, schemars::JsonSchema)]
|
||||
pub struct ListConnectionsRequest {}
|
||||
|
||||
@@ -2847,11 +2865,101 @@ impl DbxMcpServer {
|
||||
#[tool_handler(router = self.tool_router)]
|
||||
impl ServerHandler for DbxMcpServer {
|
||||
fn get_info(&self) -> ServerInfo {
|
||||
ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
|
||||
ServerInfo::new(ServerCapabilities::builder().enable_tools().enable_resources().build())
|
||||
.with_server_info(Implementation::new("dbx", env!("CARGO_PKG_VERSION")))
|
||||
.with_instructions("Use DBX connections to inspect schemas and query databases safely.")
|
||||
}
|
||||
|
||||
async fn list_resources(
|
||||
&self,
|
||||
_request: Option<rmcp::model::PaginatedRequestParams>,
|
||||
_context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
|
||||
) -> Result<ListResourcesResult, ErrorData> {
|
||||
let tools = self.policy_filtered_tools().await;
|
||||
let resources = tools
|
||||
.iter()
|
||||
.any(|tool| tool.name.as_ref() == "dbx_list_connections")
|
||||
.then(|| {
|
||||
Resource::new(CONNECTIONS_RESOURCE_URI, "dbx_connections")
|
||||
.with_title("DBX connections")
|
||||
.with_description("Database connections visible to the current DBX MCP scope")
|
||||
.with_mime_type("text/markdown")
|
||||
})
|
||||
.into_iter()
|
||||
.collect();
|
||||
Ok(ListResourcesResult { resources, meta: None, next_cursor: None })
|
||||
}
|
||||
|
||||
async fn list_resource_templates(
|
||||
&self,
|
||||
_request: Option<rmcp::model::PaginatedRequestParams>,
|
||||
_context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
|
||||
) -> Result<ListResourceTemplatesResult, ErrorData> {
|
||||
let tools = self.policy_filtered_tools().await;
|
||||
let has_tool = |name: &str| tools.iter().any(|tool| tool.name.as_ref() == name);
|
||||
let mut resource_templates = Vec::new();
|
||||
if has_tool("dbx_list_databases") {
|
||||
resource_templates.push(
|
||||
ResourceTemplate::new(DATABASES_RESOURCE_TEMPLATE, "dbx_connection_databases")
|
||||
.with_title("DBX connection databases")
|
||||
.with_description("Databases visible through a DBX connection")
|
||||
.with_mime_type("text/markdown"),
|
||||
);
|
||||
}
|
||||
if has_tool("dbx_list_tables") {
|
||||
resource_templates.push(
|
||||
ResourceTemplate::new(TABLES_RESOURCE_TEMPLATE, "dbx_connection_tables")
|
||||
.with_title("DBX connection tables")
|
||||
.with_description("Tables and views visible through a DBX connection and optional database/schema")
|
||||
.with_mime_type("text/markdown"),
|
||||
);
|
||||
}
|
||||
if has_tool("dbx_describe_table") {
|
||||
resource_templates.push(
|
||||
ResourceTemplate::new(TABLE_SCHEMA_RESOURCE_TEMPLATE, "dbx_table_schema")
|
||||
.with_title("DBX table schema")
|
||||
.with_description("Column definitions for a table visible through a DBX connection")
|
||||
.with_mime_type("text/markdown"),
|
||||
);
|
||||
}
|
||||
Ok(ListResourceTemplatesResult { resource_templates, meta: None, next_cursor: None })
|
||||
}
|
||||
|
||||
async fn read_resource(
|
||||
&self,
|
||||
request: ReadResourceRequestParams,
|
||||
_context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
|
||||
) -> Result<ReadResourceResult, ErrorData> {
|
||||
let uri = request.uri;
|
||||
let result = match parse_dbx_resource_uri(&uri)? {
|
||||
DbxResourceRequest::Connections => self.list_connections(Parameters(ListConnectionsRequest {})).await,
|
||||
DbxResourceRequest::Databases { connection_id } => {
|
||||
self.list_databases(Parameters(ListDatabasesRequest {
|
||||
selector: ConnectionSelector { connection_id: Some(connection_id), connection_name: None },
|
||||
}))
|
||||
.await
|
||||
}
|
||||
DbxResourceRequest::Tables { connection_id, database, schema } => {
|
||||
self.list_tables(Parameters(ListTablesRequest {
|
||||
selector: ConnectionSelector { connection_id: Some(connection_id), connection_name: None },
|
||||
database,
|
||||
schema,
|
||||
}))
|
||||
.await
|
||||
}
|
||||
DbxResourceRequest::TableSchema { connection_id, database, schema, table } => {
|
||||
self.describe_table(Parameters(DescribeTableRequest {
|
||||
selector: ConnectionSelector { connection_id: Some(connection_id), connection_name: None },
|
||||
table,
|
||||
database,
|
||||
schema,
|
||||
}))
|
||||
.await
|
||||
}
|
||||
};
|
||||
resource_result_from_tool(uri, result)
|
||||
}
|
||||
|
||||
/// Hide tools the global policy disallows from the advertised list, the
|
||||
/// same way scope-based route disabling does. Per-call enforcement stays
|
||||
/// in `ensure_tool_allowed`; this only stops clients (especially LLMs)
|
||||
@@ -2868,6 +2976,102 @@ impl ServerHandler for DbxMcpServer {
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_dbx_resource_uri(uri: &str) -> Result<DbxResourceRequest, ErrorData> {
|
||||
let parsed = Url::parse(uri).map_err(|_| ErrorData::invalid_params("Invalid DBX resource URI.", None))?;
|
||||
if parsed.scheme() != "dbx"
|
||||
|| parsed.host_str() != Some("connections")
|
||||
|| !parsed.username().is_empty()
|
||||
|| parsed.password().is_some()
|
||||
|| parsed.port().is_some()
|
||||
|| parsed.fragment().is_some()
|
||||
{
|
||||
return Err(ErrorData::resource_not_found(format!("Unknown DBX resource: {uri}"), None));
|
||||
}
|
||||
|
||||
let path = parsed.path().trim_matches('/');
|
||||
let segments = if path.is_empty() { Vec::new() } else { path.split('/').collect::<Vec<_>>() };
|
||||
let mut query = HashMap::new();
|
||||
for (key, value) in parsed.query_pairs() {
|
||||
let key = key.into_owned();
|
||||
if query.insert(key.clone(), value.into_owned()).is_some() {
|
||||
return Err(ErrorData::invalid_params(format!("Duplicate DBX resource parameter: {key}"), None));
|
||||
}
|
||||
}
|
||||
|
||||
match segments.as_slice() {
|
||||
[] => {
|
||||
reject_resource_parameters(&query, &[])?;
|
||||
Ok(DbxResourceRequest::Connections)
|
||||
}
|
||||
[connection_id, "databases"] => {
|
||||
reject_resource_parameters(&query, &[])?;
|
||||
Ok(DbxResourceRequest::Databases {
|
||||
connection_id: required_resource_value("connection_id", connection_id)?,
|
||||
})
|
||||
}
|
||||
[connection_id, "tables"] => {
|
||||
reject_resource_parameters(&query, &["database", "schema"])?;
|
||||
Ok(DbxResourceRequest::Tables {
|
||||
connection_id: required_resource_value("connection_id", connection_id)?,
|
||||
database: optional_resource_parameter(&query, "database"),
|
||||
schema: optional_resource_parameter(&query, "schema"),
|
||||
})
|
||||
}
|
||||
[connection_id, "table-schema"] => {
|
||||
reject_resource_parameters(&query, &["database", "schema", "table"])?;
|
||||
Ok(DbxResourceRequest::TableSchema {
|
||||
connection_id: required_resource_value("connection_id", connection_id)?,
|
||||
database: optional_resource_parameter(&query, "database"),
|
||||
schema: optional_resource_parameter(&query, "schema"),
|
||||
table: required_resource_parameter(&query, "table")?,
|
||||
})
|
||||
}
|
||||
_ => Err(ErrorData::resource_not_found(format!("Unknown DBX resource: {uri}"), None)),
|
||||
}
|
||||
}
|
||||
|
||||
fn reject_resource_parameters(parameters: &HashMap<String, String>, allowed: &[&str]) -> Result<(), ErrorData> {
|
||||
if let Some(name) = parameters.keys().find(|name| !allowed.contains(&name.as_str())) {
|
||||
return Err(ErrorData::invalid_params(format!("Unknown DBX resource parameter: {name}"), None));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn optional_resource_parameter(parameters: &HashMap<String, String>, name: &str) -> Option<String> {
|
||||
parameters.get(name).map(|value| value.trim()).filter(|value| !value.is_empty()).map(str::to_string)
|
||||
}
|
||||
|
||||
fn required_resource_parameter(parameters: &HashMap<String, String>, name: &str) -> Result<String, ErrorData> {
|
||||
optional_resource_parameter(parameters, name)
|
||||
.ok_or_else(|| ErrorData::invalid_params(format!("Missing DBX resource parameter: {name}"), None))
|
||||
}
|
||||
|
||||
fn required_resource_value(name: &str, value: &str) -> Result<String, ErrorData> {
|
||||
let value = value.trim();
|
||||
if value.is_empty() {
|
||||
Err(ErrorData::invalid_params(format!("Missing DBX resource parameter: {name}"), None))
|
||||
} else {
|
||||
Ok(value.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn resource_result_from_tool(uri: String, result: CallToolResult) -> Result<ReadResourceResult, ErrorData> {
|
||||
let output = result
|
||||
.content
|
||||
.iter()
|
||||
.filter_map(|content| content.as_text().map(|text| text.text.as_str()))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
if result.is_error == Some(true) {
|
||||
let message = if output.is_empty() { "DBX resource read failed.".to_string() } else { output };
|
||||
return Err(ErrorData::invalid_params(message, Some(json!({ "uri": uri }))));
|
||||
}
|
||||
if output.is_empty() {
|
||||
return Err(ErrorData::internal_error("DBX resource returned no text content.", Some(json!({ "uri": uri }))));
|
||||
}
|
||||
Ok(ReadResourceResult::new(vec![ResourceContents::text(output, uri).with_mime_type("text/markdown")]))
|
||||
}
|
||||
|
||||
fn text(value: impl Into<String>) -> CallToolResult {
|
||||
CallToolResult::success(vec![ContentBlock::text(value)])
|
||||
}
|
||||
@@ -4014,6 +4218,34 @@ mod tests {
|
||||
};
|
||||
use std::{collections::HashSet, sync::atomic::AtomicUsize, time::Duration};
|
||||
|
||||
#[test]
|
||||
fn parses_dbx_resource_uris_and_decodes_query_values() {
|
||||
assert_eq!(parse_dbx_resource_uri(CONNECTIONS_RESOURCE_URI).unwrap(), DbxResourceRequest::Connections);
|
||||
assert_eq!(
|
||||
parse_dbx_resource_uri("dbx://connections/connection-1/databases").unwrap(),
|
||||
DbxResourceRequest::Databases { connection_id: "connection-1".to_string() }
|
||||
);
|
||||
assert_eq!(
|
||||
parse_dbx_resource_uri(
|
||||
"dbx://connections/connection-1/table-schema?database=sales%20data&schema=public&table=order%20items"
|
||||
)
|
||||
.unwrap(),
|
||||
DbxResourceRequest::TableSchema {
|
||||
connection_id: "connection-1".to_string(),
|
||||
database: Some("sales data".to_string()),
|
||||
schema: Some("public".to_string()),
|
||||
table: "order items".to_string(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_dbx_resource_uris() {
|
||||
assert!(parse_dbx_resource_uri("dbx://connections/connection-1/table-schema").is_err());
|
||||
assert!(parse_dbx_resource_uri("dbx://connections/connection-1/tables?unknown=value").is_err());
|
||||
assert!(parse_dbx_resource_uri("file:///tmp/connections").is_err());
|
||||
}
|
||||
|
||||
struct FakeTransactionIo {
|
||||
in_transaction: bool,
|
||||
executed_sql: Arc<std::sync::Mutex<Vec<String>>>,
|
||||
|
||||
@@ -6,7 +6,10 @@ use dbx_core::{
|
||||
storage::McpGlobalPolicy,
|
||||
};
|
||||
use dbx_mcp::{DbxBackend, DbxMcpServer, McpScope};
|
||||
use rmcp::{model::CallToolRequestParams, ServiceExt};
|
||||
use rmcp::{
|
||||
model::{CallToolRequestParams, ReadResourceRequestParams, ResourceContents},
|
||||
ServiceExt,
|
||||
};
|
||||
use serde_json::{json, Map, Value};
|
||||
|
||||
struct EmptyBackend;
|
||||
@@ -72,6 +75,42 @@ impl DbxBackend for PolicyBackend {
|
||||
Ok(self.connections.clone())
|
||||
}
|
||||
|
||||
async fn list_databases(&self, _connection: &ConnectionConfig) -> Result<Vec<String>, String> {
|
||||
Ok(vec!["analytics".to_string(), "reporting".to_string()])
|
||||
}
|
||||
|
||||
async fn list_tables(
|
||||
&self,
|
||||
_connection: &ConnectionConfig,
|
||||
_database: &str,
|
||||
_schema: &str,
|
||||
) -> Result<Vec<dbx_core::db::TableInfo>, String> {
|
||||
Ok(vec![dbx_core::db::TableInfo {
|
||||
name: "orders".to_string(),
|
||||
table_type: "TABLE".to_string(),
|
||||
valid: Some(true),
|
||||
comment: Some("Customer orders".to_string()),
|
||||
parent_schema: None,
|
||||
parent_name: None,
|
||||
}])
|
||||
}
|
||||
|
||||
async fn get_columns(
|
||||
&self,
|
||||
_connection: &ConnectionConfig,
|
||||
_database: &str,
|
||||
_schema: &str,
|
||||
_table: &str,
|
||||
) -> Result<Vec<dbx_core::db::ColumnInfo>, String> {
|
||||
Ok(vec![dbx_core::db::ColumnInfo {
|
||||
name: "id".to_string(),
|
||||
data_type: "INTEGER".to_string(),
|
||||
is_nullable: false,
|
||||
is_primary_key: true,
|
||||
..Default::default()
|
||||
}])
|
||||
}
|
||||
|
||||
async fn load_connection_group_paths(&self) -> Result<HashMap<String, Vec<String>>, String> {
|
||||
self.group_paths.clone()
|
||||
}
|
||||
@@ -354,6 +393,28 @@ async fn initializes_lists_tools_and_calls_a_tool() {
|
||||
#[cfg(feature = "mq-admin")]
|
||||
assert!(names.contains(&"dbx_send_message"));
|
||||
|
||||
let server_info = client.peer_info().expect("server info");
|
||||
assert!(server_info.capabilities.resources.is_some());
|
||||
|
||||
let resources = client.peer().list_resources(None).await.expect("list resources");
|
||||
assert_eq!(resources.resources.len(), 1);
|
||||
assert_eq!(resources.resources[0].uri, "dbx://connections");
|
||||
|
||||
let templates = client.peer().list_resource_templates(None).await.expect("list resource templates");
|
||||
let template_names = templates.resource_templates.iter().map(|template| template.name.as_str()).collect::<Vec<_>>();
|
||||
assert_eq!(template_names, vec!["dbx_connection_databases", "dbx_connection_tables", "dbx_table_schema"]);
|
||||
|
||||
let resource = client
|
||||
.peer()
|
||||
.read_resource(ReadResourceRequestParams::new("dbx://connections"))
|
||||
.await
|
||||
.expect("read connections resource");
|
||||
let ResourceContents::TextResourceContents { text, mime_type, .. } = &resource.contents[0] else {
|
||||
panic!("connections resource should be text");
|
||||
};
|
||||
assert_eq!(mime_type.as_deref(), Some("text/markdown"));
|
||||
assert_eq!(text, "No connections configured in DBX.");
|
||||
|
||||
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.");
|
||||
@@ -362,6 +423,76 @@ async fn initializes_lists_tools_and_calls_a_tool() {
|
||||
server_task.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn reads_database_metadata_through_resource_templates() {
|
||||
let backend = PolicyBackend {
|
||||
policy: McpGlobalPolicy::default(),
|
||||
connections: vec![test_connection("local", "local-db")],
|
||||
group_paths: Ok(HashMap::new()),
|
||||
};
|
||||
let (server_transport, client_transport) = tokio::io::duplex(16 * 1024);
|
||||
let server = DbxMcpServer::with_runtime_options(Arc::new(backend), 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");
|
||||
|
||||
for (uri, expected) in [
|
||||
("dbx://connections/local/databases", "analytics"),
|
||||
("dbx://connections/local/tables?database=analytics", "orders (TABLE)"),
|
||||
("dbx://connections/local/table-schema?database=analytics&table=orders", "id (PK)"),
|
||||
] {
|
||||
let result = client
|
||||
.peer()
|
||||
.read_resource(ReadResourceRequestParams::new(uri))
|
||||
.await
|
||||
.unwrap_or_else(|error| panic!("read {uri}: {error}"));
|
||||
let ResourceContents::TextResourceContents { text, mime_type, .. } = &result.contents[0] else {
|
||||
panic!("{uri} should return text");
|
||||
};
|
||||
assert_eq!(mime_type.as_deref(), Some("text/markdown"));
|
||||
assert!(text.contains(expected), "{uri}: {text}");
|
||||
}
|
||||
|
||||
let invalid = client
|
||||
.peer()
|
||||
.read_resource(ReadResourceRequestParams::new("dbx://connections/local/table-schema?database=analytics"))
|
||||
.await;
|
||||
assert!(invalid.is_err());
|
||||
|
||||
client.cancel().await.expect("close MCP client");
|
||||
server_task.abort();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn resource_catalog_follows_the_tool_allowlist() {
|
||||
let backend = PolicyBackend {
|
||||
policy: McpGlobalPolicy {
|
||||
allowed_tool_names: Some(vec!["dbx_list_connections".to_string()]),
|
||||
..Default::default()
|
||||
},
|
||||
connections: vec![test_connection("local", "local-db")],
|
||||
group_paths: Ok(HashMap::new()),
|
||||
};
|
||||
let (server_transport, client_transport) = tokio::io::duplex(16 * 1024);
|
||||
let server = DbxMcpServer::with_runtime_options(Arc::new(backend), 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 resources = client.peer().list_resources(None).await.expect("list resources");
|
||||
assert_eq!(resources.resources.len(), 1);
|
||||
let templates = client.peer().list_resource_templates(None).await.expect("list resource templates");
|
||||
assert!(templates.resource_templates.is_empty());
|
||||
|
||||
let denied = client
|
||||
.peer()
|
||||
.read_resource(ReadResourceRequestParams::new("dbx://connections/local/databases"))
|
||||
.await
|
||||
.expect_err("database resource should respect the tool allowlist");
|
||||
assert!(denied.to_string().contains("TOOL_OUT_OF_SCOPE"), "{denied}");
|
||||
|
||||
client.cancel().await.expect("close MCP client");
|
||||
server_task.abort();
|
||||
}
|
||||
|
||||
#[cfg(feature = "mq-admin")]
|
||||
#[tokio::test]
|
||||
async fn kafka_peek_round_trips_over_mcp_in_local_and_web_modes() {
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
#[test]
|
||||
fn version_exits_without_initializing_storage_or_transport() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
let output = std::process::Command::new(env!("CARGO_BIN_EXE_dbx-mcp"))
|
||||
.arg("--version")
|
||||
.env("DBX_DATA_DIR", directory.path().join("must-not-exist"))
|
||||
.env("DBX_WEB_URL", "not a URL")
|
||||
.env("DBX_MCP_TRANSPORT", "invalid")
|
||||
.stdin(std::process::Stdio::null())
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(output.status.success());
|
||||
assert_eq!(String::from_utf8(output.stdout).unwrap(), format!("dbx-mcp {}\n", env!("CARGO_PKG_VERSION")));
|
||||
assert!(output.stderr.is_empty());
|
||||
assert!(!directory.path().join("must-not-exist").exists());
|
||||
}
|
||||
Reference in new issue
Block a user