Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
- Added `read: true` to `fetch_url` and to the tools of an MCP server, which returns the result in the same call where permissions are bypassed with no screening or the check says `safe`, and saves a model round. ([#3722](https://github.com/brave/bravebot/issues/3722))
- Added folding to the `/model` list: Left folds the section the cursor is in, Right opens it, and PageUp and PageDown move by a screen. The cursor stops on the heading of a folded section.
- Added removing a prompt that waits behind a turn with a click on `remove`, or on `clear all` for every waiting line, and `/mcp remove <alias>`, which disables and removes an MCP server declaration. The info panel no longer lists an MCP server whose tools you declined.
- Added `*` to `/mcp disable` and `/mcp remove`, so `/mcp remove brave-tabs*` removes every server whose name starts with `brave-tabs`. ([#4225](https://github.com/brave/bravebot/issues/4225))
- Added the update line for a copy installed with `apt` or `dnf`, given at startup and by `bravebot update`. ([#3461](https://github.com/brave/bravebot/issues/3461))
- Added the questions of an `ask_user` call and the answer to each under the call in the terminal, in lines and in the desktop window. ([#3712](https://github.com/brave/bravebot/issues/3712))
- Added the end line of a symbol to `lsp` locations where the server sends one, and a `set_link` tool that records the issue or pull request address you typed as the session's link. ([#3191](https://github.com/brave/bravebot/issues/3191))
Expand Down
83 changes: 83 additions & 0 deletions crates/config/src/mcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,43 @@ pub fn is_alias(alias: &str) -> bool {
&& characters.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
}

/// Whether `word` is a pattern for aliases: at least one `*`, and otherwise only the characters an
/// alias has.
///
/// A `*` stands for any run of characters, including none, so `brave-tabs*` names every alias that
/// starts with `brave-tabs`. A person types the pattern, and it is matched against the aliases they
/// declared and request, so it can name no more than those.
pub fn is_alias_pattern(word: &str) -> bool {
word.contains('*')
&& word.len() <= MAX_ALIAS
&& word.starts_with(|c: char| c.is_ascii_alphanumeric() || c == '*')
&& word
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '*'))
}

/// Whether `alias` is named by `pattern`, in which each `*` stands for any run of characters.
///
/// A pattern with no `*` names exactly the alias that spells it.
pub fn alias_matches(pattern: &str, alias: &str) -> bool {
let mut parts = pattern.split('*');
let first = parts.next().unwrap_or_default();
let Some(mut rest) = alias.strip_prefix(first) else {
return false;
};
let after: Vec<&str> = parts.collect();
let [between @ .., last] = after.as_slice() else {
return rest.is_empty();
};
for part in between {
match rest.find(part) {
Some(at) => rest = &rest[at + part.len()..],
None => return false,
}
}
rest.ends_with(last)
}

/// Whether `name` may name a tool in a tool allow or deny filter.
///
/// Either a tool word alone, or qualified under an alias as `alias:tool`.
Expand Down Expand Up @@ -2931,6 +2968,52 @@ mod tests {
assert!(declarations.get("docs").unwrap().declaration.is_ok());
}

/// SERVERS-19. A `*` stands for any run of characters, anywhere in the pattern, and a pattern
/// without one names one alias. Rejects a prefix-only match, which would take `*-read` for
/// everything, and a match that lets the text before a `*` overlap the text after it.
#[test]
fn a_pattern_names_the_aliases_whose_spelling_fits_around_its_stars() {
for (pattern, alias, named) in [
("brave-tabs*", "brave-tabs-manage", true),
("brave-tabs*", "brave-tabs", true),
("brave-tabs*", "brave-page-read", false),
("*-read", "brave-tabs-read", true),
("*-read", "brave-tabs-manage", false),
("brave-*-read", "brave-tabs-read", true),
("brave-*-read", "brave-read", false),
("a*a", "a", false),
("a*a", "aa", true),
("a*b*c", "axxbyyc", true),
("a*b*c", "axxcyyb", false),
("*", "weather", true),
("weather", "weather", true),
("weather", "weather2", false),
] {
assert_eq!(alias_matches(pattern, alias), named, "{pattern} on {alias}");
}
}

/// SERVERS-19. A pattern has a `*` and otherwise only what an alias has, so a word such as
/// `--help` or `../*` is never taken for one.
#[test]
fn a_pattern_has_a_star_and_only_the_characters_of_an_alias() {
for word in ["*", "brave-tabs*", "*-read", "a*b_c*"] {
assert!(is_alias_pattern(word), "{word:?} was refused");
}
for word in [
"",
"weather",
"-*",
"../*",
"a:b*",
"a b*",
"a?*",
format!("{}*", "a".repeat(64)).as_str(),
] {
assert!(!is_alias_pattern(word), "{word:?} was taken for a pattern");
}
}

#[test]
fn an_alias_that_could_not_name_a_tool_namespace_is_a_problem() {
for alias in ["", "weather:get", "-weather", "a b", &"a".repeat(65)] {
Expand Down
5 changes: 3 additions & 2 deletions crates/i18n/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -1993,7 +1993,7 @@ mcp-record-not-read = it could not be read as text, so it was left as it is
mcp-move-title = declare this server where its reply points?
mcp-start-title = start { $alias }?
command-mcp = List MCP servers, start one again, turn one off, or remove one
mcp-command-usage = /mcp takes list, reconnect [alias], disable <alias> or remove <alias>
mcp-command-usage = /mcp takes list, reconnect [alias], disable <alias> or remove <alias>. A * in the alias of disable or remove stands for any run of characters
mcp-command-none = No MCP server is requested here, and none is running
mcp-command-running = MCP servers running:
mcp-command-not-running = MCP servers requested here and not running:
Expand All @@ -2007,8 +2007,9 @@ mcp-command-disabled = { $alias } is turned off for this session. Nothing was wr
mcp-command-already-off = { $alias } is already turned off
mcp-command-needs-an-alias = /mcp { $command } needs the alias of a server
mcp-command-not-an-alias = { $alias } is not the name of an MCP server
mcp-command-no-match = No MCP server is named by { $pattern }. Nothing was changed
mcp-command-no-state-directory = There is no state directory, so no MCP server is declared and none can be removed
mcp-command-remove-hint = /mcp remove <alias> deletes a server's declaration and approval and takes it out of the settings files that ask for it, so no session starts it
mcp-command-remove-hint = /mcp remove <alias> deletes a server's declaration and approval and takes it out of the settings files that ask for it, so no session starts it. A * in the alias, as in brave-*, names every server it fits
mcp-move-declared = { $alias } is declared at { $url }
mcp-move-destination = and its reply points to { $url }
mcp-move-reaching = reaching { $authority }
Expand Down
5 changes: 3 additions & 2 deletions crates/i18n/locales/fr.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,7 @@ mcp-record-not-read = il n'a pas pu être lu comme du texte, il a donc été lai
mcp-move-title = déclarer ce serveur là où pointe sa réponse ?
mcp-start-title = démarrer { $alias } ?
command-mcp = Lister les serveurs MCP, en redémarrer un, en désactiver un ou en supprimer un
mcp-command-usage = /mcp accepte list, reconnect [alias], disable <alias> ou remove <alias>
mcp-command-usage = /mcp accepte list, reconnect [alias], disable <alias> ou remove <alias>. Un * dans l'alias de disable ou de remove remplace n'importe quelle suite de caractères
mcp-command-none = Aucun serveur MCP n'est demandé ici, et aucun ne fonctionne
mcp-command-running = Serveurs MCP en cours d'exécution :
mcp-command-not-running = Serveurs MCP demandés ici et arrêtés :
Expand All @@ -1696,8 +1696,9 @@ mcp-command-disabled = { $alias } est désactivé pour cette session. Rien n'a
mcp-command-already-off = { $alias } est déjà désactivé
mcp-command-needs-an-alias = /mcp { $command } demande l'alias d'un serveur
mcp-command-not-an-alias = { $alias } n'est pas le nom d'un serveur MCP
mcp-command-no-match = Aucun serveur MCP n'est désigné par { $pattern }. Rien n'a été modifié
mcp-command-no-state-directory = Il n'y a pas de répertoire d'état, donc aucun serveur MCP n'est déclaré et aucun ne peut être supprimé
mcp-command-remove-hint = /mcp remove <alias> supprime la déclaration et l'approbation d'un serveur et le retire des fichiers de réglages qui le demandent, si bien qu'aucune session ne le démarre
mcp-command-remove-hint = /mcp remove <alias> supprime la déclaration et l'approbation d'un serveur et le retire des fichiers de réglages qui le demandent, si bien qu'aucune session ne le démarre. Un * dans l'alias, comme dans brave-*, désigne chaque serveur qui y correspond
mcp-move-declared = { $alias } est déclaré à { $url }
mcp-move-destination = et sa réponse pointe vers { $url }
mcp-move-reaching = qui atteint { $authority }
Expand Down
Loading
Loading