Skip to main content

dbmcp_sqlite/tools/
drop_table.rs

1//! MCP tool: `dropTable`.
2
3use dbmcp_server::types::MessageResponse;
4use dbmcp_sql::SqlError;
5
6use super::prelude::*;
7use crate::connection::quote_ident;
8use crate::types::DropTableRequest;
9
10const NAME: &str = "dropTable";
11const TITLE: &str = "Drop Table";
12const DESCRIPTION: &str = include_str!("../../assets/tools/drop_table.md");
13
14/// Marker type for the `dropTable` MCP tool.
15pub(crate) struct DropTableTool;
16
17impl ToolBase for DropTableTool {
18    type Parameter = DropTableRequest;
19    type Output = MessageResponse;
20    type Error = ErrorData;
21
22    fn name() -> Cow<'static, str> {
23        NAME.into()
24    }
25
26    fn title() -> Option<String> {
27        Some(TITLE.into())
28    }
29
30    fn description() -> Option<Cow<'static, str>> {
31        Some(DESCRIPTION.into())
32    }
33
34    fn annotations() -> Option<ToolAnnotations> {
35        Some(
36            ToolAnnotations::new()
37                .read_only(false)
38                .destructive(true)
39                .idempotent(false)
40                .open_world(false),
41        )
42    }
43
44    fn input_schema() -> Option<Arc<JsonObject>> {
45        Some(input_schema::<Self::Parameter>(true))
46    }
47
48    fn output_schema() -> Option<Arc<JsonObject>> {
49        Some(output_schema::<Self::Output>())
50    }
51}
52
53impl AsyncTool<SqliteHandler> for DropTableTool {
54    async fn invoke(handler: &SqliteHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
55        handler.drop_table(params).await
56    }
57}
58
59impl SqliteHandler {
60    /// Drops a table from the database.
61    ///
62    /// # Errors
63    ///
64    /// Returns [`SqlError::ReadOnlyViolation`] in read-only mode,
65    /// [`SqlError::InvalidIdentifier`] for invalid names,
66    /// or [`SqlError::Query`] if the backend reports an error.
67    pub async fn drop_table(&self, DropTableRequest { table }: DropTableRequest) -> Result<MessageResponse, ErrorData> {
68        if self.config.read_only {
69            return Err(SqlError::ReadOnlyViolation.into());
70        }
71
72        let drop_sql = format!("DROP TABLE {}", quote_ident(&table));
73        self.connection.execute(drop_sql.as_str(), None).await?;
74
75        Ok(MessageResponse {
76            message: format!("Table '{table}' dropped successfully."),
77        })
78    }
79}