dbmcp_postgres/tools/
list_triggers.rs1use std::borrow::Cow;
4
5use dbmcp_server::pagination::Pager;
6use dbmcp_server::types::{ListTriggersRequest, ListTriggersResponse};
7use dbmcp_sql::Connection as _;
8use dbmcp_sql::sanitize::validate_ident;
9use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
10use rmcp::model::{ErrorData, ToolAnnotations};
11
12use crate::PostgresHandler;
13
14pub(crate) struct ListTriggersTool;
16
17impl ListTriggersTool {
18 const NAME: &'static str = "listTriggers";
19 const TITLE: &'static str = "List Triggers";
20 const DESCRIPTION: &'static str = r#"List all user-defined triggers on tables in the `public` schema of a database. Internal constraint and foreign-key triggers are excluded.
21
22<usecase>
23Use when:
24- Investigating side-effects on INSERT/UPDATE/DELETE for a table
25- Auditing trigger coverage across a database
26- The user asks what triggers fire in a database
27</usecase>
28
29<examples>
30✓ "What triggers are in the mydb database?" → listTriggers(database="mydb")
31✓ "Does an order-audit trigger exist?" → listTriggers to check
32✗ "Show me a trigger's body" → use readQuery against pg_trigger or information_schema.triggers
33</examples>
34
35<what_it_returns>
36A sorted JSON array of trigger name strings.
37</what_it_returns>
38
39<pagination>
40Paginated. Pass the prior response's `nextCursor` as `cursor` to fetch the next page.
41</pagination>"#;
42}
43
44impl ToolBase for ListTriggersTool {
45 type Parameter = ListTriggersRequest;
46 type Output = ListTriggersResponse;
47 type Error = ErrorData;
48
49 fn name() -> Cow<'static, str> {
50 Self::NAME.into()
51 }
52
53 fn title() -> Option<String> {
54 Some(Self::TITLE.into())
55 }
56
57 fn description() -> Option<Cow<'static, str>> {
58 Some(Self::DESCRIPTION.into())
59 }
60
61 fn annotations() -> Option<ToolAnnotations> {
62 Some(
63 ToolAnnotations::new()
64 .read_only(true)
65 .destructive(false)
66 .idempotent(true)
67 .open_world(false),
68 )
69 }
70}
71
72impl AsyncTool<PostgresHandler> for ListTriggersTool {
73 async fn invoke(handler: &PostgresHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
74 handler.list_triggers(params).await
75 }
76}
77
78impl PostgresHandler {
79 pub async fn list_triggers(
87 &self,
88 ListTriggersRequest { database, cursor }: ListTriggersRequest,
89 ) -> Result<ListTriggersResponse, ErrorData> {
90 let database = database
91 .as_deref()
92 .map(str::trim)
93 .filter(|s| !s.is_empty())
94 .map(validate_ident)
95 .transpose()?;
96
97 let pager = Pager::new(cursor, self.config.page_size);
98 let query = format!(
99 r"
100 SELECT t.tgname
101 FROM pg_trigger t
102 JOIN pg_class c ON t.tgrelid = c.oid
103 JOIN pg_namespace n ON c.relnamespace = n.oid
104 WHERE n.nspname = 'public' AND NOT t.tgisinternal
105 ORDER BY t.tgname
106 LIMIT {} OFFSET {}",
107 pager.limit(),
108 pager.offset(),
109 );
110
111 let rows: Vec<String> = self.connection.fetch_scalar(query.as_str(), database).await?;
112 let (triggers, next_cursor) = pager.finalize(rows);
113
114 Ok(ListTriggersResponse { triggers, next_cursor })
115 }
116}