Skip to main content

dbmcp_sqlite/tools/
list_triggers.rs

1//! MCP tool: `listTriggers`.
2
3use std::borrow::Cow;
4use std::sync::Arc;
5
6use dbmcp_server::pagination::Pager;
7use dbmcp_server::types::ListTriggersResponse;
8
9use dbmcp_sql::Connection as _;
10use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
11use rmcp::model::{ErrorData, JsonObject, ToolAnnotations};
12
13use crate::SqliteHandler;
14use crate::types::ListTriggersRequest;
15
16/// Marker type for the `listTriggers` MCP tool.
17pub(crate) struct ListTriggersTool;
18
19impl ListTriggersTool {
20    const NAME: &'static str = "listTriggers";
21    const TITLE: &'static str = "List Triggers";
22    const DESCRIPTION: &'static str = r#"List all triggers in the connected SQLite database.
23
24<usecase>
25Use when:
26- Investigating side-effects on INSERT/UPDATE/DELETE for a table
27- Auditing trigger coverage across a database
28- The user asks what triggers fire in the database
29</usecase>
30
31<examples>
32✓ "What triggers are in this database?"
33✓ "Does a user-audit trigger exist?" → listTriggers to check
34✗ "Show me a trigger's body" → use readQuery against sqlite_schema
35</examples>
36
37<what_it_returns>
38A sorted JSON array of trigger name strings.
39</what_it_returns>
40
41<pagination>
42Paginated. Pass the prior response's `nextCursor` as `cursor` to fetch the next page.
43</pagination>"#;
44}
45
46impl ToolBase for ListTriggersTool {
47    type Parameter = ListTriggersRequest;
48    type Output = ListTriggersResponse;
49    type Error = ErrorData;
50
51    fn name() -> Cow<'static, str> {
52        Self::NAME.into()
53    }
54
55    fn title() -> Option<String> {
56        Some(Self::TITLE.into())
57    }
58
59    fn description() -> Option<Cow<'static, str>> {
60        Some(Self::DESCRIPTION.into())
61    }
62
63    fn input_schema() -> Option<Arc<JsonObject>> {
64        None
65    }
66
67    fn annotations() -> Option<ToolAnnotations> {
68        Some(
69            ToolAnnotations::new()
70                .read_only(true)
71                .destructive(false)
72                .idempotent(true)
73                .open_world(false),
74        )
75    }
76}
77
78impl AsyncTool<SqliteHandler> for ListTriggersTool {
79    async fn invoke(handler: &SqliteHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
80        handler.list_triggers(params).await
81    }
82}
83
84impl SqliteHandler {
85    /// Lists one page of triggers in the connected database.
86    ///
87    /// # Errors
88    ///
89    /// Returns [`ErrorData`] with code `-32602` if `cursor` is malformed,
90    /// or an internal-error [`ErrorData`] if the underlying query fails.
91    pub async fn list_triggers(
92        &self,
93        ListTriggersRequest { cursor }: ListTriggersRequest,
94    ) -> Result<ListTriggersResponse, ErrorData> {
95        let pager = Pager::new(cursor, self.config.page_size);
96        let query = format!(
97            r"
98            SELECT name
99            FROM sqlite_schema
100            WHERE type = 'trigger' AND name NOT LIKE 'sqlite_%'
101            ORDER BY name
102            LIMIT {} OFFSET {}",
103            pager.limit(),
104            pager.offset(),
105        );
106
107        let rows: Vec<String> = self.connection.fetch_scalar(query.as_str(), None).await?;
108        let (triggers, next_cursor) = pager.finalize(rows);
109
110        Ok(ListTriggersResponse { triggers, next_cursor })
111    }
112}