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