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