database_mcp_sqlite/tools/
list_tables.rs1use std::borrow::Cow;
4use std::sync::Arc;
5
6use database_mcp_server::AppError;
7use database_mcp_server::types::ListTablesResponse;
8use database_mcp_sql::Connection as _;
9use rmcp::handler::server::common::schema_for_empty_input;
10use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
11use rmcp::model::{ErrorData, JsonObject, ToolAnnotations};
12use serde_json::Value;
13
14use crate::SqliteHandler;
15
16pub(crate) struct ListTablesTool;
18
19impl ListTablesTool {
20 const NAME: &'static str = "list_tables";
21 const DESCRIPTION: &'static str = r#"List all tables in the connected SQLite database. Use this tool to discover what tables are available before using other tools.
22
23<usecase>
24ALWAYS call this tool FIRST when:
25- You need to explore what tables exist in the database
26- You need a table name for get_table_schema or query tools
27- The user asks what data is available
28</usecase>
29
30<examples>
31✓ "What tables are in this database?"
32✓ "Does a users table exist?" → list_tables to check
33✗ "Show me the columns of users" → use get_table_schema instead
34</examples>
35
36<what_it_returns>
37A sorted JSON array of table name strings.
38</what_it_returns>"#;
39}
40
41impl ToolBase for ListTablesTool {
42 type Parameter = ();
43 type Output = ListTablesResponse;
44 type Error = ErrorData;
45
46 fn name() -> Cow<'static, str> {
47 Self::NAME.into()
48 }
49
50 fn description() -> Option<Cow<'static, str>> {
51 Some(Self::DESCRIPTION.into())
52 }
53
54 fn input_schema() -> Option<Arc<JsonObject>> {
55 Some(schema_for_empty_input())
56 }
57
58 fn annotations() -> Option<ToolAnnotations> {
59 Some(
60 ToolAnnotations::new()
61 .read_only(true)
62 .destructive(false)
63 .idempotent(true)
64 .open_world(false),
65 )
66 }
67}
68
69impl AsyncTool<SqliteHandler> for ListTablesTool {
70 async fn invoke(handler: &SqliteHandler, _params: Self::Parameter) -> Result<Self::Output, Self::Error> {
71 Ok(handler.list_tables().await?)
72 }
73}
74
75impl SqliteHandler {
76 pub async fn list_tables(&self) -> Result<ListTablesResponse, AppError> {
82 let sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name";
83 let rows = self.connection.fetch(sql, None).await?;
84 let tables = rows
85 .iter()
86 .filter_map(|row| row.get("name").and_then(Value::as_str).map(str::to_owned))
87 .collect::<Vec<_>>();
88 Ok(ListTablesResponse { tables })
89 }
90}