database_mcp_mysql/tools/
list_tables.rs1use 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::{quote_literal, validate_ident};
9use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
10use rmcp::model::{ErrorData, ToolAnnotations};
11
12use crate::MysqlHandler;
13
14pub(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<MysqlHandler> for ListTablesTool {
73 async fn invoke(handler: &MysqlHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
74 handler.list_tables(params).await
75 }
76}
77
78impl MysqlHandler {
79 pub async fn list_tables(
87 &self,
88 ListTablesRequest { database, cursor }: ListTablesRequest,
89 ) -> Result<ListTablesResponse, ErrorData> {
90 validate_ident(&database)?;
91
92 let pager = Pager::new(cursor, self.config.page_size);
93 let query = format!(
94 r"
95 SELECT CAST(TABLE_NAME AS CHAR)
96 FROM information_schema.TABLES
97 WHERE TABLE_SCHEMA = {}
98 ORDER BY TABLE_NAME
99 LIMIT {} OFFSET {}",
100 quote_literal(&database),
101 pager.limit(),
102 pager.offset(),
103 );
104
105 let rows: Vec<String> = self.connection.fetch_scalar(query.as_str(), None).await?;
106 let (tables, next_cursor) = pager.finalize(rows);
107
108 Ok(ListTablesResponse { tables, next_cursor })
109 }
110}