database_mcp_postgres/tools/
drop_table.rs1use std::borrow::Cow;
4
5use database_mcp_server::types::MessageResponse;
6use database_mcp_sql::Connection as _;
7use database_mcp_sql::SqlError;
8use database_mcp_sql::sanitize::{quote_ident, validate_ident};
9use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
10use rmcp::model::{ErrorData, ToolAnnotations};
11use sqlparser::dialect::PostgreSqlDialect;
12
13use crate::PostgresHandler;
14use crate::types::DropTableRequest;
15
16pub(crate) struct DropTableTool;
18
19impl DropTableTool {
20 const NAME: &'static str = "dropTable";
21 const TITLE: &'static str = "Drop Table";
22 const DESCRIPTION: &'static str = r#"Drop a table from a database. Checks for foreign key dependencies via the database engine.
23
24<usecase>
25Use when:
26- Removing a table that is no longer needed
27- Cleaning up test or temporary tables
28</usecase>
29
30<examples>
31✓ "Drop the temp_logs table" → dropTable(database="mydb", table="temp_logs")
32✓ "Force drop with dependencies" → dropTable(..., cascade=true)
33✗ "Delete rows from a table" → use writeQuery with DELETE
34✗ "Drop a database" → use dropDatabase instead
35</examples>
36
37<safety>
38IMPORTANT: This permanently deletes the table and ALL its data. This action cannot be undone.
39Set `cascade` to true to also drop dependent foreign key constraints.
40Without cascade, the drop will fail if other tables reference this one.
41</safety>
42
43<what_it_returns>
44A confirmation message with the dropped table name.
45</what_it_returns>"#;
46}
47
48impl ToolBase for DropTableTool {
49 type Parameter = DropTableRequest;
50 type Output = MessageResponse;
51 type Error = ErrorData;
52
53 fn name() -> Cow<'static, str> {
54 Self::NAME.into()
55 }
56
57 fn title() -> Option<String> {
58 Some(Self::TITLE.into())
59 }
60
61 fn description() -> Option<Cow<'static, str>> {
62 Some(Self::DESCRIPTION.into())
63 }
64
65 fn annotations() -> Option<ToolAnnotations> {
66 Some(
67 ToolAnnotations::new()
68 .read_only(false)
69 .destructive(true)
70 .idempotent(false)
71 .open_world(false),
72 )
73 }
74}
75
76impl AsyncTool<PostgresHandler> for DropTableTool {
77 async fn invoke(handler: &PostgresHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
78 Ok(handler.drop_table(params).await?)
79 }
80}
81
82impl PostgresHandler {
83 pub async fn drop_table(
95 &self,
96 DropTableRequest {
97 database,
98 table,
99 cascade,
100 }: DropTableRequest,
101 ) -> Result<MessageResponse, SqlError> {
102 if self.config.read_only {
103 return Err(SqlError::ReadOnlyViolation);
104 }
105
106 validate_ident(&database)?;
107 validate_ident(&table)?;
108
109 let mut drop_sql = format!("DROP TABLE {}", quote_ident(&table, &PostgreSqlDialect {}));
110 if cascade {
111 drop_sql.push_str(" CASCADE");
112 }
113
114 self.connection.execute(drop_sql.as_str(), Some(&database)).await?;
115
116 Ok(MessageResponse {
117 message: format!("Table '{table}' dropped successfully."),
118 })
119 }
120}