Skip to content

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

TypeScript

fix(mcp-host): gate per-serverName connected status on ownsServerStatus (R4-M1)

Diego Zacarias995716443dbe
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index b0ac82003..20a440cc2 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.295",
+ "version": "0.1.296",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.295",
+ "version": "0.1.296",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index 77e36b8e6..a2b10e42f 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.295",
+ "version": "0.1.296",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/mcp/__tests__/managerPerUserStatus.test.ts b/mcp-host/src/mcp/__tests__/managerPerUserStatus.test.ts
new file mode 100644
index 000000000..bc4989bbc
--- /dev/null
+++ b/mcp-host/src/mcp/__tests__/managerPerUserStatus.test.ts
@@ -0,0 +1,106 @@
+/**
+ * R4-M1 — a per-user (ownsServerStatus=false) admission must NEVER write the
+ * per-serverName health status.
+ *
+ * The per-serverName status reflects the SHARED representative's health. A lazy
+ * per-user partition that connects while the representative is DOWN must not flip
+ * the serverName status to `connected` (with its own partition's toolCount) — a
+ * status reader (GET /v1/runtime/status, heartbeat) would then report a down
+ * representative as healthy. This pins the gate symmetric with the already-gated
+ * markConnecting / recordAdmissionFailure in connectAndInstall.
+ */
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+import type { McpServerInfo } from '../../types'
+import type { McpTokenProvider } from '../client'
+import type { McpPrincipal, McpTokenProviderFactory } from '../manager'
+import { McpManager } from '../manager'
+
+// ─── SDK mocks (constructable) ───────────────────────────────────────────────
+
+const sdk: { connectImpl: (() => Promise<void>) | null } = { connectImpl: null }
+
+vi.mock('@modelcontextprotocol/sdk/client/index.js', () => ({
+ Client: class MockClient {
+ connect = vi.fn(async () => {
+ if (sdk.connectImpl) await sdk.connectImpl()
+ })
+ close = vi.fn().mockResolvedValue(undefined)
+ listTools = vi.fn(async () => ({
+ tools: [{ name: 'do', description: 'demo tool', inputSchema: { type: 'object' } }],
+ }))
+ callTool = vi.fn(async () => ({ content: [{ type: 'text', text: 'ok' }] }))
+ },
+}))
+
+vi.mock('@modelcontextprotocol/sdk/client/streamableHttp.js', () => ({
+ StreamableHTTPClientTransport: class {
+ close = vi.fn().mockResolvedValue(undefined)
+ constructor(_url: URL, _opts?: unknown) {}
+ },
+}))
+
+vi.mock('@modelcontextprotocol/sdk/client/sse.js', () => ({
+ SSEClientTransport: class {
+ close = vi.fn().mockResolvedValue(undefined)
+ },
+}))
+
+// ─── Helpers ─────────────────────────────────────────────────────────────────
+
+function oauthUserServer(name = 'gh'): McpServerInfo {
+ return {
+ name,
+ contextRef: 'ctx-1',
+ transport: { type: 'streamableHttp', url: `http://${name}/mcp` },
+ authKind: 'oauth-user',
+ enabled: true,
+ status: { deployed: true, ready: true },
+ }
+}
+
+/** SHARED representative is token-less; per-user principals get their own token. */
+function recordingFactory(): McpTokenProviderFactory {
+ return (_server: McpServerInfo, principal: McpPrincipal): McpTokenProvider =>
+ principal.kind === 'shared'
+ ? { resolve: async () => undefined, refresh: async () => undefined }
+ : {
+ resolve: async () => `token-for-${principal.userId}`,
+ refresh: async () => `token-for-${principal.userId}`,
+ }
+}
+
+beforeEach(() => {
+ sdk.connectImpl = null
+})
+
+describe('R4-M1 — a per-user admission never writes the per-serverName status', () => {
+ it('a lazy per-user partition does not flip a down representative to connected', async () => {
+ const manager = new McpManager(undefined, undefined, recordingFactory())
+
+ // The SHARED representative connect fails (transient/down); every subsequent
+ // connect (the per-user partition) succeeds. serverInfos is retained with the
+ // ready status, so a per-user call can still lazily admit its own partition.
+ let representativeDown = true
+ sdk.connectImpl = async () => {
+ if (representativeDown) {
+ representativeDown = false
+ throw new Error('representative down')
+ }
+ }
+
+ await expect(manager.addServer(oauthUserServer())).rejects.toThrow(/representative down/)
+ // Representative admission failed → per-serverName status is `failed`.
+ expect(manager.status.get('gh')?.state).toBe('failed')
+
+ // A per-user call lazily admits alice's partition (which connects fine).
+ const res = await manager.callTool('gh__do', {}, { userId: 'alice' })
+ expect(res.isError).toBe(false)
+
+ // OBSERVABLE (T4): the per-serverName status must NOT have been flipped to
+ // `connected` by alice's per-user admission — the representative is still
+ // down. Before the fix, installConnectedClient called markConnected
+ // unconditionally, reporting the down representative as `connected`.
+ expect(manager.status.get('gh')?.state).toBe('failed')
+ expect(manager.status.get('gh')?.state).not.toBe('connected')
+ })
+})
diff --git a/mcp-host/src/mcp/manager.ts b/mcp-host/src/mcp/manager.ts
index ca931b03f..37bdcf813 100644
--- a/mcp-host/src/mcp/manager.ts
+++ b/mcp-host/src/mcp/manager.ts
@@ -181,7 +181,8 @@ export class McpManager {
private installConnectedClient(
key: string,
serverConfig: McpServerInfo,
- client: McpClient
+ client: McpClient,
+ ownsServerStatus: boolean
): void {
this.clients.set(key, client)
this.serverInfos.set(serverConfig.name, serverConfig)
@@ -193,7 +194,14 @@ export class McpManager {
}
keys.add(key)
- this.statusTracker.markConnected(serverConfig.name, client.availableTools.length)
+ // Only the SHARED/eager owner writes the per-serverName status (symmetric
+ // with the gated markConnecting / recordAdmissionFailure in
+ // connectAndInstall). A per-user admission must NOT flip the representative's
+ // status to `connected` with its own partition's toolCount — a caller reading
+ // the per-serverName status would then see a down representative as connected.
+ if (ownsServerStatus) {
+ this.statusTracker.markConnected(serverConfig.name, client.availableTools.length)
+ }
}
private claimClientCleanup(client: McpClient): McpDetachedServerCleanup | undefined {
@@ -338,7 +346,7 @@ export class McpManager {
return 'stale'
}
this.pendingAdmissions.delete(key)
- this.installConnectedClient(key, serverConfig, client)
+ this.installConnectedClient(key, serverConfig, client, ownsServerStatus)
control.onCommit?.()
console.log(
`[McpManager] Installed client for server "${serverConfig.name}" (${
@@ -415,7 +423,8 @@ export class McpManager {
// Commit is synchronous: readers see either the complete previous
// revision or the complete connected candidate, never a missing server.
- this.installConnectedClient(key, serverConfig, candidate)
+ // replaceServer is always the SHARED/eager coordinator path → owns status.
+ this.installConnectedClient(key, serverConfig, candidate, true)
control.onCommit?.()
// The desired config changed for the whole server → per-user partitions are