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