Skip to main content

victauri_plugin/mcp/
backend_params.rs

1use schemars::JsonSchema;
2use serde::Deserialize;
3
4/// Deserialize `args` as a JSON object (or absent), rejecting scalars/arrays. The documented
5/// contract is that `args` is an object of `{parameter_name: value}`; forwarding a scalar or
6/// array to `__TAURI_INTERNALS__.invoke` would break the handler's argument expectations, so we
7/// reject it with a clear error at the boundary instead of letting it slip through.
8fn deserialize_optional_object<'de, D>(
9    deserializer: D,
10) -> Result<Option<serde_json::Value>, D::Error>
11where
12    D: serde::Deserializer<'de>,
13{
14    let value = Option::<serde_json::Value>::deserialize(deserializer)?;
15    match value {
16        None | Some(serde_json::Value::Object(_)) => Ok(value),
17        Some(_) => Err(serde::de::Error::custom(
18            "`args` must be a JSON object of {parameter_name: value} (got a scalar or array)",
19        )),
20    }
21}
22
23/// Parameters for the `get_registry` tool.
24#[derive(Debug, Deserialize, JsonSchema)]
25pub struct RegistryParams {
26    /// Search query to filter commands by name or description.
27    pub query: Option<String>,
28}
29
30/// Parameters for the `invoke_command` tool.
31#[derive(Debug, Deserialize, JsonSchema)]
32pub struct InvokeCommandParams {
33    /// The Tauri command name to invoke (e.g. "greet", "`save_settings`").
34    pub command: String,
35    /// Command arguments as a JSON OBJECT nested under this `args` key — keys are the Tauri
36    /// command's parameter names, e.g. `{"command":"get_item","args":{"itemId":42}}`. Do NOT
37    /// put parameters at the top level next to `command` (a flat `{"command":...,"itemId":42}`
38    /// leaves `args` empty and the handler sees a missing argument). Omit for no-arg commands.
39    /// Forwarded verbatim to `__TAURI_INTERNALS__.invoke(command, args)` — identical via the
40    /// MCP tool and the REST `POST /api/tools/invoke_command` endpoint.
41    #[serde(default, deserialize_with = "deserialize_optional_object")]
42    pub args: Option<serde_json::Value>,
43    /// Target webview label.
44    pub webview_label: Option<String>,
45}
46
47/// Which app directory to target.
48#[derive(Debug, Deserialize, JsonSchema, Clone, Copy)]
49#[serde(rename_all = "snake_case")]
50pub enum AppDir {
51    /// Per-user app data directory.
52    Data,
53    /// Per-user config directory.
54    Config,
55    /// Log directory.
56    Log,
57    /// Local data directory.
58    LocalData,
59}
60
61/// Parameters for the `list_app_dir` tool.
62#[derive(Debug, Deserialize, JsonSchema)]
63pub struct ListAppDirParams {
64    /// Which app directory to list. Default: data.
65    pub directory: Option<AppDir>,
66    /// Optional subdirectory path relative to the chosen root (e.g. "databases").
67    pub path: Option<String>,
68    /// Only return entries matching this glob pattern (e.g. "*.sqlite", "*.db").
69    pub pattern: Option<String>,
70    /// Maximum directory depth to recurse. Default: 1 (immediate children only).
71    pub max_depth: Option<u32>,
72}
73
74/// Parameters for the `read_app_file` tool.
75#[derive(Debug, Deserialize, JsonSchema)]
76pub struct ReadAppFileParams {
77    /// Which app directory the file is relative to. Default: data.
78    pub directory: Option<AppDir>,
79    /// File path relative to the chosen directory (e.g. "settings.json", "databases/app.db").
80    pub path: String,
81    /// Maximum number of bytes to read. Default: 1MB. Set lower for large files.
82    pub max_bytes: Option<usize>,
83    /// If true, return raw base64-encoded bytes instead of UTF-8 text.
84    pub binary: Option<bool>,
85}
86
87/// Parameters for the `query_db` tool.
88#[derive(Debug, Deserialize, JsonSchema)]
89pub struct QueryDbParams {
90    /// Path to the `SQLite` database file, relative to the app data directory.
91    /// If omitted, Victauri auto-discovers `SQLite` databases in the app data directory.
92    pub path: Option<String>,
93    /// SQL query to execute. Must be a SELECT/PRAGMA/EXPLAIN statement (read-only).
94    pub query: String,
95    /// Positional bind parameters for the query (e.g. `["value1", 42]`).
96    pub params: Option<Vec<serde_json::Value>>,
97    /// Maximum number of rows to return. Default: 100.
98    pub max_rows: Option<usize>,
99}