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