From a6ff0909aa163567e45603ddebe8bd06990efb90 Mon Sep 17 00:00:00 2001 From: rgarcia <72655+rgarcia@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:27:48 +0000 Subject: [PATCH 1/3] Let the consent page decide what an MCP connection may do The protected-resource metadata no longer lists scopes_supported, so MCP clients request no scopes and the authorization server's consent page, where the person connecting chooses permissions and projects, decides. When a Kernel-issued token names the actions it was granted, toolsets none of those actions can use are left out of the tool list, and get_connection_context reports the granted actions and projects. A token that names no action, and every API key, lists every toolset as before. The API still authorizes each call. --- docs/native-oauth.md | 7 +++ src/app/[transport]/route.test.ts | 40 ++++++++++++++ src/app/[transport]/route.ts | 16 +++++- src/lib/mcp/register.test.ts | 69 +++++++++++++++++++++++++ src/lib/mcp/register.ts | 49 +++++++++++++++++- src/lib/mcp/tools/connection-context.ts | 6 ++- src/lib/native-oauth.ts | 14 ++++- src/lib/oauth-discovery-route.test.ts | 1 - src/lib/oauth-discovery.test.ts | 1 - src/lib/oauth-discovery.ts | 1 - 10 files changed, 196 insertions(+), 8 deletions(-) diff --git a/docs/native-oauth.md b/docs/native-oauth.md index 5fe416b5..f5ccdbfa 100644 --- a/docs/native-oauth.md +++ b/docs/native-oauth.md @@ -61,6 +61,13 @@ On every request the server: is still authorized by the API. - **Requests and streams** end when the exchanged token expires (at most five minutes); clients reconnect with a fresh request. +- **Permissions** are chosen by the person who connects, on the authorization + server's consent page. The protected-resource metadata lists no + `scopes_supported`, so clients request no scopes and that page decides. + When the exchanged token names actions, toolsets none of them can use are + left out of the tool list and `get_connection_context` reports them under + `granted`. A token that names no action lists every toolset. Either way the + API authorizes each call. ## Configuration diff --git a/src/app/[transport]/route.test.ts b/src/app/[transport]/route.test.ts index 91b5a7f9..ab15e338 100644 --- a/src/app/[transport]/route.test.ts +++ b/src/app/[transport]/route.test.ts @@ -257,6 +257,46 @@ describe("native OAuth credentials", () => { expect(issuer.requests).toHaveLength(2); }); + test("list only the toolsets the token's actions can use", async () => { + issuer.respondWith({ projects: ["proj_a", "proj_b"] }); + const limited = await issuer.parent({ + scope: "openid browsers:use profiles:read", + }); + let response = await POST(mcpRequest(limited)); + expect(response.status).toBe(200); + const listed = await response.text(); + for (const tool of [ + "manage_browsers", + "computer_action", + "manage_profiles", + ]) + expect(listed).toContain(`"name":"${tool}"`); + for (const tool of ["manage_proxies", "manage_api_keys", "manage_apps"]) + expect(listed).not.toContain(`"name":"${tool}"`); + + response = await POST( + mcpRequest(limited, "tools/call", { + name: "get_connection_context", + arguments: {}, + }), + ); + expect(await response.text()).toContain( + JSON.stringify( + JSON.stringify({ + permissions: ["browsers:use", "profiles:read"], + projects: ["proj_a", "proj_b"], + }), + ).slice(1, -1), + ); + + // A token that names no action holds its whole grant; nothing is hidden. + issuer.respondWith({}); + response = await POST(mcpRequest(await refreshed())); + const everything = await response.text(); + expect(everything).toContain('"name":"manage_proxies"'); + expect(everything).toContain('"name":"manage_api_keys"'); + }); + test("reuse the entitlements snapshot across rotated tokens", async () => { for (let n = 0; n < 2; n++) { const response = await POST(mcpRequest(await refreshed())); diff --git a/src/app/[transport]/route.ts b/src/app/[transport]/route.ts index d9fede43..9dc24e3f 100644 --- a/src/app/[transport]/route.ts +++ b/src/app/[transport]/route.ts @@ -181,6 +181,7 @@ const handler = createMcpHandler(({ authInfo }) => { mcpApps: authInfo?.extra?.mcpApps === true, vaults: authInfo?.extra?.vaults === true, search: authInfo?.extra?.search === true, + permissions: authInfo?.extra?.permissions as string[] | undefined, }); return server; }); @@ -188,6 +189,10 @@ const handler = createMcpHandler(({ authInfo }) => { type AuthInfoExtra = { userId: string | null; clerkToken: string | null; + // The actions a Kernel-issued credential names and, when its grant is + // limited to some, the projects it may use. + permissions?: string[]; + projects?: string[]; }; async function handleMcpRequestWithIdentity({ @@ -296,6 +301,10 @@ async function handleAuthenticatedRequest( if (native.status !== "ok") return connectionScopeFailureResponse(req, native); const { credential } = native; + // Action scopes name a resource kind and a verb; identity scopes do not. + // A token that names no action holds its whole grant, which it does not + // list, so nothing is known about what it may do. + const actions = credential.scopes.filter((scope) => scope.includes(":")); // The exchanged token rotates; identity is the verified subject, and the // request ends when the exchanged token does, streams included. return await handleMcpRequestWithIdentity({ @@ -311,7 +320,12 @@ async function handleAuthenticatedRequest( userId: credential.subject, }), scopes: credential.scopes, - authInfoExtra: { userId: credential.subject, clerkToken: null }, + authInfoExtra: { + userId: credential.subject, + clerkToken: null, + permissions: actions.length > 0 ? actions : undefined, + projects: credential.projects, + }, credentialType: "oauth", transportSessionId, entitlementsCacheCredential: credential.grantId, diff --git a/src/lib/mcp/register.test.ts b/src/lib/mcp/register.test.ts index 1d5791cc..1346fab1 100644 --- a/src/lib/mcp/register.test.ts +++ b/src/lib/mcp/register.test.ts @@ -275,6 +275,75 @@ describe("MCP toolset allowlist", () => { }); }); +describe("MCP toolsets by granted permission", () => { + async function toolsFor(permissions?: string[]) { + const mcp = await connectTestMcp((server) => { + registerMcpCapabilities(server, { + mcpApps: true, + vaults: true, + search: true, + permissions, + }); + }, {}); + try { + return (await mcp.client.listTools()).tools.map((tool) => tool.name); + } finally { + await mcp.close(); + } + } + + test("lists everything when the credential names no action", async () => { + expect(await toolsFor()).toEqual( + expect.arrayContaining([ + "manage_browsers", + "manage_api_keys", + "manage_proxies", + "manage_vaults", + "web_search", + "open_auth_login", + ]), + ); + }); + + test("leaves out toolsets no granted action can use", async () => { + const tools = await toolsFor([ + "browsers:use", + "profiles:read", + "invocations:read", + ]); + expect(tools).toEqual( + expect.arrayContaining([ + "get_connection_context", + "search_docs", + "manage_browsers", + "computer_action", + "execute_playwright_code", + "manage_replays", + "manage_profiles", + "manage_apps", + ]), + ); + for (const hidden of [ + "manage_api_keys", + "manage_proxies", + "manage_vaults", + "manage_credentials", + "manage_auth_connections", + "open_auth_login", + "web_search", + ]) + expect(tools).not.toContain(hidden); + }); + + test("an entitled toolset still needs a granted action", async () => { + expect(await toolsFor(["vaults:read"])).toContain("manage_vaults"); + expect(await toolsFor(["searches:read"])).toContain("web_search"); + expect(await toolsFor(["auth_connections:use"])).toEqual( + expect.arrayContaining(["manage_auth_connections", "open_auth_login"]), + ); + }); +}); + describe("project selection registration", () => { const projectScopedTools = [ "manage_profiles", diff --git a/src/lib/mcp/register.ts b/src/lib/mcp/register.ts index 22f2802c..f78df18c 100644 --- a/src/lib/mcp/register.ts +++ b/src/lib/mcp/register.ts @@ -34,6 +34,10 @@ type McpRegistrationOptions = { mcpApps?: boolean; vaults?: boolean; search?: boolean; + // The actions a Kernel-issued credential names. Toolsets it cannot use at + // all are left out. Undefined means the credential names none (an API key, + // or a token that holds its whole grant), and every toolset is listed. + permissions?: readonly string[]; dependencies?: McpDependencies; }; type RegisterMcpToolset = (server: McpServer, options: McpToolOptions) => void; @@ -70,6 +74,44 @@ const mcpToolRegistrations = [ type McpToolset = (typeof mcpToolRegistrations)[number][0]; +// The resource kinds whose actions each toolset calls. A toolset is useful +// when the credential holds any action on one of them; the API still +// authorizes every call. Toolsets without an entry need no permission. +const mcpToolsetResourceKinds: Partial> = + { + profiles: ["profiles"], + browsers: ["browsers"], + projects: ["projects"], + api_keys: ["api_keys"], + browser_pools: ["browser_pools"], + config_registry: ["config_registry"], + browser_curl: ["browsers"], + browser_files: ["browsers"], + proxies: ["proxies"], + extensions: ["extensions"], + apps: ["deployments", "invocations"], + computer: ["browsers"], + shell: ["browsers"], + playwright: ["browsers"], + repl: ["browsers"], + webmcp: ["browsers"], + replays: ["browsers"], + auth_connections: ["auth_connections"], + credentials: ["credentials"], + credential_providers: ["credential_providers"], + vaults: ["vaults", "vault_provider_configs"], + search: ["searches"], + }; + +function toolsetPermitted( + permissions: readonly string[] | undefined, + toolset: McpToolset, +) { + const kinds = mcpToolsetResourceKinds[toolset]; + if (permissions === undefined || kinds === undefined) return true; + return permissions.some((action) => kinds.includes(action.split(":")[0])); +} + const mcpToolsets = mcpToolRegistrations.map(([toolset]) => toolset); const mcpToolsetSet: ReadonlySet = new Set(mcpToolsets); @@ -196,6 +238,7 @@ export function registerMcpCapabilities( mcpApps = false, vaults = false, search = false, + permissions, dependencies = defaultMcpDependencies, }: McpRegistrationOptions = {}, ) { @@ -211,7 +254,8 @@ export function registerMcpCapabilities( if ( (toolset !== "vaults" || vaults) && (toolset !== "search" || search) && - toolsetEnabled(enabledToolsets, disabledToolsets, toolset) + toolsetEnabled(enabledToolsets, disabledToolsets, toolset) && + toolsetPermitted(permissions, toolset) ) { registerToolset(server, dependencies); } @@ -222,7 +266,8 @@ export function registerMcpCapabilities( // resource) without replacing or narrowing manage_auth_connections. if ( mcpApps && - toolsetEnabled(enabledToolsets, disabledToolsets, "auth_connections") + toolsetEnabled(enabledToolsets, disabledToolsets, "auth_connections") && + toolsetPermitted(permissions, "auth_connections") ) { registerAuthLoginApp(server); } diff --git a/src/lib/mcp/tools/connection-context.ts b/src/lib/mcp/tools/connection-context.ts index 350baf08..b1db7ae4 100644 --- a/src/lib/mcp/tools/connection-context.ts +++ b/src/lib/mcp/tools/connection-context.ts @@ -8,7 +8,7 @@ export function registerConnectionContextTool(server: McpServer) { "get_connection_context", { description: - "inspect the authenticated KERNEL connection before a project-scoped operation. connection_scope.kind=organization may omit project for organization-wide reads and default-project creates, or pass a project name or id to select a project. connection_scope.kind=project is fixed to connection_scope.project_id; omit project or pass that project.", + "inspect the authenticated KERNEL connection before a project-scoped operation. connection_scope.kind=organization may omit project for organization-wide reads and default-project creates, or pass a project name or id to select a project. connection_scope.kind=project is fixed to connection_scope.project_id; omit project or pass that project. when the person who connected chose what this connection may do, granted.permissions lists the allowed actions and granted.projects the only projects it may use (absent means every project); with one granted project, omit project to use it.", inputSchema: z.object({}), annotations: { title: "get KERNEL connection context", @@ -23,8 +23,12 @@ export function registerConnectionContextTool(server: McpServer) { const { authContext, scope } = connectionContextFromAuthInfo( ctx.http.authInfo, ); + const { permissions, projects } = ctx.http.authInfo.extra ?? {}; return jsonResponse({ ...authContext, + ...(permissions !== undefined && { + granted: { permissions, projects }, + }), connection_scope: { kind: scope.kind, organization_id: scope.organizationId, diff --git a/src/lib/native-oauth.ts b/src/lib/native-oauth.ts index ad6d2ee3..5deda722 100644 --- a/src/lib/native-oauth.ts +++ b/src/lib/native-oauth.ts @@ -55,11 +55,13 @@ const exchangeSchema = z.object({ issued_token_type: z.literal(ACCESS_TOKEN_URN), expires_in: z.number().int().nonnegative(), scope: z.string().min(1), + projects: z.array(z.string()).optional(), }); const cachedExchangeSchema = z.object({ token: z.string().min(1).max(MAX_TOKEN_BYTES), expiresAt: z.number(), + projects: z.array(z.string()).optional(), }); export type NativeCredential = { @@ -68,6 +70,8 @@ export type NativeCredential = { subject: string; grantId: string; scopes: string[]; + // The projects the grant is limited to; absent when it covers every project. + projects?: string[]; // When the exchanged token stops being valid, in local time. expiresAt: number; }; @@ -390,7 +394,12 @@ async function exchange( const exchanged = exchangeSchema.safeParse(body); if (!exchanged.success) throw serverFault("native exchange response is not RFC 8693"); - const { access_token: derivedToken, expires_in, scope } = exchanged.data; + const { + access_token: derivedToken, + expires_in, + scope, + projects, + } = exchanged.data; const derived = await verifyExchangedToken(derivedToken, parent, config); if (derived.scope !== scope) throw serverFault("exchanged token lineage mismatch: scope"); @@ -408,6 +417,7 @@ async function exchange( subject: parent.sub, grantId: parent.grant_id, scopes: scope.split(" "), + projects, expiresAt, }; } @@ -444,6 +454,7 @@ function sealExchange( const plaintext = JSON.stringify({ token: credential.token, expiresAt: credential.expiresAt, + projects: credential.projects, }); const sealed = Buffer.concat([ iv, @@ -505,6 +516,7 @@ async function cachedExchange( subject: parent.sub, grantId: parent.grant_id, scopes: derived.scope.split(" "), + projects: cached.projects, expiresAt: Math.min(cached.expiresAt, derived.exp * 1000), }; } catch (error) { diff --git a/src/lib/oauth-discovery-route.test.ts b/src/lib/oauth-discovery-route.test.ts index d860c8d0..a601f0e0 100644 --- a/src/lib/oauth-discovery-route.test.ts +++ b/src/lib/oauth-discovery-route.test.ts @@ -31,7 +31,6 @@ describe("/.well-known/oauth-protected-resource/mcp", () => { authorization_endpoint: `${issuer}/authorize`, token_endpoint: `${issuer}/token`, registration_endpoint: `${issuer}/register`, - scopes_supported: ["openid"], resource_name: "KERNEL", resource_documentation: "https://www.kernel.sh/docs", }); diff --git a/src/lib/oauth-discovery.test.ts b/src/lib/oauth-discovery.test.ts index c0f374a0..d91c6a24 100644 --- a/src/lib/oauth-discovery.test.ts +++ b/src/lib/oauth-discovery.test.ts @@ -55,7 +55,6 @@ describe("OAuth protected-resource discovery", () => { authorization_endpoint: "https://auth.onkernel.com/authorize", token_endpoint: "https://auth.onkernel.com/token", registration_endpoint: "https://auth.onkernel.com/register", - scopes_supported: ["openid"], resource_name: "KERNEL", resource_documentation: "https://www.kernel.sh/docs", }); diff --git a/src/lib/oauth-discovery.ts b/src/lib/oauth-discovery.ts index 04a9b527..ca7144ac 100644 --- a/src/lib/oauth-discovery.ts +++ b/src/lib/oauth-discovery.ts @@ -62,7 +62,6 @@ export function oauthResourceMetadata( authorization_endpoint: `${authorizationServer}/authorize`, token_endpoint: `${authorizationServer}/token`, registration_endpoint: `${authorizationServer}/register`, - scopes_supported: ["openid"], resource_name: "KERNEL", resource_documentation: "https://www.kernel.sh/docs", }; From 975c82dad275bfd9bedc0cbf2c4d58a37320fe16 Mon Sep 17 00:00:00 2001 From: rgarcia <72655+rgarcia@users.noreply.github.com> Date: Sun, 22 Nov 2026 19:52:52 +0000 Subject: [PATCH 2/3] Cover the task and workload identity toolsets in the permission filter Make the toolset-to-resource-kind table exhaustive so a new toolset cannot be added without deciding how a granted permission affects it. Task tools need a tasks action; workload identity tools, like API key tools, are never listed for a token that names actions. Document that the vault and search tools also need organizations:read, because entitlements are read with the connection's token. --- docs/native-oauth.md | 7 +++++ src/lib/mcp/register.test.ts | 8 +++++ src/lib/mcp/register.ts | 57 +++++++++++++++++++----------------- 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/docs/native-oauth.md b/docs/native-oauth.md index f5ccdbfa..862df8ce 100644 --- a/docs/native-oauth.md +++ b/docs/native-oauth.md @@ -68,6 +68,13 @@ On every request the server: left out of the tool list and `get_connection_context` reports them under `granted`. A token that names no action lists every toolset. Either way the API authorizes each call. + - API key and workload identity tools are never listed for a token that + names actions: a sign-in cannot hold those permissions. + - The vault and search tools also depend on the organization's entitlements, + which the server reads with the connection's own token. Without + `organizations:read` (on the consent page, "view plan entitlements and + limits") that read is refused and both toolsets are left out, whatever + else was granted. ## Configuration diff --git a/src/lib/mcp/register.test.ts b/src/lib/mcp/register.test.ts index 839279cb..746dcf7f 100644 --- a/src/lib/mcp/register.test.ts +++ b/src/lib/mcp/register.test.ts @@ -303,6 +303,8 @@ describe("MCP toolsets by granted permission", () => { expect.arrayContaining([ "manage_browsers", "manage_api_keys", + "manage_workload_identity", + "manage_tasks", "manage_proxies", "manage_vaults", "web_search", @@ -331,6 +333,8 @@ describe("MCP toolsets by granted permission", () => { ); for (const hidden of [ "manage_api_keys", + "manage_workload_identity", + "manage_tasks", "manage_proxies", "manage_vaults", "manage_credentials", @@ -341,6 +345,10 @@ describe("MCP toolsets by granted permission", () => { expect(tools).not.toContain(hidden); }); + test("lists the task tools for a task action", async () => { + expect(await toolsFor(["tasks:read"])).toContain("manage_tasks"); + }); + test("an entitled toolset still needs a granted action", async () => { expect(await toolsFor(["vaults:read"])).toContain("manage_vaults"); expect(await toolsFor(["searches:read"])).toContain("web_search"); diff --git a/src/lib/mcp/register.ts b/src/lib/mcp/register.ts index ba520d4e..52bbc758 100644 --- a/src/lib/mcp/register.ts +++ b/src/lib/mcp/register.ts @@ -80,39 +80,42 @@ type McpToolset = (typeof mcpToolRegistrations)[number][0]; // The resource kinds whose actions each toolset calls. A toolset is useful // when the credential holds any action on one of them; the API still -// authorizes every call. Toolsets without an entry need no permission. -const mcpToolsetResourceKinds: Partial> = - { - profiles: ["profiles"], - browsers: ["browsers"], - projects: ["projects"], - api_keys: ["api_keys"], - browser_pools: ["browser_pools"], - config_registry: ["config_registry"], - browser_curl: ["browsers"], - browser_files: ["browsers"], - proxies: ["proxies"], - extensions: ["extensions"], - apps: ["deployments", "invocations"], - computer: ["browsers"], - shell: ["browsers"], - playwright: ["browsers"], - repl: ["browsers"], - webmcp: ["browsers"], - replays: ["browsers"], - auth_connections: ["auth_connections"], - credentials: ["credentials"], - credential_providers: ["credential_providers"], - vaults: ["vaults", "vault_provider_configs"], - search: ["searches"], - }; +// authorizes every call. Null marks a toolset that calls no KERNEL resource, +// and an empty list one that no listed action can use. +const mcpToolsetResourceKinds: Record = { + profiles: ["profiles"], + tasks: ["tasks"], + docs: null, + browsers: ["browsers"], + projects: ["projects"], + api_keys: ["api_keys"], + workload_identity: [], + browser_pools: ["browser_pools"], + config_registry: ["config_registry"], + browser_curl: ["browsers"], + browser_files: ["browsers"], + proxies: ["proxies"], + extensions: ["extensions"], + apps: ["deployments", "invocations"], + computer: ["browsers"], + shell: ["browsers"], + playwright: ["browsers"], + repl: ["browsers"], + webmcp: ["browsers"], + replays: ["browsers"], + auth_connections: ["auth_connections"], + credentials: ["credentials"], + credential_providers: ["credential_providers"], + vaults: ["vaults", "vault_provider_configs"], + search: ["searches"], +}; function toolsetPermitted( permissions: readonly string[] | undefined, toolset: McpToolset, ) { const kinds = mcpToolsetResourceKinds[toolset]; - if (permissions === undefined || kinds === undefined) return true; + if (permissions === undefined || kinds === null) return true; return permissions.some((action) => kinds.includes(action.split(":")[0])); } From 7e3df379abb0f592066baa897401bd98e132fd5a Mon Sep 17 00:00:00 2001 From: rgarcia <72655+rgarcia@users.noreply.github.com> Date: Sun, 22 Nov 2026 20:32:25 +0000 Subject: [PATCH 3/3] Report granted projects without actions and name the toolset rules Report granted in get_connection_context when the grant is limited to some projects even if the token names no action. Replace the null and empty-list markers in the toolset table with "any" and "never", type the resource kinds so a misspelled one does not compile, and hide API key tools by the same rule as workload identity tools. Bump the exchange cache key because the sealed entry now carries projects. --- src/app/[transport]/route.test.ts | 34 ++++++++++++++----- src/lib/mcp/register.ts | 45 +++++++++++++++++++------ src/lib/mcp/tools/connection-context.ts | 4 +-- src/lib/native-oauth.ts | 2 +- 4 files changed, 64 insertions(+), 21 deletions(-) diff --git a/src/app/[transport]/route.test.ts b/src/app/[transport]/route.test.ts index 60028fc7..ac3e4015 100644 --- a/src/app/[transport]/route.test.ts +++ b/src/app/[transport]/route.test.ts @@ -261,6 +261,15 @@ describe("native OAuth credentials", () => { expect(issuer.requests).toHaveLength(2); }); + // The JSON a get_connection_context call returned. + async function connectionContext(response: Response) { + expect(response.status).toBe(200); + const text = await response.text(); + const event = text.split("\n").find((line) => line.startsWith("data: ")); + const message = JSON.parse(event ? event.slice(6) : text); + return JSON.parse(message.result.content[0].text); + } + test("list only the toolsets the token's actions can use", async () => { issuer.respondWith({ projects: ["proj_a", "proj_b"] }); const limited = await issuer.parent({ @@ -284,14 +293,10 @@ describe("native OAuth credentials", () => { arguments: {}, }), ); - expect(await response.text()).toContain( - JSON.stringify( - JSON.stringify({ - permissions: ["browsers:use", "profiles:read"], - projects: ["proj_a", "proj_b"], - }), - ).slice(1, -1), - ); + expect((await connectionContext(response)).granted).toEqual({ + permissions: ["browsers:use", "profiles:read"], + projects: ["proj_a", "proj_b"], + }); // A token that names no action holds its whole grant; nothing is hidden. issuer.respondWith({}); @@ -301,6 +306,19 @@ describe("native OAuth credentials", () => { expect(everything).toContain('"name":"manage_api_keys"'); }); + test("report granted projects for a token that names no action", async () => { + issuer.respondWith({ projects: ["proj_a"] }); + const response = await POST( + mcpRequest(await refreshed(), "tools/call", { + name: "get_connection_context", + arguments: {}, + }), + ); + expect((await connectionContext(response)).granted).toEqual({ + projects: ["proj_a"], + }); + }); + test("reuse the entitlements snapshot across rotated tokens", async () => { for (let n = 0; n < 2; n++) { const response = await POST(mcpRequest(await refreshed())); diff --git a/src/lib/mcp/register.ts b/src/lib/mcp/register.ts index 52bbc758..312a0338 100644 --- a/src/lib/mcp/register.ts +++ b/src/lib/mcp/register.ts @@ -78,18 +78,41 @@ const mcpToolRegistrations = [ type McpToolset = (typeof mcpToolRegistrations)[number][0]; -// The resource kinds whose actions each toolset calls. A toolset is useful -// when the credential holds any action on one of them; the API still -// authorizes every call. Null marks a toolset that calls no KERNEL resource, -// and an empty list one that no listed action can use. -const mcpToolsetResourceKinds: Record = { +// The KERNEL API resource kinds the toolsets act on; an action is named +// ":". +type McpResourceKind = + | "auth_connections" + | "browser_pools" + | "browsers" + | "config_registry" + | "credential_providers" + | "credentials" + | "deployments" + | "extensions" + | "invocations" + | "profiles" + | "projects" + | "proxies" + | "searches" + | "tasks" + | "vault_provider_configs" + | "vaults"; + +// What a credential that names its actions needs for each toolset to be +// listed: any action on one of the listed resource kinds, nothing ("any"), or +// a permission no such credential can hold ("never"). The API still +// authorizes every call. +const mcpToolsetRequirements: Record< + McpToolset, + readonly McpResourceKind[] | "any" | "never" +> = { profiles: ["profiles"], tasks: ["tasks"], - docs: null, + docs: "any", browsers: ["browsers"], projects: ["projects"], - api_keys: ["api_keys"], - workload_identity: [], + api_keys: "never", + workload_identity: "never", browser_pools: ["browser_pools"], config_registry: ["config_registry"], browser_curl: ["browsers"], @@ -114,8 +137,10 @@ function toolsetPermitted( permissions: readonly string[] | undefined, toolset: McpToolset, ) { - const kinds = mcpToolsetResourceKinds[toolset]; - if (permissions === undefined || kinds === null) return true; + const required = mcpToolsetRequirements[toolset]; + if (permissions === undefined || required === "any") return true; + if (required === "never") return false; + const kinds: readonly string[] = required; return permissions.some((action) => kinds.includes(action.split(":")[0])); } diff --git a/src/lib/mcp/tools/connection-context.ts b/src/lib/mcp/tools/connection-context.ts index 43e6f2de..a8f3cee9 100644 --- a/src/lib/mcp/tools/connection-context.ts +++ b/src/lib/mcp/tools/connection-context.ts @@ -8,7 +8,7 @@ export function registerConnectionContextTool(server: McpServer) { "get_connection_context", { description: - "inspect the authenticated KERNEL principal, organization, credential and effective scope, and current effective policy before an operation. service-account principals may also include sanitized workload_identity provenance; no credential secrets or original identity-provider claims are returned. connection_scope.kind=organization may omit project for organization-wide reads and default-project creates, or pass a project name or id. connection_scope.kind=project is fixed to connection_scope.project_id. when the person who connected chose what this connection may do, granted.permissions lists the allowed actions and granted.projects the only projects it may use (absent means every project); with one granted project, omit project to use it.", + "inspect the authenticated KERNEL principal, organization, credential and effective scope, and current effective policy before an operation. service-account principals may also include sanitized workload_identity provenance; no credential secrets or original identity-provider claims are returned. connection_scope.kind=organization may omit project for organization-wide reads and default-project creates, or pass a project name or id. connection_scope.kind=project is fixed to connection_scope.project_id. when the person who connected chose what this connection may do, granted.permissions lists the allowed actions and granted.projects the only projects it may use (absent means every project); with one granted project, omit project to use it: KERNEL runs a request that names no project in that project.", inputSchema: z.object({}), annotations: { title: "get KERNEL connection context", @@ -26,7 +26,7 @@ export function registerConnectionContextTool(server: McpServer) { const { permissions, projects } = ctx.http.authInfo.extra ?? {}; return jsonResponse({ ...authContext, - ...(permissions !== undefined && { + ...((permissions !== undefined || projects !== undefined) && { granted: { permissions, projects }, }), connection_scope: { diff --git a/src/lib/native-oauth.ts b/src/lib/native-oauth.ts index 5c581a25..05a63945 100644 --- a/src/lib/native-oauth.ts +++ b/src/lib/native-oauth.ts @@ -27,7 +27,7 @@ const ACCESS_TOKEN_URN = "urn:ietf:params:oauth:token-type:access_token"; const JWKS_PATH = "/.well-known/jwks.json"; const EXCHANGE_TIMEOUT_MS = 5_000; const CACHE_READ_TIMEOUT_MS = 500; -const CACHE_KEY_INFO = "kernel-mcp native exchange cache v1"; +const CACHE_KEY_INFO = "kernel-mcp native exchange cache v2"; // An exchanged token is reused only while at least this much of its lifetime // remains, so every request gets a usable window and clock skew is covered.