Skip to main content

dbmcp_postgres/tools/
drop_database.rs

1//! MCP tool: `dropDatabase`.
2
3use dbmcp_server::types::{DropDatabaseRequest, MessageResponse};
4use dbmcp_sql::SqlError;
5
6use super::prelude::*;
7use crate::connection::quote_ident;
8
9const NAME: &str = "dropDatabase";
10const TITLE: &str = "Drop Database";
11const DESCRIPTION: &str = include_str!("../../assets/tools/drop_database/default.md");
12
13/// Marker type for the `dropDatabase` MCP tool.
14pub(crate) struct DropDatabaseTool;
15
16impl ToolBase for DropDatabaseTool {
17    type Parameter = DropDatabaseRequest;
18    type Output = MessageResponse;
19    type Error = ErrorData;
20
21    fn name() -> Cow<'static, str> {
22        NAME.into()
23    }
24
25    fn title() -> Option<String> {
26        Some(TITLE.into())
27    }
28
29    fn description() -> Option<Cow<'static, str>> {
30        Some(DESCRIPTION.into())
31    }
32
33    fn annotations() -> Option<ToolAnnotations> {
34        Some(
35            ToolAnnotations::new()
36                .read_only(false)
37                .destructive(true)
38                .idempotent(false)
39                .open_world(false),
40        )
41    }
42
43    fn input_schema() -> Option<Arc<JsonObject>> {
44        Some(input_schema::<Self::Parameter>(false))
45    }
46
47    fn output_schema() -> Option<Arc<JsonObject>> {
48        Some(output_schema::<Self::Output>())
49    }
50}
51
52impl AsyncTool<PostgresHandler> for DropDatabaseTool {
53    async fn invoke(handler: &PostgresHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
54        handler.drop_database(params).await
55    }
56}
57
58impl PostgresHandler {
59    /// Drops an existing database.
60    ///
61    /// Refuses to drop the currently connected (default) database and
62    /// evicts the corresponding pool cache entry after a successful drop.
63    ///
64    /// # Errors
65    ///
66    /// Returns [`SqlError::ReadOnlyViolation`] in read-only mode,
67    /// [`SqlError::InvalidIdentifier`] for invalid names,
68    /// or [`SqlError::Query`] if the target is the active database
69    /// or the backend reports an error.
70    pub async fn drop_database(
71        &self,
72        DropDatabaseRequest { database }: DropDatabaseRequest,
73    ) -> Result<MessageResponse, ErrorData> {
74        if self.config.read_only {
75            return Err(SqlError::ReadOnlyViolation.into());
76        }
77
78        // Guard: prevent dropping the currently connected database.
79        if self.connection.default_database_name() == database.as_str() {
80            return Err(SqlError::Query(format!("Cannot drop the currently connected database '{database}'.")).into());
81        }
82
83        let drop_sql = format!("DROP DATABASE {}", quote_ident(&database));
84        self.connection.execute(drop_sql.as_str(), None).await?;
85
86        self.connection.invalidate(&database).await;
87
88        Ok(MessageResponse {
89            message: format!("Database '{database}' dropped successfully."),
90        })
91    }
92}