use serde::Deserialize;
use crate::errors::{Result, WinxError};
pub const ALL_TOOL_NAMES: [&str; 9] = [
"Initialize",
"BashCommand",
"ReadFiles",
"FileWriteOrEdit",
"MultiFileEdit",
"UndoEdit",
"ContextSave",
"ReadImage",
"CodeMap",
];
const INITIALIZE: u16 = 1 << 0;
const BASH_COMMAND: u16 = 1 << 1;
const READ_FILES: u16 = 1 << 2;
const FILE_WRITE_OR_EDIT: u16 = 1 << 3;
const MULTI_FILE_EDIT: u16 = 1 << 4;
const UNDO_EDIT: u16 = 1 << 5;
const CONTEXT_SAVE: u16 = 1 << 6;
const READ_IMAGE: u16 = 1 << 7;
const CODE_MAP: u16 = 1 << 8;
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, clap::ValueEnum)]
#[serde(rename_all = "kebab-case")]
pub enum ToolProfile {
#[default]
Full,
Coding,
ReadOnly,
Terminal,
}
impl ToolProfile {
const fn mask(self) -> u16 {
match self {
Self::Full => {
INITIALIZE
| BASH_COMMAND
| READ_FILES
| FILE_WRITE_OR_EDIT
| MULTI_FILE_EDIT
| UNDO_EDIT
| CONTEXT_SAVE
| READ_IMAGE
| CODE_MAP
}
Self::Coding => {
INITIALIZE
| BASH_COMMAND
| READ_FILES
| FILE_WRITE_OR_EDIT
| MULTI_FILE_EDIT
| UNDO_EDIT
| CODE_MAP
}
Self::ReadOnly => INITIALIZE | READ_FILES | READ_IMAGE | CODE_MAP,
Self::Terminal => INITIALIZE | BASH_COMMAND,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ToolPolicy {
mask: u16,
}
impl Default for ToolPolicy {
fn default() -> Self {
Self::from_profile(ToolProfile::Full)
}
}
impl ToolPolicy {
pub const fn from_profile(profile: ToolProfile) -> Self {
Self { mask: profile.mask() }
}
pub fn from_allowed_tools<I, S>(names: I) -> Result<Self>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut mask = 0_u16;
let mut saw_name = false;
for name in names {
saw_name = true;
let name = name.as_ref();
let bit = tool_bit(name).ok_or_else(|| {
WinxError::ConfigurationError(format!(
"unknown MCP tool {name:?}; expected one of {}",
ALL_TOOL_NAMES.join(", ")
))
})?;
mask |= bit;
}
if !saw_name {
return Err(WinxError::ConfigurationError(
"MCP tool allowlist cannot be empty".to_string(),
));
}
Ok(Self { mask })
}
pub fn resolve(profile: ToolProfile, allowed_tools: Option<&[String]>) -> Result<Self> {
allowed_tools.map_or_else(
|| Ok(Self::from_profile(profile)),
|names| Self::from_allowed_tools(names.iter()),
)
}
pub fn allows(self, name: &str) -> bool {
tool_bit(name).is_some_and(|bit| self.mask & bit != 0)
}
pub fn names(self) -> impl Iterator<Item = &'static str> {
ALL_TOOL_NAMES.into_iter().filter(move |name| self.allows(name))
}
pub fn len(self) -> usize {
self.mask.count_ones() as usize
}
pub fn is_empty(self) -> bool {
self.mask == 0
}
}
fn tool_bit(name: &str) -> Option<u16> {
ALL_TOOL_NAMES.iter().position(|candidate| *candidate == name).map(|index| 1 << index)
}
#[cfg(test)]
mod tests {
#![allow(clippy::expect_used)]
use super::{ToolPolicy, ToolProfile, ALL_TOOL_NAMES};
#[test]
fn profiles_have_stable_expected_catalogs() {
assert_eq!(ToolPolicy::default().names().collect::<Vec<_>>(), ALL_TOOL_NAMES);
assert_eq!(
ToolPolicy::from_profile(ToolProfile::Coding).names().collect::<Vec<_>>(),
vec![
"Initialize",
"BashCommand",
"ReadFiles",
"FileWriteOrEdit",
"MultiFileEdit",
"UndoEdit",
"CodeMap",
]
);
assert_eq!(
ToolPolicy::from_profile(ToolProfile::ReadOnly).names().collect::<Vec<_>>(),
vec!["Initialize", "ReadFiles", "ReadImage", "CodeMap"]
);
assert_eq!(
ToolPolicy::from_profile(ToolProfile::Terminal).names().collect::<Vec<_>>(),
vec!["Initialize", "BashCommand"]
);
}
#[test]
fn explicit_allowlist_is_exact_deduplicated_and_validated() {
let policy = ToolPolicy::from_allowed_tools(["ReadFiles", "Initialize", "ReadFiles"])
.expect("valid policy");
assert_eq!(policy.names().collect::<Vec<_>>(), vec!["Initialize", "ReadFiles"]);
assert!(ToolPolicy::from_allowed_tools(["readfiles"]).is_err());
assert!(ToolPolicy::from_allowed_tools(Vec::<String>::new()).is_err());
}
}