Skip to content

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

TypeScript

feat(mcp-host): classify provider-authorization failures as unresolved or error

Jose Ramirez8337eaa807c0
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 1b2cdbea5..6447202a1 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.233",
+ "version": "0.1.234",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.233",
+ "version": "0.1.234",
"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 ec5770fb2..52b75c433 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.233",
+ "version": "0.1.234",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/__tests__/providerMessageAuthorization.reason.test.ts b/mcp-host/src/__tests__/providerMessageAuthorization.reason.test.ts
new file mode 100644
index 000000000..a79855faa
--- /dev/null
+++ b/mcp-host/src/__tests__/providerMessageAuthorization.reason.test.ts
@@ -0,0 +1,36 @@
+import { describe, expect, it, vi } from 'vitest'
+import { WorkflowBrokerRequestError } from '../core/tools/workflowBrokerClient'
+import { classifyAuthorizationFailure } from '../main'
+
+describe('classifyAuthorizationFailure', () => {
+ it('maps a 404 medium_account_not_found to unresolved', () => {
+ const err = new WorkflowBrokerRequestError(404, 'medium_account_not_found', 'not found')
+ expect(classifyAuthorizationFailure(err)).toBe('unresolved')
+ })
+
+ it('maps a 403 communication_channel_access_denied to unresolved', () => {
+ const err = new WorkflowBrokerRequestError(403, 'communication_channel_access_denied', 'denied')
+ expect(classifyAuthorizationFailure(err)).toBe('unresolved')
+ })
+
+ it('maps a 500 to error', () => {
+ expect(classifyAuthorizationFailure(new WorkflowBrokerRequestError(500, null, 'boom'))).toBe(
+ 'error'
+ )
+ })
+
+ it('maps a 429 to error', () => {
+ expect(classifyAuthorizationFailure(new WorkflowBrokerRequestError(429, null, 'slow'))).toBe(
+ 'error'
+ )
+ })
+
+ it('maps a network throw to error', () => {
+ expect(classifyAuthorizationFailure(new Error('fetch failed'))).toBe('error')
+ })
+
+ it('maps a 404 with an unrecognized code to error', () => {
+ const err = new WorkflowBrokerRequestError(404, 'something_else', 'nope')
+ expect(classifyAuthorizationFailure(err)).toBe('error')
+ })
+})
diff --git a/mcp-host/src/__tests__/server.interface.contract.test.ts b/mcp-host/src/__tests__/server.interface.contract.test.ts
index 9062fe480..eb33e2aa8 100644
--- a/mcp-host/src/__tests__/server.interface.contract.test.ts
+++ b/mcp-host/src/__tests__/server.interface.contract.test.ts
@@ -683,6 +683,41 @@ describe('RPCServer v1 runtime interface contract', () => {
}
})
+ it('round-trips an unresolved authorization reason through the channel-reader edge', async () => {
+ const onAuthorize = vi.fn(async () => ({ authorized: false, reason: 'unresolved' as const }))
+ const { server, baseUrl } = await startServer(rpcServer => {
+ rpcServer.onProviderMessageAuthorization(onAuthorize)
+ })
+ const providerIdentity = {
+ medium: 'telegram',
+ providerUserId: '123456',
+ providerChannelId: 'telegram-chat-1',
+ providerChannelType: 'private',
+ providerTarget: {
+ hostRef: 'agent-a',
+ communicationChannelNamespace: 'channels',
+ communicationChannelName: 'agent-a-telegram',
+ },
+ }
+
+ try {
+ const response = await fetch(`${baseUrl}/v1/runtime/provider-messages/authorize`, {
+ method: 'POST',
+ headers: channelReaderEdgeHeaders(
+ { channelType: 'telegram', channelId: 'telegram-chat-1', sender: '123456' },
+ { 'Content-Type': 'application/json' }
+ ),
+ body: JSON.stringify({ providerIdentity }),
+ })
+
+ expect(response.status).toBe(200)
+ await expect(response.json()).resolves.toEqual({ authorized: false, reason: 'unresolved' })
+ expect(onAuthorize).toHaveBeenCalledWith({ providerIdentity })
+ } finally {
+ await server.stop()
+ }
+ })
+
it('routes workflow approval notification claim and terminal updates through channel-reader edges', async () => {
const onNotificationClaim = vi.fn(async () => ({
deliveries: [
diff --git a/mcp-host/src/main.ts b/mcp-host/src/main.ts
index 6804f6be5..f0c09ca0a 100644
--- a/mcp-host/src/main.ts
+++ b/mcp-host/src/main.ts
@@ -47,6 +47,7 @@ import { FsSpilloverResolver } from './core/spillover/fsResolver'
import { isInternalGeneratedArtifactAttachment } from './core/tools/generatedArtifactAttachments'
import { NativeToolRegistry } from './core/tools/nativeToolRegistry'
import { WorkflowResultTool } from './core/tools/workflow'
+import { WorkflowBrokerRequestError } from './core/tools/workflowBrokerClient.js'
import type { Attachment } from './core/types'
import { ConversationState } from './core/types'
import { wireActivityEvents } from './eventWiring'
@@ -1787,9 +1788,30 @@ async function handleProviderWorkflowApprovalDecision(
return submitProviderWorkflowApprovalDecision(decision, runtimeAuth)
}
+const UNRESOLVED_BROKER_CODES = new Set([
+ 'medium_account_not_found',
+ 'communication_channel_access_denied',
+])
+
+/**
+ * An unlinked or access-denied identity reaches us as a THROWN 404/403 from the
+ * broker, not as a null context. Only these map to 'unresolved'; everything else
+ * (5xx, timeouts, network failures, unrecognized codes) is 'error', which keeps
+ * channel-reader silent so a control-api outage never tells linked users they are
+ * unlinked.
+ */
+export function classifyAuthorizationFailure(error: unknown): 'unresolved' | 'error' {
+ if (error instanceof WorkflowBrokerRequestError) {
+ if ((error.status === 404 || error.status === 403) && error.code) {
+ return UNRESOLVED_BROKER_CODES.has(error.code) ? 'unresolved' : 'error'
+ }
+ }
+ return 'error'
+}
+
async function handleProviderMessageAuthorization(
input: ProviderMessageAuthorization
-): Promise<{ authorized: boolean }> {
+): Promise<{ authorized: boolean; reason?: 'unresolved' | 'error' }> {
const identity = input.providerIdentity
try {
const context = await resolveProviderWorkflowCallerContext(
@@ -1805,14 +1827,22 @@ async function handleProviderMessageAuthorization(
},
key => process.env[key]
)
- return { authorized: Boolean(context?.targetUserId) }
+ if (context?.targetUserId) {
+ return { authorized: true }
+ }
+ // A null context is unreachable for validated Slack traffic: channel-reader's
+ // handoff validation already rejects malformed provider identities before this
+ // runs. Treat it as 'error' (fail closed, stay silent) rather than invent a
+ // reason we cannot substantiate.
+ return { authorized: false, reason: 'error' }
} catch (error) {
+ const reason = classifyAuthorizationFailure(error)
console.warn(
- `[Main] Provider message authorization failed closed: ${
+ `[Main] Provider message authorization failed closed (${reason}): ${
error instanceof Error ? error.message : String(error)
}`
)
- return { authorized: false }
+ return { authorized: false, reason }
}
}
diff --git a/mcp-host/src/server/types.ts b/mcp-host/src/server/types.ts
index f704b3acb..4d688bb87 100644
--- a/mcp-host/src/server/types.ts
+++ b/mcp-host/src/server/types.ts
@@ -330,7 +330,7 @@ export type ProviderWorkflowResultRequestHandler = (
) => Promise<MessageResponse>
export type ProviderMessageAuthorizationHandler = (
input: ProviderMessageAuthorization
-) => Promise<{ authorized: boolean }>
+) => Promise<{ authorized: boolean; reason?: 'unresolved' | 'error' }>
export type WorkflowApprovalNotificationClaimHandler = (
input: WorkflowApprovalNotificationClaim
) => Promise<{ deliveries: WorkflowApprovalNotificationDelivery[] } | { error: string }>