Skip to main content

dbmcp_sqlite/tools/
list_views.rs

1//! MCP tool: `listViews`.
2
3use std::borrow::Cow;
4use std::sync::Arc;
5
6use dbmcp_server::pagination::Pager;
7use dbmcp_server::types::ListViewsResponse;
8
9use dbmcp_sql::Connection as _;
10use rmcp::handler::server::router::tool::{AsyncTool, ToolBase};
11use rmcp::model::{ErrorData, JsonObject, ToolAnnotations};
12
13use crate::SqliteHandler;
14use crate::types::ListViewsRequest;
15
16const NAME: &str = "listViews";
17const TITLE: &str = "List Views";
18const DESCRIPTION: &str = include_str!("../../assets/tools/list_views.md");
19
20/// Marker type for the `listViews` MCP tool.
21pub(crate) struct ListViewsTool;
22
23impl ToolBase for ListViewsTool {
24    type Parameter = ListViewsRequest;
25    type Output = ListViewsResponse;
26    type Error = ErrorData;
27
28    fn name() -> Cow<'static, str> {
29        NAME.into()
30    }
31
32    fn title() -> Option<String> {
33        Some(TITLE.into())
34    }
35
36    fn description() -> Option<Cow<'static, str>> {
37        Some(DESCRIPTION.into())
38    }
39
40    fn input_schema() -> Option<Arc<JsonObject>> {
41        None
42    }
43
44    fn annotations() -> Option<ToolAnnotations> {
45        Some(
46            ToolAnnotations::new()
47                .read_only(true)
48                .destructive(false)
49                .idempotent(true)
50                .open_world(false),
51        )
52    }
53}
54
55impl AsyncTool<SqliteHandler> for ListViewsTool {
56    async fn invoke(handler: &SqliteHandler, params: Self::Parameter) -> Result<Self::Output, Self::Error> {
57        handler.list_views(params).await
58    }
59}
60
61impl SqliteHandler {
62    /// Lists one page of views in the connected database.
63    ///
64    /// # Errors
65    ///
66    /// Returns [`ErrorData`] with code `-32602` if `cursor` is malformed,
67    /// or an internal-error [`ErrorData`] if the underlying query fails.
68    pub async fn list_views(
69        &self,
70        ListViewsRequest { cursor }: ListViewsRequest,
71    ) -> Result<ListViewsResponse, ErrorData> {
72        let pager = Pager::new(cursor, self.config.page_size);
73        let query = format!(
74            r"
75            SELECT name
76            FROM sqlite_schema
77            WHERE type = 'view' AND name NOT LIKE 'sqlite_%'
78            ORDER BY name
79            LIMIT {} OFFSET {}",
80            pager.limit(),
81            pager.offset(),
82        );
83
84        let rows: Vec<String> = self.connection.fetch_scalar(query.as_str(), None).await?;
85        let (views, next_cursor) = pager.paginate(rows);
86
87        Ok(ListViewsResponse::brief(views, next_cursor))
88    }
89}