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 = include_str!("../../assets/tools/drop_table.md");
23}
24
25impl ToolBase for DropTableTool {
26    type Parameter = DropTableRequest;
27    type Output = MessageResponse;
28    type Error = ErrorData;
29
30    fn name() -> Cow<'static, str> {
31        Self::NAME.into()
32    }
33
34    fn title() -> Option<String> {
35        Some(Self::TITLE.into())
36    }
37
38    fn description() -> Option<Cow<'static, str>> {
39        Some(Self::DESCRIPTION.into())
40    }
41
42    fn annotations() -> Option<ToolAnnotations> {
43        Some(
44            ToolAnnotations::new()
45                .read_only(false)
46                .destructive(true)
47                .idempotent(false)
48                .open_world(false),
49        )
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}