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 DESCRIPTION: &'static str = r#"Drop a table from the 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" → drop_table(table_name="temp_logs")
30✗ "Delete rows from a table" → use write_query with DELETE
31</examples>
32
33<safety>
34IMPORTANT: This permanently deletes the table and ALL its data. This action cannot be undone.
35</safety>
36
37<what_it_returns>
38A confirmation message with the dropped table name.
39</what_it_returns>"#;
40}
41
42impl ToolBase for DropTableTool {
43    type Parameter = DropTableRequest;
44    type Output = MessageResponse;
45    type Error = ErrorData;
46
47    fn name() -> Cow<'static, str> {
48        Self::NAME.into()
49    }
50
51    fn description() -> Option<Cow<'static, str>> {
52        Some(Self::DESCRIPTION.into())
53    }
54
55    fn annotations() -> Option<ToolAnnotations> {
56        Some(
57            ToolAnnotations::new()
58                .read_only(false)
59                .destructive(true)
60                .idempotent(false)
61                .open_world(false),
62        )
63    }
64}
65
66impl AsyncTool<SqliteHandler> for DropTableTool {
67    async fn invoke(handler: &SqliteHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
68        Ok(handler.drop_table(&params).await?)
69    }
70}
71
72impl SqliteHandler {
73    /// Drops a table from the database.
74    ///
75    /// # Errors
76    ///
77    /// Returns [`AppError::ReadOnlyViolation`] in read-only mode,
78    /// [`AppError::InvalidIdentifier`] for invalid names,
79    /// or [`AppError::Query`] if the backend reports an error.
80    pub async fn drop_table(&self, request: &DropTableRequest) -> Result<MessageResponse, AppError> {
81        if self.config.read_only {
82            return Err(AppError::ReadOnlyViolation);
83        }
84
85        let table = &request.table_name;
86        validate_identifier(table)?;
87
88        let drop_sql = format!("DROP TABLE {}", self.connection.quote_identifier(table));
89        self.connection.execute(drop_sql.as_str(), None).await?;
90
91        Ok(MessageResponse {
92            message: format!("Table '{table}' dropped successfully."),
93        })
94    }
95}