Skip to main content

database_mcp_sqlite/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::SqliteHandler;
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 TITLE: &'static str = "Drop Table";
21    const DESCRIPTION: &'static str = r#"Drop a table from the database.
22
23<usecase>
24Use when:
25- Removing a table that is no longer needed
26- Cleaning up test or temporary tables
27</usecase>
28
29<examples>
30✓ "Drop the temp_logs table" → drop_table(table_name="temp_logs")
31✗ "Delete rows from a table" → use write_query with DELETE
32</examples>
33
34<safety>
35IMPORTANT: This permanently deletes the table and ALL its data. This action cannot be undone.
36</safety>
37
38<what_it_returns>
39A confirmation message with the dropped table name.
40</what_it_returns>"#;
41}
42
43impl ToolBase for DropTableTool {
44    type Parameter = DropTableRequest;
45    type Output = MessageResponse;
46    type Error = ErrorData;
47
48    fn name() -> Cow<'static, str> {
49        Self::NAME.into()
50    }
51
52    fn title() -> Option<String> {
53        Some(Self::TITLE.into())
54    }
55
56    fn description() -> Option<Cow<'static, str>> {
57        Some(Self::DESCRIPTION.into())
58    }
59
60    fn annotations() -> Option<ToolAnnotations> {
61        Some(
62            ToolAnnotations::new()
63                .read_only(false)
64                .destructive(true)
65                .idempotent(false)
66                .open_world(false),
67        )
68    }
69}
70
71impl AsyncTool<SqliteHandler> for DropTableTool {
72    async fn invoke(handler: &SqliteHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
73        Ok(handler.drop_table(&params).await?)
74    }
75}
76
77impl SqliteHandler {
78    /// Drops a table from the database.
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
90        let table = &request.table_name;
91        validate_identifier(table)?;
92
93        let drop_sql = format!("DROP TABLE {}", self.connection.quote_identifier(table));
94        self.connection.execute(drop_sql.as_str(), None).await?;
95
96        Ok(MessageResponse {
97            message: format!("Table '{table}' dropped successfully."),
98        })
99    }
100}