Skip to main content

database_mcp_sqlite/tools/
drop_table.rs

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