database_mcp_postgres/tools/
drop_database.rs1use std::borrow::Cow;
4
5use database_mcp_server::AppError;
6use database_mcp_server::types::{DropDatabaseRequest, 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;
13
14pub(crate) struct DropDatabaseTool;
16
17impl DropDatabaseTool {
18 const NAME: &'static str = "drop_database";
19 const DESCRIPTION: &'static str = r#"Drop an existing database from the connected server.
20
21<usecase>
22Use when:
23- Removing a database that is no longer needed
24- Cleaning up test or temporary databases
25</usecase>
26
27<examples>
28✓ "Drop the test_db database" → drop_database(database_name="test_db")
29✗ "Drop a table" → use drop_table instead
30</examples>
31
32<safety>
33IMPORTANT: This permanently deletes the database and ALL its data. This action cannot be undone.
34Cannot drop the database you are currently connected to.
35</safety>
36
37<what_it_returns>
38A confirmation message with the dropped database name.
39</what_it_returns>"#;
40}
41
42impl ToolBase for DropDatabaseTool {
43 type Parameter = DropDatabaseRequest;
44 type Output = MessageResponse;
45 type Error = ErrorData;
46
47 fn name() -> Cow<'static, str> {
48 Self::NAME.into()
49 }
50
51 fn description() -> Option<Cow<'static, str>> {
52 Some(Self::DESCRIPTION.into())
53 }
54
55 fn annotations() -> Option<ToolAnnotations> {
56 Some(
57 ToolAnnotations::new()
58 .read_only(false)
59 .destructive(true)
60 .idempotent(false)
61 .open_world(false),
62 )
63 }
64}
65
66impl AsyncTool<PostgresHandler> for DropDatabaseTool {
67 async fn invoke(handler: &PostgresHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
68 Ok(handler.drop_database(¶ms).await?)
69 }
70}
71
72impl PostgresHandler {
73 pub async fn drop_database(&self, request: &DropDatabaseRequest) -> Result<MessageResponse, AppError> {
85 if self.config.read_only {
86 return Err(AppError::ReadOnlyViolation);
87 }
88 let name = &request.database_name;
89 validate_identifier(name)?;
90
91 if self.connection.default_database_name() == name.as_str() {
93 return Err(AppError::Query(format!(
94 "Cannot drop the currently connected database '{name}'."
95 )));
96 }
97
98 let drop_sql = format!("DROP DATABASE {}", self.connection.quote_identifier(name));
99 self.connection.execute(drop_sql.as_str(), None).await?;
100
101 self.connection.invalidate(name).await;
104
105 Ok(MessageResponse {
106 message: format!("Database '{name}' dropped successfully."),
107 })
108 }
109}