database_mcp_mysql/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::MysqlHandler;
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<MysqlHandler> for DropDatabaseTool {
67 async fn invoke(handler: &MysqlHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
68 Ok(handler.drop_database(¶ms).await?)
69 }
70}
71
72impl MysqlHandler {
73 pub async fn drop_database(&self, request: &DropDatabaseRequest) -> Result<MessageResponse, AppError> {
84 if self.config.read_only {
85 return Err(AppError::ReadOnlyViolation);
86 }
87 let name = &request.database_name;
88 validate_identifier(name)?;
89
90 if self.connection.default_database_name().eq_ignore_ascii_case(name) {
92 return Err(AppError::Query(format!(
93 "Cannot drop the currently connected database '{name}'."
94 )));
95 }
96
97 let drop_sql = format!("DROP DATABASE {}", self.connection.quote_identifier(name));
98 self.connection.execute(drop_sql.as_str(), None).await?;
99
100 self.connection.invalidate(name).await;
103
104 Ok(MessageResponse {
105 message: format!("Database '{name}' dropped successfully."),
106 })
107 }
108}