use std::borrow::Cow;
use database_mcp_server::AppError;
use database_mcp_server::types::{DropDatabaseRequest, MessageResponse};
use database_mcp_sql::Connection as _;
use database_mcp_sql::identifier::validate_identifier;
use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
use rmcp::model::{ErrorData, ToolAnnotations};
use crate::PostgresHandler;
pub(crate) struct DropDatabaseTool;
impl DropDatabaseTool {
const NAME: &'static str = "drop_database";
const DESCRIPTION: &'static str = r#"Drop an existing database from the connected server.
<usecase>
Use when:
- Removing a database that is no longer needed
- Cleaning up test or temporary databases
</usecase>
<examples>
✓ "Drop the test_db database" → drop_database(database_name="test_db")
✗ "Drop a table" → use drop_table instead
</examples>
<safety>
IMPORTANT: This permanently deletes the database and ALL its data. This action cannot be undone.
Cannot drop the database you are currently connected to.
</safety>
<what_it_returns>
A confirmation message with the dropped database name.
</what_it_returns>"#;
}
impl ToolBase for DropDatabaseTool {
type Parameter = DropDatabaseRequest;
type Output = MessageResponse;
type Error = ErrorData;
fn name() -> Cow<'static, str> {
Self::NAME.into()
}
fn description() -> Option<Cow<'static, str>> {
Some(Self::DESCRIPTION.into())
}
fn annotations() -> Option<ToolAnnotations> {
Some(
ToolAnnotations::new()
.read_only(false)
.destructive(true)
.idempotent(false)
.open_world(false),
)
}
}
impl AsyncTool<PostgresHandler> for DropDatabaseTool {
async fn invoke(handler: &PostgresHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
Ok(handler.drop_database(¶ms).await?)
}
}
impl PostgresHandler {
pub async fn drop_database(&self, request: &DropDatabaseRequest) -> Result<MessageResponse, AppError> {
if self.config.read_only {
return Err(AppError::ReadOnlyViolation);
}
let name = &request.database_name;
validate_identifier(name)?;
if self.connection.default_database_name() == name.as_str() {
return Err(AppError::Query(format!(
"Cannot drop the currently connected database '{name}'."
)));
}
let drop_sql = format!("DROP DATABASE {}", self.connection.quote_identifier(name));
self.connection.execute(drop_sql.as_str(), None).await?;
self.connection.invalidate(name).await;
Ok(MessageResponse {
message: format!("Database '{name}' dropped successfully."),
})
}
}