From 7981ac8f1efaef641173f51bf2fd7ba40def20f9 Mon Sep 17 00:00:00 2001 From: hugocasa Date: Fri, 13 Nov 2026 18:15:01 +0100 Subject: [PATCH] fix: reserve the run tools' names, query a set's exact and subtree paths apart Co-Authored-By: Claude Opus 5.5 --- .../windmill-api/src/ai_session_tools/runs.rs | 38 +++++++++++++------ backend/windmill-common/src/mcp_tool_sets.rs | 22 ++++++++--- backend/windmill-mcp/src/server/runner.rs | 11 ++++-- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/backend/windmill-api/src/ai_session_tools/runs.rs b/backend/windmill-api/src/ai_session_tools/runs.rs index d67eef102b..defbb773ed 100644 --- a/backend/windmill-api/src/ai_session_tools/runs.rs +++ b/backend/windmill-api/src/ai_session_tools/runs.rs @@ -1851,13 +1851,6 @@ async fn list_runs(ctx: &Ctx<'_>, args: &Value) -> ToolResult { // the page before the set's filter below drops them. query.push_str("&job_kinds=script,flow"); } - if let Some(prefixes) = set - .as_ref() - .filter(|_| path.is_none()) - .and_then(ToolSetSelection::path_prefixes) - { - query.push_str(&format!("&script_path_start={}", enc(&prefixes.join(",")))); - } for (param, arg) in [ ("script_path_exact", "path"), ("created_by", "created_by"), @@ -1872,13 +1865,34 @@ async fn list_runs(ctx: &Ctx<'_>, args: &Value) -> ToolResult { query.push_str(&format!("&{flag}={value}")); } } - let jobs = ctx.get(&ctx.ws(&format!("jobs/list?{query}"))).await?; - let runs: Vec = jobs - .as_array() + let queries = match set + .as_ref() + .filter(|_| path.is_none()) + .and_then(ToolSetSelection::path_filters) + { + Some((exact, prefixes)) => [ + ("script_path_exact", exact), + ("script_path_start", prefixes), + ] .into_iter() - .flatten() - // The prefixes match more than the set's patterns: `f/a/x` also starts `f/a/xy`. + .filter(|(_, paths)| !paths.is_empty()) + .map(|(param, paths)| format!("{query}&{param}={}", enc(&paths.join(",")))) + .collect(), + None => vec![query], + }; + let mut jobs: Vec = Vec::new(); + for query in queries { + let page = ctx.get(&ctx.ws(&format!("jobs/list?{query}"))).await?; + jobs.extend(page.as_array().into_iter().flatten().cloned()); + } + // Newest first across both queries; ISO timestamps sort as text. + jobs.sort_by(|a, b| b["created_at"].as_str().cmp(&a["created_at"].as_str())); + jobs.dedup_by(|a, b| a["id"] == b["id"]); + let runs: Vec = jobs + .iter() + // A prefix's `_` is a LIKE wildcard, and the kinds of a set's paths still need checking. .filter(|job| set.as_ref().is_none_or(|set| in_set(set, job))) + .take(limit as usize) .map(|job| Value::Object(summarize_run(job))) .collect(); Ok(pretty(&Value::Array(runs))) diff --git a/backend/windmill-common/src/mcp_tool_sets.rs b/backend/windmill-common/src/mcp_tool_sets.rs index 6fc43f9cdf..5a5c7600e1 100644 --- a/backend/windmill-common/src/mcp_tool_sets.rs +++ b/backend/windmill-common/src/mcp_tool_sets.rs @@ -72,17 +72,23 @@ impl ToolSetSelection { }) } - /// Path prefixes covering the whole set, to narrow a runs query before `allows` filters it; - /// `None` when the set reaches every path. - pub fn path_prefixes(&self) -> Option> { + /// The set's exact paths and its subtrees' prefixes (`f/ops/`), to narrow a runs query before + /// `allows` filters it; `None` when the set reaches every path. Two lists, as the jobs API + /// ANDs its exact and prefix filters, and a prefix for an exact path would also match its + /// longer siblings (`f/a/x` starts `f/a/xy`). + pub fn path_filters(&self) -> Option<(Vec, Vec)> { let all = self.scripts.iter().chain(&self.flows); if all.clone().any(|p| p == "*") { return None; } - Some( - all.map(|p| p.strip_suffix('*').unwrap_or(p).to_string()) + let (subtrees, exact): (Vec<&String>, Vec<&String>) = all.partition(|p| p.ends_with("/*")); + Some(( + exact.into_iter().cloned().collect(), + subtrees + .into_iter() + .map(|p| p.trim_end_matches('*').to_string()) .collect(), - ) + )) } } @@ -286,5 +292,9 @@ mod tests { assert!(!set.allows("flow", "f/a/x")); assert!(!set.allows("singlestepflow", "f/ops/check")); assert!(tool_set_selection(&["mcp:scripts:f/ops/*".to_string()]).is_none()); + // An exact path is not queried as a prefix, which would also match its longer siblings. + let (exact, prefixes) = set.path_filters().unwrap(); + assert_eq!(exact, ["f/a/x", "f/a/sync"]); + assert_eq!(prefixes, ["f/ops/"]); } } diff --git a/backend/windmill-mcp/src/server/runner.rs b/backend/windmill-mcp/src/server/runner.rs index 45cf791588..0b43ca6d22 100644 --- a/backend/windmill-mcp/src/server/runner.rs +++ b/backend/windmill-mcp/src/server/runner.rs @@ -177,7 +177,8 @@ pub(super) struct McpContext { } /// The session's run tools a tool set with run history serves; their handlers confine them to -/// the set's runs. +/// the set's runs. Reserved for every token, so a runnable keeps one name whether or not a set +/// turns run history on. const RUN_HISTORY_TOOLS: &[&str] = &["list_runs", "get_run"]; impl Runner { @@ -690,8 +691,6 @@ impl ServerHandler for Runner { .list_tools_single(&auth, &workspace_id, &scope_config, read_only) .await?; if run_history { - list.tools - .retain(|t| !RUN_HISTORY_TOOLS.contains(&t.name.as_ref())); list.tools.extend( self.backend .session_tools(SessionAccess::ReadOnly) @@ -852,7 +851,11 @@ impl Runner { .filter(|(t, p)| !scope_config.granular || scope_config.is_allowed(t, p)) .collect(); let endpoint_tools = self.backend.all_endpoint_tools(); - let reserved: HashSet<&str> = endpoint_tools.iter().map(|t| t.name.as_ref()).collect(); + let reserved: HashSet<&str> = endpoint_tools + .iter() + .map(|t| t.name.as_ref()) + .chain(RUN_HISTORY_TOOLS.iter().copied()) + .collect(); let pairs: Vec<(&str, &str)> = items.iter().map(|(t, p)| (*t, p.as_str())).collect(); let names = assign_tool_names(&pairs, &reserved); Ok(items