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::timeout::execute_with_timeout;
9use rmcp::handler::server::common::schema_for_empty_input;
10use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
11use rmcp::model::{ErrorData, JsonObject, ToolAnnotations};
12
13use crate::SqliteHandler;
14
15pub(crate) struct ListTablesTool;
17
18impl ListTablesTool {
19 const NAME: &'static str = "list_tables";
20 const DESCRIPTION: &'static str = "List all tables in the connected `SQLite` database.";
21}
22
23impl ToolBase for ListTablesTool {
24 type Parameter = ();
25 type Output = ListTablesResponse;
26 type Error = ErrorData;
27
28 fn name() -> Cow<'static, str> {
29 Self::NAME.into()
30 }
31
32 fn description() -> Option<Cow<'static, str>> {
33 Some(Self::DESCRIPTION.into())
34 }
35
36 fn input_schema() -> Option<Arc<JsonObject>> {
37 Some(schema_for_empty_input())
38 }
39
40 fn annotations() -> Option<ToolAnnotations> {
41 Some(
42 ToolAnnotations::new()
43 .read_only(true)
44 .destructive(false)
45 .idempotent(true)
46 .open_world(false),
47 )
48 }
49}
50
51impl AsyncTool<SqliteHandler> for ListTablesTool {
52 async fn invoke(handler: &SqliteHandler, _params: Self::Parameter) -> Result<Self::Output, Self::Error> {
53 Ok(handler.list_tables().await?)
54 }
55}
56
57impl SqliteHandler {
58 pub async fn list_tables(&self) -> Result<ListTablesResponse, AppError> {
64 let pool = self.pool.clone();
65 let sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name";
66 let rows: Vec<(String,)> =
67 execute_with_timeout(self.config.query_timeout, sql, sqlx::query_as(sql).fetch_all(&pool)).await?;
68 Ok(ListTablesResponse {
69 tables: rows.into_iter().map(|r| r.0).collect(),
70 })
71 }
72}