Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(control-api): r1-m3 share the allowlist union between gate and invoke

Diego Zacariase17a99306242
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index d8062d02c..440d29703 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.741",
+ "version": "0.1.742",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.741",
+ "version": "0.1.742",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",
diff --git a/control-api/package.json b/control-api/package.json
index fedcb3320..0036854a4 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.741",
+ "version": "0.1.742",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/src/services/access/mcpInvocable.ts b/control-api/src/services/access/mcpInvocable.ts
index 3e5f8922b..7b26ed14a 100644
--- a/control-api/src/services/access/mcpInvocable.ts
+++ b/control-api/src/services/access/mcpInvocable.ts
@@ -174,6 +174,30 @@ async function loadAllowedNamesByContext(
return byContext
}
+/**
+ * Union of the `spec.mcpServers` allowlists of the given Contexts (by
+ * `spec.contextId`), names trimmed. The single definition of "these Contexts
+ * enable these servers" for the consent gate and the rpc-proxy rail, so the
+ * two cannot drift. An empty `contextIds` resolves without listing Contexts.
+ */
+export async function allowedMcpServerNamesForContexts(
+ gateway: Pick<K8sGateway, 'listResource'>,
+ mcpServersNamespace: string,
+ contextIds: readonly string[]
+): Promise<Set<string>> {
+ const union = new Set<string>()
+ if (contextIds.length === 0) return union
+ const allowedByContext = await loadAllowedNamesByContext(
+ gateway,
+ mcpServersNamespace,
+ new Set(contextIds)
+ )
+ for (const names of allowedByContext.values()) {
+ for (const name of names) union.add(name)
+ }
+ return union
+}
+
/**
* Whether some Context the user belongs to enables `mcpServerName` through its
* `spec.mcpServers` allowlist. This is the access rule for consenting to (and
@@ -195,16 +219,12 @@ export async function userHasContextEnablingMcpServer(opts: {
mcpServerName: string
}): Promise<boolean> {
const { contextIds } = await getUserContexts(opts.userId)
- if (contextIds.length === 0) return false
- const allowedByContext = await loadAllowedNamesByContext(
+ const allowed = await allowedMcpServerNamesForContexts(
opts.gateway,
opts.mcpServersNamespace,
- new Set(contextIds)
+ contextIds
)
- for (const names of allowedByContext.values()) {
- if (names.has(opts.mcpServerName)) return true
- }
- return false
+ return allowed.has(opts.mcpServerName)
}
/**
@@ -338,19 +358,14 @@ export async function resolveInvocableMcpServersForContexts(
callerUserId: string,
db: DbClient
): Promise<InvocableMcpServer[]> {
- const scoped = new Set(scopedContextIds)
- if (scoped.size === 0) return []
+ if (scopedContextIds.length === 0) return []
- const [allowedByContext, mcpServers] = await Promise.all([
- loadAllowedNamesByContext(gateway, mcpServersNamespace, scoped),
+ const [union, mcpServers] = await Promise.all([
+ allowedMcpServerNamesForContexts(gateway, mcpServersNamespace, scopedContextIds),
gateway.listResource('mcpservers', mcpServersNamespace),
])
const serverList = asArray<McpServerCR>(mcpServers)
- const union = new Set<string>()
- for (const names of allowedByContext.values()) {
- for (const name of names) union.add(name)
- }
const grantPresence = await computeGrantPresence(
db,
mcpServersNamespace,
diff --git a/control-api/test/services.access.userMcpServerAccess.test.ts b/control-api/test/services.access.userMcpServerAccess.test.ts
index a8dde8d02..2104eae69 100644
--- a/control-api/test/services.access.userMcpServerAccess.test.ts
+++ b/control-api/test/services.access.userMcpServerAccess.test.ts
@@ -1,6 +1,10 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
+import fc from 'fast-check'
import { config } from '../src/config.js'
-import { userHasContextEnablingMcpServer } from '../src/services/access/mcpInvocable.js'
+import {
+ allowedMcpServerNamesForContexts,
+ userHasContextEnablingMcpServer,
+} from '../src/services/access/mcpInvocable.js'
import { membership, seedAllowlistedContext } from './fixtures/contextAllowlist.js'
import { MockGateway } from './mockGateway.js'
@@ -87,3 +91,44 @@ describe('userHasContextEnablingMcpServer', () => {
await expect(check(gateway, 'user-1', 'notion')).rejects.toThrow('apiserver down')
})
})
+
+describe('allowedMcpServerNamesForContexts', () => {
+ const contextNames = ['ctx-a', 'ctx-b', 'ctx-c', 'ctx-d']
+ // Padded variants are reachable: the allowlist writer stores the name as given.
+ const serverName = fc.constantFrom('notion', 'gdrive', 'slack', ' notion', 'gdrive ', '')
+
+ it('is exactly the trimmed union of the asked Contexts allowlists (oracle)', async () => {
+ await fc.assert(
+ fc.asyncProperty(
+ fc.dictionary(
+ fc.constantFrom(...contextNames),
+ fc.uniqueArray(serverName, { maxLength: 4 })
+ ),
+ fc.uniqueArray(fc.constantFrom(...contextNames, 'ctx-unseeded'), { maxLength: 5 }),
+ async (allowlists, asked) => {
+ const gateway = new MockGateway(MCP_NS)
+ for (const [name, servers] of Object.entries(allowlists)) {
+ await seedAllowlistedContext(gateway, name, servers)
+ }
+
+ const out = await allowedMcpServerNamesForContexts(gateway as never, MCP_NS, asked)
+
+ const expected = new Set<string>()
+ for (const id of asked) {
+ for (const n of allowlists[id] ?? []) if (n.trim()) expected.add(n.trim())
+ }
+ expect([...out].sort()).toEqual([...expected].sort())
+ }
+ ),
+ { numRuns: 300 }
+ )
+ })
+
+ it('resolves empty without listing Contexts when asked for none', async () => {
+ const gateway = new MockGateway(MCP_NS)
+ await seedAllowlistedContext(gateway, 'ctx-a', ['notion'])
+ const listSpy = vi.spyOn(gateway, 'listResource')
+ expect(await allowedMcpServerNamesForContexts(gateway as never, MCP_NS, [])).toEqual(new Set())
+ expect(listSpy).not.toHaveBeenCalled()
+ })
+})