Skip to main content

dbmcp_postgres/tools/
write_query.rs

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