Skip to main content

dbmcp_sqlite/tools/
write_query.rs

1//! MCP tool: `writeQuery`.
2
3use std::borrow::Cow;
4
5use dbmcp_pii::MaybeRedact as _;
6use dbmcp_server::types::QueryResponse;
7
8use dbmcp_sql::Connection as _;
9use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
10use rmcp::model::{ErrorData, ToolAnnotations};
11
12use crate::SqliteHandler;
13use crate::types::QueryRequest;
14
15const NAME: &str = "writeQuery";
16const TITLE: &str = "Write Query";
17const DESCRIPTION: &str = include_str!("../../assets/tools/write_query.md");
18
19/// Marker type for the `writeQuery` MCP tool.
20pub(crate) struct WriteQueryTool;
21
22impl ToolBase for WriteQueryTool {
23    type Parameter = QueryRequest;
24    type Output = QueryResponse;
25    type Error = ErrorData;
26
27    fn name() -> Cow<'static, str> {
28        NAME.into()
29    }
30
31    fn title() -> Option<String> {
32        Some(TITLE.into())
33    }
34
35    fn description() -> Option<Cow<'static, str>> {
36        Some(DESCRIPTION.into())
37    }
38
39    fn annotations() -> Option<ToolAnnotations> {
40        Some(
41            ToolAnnotations::new()
42                .read_only(false)
43                .destructive(true)
44                .idempotent(false)
45                .open_world(true),
46        )
47    }
48}
49
50impl AsyncTool<SqliteHandler> for WriteQueryTool {
51    async fn invoke(handler: &SqliteHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
52        handler.write_query(params).await
53    }
54}
55
56impl SqliteHandler {
57    /// Executes a write SQL query.
58    ///
59    /// # Errors
60    ///
61    /// Returns [`SqlError`] if the query fails.
62    pub async fn write_query(&self, QueryRequest { query }: QueryRequest) -> Result<QueryResponse, ErrorData> {
63        let rows = self.connection.fetch_json(query.as_str(), None).await?;
64        let rows = self.redactor.redact_rows(rows).await?;
65        Ok(QueryResponse { rows })
66    }
67}