Skip to main content

dbmcp_postgres/tools/
drop_table.rs

1//! MCP tool: `dropTable`.
2
3use std::borrow::Cow;
4
5use dbmcp_server::types::MessageResponse;
6use dbmcp_sql::Connection as _;
7use dbmcp_sql::SqlError;
8use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
9use rmcp::model::{ErrorData, ToolAnnotations};
10
11use crate::PostgresHandler;
12use crate::connection::quote_ident;
13use crate::types::{PinnedDropTableRequest, UnpinnedDropTableRequest};
14
15const NAME: &str = "dropTable";
16const TITLE: &str = "Drop Table";
17const DESCRIPTION_PINNED: &str = include_str!("../../assets/tools/drop_table/pinned.md");
18const DESCRIPTION_UNPINNED: &str = include_str!("../../assets/tools/drop_table/unpinned.md");
19
20fn annotations() -> ToolAnnotations {
21    ToolAnnotations::new()
22        .read_only(false)
23        .destructive(true)
24        .idempotent(false)
25        .open_world(false)
26}
27
28/// Marker type for the `dropTable` MCP tool (pinned variant — no `database` field).
29pub(crate) struct PinnedDropTableTool;
30
31impl ToolBase for PinnedDropTableTool {
32    type Parameter = PinnedDropTableRequest;
33    type Output = MessageResponse;
34    type Error = ErrorData;
35
36    fn name() -> Cow<'static, str> {
37        NAME.into()
38    }
39
40    fn title() -> Option<String> {
41        Some(TITLE.into())
42    }
43
44    fn description() -> Option<Cow<'static, str>> {
45        Some(DESCRIPTION_PINNED.into())
46    }
47
48    fn annotations() -> Option<ToolAnnotations> {
49        Some(annotations())
50    }
51}
52
53impl AsyncTool<PostgresHandler> for PinnedDropTableTool {
54    async fn invoke(handler: &PostgresHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
55        handler.drop_table(None, params.table, params.cascade).await
56    }
57}
58
59/// Marker type for the `dropTable` MCP tool (unpinned variant — carries `database`).
60pub(crate) struct UnpinnedDropTableTool;
61
62impl ToolBase for UnpinnedDropTableTool {
63    type Parameter = UnpinnedDropTableRequest;
64    type Output = MessageResponse;
65    type Error = ErrorData;
66
67    fn name() -> Cow<'static, str> {
68        NAME.into()
69    }
70
71    fn title() -> Option<String> {
72        Some(TITLE.into())
73    }
74
75    fn description() -> Option<Cow<'static, str>> {
76        Some(DESCRIPTION_UNPINNED.into())
77    }
78
79    fn annotations() -> Option<ToolAnnotations> {
80        Some(annotations())
81    }
82}
83
84impl AsyncTool<PostgresHandler> for UnpinnedDropTableTool {
85    async fn invoke(handler: &PostgresHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
86        handler
87            .drop_table(params.database, params.inner.table, params.inner.cascade)
88            .await
89    }
90}
91
92impl PostgresHandler {
93    /// Drops a table from a database.
94    ///
95    /// Validates identifiers, then executes `DROP TABLE`. When `cascade`
96    /// is true the statement uses `CASCADE` to also remove dependent
97    /// foreign-key constraints.
98    ///
99    /// # Errors
100    ///
101    /// Returns [`SqlError::ReadOnlyViolation`] in read-only mode,
102    /// [`SqlError::InvalidIdentifier`] for invalid names,
103    /// or [`SqlError::Query`] if the backend reports an error.
104    pub async fn drop_table(
105        &self,
106        database: Option<String>,
107        table: String,
108        cascade: bool,
109    ) -> Result<MessageResponse, ErrorData> {
110        if self.config.read_only {
111            return Err(SqlError::ReadOnlyViolation.into());
112        }
113
114        let database = database.as_deref().map(str::trim).filter(|s| !s.is_empty());
115
116        let mut drop_sql = format!("DROP TABLE {}", quote_ident(&table));
117        if cascade {
118            drop_sql.push_str(" CASCADE");
119        }
120
121        self.connection.execute(drop_sql.as_str(), database).await?;
122
123        Ok(MessageResponse {
124            message: format!("Table '{table}' dropped successfully."),
125        })
126    }
127}