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::identifier::validate_identifier;
8use database_mcp_sql::timeout::execute_with_timeout;
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 = "Drop a table from the database.";
21}
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        Self::NAME.into()
30    }
31
32    fn description() -> Option<Cow<'static, str>> {
33        Some(Self::DESCRIPTION.into())
34    }
35
36    fn annotations() -> Option<ToolAnnotations> {
37        Some(
38            ToolAnnotations::new()
39                .read_only(false)
40                .destructive(true)
41                .idempotent(false)
42                .open_world(false),
43        )
44    }
45}
46
47impl AsyncTool<SqliteHandler> for DropTableTool {
48    async fn invoke(handler: &SqliteHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
49        Ok(handler.drop_table(&params).await?)
50    }
51}
52
53impl SqliteHandler {
54    /// Drops a table from the database.
55    ///
56    /// # Errors
57    ///
58    /// Returns [`AppError::ReadOnlyViolation`] in read-only mode,
59    /// [`AppError::InvalidIdentifier`] for invalid names,
60    /// or [`AppError::Query`] if the backend reports an error.
61    pub async fn drop_table(&self, request: &DropTableRequest) -> Result<MessageResponse, AppError> {
62        if self.config.read_only {
63            return Err(AppError::ReadOnlyViolation);
64        }
65
66        let table = &request.table_name;
67        validate_identifier(table)?;
68
69        let pool = self.pool.clone();
70        let drop_sql = format!("DROP TABLE {}", Self::quote_identifier(table));
71        execute_with_timeout(
72            self.config.query_timeout,
73            &drop_sql,
74            sqlx::query(&drop_sql).execute(&pool),
75        )
76        .await?;
77
78        Ok(MessageResponse {
79            message: format!("Table '{table}' dropped successfully."),
80        })
81    }
82}