Skip to main content

dbmcp_postgres/tools/
write_query.rs

1//! MCP tool: `writeQuery`.
2
3use std::borrow::Cow;
4
5use dbmcp_server::types::{PinnedQueryRequest, QueryResponse, UnpinnedQueryRequest};
6use dbmcp_sql::Connection as _;
7use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
8use rmcp::model::{ErrorData, ToolAnnotations};
9
10use crate::PostgresHandler;
11
12const NAME: &str = "writeQuery";
13const TITLE: &str = "Write Query";
14const DESCRIPTION_PINNED: &str = include_str!("../../assets/tools/write_query/pinned.md");
15const DESCRIPTION_UNPINNED: &str = include_str!("../../assets/tools/write_query/unpinned.md");
16
17fn annotations() -> ToolAnnotations {
18    ToolAnnotations::new()
19        .read_only(false)
20        .destructive(true)
21        .idempotent(false)
22        .open_world(true)
23}
24
25/// Marker type for the `writeQuery` MCP tool (pinned variant — no `database` field).
26pub(crate) struct PinnedWriteQueryTool;
27
28impl ToolBase for PinnedWriteQueryTool {
29    type Parameter = PinnedQueryRequest;
30    type Output = QueryResponse;
31    type Error = ErrorData;
32
33    fn name() -> Cow<'static, str> {
34        NAME.into()
35    }
36
37    fn title() -> Option<String> {
38        Some(TITLE.into())
39    }
40
41    fn description() -> Option<Cow<'static, str>> {
42        Some(DESCRIPTION_PINNED.into())
43    }
44
45    fn annotations() -> Option<ToolAnnotations> {
46        Some(annotations())
47    }
48}
49
50impl AsyncTool<PostgresHandler> for PinnedWriteQueryTool {
51    async fn invoke(handler: &PostgresHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
52        handler.write_query(params.query, None).await
53    }
54}
55
56/// Marker type for the `writeQuery` MCP tool (unpinned variant — carries `database`).
57pub(crate) struct UnpinnedWriteQueryTool;
58
59impl ToolBase for UnpinnedWriteQueryTool {
60    type Parameter = UnpinnedQueryRequest;
61    type Output = QueryResponse;
62    type Error = ErrorData;
63
64    fn name() -> Cow<'static, str> {
65        NAME.into()
66    }
67
68    fn title() -> Option<String> {
69        Some(TITLE.into())
70    }
71
72    fn description() -> Option<Cow<'static, str>> {
73        Some(DESCRIPTION_UNPINNED.into())
74    }
75
76    fn annotations() -> Option<ToolAnnotations> {
77        Some(annotations())
78    }
79}
80
81impl AsyncTool<PostgresHandler> for UnpinnedWriteQueryTool {
82    async fn invoke(handler: &PostgresHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
83        handler.write_query(params.inner.query, params.database).await
84    }
85}
86
87impl PostgresHandler {
88    /// Executes a write SQL query.
89    ///
90    /// # Errors
91    ///
92    /// Returns [`SqlError`] if the query fails.
93    pub async fn write_query(&self, query: String, database: Option<String>) -> Result<QueryResponse, ErrorData> {
94        let database = database.as_deref().map(str::trim).filter(|s| !s.is_empty());
95        let mut rows = self.connection.fetch_json(query.as_str(), database).await?;
96        if let Some(r) = &self.redactor {
97            r.apply(&mut rows)?;
98        }
99        Ok(QueryResponse { rows })
100    }
101}