Skip to main content

dbmcp_sqlite/tools/
drop_table.rs

1//! MCP tool: `dropTable`.
2
3use std::borrow::Cow;
4
5use dbmcp_server::types::MessageResponse;
6use dbmcp_sql::SqlError;
7
8use dbmcp_sql::Connection as _;
9use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
10use rmcp::model::{ErrorData, ToolAnnotations};
11
12use crate::SqliteHandler;
13use crate::connection::quote_ident;
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 the 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" → dropTable(table="temp_logs")
32✗ "Delete rows from a table" → use writeQuery with DELETE
33</examples>
34
35<safety>
36IMPORTANT: This permanently deletes the table and ALL its data. This action cannot be undone.
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 title() -> Option<String> {
54        Some(Self::TITLE.into())
55    }
56
57    fn description() -> Option<Cow<'static, str>> {
58        Some(Self::DESCRIPTION.into())
59    }
60
61    fn annotations() -> Option<ToolAnnotations> {
62        Some(
63            ToolAnnotations::new()
64                .read_only(false)
65                .destructive(true)
66                .idempotent(false)
67                .open_world(false),
68        )
69    }
70}
71
72impl AsyncTool<SqliteHandler> for DropTableTool {
73    async fn invoke(handler: &SqliteHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
74        handler.drop_table(params).await
75    }
76}
77
78impl SqliteHandler {
79    /// Drops a table from the database.
80    ///
81    /// # Errors
82    ///
83    /// Returns [`SqlError::ReadOnlyViolation`] in read-only mode,
84    /// [`SqlError::InvalidIdentifier`] for invalid names,
85    /// or [`SqlError::Query`] if the backend reports an error.
86    pub async fn drop_table(&self, DropTableRequest { table }: DropTableRequest) -> Result<MessageResponse, ErrorData> {
87        if self.config.read_only {
88            return Err(SqlError::ReadOnlyViolation.into());
89        }
90
91        let drop_sql = format!("DROP TABLE {}", quote_ident(&table));
92        self.connection.execute(drop_sql.as_str(), None).await?;
93
94        Ok(MessageResponse {
95            message: format!("Table '{table}' dropped successfully."),
96        })
97    }
98}