feat: one per-workspace access list on the consent page and token forms, in place of the narrowing alert

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
hugocasa
2026-12-03 09:43:32 +01:00
co-authored by Claude Opus 5.5
parent 168861a40e
commit 0acfdb938d
9 changed files with 125 additions and 189 deletions
@@ -15,16 +15,6 @@
full: ['mcp:all', MCP_SESSION]
}
/** What each access level lets the client do. */
export const ACCESS_HINTS: Record<McpAccess, string> = {
full: 'Everything you can do yourself: explore, write, test and deploy drafts, and run.',
read_run:
'Explore, and run deployed scripts, flows and agents. The client changes nothing itself; what it runs can.',
read: 'Explore and read. Nothing is run or changed.',
selected:
'The scripts and flows you pick, each as its own tool, and nothing else. To share a few automations.'
}
/** The access a token's MCP scopes give: `mcp:all` and how it is narrowed, or else a
* selection of scripts, flows and endpoints. */
export function mcpAccessOf(scopes: string[]): McpAccess {
@@ -42,17 +32,11 @@
import McpToolsModal from './McpToolsModal.svelte'
import { Button } from '$lib/components/common'
import type { Snippet } from 'svelte'
import { resource } from 'runed'
import Alert from '$lib/components/common/alert/Alert.svelte'
import { resolveSessionAccess } from '$lib/components/copilot/chat/global/sessionAccess'
import { accessNarrowing } from './accessNarrowing'
import { userWorkspaces } from '$lib/stores'
import { ACCESS_HINTS } from './workspaceAccess'
interface Props {
/** The workspace the selection picker browses. */
workspaceId: string
/** The workspaces the token reaches, to warn where one holds it to less than `access`. */
workspaces?: string[]
/** The token's MCP scopes, the workspace list aside. */
scopes?: string[]
access?: McpAccess
@@ -67,7 +51,6 @@
let {
workspaceId,
workspaces = [],
scopes = $bindable(),
access = $bindable(),
initialScopes = [],
@@ -103,21 +86,6 @@
let toolsOpen = $state(false)
const toolsetAccess = $derived(access === 'selected' ? undefined : access)
// Each workspace's access, as the chat resolves it, to compare with the level picked.
const workspaceAccess = resource(
// Rerun once the workspace list loads: until then a role reads as its legacy defaults.
() => ({ ids: [...workspaces], roles: $userWorkspaces }),
({ ids }) => Promise.all(ids.map(async (id) => ({ id, there: await resolveSessionAccess(id) })))
)
const narrowed = $derived(
access
? (workspaceAccess.current ?? []).flatMap(({ id, there }) => {
const why = accessNarrowing(access!, there)
return why ? [{ id, why }] : []
})
: []
)
</script>
<div class="flex flex-col gap-4">
@@ -135,15 +103,6 @@
</div>
{/if}
</div>
{#if narrowed.length > 0}
<Alert type="warning" size="xs" title="Narrower in some workspaces">
<ul class="flex flex-col gap-0.5">
{#each narrowed as { id, why } (id)}
<li>In <span class="font-semibold">{id}</span>, {why}.</li>
{/each}
</ul>
</Alert>
{/if}
{@render afterAccess?.()}
{#if access === 'selected'}
<!-- A selection names paths of one workspace, so switching workspace starts it over. -->
@@ -3,8 +3,15 @@
import type { McpWorkspaceAccess } from '$lib/gen'
import type { McpAccess } from './McpAccessPicker.svelte'
import Popover from '$lib/components/Popover.svelte'
import Tooltip from '$lib/components/Tooltip.svelte'
import { Badge } from '$lib/components/common'
import { accessLabel, connectsSeparately, restriction, workspaceMcpUrl } from './workspaceAccess'
import {
accessHint,
accessLabel,
connectsSeparately,
restriction,
workspaceMcpUrl
} from './workspaceAccess'
interface Props {
workspaces: McpWorkspaceAccess[]
@@ -12,24 +19,27 @@
covered: string[]
all: boolean
access: McpAccess
title?: string
}
let { workspaces, covered, all, access }: Props = $props()
let { workspaces, covered, all, access, title }: Props = $props()
const listed = $derived(workspaces.filter((w) => all || covered.includes(w.id)))
</script>
{#if listed.length > 0}
<div class="flex flex-col gap-1">
<p class="text-xs font-semibold text-emphasis">What it gets</p>
{#if title}
<p class="text-xs font-semibold text-emphasis">{title}</p>
{/if}
<ul class="flex flex-col divide-y rounded-md border max-h-60 overflow-y-auto">
{#each listed as workspace (workspace.id)}
{@const separate = connectsSeparately(workspace, all, covered, access)}
{@const restricted = restriction(workspace, access)}
<li class="flex flex-col gap-1 px-2 py-1.5">
<div class="flex items-baseline justify-between gap-2">
<div class="flex items-center justify-between gap-2">
<span class="text-xs text-emphasis truncate">{workspace.name}</span>
<span class="text-xs text-secondary text-right">
<span class="flex shrink-0 items-center gap-1.5 text-xs text-secondary">
{accessLabel(workspace, access)}{separate ? ', connect separately' : ''}
{#if restricted}
<Popover notClickable>
@@ -39,6 +49,7 @@
<Badge color="gray" small>Restricted</Badge>
</Popover>
{/if}
<Tooltip small>{accessHint(workspace, access)}</Tooltip>
</span>
</div>
{#if separate}
@@ -1,31 +0,0 @@
import { describe, expect, it } from 'vitest'
import { accessNarrowing } from './accessNarrowing'
import {
capabilitiesForRole,
fullSessionAccess
} from '$lib/components/copilot/chat/global/sessionAccess'
import { NO_RUN_ACCESS, USE_MODE_ACCESS } from '$lib/components/copilot/chat/sessionCapabilities'
import { FULL_CAPABILITIES } from '$lib/roleCapabilities'
describe('accessNarrowing', () => {
it('says nothing where the workspace holds the token to what was picked', () => {
for (const picked of ['full', 'read_run', 'read', 'selected'] as const)
expect(accessNarrowing(picked, fullSessionAccess())).toBeUndefined()
expect(accessNarrowing('read_run', USE_MODE_ACCESS)).toBeUndefined()
expect(accessNarrowing('read', NO_RUN_ACCESS)).toBeUndefined()
})
it('names how a workspace narrows each level', () => {
expect(accessNarrowing('read_run', NO_RUN_ACCESS)).toContain('only reads')
expect(accessNarrowing('selected', NO_RUN_ACCESS)).toContain('no tools')
expect(accessNarrowing('full', USE_MODE_ACCESS)).toContain('protected')
const noCode = capabilitiesForRole({
isAdmin: false,
deployRulesPass: false,
capabilities: { ...FULL_CAPABILITIES, code: false, flows: 'compose', apps: 'compose' }
})
expect(accessNarrowing('full', noCode)).toBe(
"your role can't build scripts and the deploy rules don't let you deploy, so the client can't either"
)
})
})
@@ -1,38 +0,0 @@
import type {
SessionAccess,
SessionCapability
} from '$lib/components/copilot/chat/sessionCapabilities'
import type { McpAccess } from './McpAccessPicker.svelte'
const BUILDS: [SessionCapability, string][] = [
['write_draft', 'scripts'],
['write_flow_draft', 'flows'],
['write_app_draft', 'apps'],
['write_schedule_draft', 'schedules'],
['write_trigger_draft', 'triggers']
]
/** How a workspace narrows the access picked for a token, read from the session access the chat
* resolves there (`resolveSessionAccess`), the twin of what the MCP server holds the token to. */
export function accessNarrowing(picked: McpAccess, there: SessionAccess): string | undefined {
if (picked === 'read') return undefined
if (!there.has('run'))
return picked === 'selected'
? "your role can't run anything, so the client gets no tools"
: "your role can't run anything, so the client only reads"
if (picked !== 'full') return undefined
if (!there.has('modify_workspace'))
return 'it is protected against direct changes, so the client only reads and runs'
const cannot = BUILDS.filter(([cap]) => !there.has(cap)).map(([, what]) => what)
const parts: string[] = []
if (cannot.length > 0) parts.push(`your role can't build ${listed(cannot)}`)
if (!there.has('deploy')) parts.push("the deploy rules don't let you deploy")
if (parts.length === 0) return undefined
return `${parts.join(' and ')}, so the client can't either`
}
function listed(items: string[]): string {
return items.length === 1
? items[0]
: `${items.slice(0, -1).join(', ')} or ${items[items.length - 1]}`
}
@@ -4,6 +4,7 @@ import {
accessLabel,
connectsSeparately,
defaultConnection,
narrows,
restriction,
servedOn,
connectionToolset,
@@ -85,3 +86,16 @@ it('tools are part of a connection to their workspace alone', () => {
expect(connectsSeparately(ws('a', 'runnables'), true, [], 'read')).toBe(false)
expect(connectsSeparately(ws('a', 'read_only'), true, [], 'full')).toBe(false)
})
describe('narrows', () => {
it('flags a workspace giving less or other than the pick', () => {
expect(narrows(ws('a', 'build_and_run'), 'full')).toBe(false)
expect(narrows(ws('a', 'read_and_run'), 'full')).toBe(true)
expect(narrows(ws('a', 'run_and_edit'), 'full')).toBe(true)
expect(narrows(ws('a', 'read_only'), 'read_run')).toBe(true)
expect(narrows(ws('a', 'read_only'), 'read')).toBe(false)
expect(narrows(ws('a', 'runnables'), 'full')).toBe(true)
expect(narrows(ws('a', 'read_only'), 'selected')).toBe(true)
expect(narrows(ws('a', 'off'), 'read')).toBe(true)
})
})
@@ -5,6 +5,16 @@ import type { ToolsetLevel } from './toolsets.svelte'
export type WorkspaceAccess = McpWorkspaceAccess['access']
/** What each access level lets the client do. */
export const ACCESS_HINTS: Record<McpAccess, string> = {
full: 'Everything you can do yourself: explore, write, test and deploy drafts, and run.',
read_run:
'Explore, and run deployed scripts, flows and agents. The client changes nothing itself; what it runs can.',
read: 'Explore and read. Nothing is run or changed.',
selected:
'The scripts and flows you pick, each as its own tool, and nothing else. To share a few automations.'
}
const LABELS: Record<WorkspaceAccess, string> = {
build_and_run: 'Build and run',
run_and_edit: 'Build and run',
@@ -127,3 +137,21 @@ export function connectsSeparately(
) {
return workspace.access === 'runnables' && picked !== 'read' && (all || covered.length > 1)
}
/** What a level means in a workspace, and whether its role set it. */
export function accessHint(workspace: McpWorkspaceAccess, picked: McpAccess): string {
const level = toolsetLevel(workspace, picked)
const hint = !givesAnything(workspace, picked)
? 'Nothing the client can use there with this access.'
: workspace.access === 'runnables'
? 'The scripts and flows your role offers, each as its own tool, and nothing else.'
: ACCESS_HINTS[level ?? picked]
return workspace.limited ? `${hint} Set by your role.` : hint
}
/** Whether a workspace gives less than, or other than, the `picked` access. */
export function narrows(workspace: McpWorkspaceAccess, picked: McpAccess): boolean {
if (!givesAnything(workspace, picked) || restriction(workspace, picked)) return true
if (picked === 'read' || picked === 'selected') return false
return workspace.access === 'runnables' || toolsetLevel(workspace, picked) !== picked
}
@@ -32,7 +32,7 @@
import Select from '../select/Select.svelte'
import Section from '../Section.svelte'
import ClipboardPanel from '../details/ClipboardPanel.svelte'
import { workspaceMcpUrl } from '../mcp/workspaceAccess'
import { narrows, workspaceMcpUrl } from '../mcp/workspaceAccess'
import McpAccessPicker, { type McpAccess } from '../mcp/McpAccessPicker.svelte'
import McpWorkspaceAccessList from '../mcp/McpWorkspaceAccessList.svelte'
import MultiSelect from '../select/MultiSelect.svelte'
@@ -387,10 +387,10 @@
(w) => w.id === mcpBoundWorkspace && w.access === 'runnables'
)
)
// Shown only where a role sets the access: elsewhere the picked access is what the token gets.
const mcpRoleLimited = $derived(
// Shown only where a workspace gives other than the picked access.
const mcpNarrowed = $derived(
!!mcpWorkspaceAccess.current?.some(
(w) => w.limited && (mcpAllWorkspaces || mcpWorkspaces.includes(w.id))
(w) => (mcpAllWorkspaces || mcpWorkspaces.includes(w.id)) && narrows(w, mcpAccess)
)
)
const mcpUrl = $derived(
@@ -428,9 +428,6 @@
{/if}
<McpAccessPicker
workspaceId={scopeWorkspaceId}
workspaces={mcpAllWorkspaces
? ($usersWorkspaceStore?.workspaces ?? []).map((w) => w.id)
: mcpWorkspaces}
bind:scopes={mcpScopes}
bind:access={() => mcpAccess, setMcpAccess}
showTools
@@ -456,12 +453,13 @@
{/if}
{/snippet}
</McpAccessPicker>
{#if mcpRoleLimited}
{#if mcpNarrowed}
<McpWorkspaceAccessList
workspaces={mcpWorkspaceAccess.current ?? []}
covered={mcpWorkspaces}
all={mcpAllWorkspaces}
access={mcpAccess}
title="What it gets"
/>
{/if}
{#if !mcpOnly}
@@ -1,6 +1,9 @@
<script lang="ts">
import { untrack } from 'svelte'
import { UserService } from '$lib/gen'
import { McpOauthService, UserService } from '$lib/gen'
import { resource } from 'runed'
import McpWorkspaceAccessList from '$lib/components/mcp/McpWorkspaceAccessList.svelte'
import { narrows } from '$lib/components/mcp/workspaceAccess'
import { sendUserToast } from '$lib/toast'
import Button from '../common/button/Button.svelte'
import Modal from '../common/modal/Modal.svelte'
@@ -102,6 +105,16 @@
const warnsFor = $derived(
mcpEdited || (initialScopes ?? []).some(describedByPicker) ? mcpReached : []
)
const mcpWorkspaceAccess = resource(
() => isMcp,
async (on) => (on ? await McpOauthService.listMcpWorkspaceAccess({}).catch(() => []) : [])
)
const mcpAccess = $derived(mcpAccessOf(mcpScopes))
const mcpNarrowed = $derived(
(mcpWorkspaceAccess.current ?? []).some(
(w) => warnsFor.includes(w.id) && narrows(w, mcpAccess)
)
)
// Carried over as is, the picker knowing nothing of them: dropping the workspace list
// would widen the token to every workspace, dropping `mcp:session` would move a sign-in
// back to the endpoint tools once it is given `mcp:all` again.
@@ -249,11 +262,19 @@
>
<McpAccessPicker
workspaceId={mcpWorkspaceId}
workspaces={warnsFor}
initialScopes={(initialScopes ?? []).filter((s) => !isWorkspaceList(s))}
bind:scopes={mcpScopes}
/>
</div>
{#if mcpNarrowed}
<McpWorkspaceAccessList
workspaces={mcpWorkspaceAccess.current ?? []}
covered={warnsFor}
all={false}
access={mcpAccess}
title="What it gets"
/>
{/if}
{:else}
<ScopesPicker workspaceId={mcpWorkspaceId} {initialScopes} bind:value={pickedScopes} />
{/if}
@@ -13,7 +13,6 @@
import Tooltip from '$lib/components/Tooltip.svelte'
import Popover from '$lib/components/Popover.svelte'
import McpAccessPicker, {
ACCESS_HINTS,
ACCESS_SCOPES,
MCP_SESSION,
RUNNABLES_AS_TOOLS,
@@ -21,14 +20,15 @@
type McpAccess
} from '$lib/components/mcp/McpAccessPicker.svelte'
import McpToolsModal from '$lib/components/mcp/McpToolsModal.svelte'
import McpWorkspaceAccessList from '$lib/components/mcp/McpWorkspaceAccessList.svelte'
import {
accessHint,
accessLabel,
defaultConnection,
connectionToolset,
givesAnything,
restriction,
servedOn,
toolsetLevel,
workspaceMcpUrl
} from '$lib/components/mcp/workspaceAccess'
import ClipboardPanel from '$lib/components/details/ClipboardPanel.svelte'
@@ -189,30 +189,16 @@
? workspaceAccess.filter((w) => isTools(w) && !served.includes(w))
: []
)
// Served workspaces grouped by what they get, so a role's access reads once per level.
const groups = $derived.by(() => {
const byLabel = new Map<string, McpWorkspaceAccess[]>()
for (const w of served) {
const key = `${accessLabel(w, access)}\n${restriction(w, access) ?? ''}`
byLabel.set(key, [...(byLabel.get(key) ?? []), w])
}
return [...byLabel].map(([key, ws]) => {
const label = accessLabel(ws[0], access)
const level = toolsetLevel(ws[0], access)
const hint = !givesAnything(ws[0], access)
? 'Nothing the client can use there with this access.'
: isTools(ws[0])
? 'The scripts and flows your role offers, each as its own tool, and nothing else.'
: ACCESS_HINTS[level ?? access]
return {
key,
label,
ws,
restricted: restriction(ws[0], access),
hint: ws.every((w) => w.limited) ? `${hint} Set by your role.` : hint
}
})
})
// One line when every served workspace gets the same, else one row per workspace.
const same = $derived(
served.length > 0 &&
served.every(
(w) =>
accessLabel(w, access) === accessLabel(served[0], access) &&
restriction(w, access) === restriction(served[0], access) &&
accessHint(w, access) === accessHint(served[0], access)
)
)
// A role limit settles the access; a narrower one the client asked for stays editable.
const editable = $derived(served.every((w) => !w.limited) || access !== 'full')
// One tool list per connection: the picked level's, or what a bound workspace leaves.
@@ -611,51 +597,39 @@
</Button>
{/if}
{/snippet}
{#snippet accessRow(group: (typeof groups)[number], named: boolean)}
<div class="flex flex-col gap-1">
<div class="flex flex-wrap items-center gap-x-1.5">
<span class="text-xs text-primary">
{#if named}
<span class="font-semibold text-emphasis">{group.label}</span> in
{group.ws.map((w) => w.name).join(', ')}
{:else}
{group.label}
{/if}
</span>
{#if group.restricted}
<Popover notClickable>
{#snippet text()}
<div class="text-xs max-w-xs">{group.restricted}</div>
{/snippet}
<Badge color="gray" small>Restricted</Badge>
</Popover>
{/if}
<Tooltip small>{group.hint}</Tooltip>
{#if !named}
{@render pen()}
{@render toolsToggle()}
{/if}
</div>
</div>
{/snippet}
{#if !loadingWorkspaces && (groups.length > 0 || access === 'selected')}
{#if !loadingWorkspaces && (served.length > 0 || access === 'selected')}
{@const first = served[0]}
<div class="mb-6 flex flex-col gap-1">
<div class="flex items-center gap-1.5">
<p class="text-xs font-semibold text-emphasis">Access</p>
{#if groups.length !== 1}
{#if !same}
{@render pen()}
{@render toolsToggle()}
{/if}
</div>
{#if groups.length === 1}
{@render accessRow(groups[0], false)}
{:else if groups.length > 1}
<div class="flex flex-col divide-y rounded-md border">
{#each groups as group (group.key)}
<div class="px-2 py-1.5">{@render accessRow(group, true)}</div>
{/each}
{#if same}
{@const restricted = restriction(first, access)}
<div class="flex flex-wrap items-center gap-x-1.5">
<span class="text-xs text-primary">{accessLabel(first, access)}</span>
{#if restricted}
<Popover notClickable>
{#snippet text()}
<div class="text-xs max-w-xs">{restricted}</div>
{/snippet}
<Badge color="gray" small>Restricted</Badge>
</Popover>
{/if}
<Tooltip small>{accessHint(first, access)}</Tooltip>
{@render pen()}
{@render toolsToggle()}
</div>
{:else if served.length > 0}
<McpWorkspaceAccessList
workspaces={served}
covered={served.map((w) => w.id)}
all={false}
{access}
/>
{:else}
<p class="text-xs text-primary">
Scripts and flows as tools. Choose the workspace and its scripts and flows.