Skip to content

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

TypeScript

fix(files): log the original resolver error and check the caller before the references

Alfredo Lopez468c8989550c
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 50564750a..7da4bd368 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.468",
+ "version": "0.1.469",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.468",
+ "version": "0.1.469",
"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 2467f53fa..b67c0c99f 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.468",
+ "version": "0.1.469",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/agent/__tests__/fileReferenceResolver.test.ts b/mcp-host/src/agent/__tests__/fileReferenceResolver.test.ts
index 936eba995..308bc320e 100644
--- a/mcp-host/src/agent/__tests__/fileReferenceResolver.test.ts
+++ b/mcp-host/src/agent/__tests__/fileReferenceResolver.test.ts
@@ -14,6 +14,7 @@ import {
classifyBytes,
} from '@clerum/gfs-interaction-policy'
import { type GfsRuntimeEnv, GfscHttpError, createGfscClient } from '../../internalTools/gfsClient'
+import { logger } from '../../logger'
import { VISUAL_INPUT_LIMITS } from '../../visualInput/policy'
import {
FILE_REFERENCE_AVAILABILITY_CODES,
@@ -351,6 +352,28 @@ describe('resolveFileReferences (#666)', () => {
expect((caught as { cause?: unknown }).cause).toBeInstanceOf(TypeError)
})
+ it('logs the original error class of an unexpected rethrow, not the wrapper', async () => {
+ const original = new TypeError(
+ 'Headers.append: "Bearer secret-token" is an invalid header value.'
+ )
+ const resolve = vi.fn<FileReferenceGfscClient['resolve']>(async () => {
+ throw original
+ })
+ const logged = vi.spyOn(logger, 'error').mockImplementation(() => undefined)
+ try {
+ await expect(resolveFileReferences([gfsReference()], { resolve })).rejects.toThrow(
+ 'file reference resolution failed: unexpected Host error'
+ )
+ // The log call is the witness: the serializer reduces `err` to name, code
+ // and status, so passing the original (not the wrapper) is what keeps the
+ // class visible and the message out.
+ expect(logged).toHaveBeenCalledTimes(1)
+ expect(logged.mock.calls[0]![0]).toEqual({ err: original })
+ } finally {
+ logged.mockRestore()
+ }
+ })
+
it('fails transient on a timeout signal', async () => {
const gfsc = client(() => {
throw new DOMException('The operation timed out.', 'TimeoutError')
diff --git a/mcp-host/src/agent/fileReferenceResolver.ts b/mcp-host/src/agent/fileReferenceResolver.ts
index 714e9f71e..14ffceb2d 100644
--- a/mcp-host/src/agent/fileReferenceResolver.ts
+++ b/mcp-host/src/agent/fileReferenceResolver.ts
@@ -12,6 +12,7 @@ import { type FileReferenceV1, parseFileReferenceV1 } from '@clerum/gfs-interact
import { FileReferenceErrorCode } from '../core/errors'
import type { TurnContextReferencedFile } from '../core/orchestration/turnContext'
import { GfscHttpError } from '../internalTools/gfsClient'
+import { logger } from '../logger'
import { VISUAL_INPUT_LIMITS } from '../visualInput/policy'
export type FileReferenceAvailability =
@@ -318,10 +319,11 @@ export async function resolveFileReferences(
// of a causeless TypeError, and the message route answers error.message
// verbatim. Wrap the rethrow so only this fixed text can leave the Host;
// the cause keeps the original error for a caller that inspects it. The
- // route logs `err` through the Host logger, whose serializer keeps only name,
- // code and status of that Error and does not follow `cause`, so the original
- // message stays out of the log too; the cost is that the log line does not
- // name the original error class.
+ // route logs the wrapper through the Host logger, whose serializer keeps only
+ // name, code and status of an Error and does not follow `cause`, so the
+ // original class would be lost. Log the original here instead: the same
+ // serializer reduces it to name, code and status and drops its message.
+ logger.error({ err: error }, '[FileReferences] unexpected error while resolving references')
throw new Error('file reference resolution failed: unexpected Host error', { cause: error })
} finally {
clearTimeout(timer)
diff --git a/mcp-host/src/server/__tests__/messageRoute.test.ts b/mcp-host/src/server/__tests__/messageRoute.test.ts
index ac5a4f00f..df8deb9c2 100644
--- a/mcp-host/src/server/__tests__/messageRoute.test.ts
+++ b/mcp-host/src/server/__tests__/messageRoute.test.ts
@@ -538,4 +538,27 @@ describe('handleMessageRoute — structured file references (#666)', () => {
})
expect(dispatch).toHaveBeenCalledTimes(1)
})
+
+ it('answers 401 before it looks at the references of an unauthenticated request', async () => {
+ const tooMany = Array.from({ length: FILE_REFERENCE_MAX_COUNT + 1 }, (_, i) => reference(i + 1))
+ const { dispatch, handlers } = route()
+
+ // Witness: with a caller, the same body reaches the reference check and is
+ // refused for its count, so the 401 below is the caller check running first.
+ const authenticated = makeRes()
+ await handleMessageRoute(request(tooMany), authenticated.res, handlers)
+ expect(authenticated.statusCode).toBe(200)
+ expect(authenticated.jsonBody).toMatchObject({
+ success: false,
+ error: { code: 'FILE_REFERENCE_INVALID' },
+ })
+
+ const anonymous = makeRes()
+ const unauthenticatedRequest = request(tooMany)
+ delete (unauthenticatedRequest as { runtimeCaller?: unknown }).runtimeCaller
+ await handleMessageRoute(unauthenticatedRequest, anonymous.res, handlers)
+ expect(anonymous.statusCode).toBe(401)
+ expect(anonymous.jsonBody).toEqual({ error: 'Missing rpc edge caller context' })
+ expect(dispatch).not.toHaveBeenCalled()
+ })
})
diff --git a/mcp-host/src/server/routes.ts b/mcp-host/src/server/routes.ts
index bf1852cf1..e9ca90a9f 100644
--- a/mcp-host/src/server/routes.ts
+++ b/mcp-host/src/server/routes.ts
@@ -316,30 +316,6 @@ export async function handleMessageRoute(
return
}
- // Issue #666 — a malformed reference or an unknown schema version refuses
- // the whole message: the turn must not run without the file it names. The
- // refusal is a 200 `MessageResponse`, like every other admission refusal,
- // so rpc-proxy relays its code instead of reporting a failed upstream.
- const fileReferences = parseIncomingFileReferences(
- message.fileReferences,
- config.fileReferenceMaxCount
- )
- if (!fileReferences.ok) {
- json(res, 200, {
- success: false,
- error: {
- code: fileReferences.code,
- message: fileReferences.message,
- retryable: false,
- provider: 'unknown',
- },
- })
- return
- }
- message.fileReferences = fileReferences.references.length
- ? fileReferences.references
- : undefined
-
// Identity invariant for the desktop / rpc channel: the sender MUST be the
// trusted rpc-proxy edge user, regardless of what the body claims.
// rpc-proxy already does this; re-enforcing here is defense-in-depth for
@@ -377,6 +353,31 @@ export async function handleMessageRoute(
}
}
+ // Issue #666 — after the caller checks, so an unauthenticated request learns
+ // nothing about the reference limit. A malformed reference or an unknown schema version refuses
+ // the whole message: the turn must not run without the file it names. The
+ // refusal is a 200 `MessageResponse`, like every other admission refusal,
+ // so rpc-proxy relays its code instead of reporting a failed upstream.
+ const fileReferences = parseIncomingFileReferences(
+ message.fileReferences,
+ config.fileReferenceMaxCount
+ )
+ if (!fileReferences.ok) {
+ json(res, 200, {
+ success: false,
+ error: {
+ code: fileReferences.code,
+ message: fileReferences.message,
+ retryable: false,
+ provider: 'unknown',
+ },
+ })
+ return
+ }
+ message.fileReferences = fileReferences.references.length
+ ? fileReferences.references
+ : undefined
+
logger.info(
{ channelType: message.channelType, sender: message.sender },
'[Server] Received message'