Skip to main content

database_mcp_postgres/tools/
list_tables.rs

1//! MCP tool: `listTables`.
2
3use std::borrow::Cow;
4
5use database_mcp_server::pagination::Pager;
6use database_mcp_server::types::{ListTablesRequest, ListTablesResponse};
7use database_mcp_sql::Connection as _;
8use database_mcp_sql::sanitize::validate_ident;
9use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
10use rmcp::model::{ErrorData, ToolAnnotations};
11
12use crate::PostgresHandler;
13
14/// Marker type for the `listTables` MCP tool.
15pub(crate) struct ListTablesTool;
16
17impl ListTablesTool {
18    const NAME: &'static str = "listTables";
19    const TITLE: &'static str = "List Tables";
20    const DESCRIPTION: &'static str = r#"List all tables in a specific database. Requires `database` — call `listDatabases` first to discover available databases.
21
22<usecase>
23Use when:
24- Exploring a database to find relevant tables
25- Verifying a table exists before querying or inspecting it
26- The user asks what tables are in a database
27</usecase>
28
29<examples>
30✓ "What tables are in the mydb database?" → listTables(database="mydb")
31✓ "Does a users table exist?" → listTables to check
32✗ "Show me the columns of users" → use getTableSchema instead
33</examples>
34
35<what_it_returns>
36A sorted JSON array of table 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 ListTablesTool {
45    type Parameter = ListTablesRequest;
46    type Output = ListTablesResponse;
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 ListTablesTool {
73    async fn invoke(handler: &PostgresHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
74        handler.list_tables(params).await
75    }
76}
77
78impl PostgresHandler {
79    /// Lists one page of tables in a database.
80    ///
81    /// # Errors
82    ///
83    /// Returns [`ErrorData`] with code `-32602` if `cursor` is malformed,
84    /// or an internal-error [`ErrorData`] if `database` is invalid
85    /// or the underlying query fails.
86    pub async fn list_tables(
87        &self,
88        ListTablesRequest { database, cursor }: ListTablesRequest,
89    ) -> Result<ListTablesResponse, ErrorData> {
90        let db = Some(database.trim()).filter(|s| !s.is_empty());
91        if let Some(name) = db {
92            validate_ident(name)?;
93        }
94
95        let pager = Pager::new(cursor, self.config.page_size);
96        let query = format!(
97            r"
98            SELECT tablename
99            FROM pg_tables
100            WHERE schemaname = 'public'
101            ORDER BY tablename
102            LIMIT {} OFFSET {}",
103            pager.limit(),
104            pager.offset(),
105        );
106
107        let rows: Vec<String> = self.connection.fetch_scalar(query.as_str(), db).await?;
108        let (tables, next_cursor) = pager.finalize(rows);
109
110        Ok(ListTablesResponse { tables, next_cursor })
111    }
112}