Skip to main content

crisol_core/agent/
permissions.rs

1//! The permission model of one MCP session.
2
3use std::collections::HashSet;
4
5/// Read/write permissions for one MCP session.
6#[derive(Clone, Debug)]
7#[non_exhaustive]
8/// # Example
9///
10/// ```
11/// use crisol_core::agent::Permissions;
12/// ```
13pub struct Permissions {
14    /// When true (the default), every mutating tool is denied.
15    pub read_only: bool,
16    /// When set, commands outside this set are denied.
17    pub allowed_commands: Option<HashSet<String>>,
18    /// Commands that need `arguments.confirmation` to match `confirmation_token`.
19    pub sensitive_commands: HashSet<String>,
20    /// The confirmation value for sensitive commands.
21    pub confirmation_token: Option<String>,
22}
23
24impl Default for Permissions {
25    fn default() -> Self {
26        Self {
27            read_only: true,
28            allowed_commands: None,
29            sensitive_commands: HashSet::new(),
30            confirmation_token: None,
31        }
32    }
33}
34
35impl Permissions {
36    /// Allow reads and writes.
37    pub fn read_write() -> Self {
38        Self {
39            read_only: false,
40            ..Self::default()
41        }
42    }
43
44    /// Allow only these commands.
45    pub fn allow_commands<I, S>(mut self, commands: I) -> Self
46    where
47        I: IntoIterator<Item = S>,
48        S: Into<String>,
49    {
50        self.allowed_commands = Some(commands.into_iter().map(Into::into).collect());
51        self
52    }
53
54    /// Require a confirmation value for these commands.
55    pub fn confirm<I, S>(mut self, commands: I, token: impl Into<String>) -> Self
56    where
57        I: IntoIterator<Item = S>,
58        S: Into<String>,
59    {
60        self.sensitive_commands = commands.into_iter().map(Into::into).collect();
61        self.confirmation_token = Some(token.into());
62        self
63    }
64
65    /// Require confirmation for these commands without configuring a value.
66    ///
67    /// Every call is refused with `ConfirmationRequired` until a
68    /// token is configured, which is the safe default for `--read-write` without a secret.
69    pub fn require_confirmation<I, S>(mut self, commands: I) -> Self
70    where
71        I: IntoIterator<Item = S>,
72        S: Into<String>,
73    {
74        self.sensitive_commands = commands.into_iter().map(Into::into).collect();
75        self.confirmation_token = None;
76        self
77    }
78}