fix: keep a set token's other restrictions, remount set rows on discard

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
hugocasa
2026-11-13 17:47:03 +01:00
co-authored by Claude Opus 5.5
parent 3ee1b3be77
commit 18a8799861
4 changed files with 20 additions and 4 deletions
+9 -2
View File
@@ -662,12 +662,19 @@ async fn expand_tool_set(db: &DB, request: &mut Request<axum::body::Body>) -> Re
let set = get_tool_set(db, &w_id, &name)
.await
.map_err(|e| e.into_response())?;
let scopes = match &set {
let selection = match &set {
Some(set) => tool_set_scopes(&name, set),
None => vec![format!("{SET_SCOPE_PREFIX}{name}")],
};
if let Some(authed) = request.extensions_mut().get_mut::<ApiAuthed>() {
authed.scopes = Some(scopes);
// Only the MCP selection is the set's: other restrictions, a tag fence among them, stay.
let kept = authed
.scopes
.iter()
.flatten()
.filter(|s| !s.starts_with("mcp:"))
.cloned();
authed.scopes = Some(kept.chain(selection).collect());
}
if let Some(instructions) = set.and_then(|s| s.instructions) {
request
@@ -83,6 +83,8 @@ pub fn validate_tool_sets(sets: &McpToolSets) -> Result<()> {
Ok(())
}
/// Read with the server's pool, whoever asks: callers must have authenticated the request for
/// `w_id` already. A set only narrows the token it is read for, so it grants nothing by itself.
pub async fn get_tool_set(db: &DB, w_id: &str, name: &str) -> Result<Option<McpToolSet>> {
let value = sqlx::query_scalar!(
"SELECT mcp_tool_sets -> $2 FROM workspace_settings WHERE workspace_id = $1",
@@ -25,6 +25,12 @@
}
}
/** The saved sets with new row ids, so each row remounts: its scope selector reads the
* selection once, and would otherwise keep the discarded one. */
export function discardMcpToolSets(saved: McpToolSetsType): McpToolSetsType {
return { sets: saved.sets.map((s) => ({ ...s, id: randomUUID() })) }
}
export function convertMcpToolSetsToBackend(
settings: McpToolSetsType
): Record<string, StoredSet> {
@@ -93,6 +93,7 @@
} from '$lib/components/workspaceSettings/DbtSettings.svelte'
import McpToolSetsSettings, {
convertMcpToolSetsFromBackend,
discardMcpToolSets,
type McpToolSetsType
} from '$lib/components/workspaceSettings/McpToolSetsSettings.svelte'
import DucklakeSettings, {
@@ -1556,7 +1557,7 @@
dbtSettings = clone(dbtSavedSettings)
break
case 'mcp_tool_sets':
mcpToolSets = clone(mcpToolSetsSaved)
mcpToolSets = discardMcpToolSets(mcpToolSetsSaved)
break
case 'default_app':
discardDefaultAppSettingsChanges()
@@ -2745,7 +2746,7 @@ export async function main(
bind:mcpToolSets
bind:mcpToolSetsSaved
onDiscard={() => {
mcpToolSets = clone(mcpToolSetsSaved)
mcpToolSets = discardMcpToolSets(mcpToolSetsSaved)
}}
/>
{:else if tab == 'dbt'}