Skip to main content

dbmcp_server/
types.rs

1//! Request and response types for MCP tool parameters.
2//!
3//! Each struct maps to the JSON input or output schema of one MCP tool.
4
5use rmcp::schemars;
6use rmcp::schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8use serde_json::Value;
9
10use crate::pagination::Cursor;
11
12/// Response for tools with no structured return data.
13#[derive(Debug, Serialize, JsonSchema)]
14#[serde(rename_all = "camelCase")]
15pub struct MessageResponse {
16    /// Description of the completed operation.
17    pub message: String,
18}
19
20/// Request for the `listDatabases` tool.
21#[derive(Debug, Default, Deserialize, JsonSchema)]
22#[serde(rename_all = "camelCase")]
23pub struct ListDatabasesRequest {
24    /// Opaque cursor from a prior response's `nextCursor`; omit for the first page.
25    #[serde(default)]
26    pub cursor: Option<Cursor>,
27}
28
29/// Response for the `listDatabases` tool.
30#[derive(Debug, Serialize, JsonSchema)]
31#[serde(rename_all = "camelCase")]
32pub struct ListDatabasesResponse {
33    /// Sorted list of database names for this page.
34    pub databases: Vec<String>,
35    /// Opaque cursor pointing to the next page. Absent when this is the final page.
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub next_cursor: Option<Cursor>,
38}
39
40/// Request for the `createDatabase` tool.
41#[derive(Debug, Default, Deserialize, JsonSchema)]
42#[serde(rename_all = "camelCase")]
43pub struct CreateDatabaseRequest {
44    /// Name of the database to create. Must contain only alphanumeric characters and underscores.
45    pub database: String,
46}
47
48/// Request for the `dropDatabase` tool.
49#[derive(Debug, Default, Deserialize, JsonSchema)]
50#[serde(rename_all = "camelCase")]
51pub struct DropDatabaseRequest {
52    /// Name of the database to drop. Must contain only alphanumeric characters and underscores.
53    pub database: String,
54}
55
56/// Request for the `listTables` tool.
57#[derive(Debug, Default, Deserialize, JsonSchema)]
58#[serde(rename_all = "camelCase")]
59pub struct ListTablesRequest {
60    /// Database to list tables from. Defaults to the active database.
61    #[serde(default)]
62    pub database: Option<String>,
63    /// Opaque cursor from a prior response's `nextCursor`; omit for the first page.
64    #[serde(default)]
65    pub cursor: Option<Cursor>,
66}
67
68/// Response for the `listTables` tool.
69#[derive(Debug, Serialize, JsonSchema)]
70#[serde(rename_all = "camelCase")]
71pub struct ListTablesResponse {
72    /// Sorted list of table names for this page.
73    pub tables: Vec<String>,
74    /// Opaque cursor pointing to the next page. Absent when this is the final page.
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub next_cursor: Option<Cursor>,
77}
78
79/// Request for the `listViews` tool.
80#[derive(Debug, Default, Deserialize, JsonSchema)]
81#[serde(rename_all = "camelCase")]
82pub struct ListViewsRequest {
83    /// Database to list views from. Defaults to the active database.
84    #[serde(default)]
85    pub database: Option<String>,
86    /// Opaque cursor from a prior response's `nextCursor`; omit for the first page.
87    #[serde(default)]
88    pub cursor: Option<Cursor>,
89}
90
91/// Response for the `listViews` tool.
92#[derive(Debug, Serialize, JsonSchema)]
93#[serde(rename_all = "camelCase")]
94pub struct ListViewsResponse {
95    /// Sorted list of view names for this page.
96    pub views: Vec<String>,
97    /// Opaque cursor pointing to the next page. Absent when this is the final page.
98    #[serde(skip_serializing_if = "Option::is_none")]
99    pub next_cursor: Option<Cursor>,
100}
101
102/// Request for the `listTriggers` tool.
103#[derive(Debug, Default, Deserialize, JsonSchema)]
104#[serde(rename_all = "camelCase")]
105pub struct ListTriggersRequest {
106    /// Database to list triggers from. Defaults to the active database.
107    #[serde(default)]
108    pub database: Option<String>,
109    /// Opaque cursor from a prior response's `nextCursor`; omit for the first page.
110    #[serde(default)]
111    pub cursor: Option<Cursor>,
112}
113
114/// Response for the `listTriggers` tool.
115#[derive(Debug, Serialize, JsonSchema)]
116#[serde(rename_all = "camelCase")]
117pub struct ListTriggersResponse {
118    /// Sorted list of trigger names for this page.
119    pub triggers: Vec<String>,
120    /// Opaque cursor pointing to the next page. Absent when this is the final page.
121    #[serde(skip_serializing_if = "Option::is_none")]
122    pub next_cursor: Option<Cursor>,
123}
124
125/// Request for the `listFunctions` tool.
126#[derive(Debug, Default, Deserialize, JsonSchema)]
127#[serde(rename_all = "camelCase")]
128pub struct ListFunctionsRequest {
129    /// Database to list functions from. Defaults to the active database.
130    #[serde(default)]
131    pub database: Option<String>,
132    /// Opaque cursor from a prior response's `nextCursor`; omit for the first page.
133    #[serde(default)]
134    pub cursor: Option<Cursor>,
135}
136
137/// Response for the `listFunctions` tool.
138#[derive(Debug, Serialize, JsonSchema)]
139#[serde(rename_all = "camelCase")]
140pub struct ListFunctionsResponse {
141    /// Sorted list of function names for this page.
142    pub functions: Vec<String>,
143    /// Opaque cursor pointing to the next page. Absent when this is the final page.
144    #[serde(skip_serializing_if = "Option::is_none")]
145    pub next_cursor: Option<Cursor>,
146}
147
148/// Request for the `listProcedures` tool.
149#[derive(Debug, Default, Deserialize, JsonSchema)]
150#[serde(rename_all = "camelCase")]
151pub struct ListProceduresRequest {
152    /// Database to list procedures from. Defaults to the active database.
153    #[serde(default)]
154    pub database: Option<String>,
155    /// Opaque cursor from a prior response's `nextCursor`; omit for the first page.
156    #[serde(default)]
157    pub cursor: Option<Cursor>,
158}
159
160/// Response for the `listProcedures` tool.
161#[derive(Debug, Serialize, JsonSchema)]
162#[serde(rename_all = "camelCase")]
163pub struct ListProceduresResponse {
164    /// Sorted list of procedure names for this page.
165    pub procedures: Vec<String>,
166    /// Opaque cursor pointing to the next page. Absent when this is the final page.
167    #[serde(skip_serializing_if = "Option::is_none")]
168    pub next_cursor: Option<Cursor>,
169}
170
171/// Request for the `listMaterializedViews` tool.
172#[derive(Debug, Default, Deserialize, JsonSchema)]
173#[serde(rename_all = "camelCase")]
174pub struct ListMaterializedViewsRequest {
175    /// Database to list materialized views from. Defaults to the active database.
176    #[serde(default)]
177    pub database: Option<String>,
178    /// Opaque cursor from a prior response's `nextCursor`; omit for the first page.
179    #[serde(default)]
180    pub cursor: Option<Cursor>,
181}
182
183/// Response for the `listMaterializedViews` tool.
184#[derive(Debug, Serialize, JsonSchema)]
185#[serde(rename_all = "camelCase")]
186pub struct ListMaterializedViewsResponse {
187    /// Sorted list of materialized-view names for this page.
188    pub materialized_views: Vec<String>,
189    /// Opaque cursor pointing to the next page. Absent when this is the final page.
190    #[serde(skip_serializing_if = "Option::is_none")]
191    pub next_cursor: Option<Cursor>,
192}
193
194/// Request for the `getTableSchema` tool.
195#[derive(Debug, Default, Deserialize, JsonSchema)]
196#[serde(rename_all = "camelCase")]
197pub struct GetTableSchemaRequest {
198    /// Database containing the table. Defaults to the active database.
199    #[serde(default)]
200    pub database: Option<String>,
201    /// Table to inspect.
202    pub table: String,
203}
204
205/// Response for the `getTableSchema` tool.
206#[derive(Debug, Serialize, JsonSchema)]
207#[serde(rename_all = "camelCase")]
208pub struct TableSchemaResponse {
209    /// Name of the inspected table.
210    pub table: String,
211    /// Column definitions keyed by column name.
212    pub columns: Value,
213}
214
215/// Request for the `writeQuery` tool.
216#[derive(Debug, Default, Deserialize, JsonSchema)]
217#[serde(rename_all = "camelCase")]
218pub struct QueryRequest {
219    /// The SQL query to execute.
220    pub query: String,
221    /// Database to run the query against. Defaults to the active database.
222    #[serde(default)]
223    pub database: Option<String>,
224}
225
226/// Request for the `readQuery` tool.
227#[derive(Debug, Default, Deserialize, JsonSchema)]
228#[serde(rename_all = "camelCase")]
229pub struct ReadQueryRequest {
230    /// The SQL query to execute.
231    pub query: String,
232    /// Database to run the query against. Defaults to the active database.
233    #[serde(default)]
234    pub database: Option<String>,
235    /// Opaque cursor from a prior response's `nextCursor`; omit for the first page.
236    #[serde(default)]
237    pub cursor: Option<Cursor>,
238}
239
240/// Response for the `writeQuery` and `explainQuery` tools.
241#[derive(Debug, Serialize, JsonSchema)]
242#[serde(rename_all = "camelCase")]
243pub struct QueryResponse {
244    /// Result rows, each a JSON object keyed by a column name.
245    pub rows: Vec<Value>,
246}
247
248/// Response for the `readQuery` tool.
249#[derive(Debug, Serialize, JsonSchema)]
250#[serde(rename_all = "camelCase")]
251pub struct ReadQueryResponse {
252    /// Result rows, each a JSON object keyed by a column name.
253    pub rows: Vec<Value>,
254    /// Opaque cursor pointing to the next page. Absent when this is the final
255    /// page, when the result fits in one page, or when the statement is a
256    /// non-`SELECT` kind that does not paginate (e.g. `SHOW`, `EXPLAIN`).
257    #[serde(skip_serializing_if = "Option::is_none")]
258    pub next_cursor: Option<Cursor>,
259}
260
261/// Request for the `explainQuery` tool.
262#[derive(Debug, Default, Deserialize, JsonSchema)]
263#[serde(rename_all = "camelCase")]
264pub struct ExplainQueryRequest {
265    /// Database to explain against. Defaults to the active database.
266    #[serde(default)]
267    pub database: Option<String>,
268    /// The SQL query to explain.
269    pub query: String,
270    /// If true, use EXPLAIN ANALYZE for actual execution statistics. In read-only mode, only allowed for read-only statements. Defaults to false.
271    #[serde(default)]
272    pub analyze: bool,
273}