Skip to main content

database_mcp_mysql/tools/
drop_table.rs

1//! MCP tool: `dropTable`.
2
3use std::borrow::Cow;
4
5use database_mcp_server::types::MessageResponse;
6use database_mcp_sql::Connection as _;
7use database_mcp_sql::SqlError;
8use database_mcp_sql::sanitize::{quote_ident, validate_ident};
9use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
10use rmcp::model::{ErrorData, ToolAnnotations};
11use sqlparser::dialect::MySqlDialect;
12
13use crate::MysqlHandler;
14use crate::types::DropTableRequest;
15
16/// Marker type for the `dropTable` MCP tool.
17pub(crate) struct DropTableTool;
18
19impl DropTableTool {
20    const NAME: &'static str = "dropTable";
21    const TITLE: &'static str = "Drop Table";
22    const DESCRIPTION: &'static str = r#"Drop a table from a database.
23
24<usecase>
25Use when:
26- Removing a table that is no longer needed
27- Cleaning up test or temporary tables
28</usecase>
29
30<examples>
31✓ "Drop the temp_logs table from mydb" → dropTable(database="mydb", table="temp_logs")
32✗ "Delete rows from a table" → use writeQuery with DELETE
33✗ "Drop a database" → use dropDatabase instead
34</examples>
35
36<safety>
37IMPORTANT: This permanently deletes the table and ALL its data. This action cannot be undone.
38If the table has foreign key dependencies, the drop will fail — resolve dependencies first.
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 title() -> Option<String> {
56        Some(Self::TITLE.into())
57    }
58
59    fn description() -> Option<Cow<'static, str>> {
60        Some(Self::DESCRIPTION.into())
61    }
62
63    fn annotations() -> Option<ToolAnnotations> {
64        Some(
65            ToolAnnotations::new()
66                .read_only(false)
67                .destructive(true)
68                .idempotent(false)
69                .open_world(false),
70        )
71    }
72}
73
74impl AsyncTool<MysqlHandler> for DropTableTool {
75    async fn invoke(handler: &MysqlHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
76        Ok(handler.drop_table(params).await?)
77    }
78}
79
80impl MysqlHandler {
81    /// Drops a table from a database.
82    ///
83    /// Switches to the target database with `USE`, then executes
84    /// `DROP TABLE`.
85    ///
86    /// # Errors
87    ///
88    /// Returns [`SqlError::ReadOnlyViolation`] in read-only mode,
89    /// [`SqlError::InvalidIdentifier`] for invalid names,
90    /// or [`SqlError::Query`] if the backend reports an error.
91    pub async fn drop_table(
92        &self,
93        DropTableRequest { database, table }: DropTableRequest,
94    ) -> Result<MessageResponse, SqlError> {
95        if self.config.read_only {
96            return Err(SqlError::ReadOnlyViolation);
97        }
98
99        validate_ident(&database)?;
100        validate_ident(&table)?;
101
102        let drop_sql = format!("DROP TABLE {}", quote_ident(&table, &MySqlDialect {}));
103        self.connection.execute(drop_sql.as_str(), Some(&database)).await?;
104
105        Ok(MessageResponse {
106            message: format!("Table '{table}' dropped successfully."),
107        })
108    }
109}