Skip to main content

database_mcp_mysql/tools/
drop_table.rs

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