Skip to main content

dbmcp_sqlite/tools/
write_query.rs

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