Skip to main content

dbmcp_sqlite/tools/
write_query.rs

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