Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(mcp-host): verify GFS vision through provider capabilities
Alfredo Lopez1de397f19274
diff --git a/desktop-app/test/e2e-playwright/gfs-agent-image-read.test.ts b/desktop-app/test/e2e-playwright/gfs-agent-image-read.test.tsindex d1523f103..ab1b84b8c 100644--- a/desktop-app/test/e2e-playwright/gfs-agent-image-read.test.ts+++ b/desktop-app/test/e2e-playwright/gfs-agent-image-read.test.ts@@ -6,6 +6,7 @@* Folder/grant/account seeding is a named precondition, never file upload.*/import { type Page, _electron as electron, expect, test } from '@playwright/test'+import { execFileSync } from 'node:child_process'import { randomUUID } from 'node:crypto'import { mkdtemp, rm, writeFile } from 'node:fs/promises'import os from 'node:os'@@ -14,6 +15,7 @@ import {cleanupGfsFixture,getE2EUserId,getGfsChildResourceSummary,+ kubectlOut,seedGfsDirectoryFixture,seedGfsGrant,uniqueGfsFixtureName,@@ -43,6 +45,101 @@ function requireOwnedRuntime(): void {}}+/** Named precondition: choose an available model using existing runtime credentials.+ * Mutations re-enter T2's validated lease. No image, answer, or session is seeded.+ */+function visualModelFixture(agent: ReturnType<typeof discoverManagedGfsAgent>) {+ const provider = process.env.E2E_GFS_VISUAL_PROVIDER+ const model = process.env.E2E_GFS_VISUAL_MODEL+ if (!provider && !model) return undefined+ if (!provider || !model) throw new Error('Visual model fixture needs both provider and model')+ const read = () => {+ const [uid, currentProvider, currentModel] = kubectlOut([+ '-n',+ agent.namespace,+ 'get',+ 'host',+ agent.name,+ '-o',+ 'jsonpath={.metadata.uid}{"\\t"}{.spec.model.provider}{"\\t"}{.spec.model.name}',+ ])+ .trim()+ .split('\t')+ if (!uid || !currentProvider || !currentModel)+ throw new Error('Host model fixture identity is incomplete')+ return { uid, provider: currentProvider, model: currentModel }+ }+ const original = read()+ const selected = { ...original, provider, model }+ const mutate = (from: typeof original, to: typeof original) => {+ const root = path.resolve(__dirname, '../../..')+ execFileSync(+ 'bash',+ [+ path.join(root, 'scripts/minikube/with-t2-mutation-lock.sh'),+ '--',+ 'kubectl',+ `--context=${process.env.E2E_K8S_CONTEXT}`,+ '--request-timeout=15s',+ '-n',+ agent.namespace,+ 'patch',+ 'host',+ agent.name,+ '--type=json',+ '-p',+ JSON.stringify([+ { op: 'test', path: '/metadata/uid', value: from.uid },+ { op: 'test', path: '/spec/model/provider', value: from.provider },+ { op: 'test', path: '/spec/model/name', value: from.model },+ { op: 'replace', path: '/spec/model/provider', value: to.provider },+ { op: 'replace', path: '/spec/model/name', value: to.model },+ ]),+ ],+ { cwd: root, timeout: 45_000, stdio: ['ignore', 'pipe', 'pipe'] }+ )+ }+ return {+ apply: () => mutate(original, selected),+ restore: () => {+ const current = read()+ if (+ current.uid === original.uid &&+ current.provider === original.provider &&+ current.model === original.model+ )+ return+ // The JSON tests refuse to overwrite a concurrent user's different choice.+ mutate(selected, original)+ },+ }+}++async function waitForAgentBinding(+ agent: ReturnType<typeof discoverManagedGfsAgent>+): Promise<void> {+ await expect+ .poll(+ () => {+ try {+ return discoverManagedGfsAgent()+ } catch (error) {+ // During a model change HCC must rebind the same Host generation. Retry+ // only the missing-binding state; actual Kubernetes failures stay loud.+ if (+ error instanceof Error &&+ (error.message === `HCC agent "${agent.name}" is missing or ambiguous` ||+ error.message === 'GFS agent E2E requires one unambiguous HCC-managed agent name')+ )+ return null+ throw error+ }+ },+ { timeout: 120_000 }+ )+ .toEqual(agent)+}+async function visibleLogin(page: Page): Promise<void> {const email = page.locator('#email-input')const settings = page.getByTestId('nav-settings-menu')@@ -157,6 +254,7 @@ function parseVisualAnswer(raw: string) {}test('GFS image bytes reach vision after visible upload; a host without a grant cannot read another image', async () => {+ test.setTimeout(600_000)requireOwnedRuntime()assertGfsInfraHealthy()const agent = discoverManagedGfsAgent()@@ -167,7 +265,11 @@ test('GFS image bytes reach vision after visible upload; a host without a grantconst seeded: string[] = []const local = await mkdtemp(path.join(os.tmpdir(), 'evenfire-gfs-visual-'))let app: Awaited<ReturnType<typeof electron.launch>> | undefined+ let modelFixture: ReturnType<typeof visualModelFixture>try {+ modelFixture = visualModelFixture(agent)+ modelFixture?.apply()+ if (modelFixture) await waitForAgentBinding(agent)const granted = seedGfsDirectoryFixture(uniqueGfsFixtureName('e2e-gfs-visual'))seeded.push(granted.name)const denied = seedGfsDirectoryFixture(uniqueGfsFixtureName('e2e-gfs-visual-denied'))@@ -292,6 +394,12 @@ test('GFS image bytes reach vision after visible upload; a host without a grant} catch (error) {cleanupErrors.push(error)}+ try {+ modelFixture?.restore()+ if (modelFixture) await waitForAgentBinding(agent)+ } catch (error) {+ cleanupErrors.push(error)+ }if (cleanupErrors.length)throw new Error(`GFS visual fixture cleanup failed in ${cleanupErrors.length} operations`)}diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.jsonindex 859359182..238251a96 100644--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-mcp-host",- "version": "0.1.338",+ "version": "0.1.339","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.338",+ "version": "0.1.339","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --git a/mcp-host/package.json b/mcp-host/package.jsonindex 74e253d43..7ab100ca0 100644--- a/mcp-host/package.json+++ b/mcp-host/package.json@@ -1,6 +1,6 @@{"name": "clerum-mcp-host",- "version": "0.1.338",+ "version": "0.1.339","description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude","main": "dist/main.js","engines": {diff --git a/mcp-host/src/internalTools/gfsImageInput.integration.test.ts b/mcp-host/src/internalTools/gfsImageInput.integration.test.tsindex dd35a5cc1..6bff95a76 100644--- a/mcp-host/src/internalTools/gfsImageInput.integration.test.ts+++ b/mcp-host/src/internalTools/gfsImageInput.integration.test.ts@@ -1,12 +1,16 @@import { afterEach, describe, expect, it, vi } from 'vitest'import { createCanvas } from '@napi-rs/canvas'+import { Readable } from 'node:stream'import { LlmPortAdapter } from '../core/adapters/llmPortAdapter'import type { NativeToolConfig } from '../core/interfaces'import { appendToolResults } from '../core/orchestration/toolUseLoopMessages'import { NativeToolRegistry } from '../core/tools/nativeToolRegistry'import type { Attachment, ChatMessage, ToolResult } from '../core/types'+import { ClaudeProvider } from '../llm/claude'import { OpenAIProvider } from '../llm/openai'import { OpenAICompatibleProvider } from '../llm/openaiCompatible'+import type { LlmProvider } from '../llm/registryCore'+import type { SingleTurnProvider } from '../llm/types'import { VisualInputBudget } from '../visualInput/policy'import { projectGfsApproval } from '../visualInput/suspension'@@ -43,7 +47,12 @@ const config: NativeToolConfig = {async function setup(bytes: Buffer,- options: { name?: string; modalities?: string[]; missing?: boolean } = {}+ options: {+ name?: string+ modalities?: string[]+ missing?: boolean+ llm?: { provider: SingleTurnProvider; model: string; name: LlmProvider }+ } = {}) {const { createGfscClient } = await vi.importActual<typeof import('./gfsClient')>('./gfsClient')const gfsFetch = vi.fn(async (url: string) =>@@ -99,20 +108,22 @@ async function setup(choices: [{ message: { content: 'Done' }, finish_reason: 'stop' }],usage: { prompt_tokens: 10, completion_tokens: 2, total_tokens: 12 },})- const provider = new OpenAICompatibleProvider(- {- id: 'openrouter',- baseURL: 'https://openrouter.ai/api/v1',- defaultModel: model,- },- 'integration-only-provider-identity',- model- )+ const provider =+ options.llm?.provider ??+ new OpenAICompatibleProvider(+ {+ id: 'openrouter',+ baseURL: 'https://openrouter.ai/api/v1',+ defaultModel: model,+ },+ 'integration-only-provider-identity',+ model+ )const budget = new VisualInputBudget()const adapter = new LlmPortAdapter(provider,- model,- 'openrouter',+ options.llm?.model ?? model,+ options.llm?.name ?? 'openrouter',undefined,undefined,undefined,@@ -154,6 +165,87 @@ afterEach(() => {})describe('GFS bytes to actual provider request', () => {+ it.each([+ ['image/png', false],+ ['image/jpeg', false],+ ['image/png', true],+ ['image/jpeg', true],+ ] as const)(+ 'delivers %s through Claude with cache=%s without OpenRouter',+ async (mime, cache) => {+ const targetModel = 'claude-sonnet-4-6'+ // Credential-free SDK boundary double. The production provider serializer,+ // raw metadata stream bridge, client/tool registry and loop still execute.+ const create = vi.fn(async () => ({+ content: [{ type: 'text', text: 'Done' }],+ stop_reason: 'end_turn',+ usage: { input_tokens: 10, output_tokens: 2 },+ }))+ const buildRequest = vi.fn(({ path }) => ({+ url: `https://api.anthropic.com${path}`,+ req: { method: 'GET' },+ }))+ const metadata = JSON.stringify({+ type: 'model',+ id: targetModel,+ capabilities: { image_input: { supported: true } },+ })+ const fetchWithTimeout = vi.fn(async () => ({+ status: 200,+ redirected: false,+ // Exercise the SDK's Node body and an injected native-fetch Web body.+ body: cache ? new Response(metadata).body : Readable.from([Buffer.from(metadata)]),+ }))+ const client = { messages: { create }, buildRequest, fetchWithTimeout }+ const canvas = createCanvas(8, 8)+ canvas.getContext('2d').fillRect(1, 1, 4, 4)+ const bytes =+ mime === 'image/png' ? canvas.toBuffer('image/png') : canvas.toBuffer('image/jpeg')+ const subject = await setup(bytes, {+ llm: {+ provider: new ClaudeProvider(client as never, targetModel),+ model: targetModel,+ name: 'claude',+ },+ })+ expect(subject.output.is_error).toBe(false)+ await subject.adapter.completeWithTools({+ messages: subject.messages,+ tools: [],+ ...(cache+ ? {+ systemPromptParts: {+ stable: 'system instructions',+ context: 'context',+ stableHash: 'fixture',+ contextHash: 'fixture',+ },+ }+ : {}),+ })+ expect(subject.metadataFetch).not.toHaveBeenCalled()+ expect(buildRequest).toHaveBeenCalledWith({+ method: 'get',+ path: `/v1/models/${targetModel}`,+ })+ expect(fetchWithTimeout).toHaveBeenCalledOnce()+ const request = (create.mock.calls as unknown as Array<[Record<string, any>]>)[0][0]+ const blocks = request.messages.flatMap((m: any) =>+ Array.isArray(m.content) ? m.content : []+ )+ expect(blocks.filter((b: any) => b.type === 'image')).toEqual([+ {+ type: 'image',+ source: { type: 'base64', media_type: mime, data: bytes.toString('base64') },+ },+ ])+ expect(JSON.stringify(blocks.filter((b: any) => b.type !== 'image'))).not.toContain(+ bytes.toString('base64')+ )+ if (cache) expect(request.system[0].cache_control).toEqual({ type: 'ephemeral' })+ subject.budget.close()+ }+ )it.each(['unchanged', 'replaced', 'revoked'] as const)('requires a new authorized read after suspension when the resource is %s',async state => {diff --git a/mcp-host/src/llm/__tests__/claude.singleTurn.test.ts b/mcp-host/src/llm/__tests__/claude.singleTurn.test.tsindex 00702605d..aed7e1867 100644--- a/mcp-host/src/llm/__tests__/claude.singleTurn.test.ts+++ b/mcp-host/src/llm/__tests__/claude.singleTurn.test.ts@@ -1,7 +1,38 @@import { describe, expect, it, vi } from 'vitest'import { type ChatMessage, FinishReason } from '../../core/types'+import { VISUAL_INPUT_LIMITS } from '../../visualInput/policy'import { ClaudeProvider } from '../claude'+describe('Claude visual request limits', () => {+ it.each([false, true])('bounds the final serialized request with cache=%s', async cache => {+ const client = createMockClaudeClient()+ const provider = new ClaudeProvider(client as never, 'claude-sonnet-4-6')+ // Only serialization size is under test; decoding occurs upstream.+ const messages: ChatMessage[] = [+ {+ role: 'user',+ content: '',+ contentParts: [{ type: 'image', mimeType: 'image/png', data: 'AA==' }],+ },+ ]+ const large = 'x'.repeat(VISUAL_INPUT_LIMITS.requestBytes)+ const operation = cache+ ? provider.completeSingleTurnWithToolsAndCache(+ { stable: large, context: '', stableHash: 'fixture', contextHash: 'fixture' },+ messages,+ [],+ { verifyImageInput: true }+ )+ : provider.completeSingleTurnWithTools(+ messages,+ [{ name: 'large_schema', description: large, parameters: {} }],+ { verifyImageInput: true }+ )+ await expect(operation).rejects.toMatchObject({ code: 'limit_exceeded' })+ expect(client.messages.create).not.toHaveBeenCalled()+ })+})+function createMockClaudeClient() {return {messages: {diff --git a/mcp-host/src/llm/claude.ts b/mcp-host/src/llm/claude.tsindex 60c39a351..7aba12873 100644--- a/mcp-host/src/llm/claude.ts+++ b/mcp-host/src/llm/claude.ts@@ -2,6 +2,7 @@* Anthropic Claude LLM provider with tool/function calling support.*/import Anthropic from '@anthropic-ai/sdk'+import { Readable } from 'node:stream'import { LlmErrorCode } from '../core/errors'import type { SystemPromptParts } from '../core/reasoning/systemPrompt'import {@@ -16,6 +17,10 @@ import {ToolCompletionResponse,ToolDefinition,} from '../core/types'+import { logger } from '../logger'+import { createAnthropicImageCapabilityResolver } from '../visualInput/capabilities'+import { type ImageInputCapability, VISUAL_INPUT_LIMITS } from '../visualInput/policy'+import { assertVisualRequestFits } from '../visualInput/requestPolicy'import { convertToClaudeMessages, separateSystemMessage } from './claude/messageTranslate'import { classifyByHttpStatus, classifyUnknown } from './errorClassification'import type { LlmProvider } from './registryCore'@@ -24,6 +29,7 @@ import type { ClassifiedError, SingleTurnProvider } from './types'export class ClaudeProvider implements SingleTurnProvider {private client: Anthropicprivate defaultModel: string+ private readonly imageCapabilityResolver: (signal?: AbortSignal) => Promise<ImageInputCapability>constructor(apiKeyOrClient: string | Anthropic, defaultModel: string = 'claude-sonnet-4-6') {if (typeof apiKeyOrClient === 'string') {@@ -32,7 +38,50 @@ export class ClaudeProvider implements SingleTurnProvider {this.client = apiKeyOrClient}this.defaultModel = defaultModel- console.log(`[Claude] Initialized with model: ${defaultModel}`)+ this.imageCapabilityResolver = createAnthropicImageCapabilityResolver(+ defaultModel,+ async (path, signal) => {+ // Use this exact SDK connection/authentication, but consume raw transport+ // before SDK error parsing, which would otherwise read an unbounded body.+ const { url, req } = this.client.buildRequest({ method: 'get', path })+ const controller = new AbortController()+ const abort = () => controller.abort()+ signal.addEventListener('abort', abort, { once: true })+ try {+ signal.throwIfAborted()+ const response = await this.client.fetchWithTimeout(+ url,+ { ...req, signal: undefined, redirect: 'error' },+ VISUAL_INPUT_LIMITS.validationTimeoutMs,+ controller+ )+ if (response.status !== 200)+ logger.warn(+ { provider: 'claude', model: defaultModel, status: response.status },+ 'Model capability lookup failed'+ )+ // SDK 0.32 defaults to node-fetch, while injected fetch implementations+ // may return Web streams. Cancellation of the Node bridge destroys its body.+ const body =+ response.body instanceof Readable+ ? Readable.toWeb(response.body, {+ strategy: {+ highWaterMark: VISUAL_INPUT_LIMITS.metadataBytes,+ size: chunk => chunk.byteLength,+ },+ })+ : response.body+ return { status: response.status, redirected: response.redirected, body }+ } finally {+ signal.removeEventListener('abort', abort)+ }+ }+ )+ logger.info({ model: defaultModel }, 'Claude transport initialized')+ }++ getImageInputCapability(signal?: AbortSignal): Promise<ImageInputCapability> {+ return this.imageCapabilityResolver(signal)}/**@@ -188,6 +237,7 @@ export class ClaudeProvider implements SingleTurnProvider {temperature?: numbertool_choice?: stringsignal?: AbortSignal+ verifyImageInput?: boolean}): Promise<ToolCompletionResponse> {const { systemPrompt, claudeMessages } = this.separateSystemMessage(messages)@@ -199,17 +249,16 @@ export class ClaudeProvider implements SingleTurnProvider {input_schema: t.parameters as Anthropic.Tool['input_schema'],}))- const response = await this.client.messages.create(- {- model: this.defaultModel,- system: systemPrompt || undefined,- messages: formattedMessages,- tools: claudeTools.length > 0 ? claudeTools : undefined,- max_tokens: options?.max_tokens ?? 4096,- temperature: options?.temperature,- },- { signal: options?.signal }- )+ const request: Anthropic.MessageCreateParamsNonStreaming = {+ model: this.defaultModel,+ system: systemPrompt || undefined,+ messages: formattedMessages,+ tools: claudeTools.length > 0 ? claudeTools : undefined,+ max_tokens: options?.max_tokens ?? 4096,+ temperature: options?.temperature,+ }+ assertVisualRequestFits(messages, request, options?.verifyImageInput === true)+ const response = await this.client.messages.create(request, { signal: options?.signal })const textContent =response.content@@ -260,6 +309,7 @@ export class ClaudeProvider implements SingleTurnProvider {temperature?: numbertool_choice?: stringsignal?: AbortSignal+ verifyImageInput?: boolean}): Promise<ToolCompletionResponse> {const formattedMessages = this.convertToClaudeMessages(messages)@@ -288,17 +338,16 @@ export class ClaudeProvider implements SingleTurnProvider {} as Anthropic.TextBlockParam)}- const response = await this.client.messages.create(- {- model: this.defaultModel,- system: systemBlocks.length > 0 ? systemBlocks : undefined,- messages: formattedMessages,- tools: claudeTools.length > 0 ? claudeTools : undefined,- max_tokens: options?.max_tokens ?? 4096,- temperature: options?.temperature,- },- { signal: options?.signal }- )+ const request: Anthropic.MessageCreateParamsNonStreaming = {+ model: this.defaultModel,+ system: systemBlocks.length > 0 ? systemBlocks : undefined,+ messages: formattedMessages,+ tools: claudeTools.length > 0 ? claudeTools : undefined,+ max_tokens: options?.max_tokens ?? 4096,+ temperature: options?.temperature,+ }+ assertVisualRequestFits(messages, request, options?.verifyImageInput === true)+ const response = await this.client.messages.create(request, { signal: options?.signal })const textContent =response.contentdiff --git a/mcp-host/src/visualInput/capabilities.test.ts b/mcp-host/src/visualInput/capabilities.test.tsindex c932ed0ba..02875d89e 100644--- a/mcp-host/src/visualInput/capabilities.test.ts+++ b/mcp-host/src/visualInput/capabilities.test.ts@@ -1,9 +1,15 @@import { afterEach, describe, expect, it, vi } from 'vitest'import {+ ANTHROPIC_CAPABILITY_CACHE_MS,+ ANTHROPIC_METADATA_EVIDENCE,+ ANTHROPIC_PROVIDER,+ type AnthropicMetadataReader,+ type ModelMetadataResponse,OPENROUTER_CAPABILITY_CACHE_MS,OPENROUTER_METADATA_EVIDENCE,OPENROUTER_PROVIDER,type OpenRouterMetadataReader,+ createAnthropicImageCapabilityResolver,createOpenRouterImageCapabilityResolver,} from './capabilities'import { VISUAL_INPUT_LIMITS, VisualInputError } from './policy'@@ -407,3 +413,346 @@ describe('OpenRouter image capability resolver', () => {expect(readB.mock.calls[0][0]).toBe('/model/anthropic/claude-sonnet-4')})})++const CLAUDE_MODEL = 'claude-sonnet-4-5'+const CLAUDE_MODEL_PATH = '/v1/models/claude-sonnet-4-5'++/** The documented `GET /v1/models/{model_id}` body, with a controllable flag. */+function anthropicModel(id: string, supported: unknown): Response {+ return new Response(+ JSON.stringify({ type: 'model', id, capabilities: { image_input: { supported } } }),+ { status: 200 }+ )+}++describe('Anthropic image capability resolver', () => {+ it('requests the exact model path and reports supported from the documented flag', async () => {+ const readMetadata = vi.fn<AnthropicMetadataReader>(async () =>+ anthropicModel(CLAUDE_MODEL, true)+ )+ const resolveCapability = createAnthropicImageCapabilityResolver(CLAUDE_MODEL, readMetadata)++ await expect(resolveCapability()).resolves.toEqual({+ status: 'supported',+ provider: 'claude',+ model: CLAUDE_MODEL,+ evidence: 'anthropic-model-metadata',+ })++ expect(readMetadata).toHaveBeenCalledTimes(1)+ expect(readMetadata.mock.calls[0][0]).toBe(CLAUDE_MODEL_PATH)+ expect(readMetadata.mock.calls[0][1]).toBeInstanceOf(AbortSignal)+ // The transport receives the path and a per-call signal only: no headers, no key.+ expect(readMetadata.mock.calls[0]).toHaveLength(2)+ expect(ANTHROPIC_PROVIDER).toBe('claude')+ expect(ANTHROPIC_METADATA_EVIDENCE).toBe('anthropic-model-metadata')+ expect(ANTHROPIC_CAPABILITY_CACHE_MS).toBe(60_000)+ })++ it('accepts an injected structural response, not only a native Response', async () => {+ const payload = JSON.stringify({+ type: 'model',+ id: CLAUDE_MODEL,+ capabilities: { image_input: { supported: true } },+ })+ const bridged: ModelMetadataResponse = {+ status: 200,+ redirected: false,+ body: new ReadableStream<Uint8Array>({+ start(controller) {+ controller.enqueue(new TextEncoder().encode(payload))+ controller.close()+ },+ }),+ }+ const readMetadata = vi.fn<AnthropicMetadataReader>(async () => bridged)++ await expect(+ createAnthropicImageCapabilityResolver(CLAUDE_MODEL, readMetadata)()+ ).resolves.toMatchObject({ status: 'supported', model: CLAUDE_MODEL })+ })++ it('keeps a reserved-character model id inside one path segment', async () => {+ const model = 'claude-sonnet-4.5?x=1#frag'+ const readMetadata = vi.fn<AnthropicMetadataReader>(async () => anthropicModel(model, true))++ await expect(+ createAnthropicImageCapabilityResolver(model, readMetadata)()+ ).resolves.toMatchObject({ status: 'supported' })++ const path = readMetadata.mock.calls[0][0]+ expect(path).toBe(`/v1/models/${encodeURIComponent(model)}`)+ expect(path.slice('/v1/models/'.length)).not.toContain('/')+ expect(path).not.toContain('?')+ expect(path).not.toContain('#')+ })++ it('encodes a slash in the model id instead of creating another route', async () => {+ const model = 'models/../admin'+ const readMetadata = vi.fn<AnthropicMetadataReader>(async () => anthropicModel(model, true))++ await expect(+ createAnthropicImageCapabilityResolver(model, readMetadata)()+ ).resolves.toMatchObject({ status: 'supported' })++ const path = readMetadata.mock.calls[0][0]+ expect(path).toBe('/v1/models/models%2F..%2Fadmin')+ // '' , 'v1', 'models', the encoded id: the id never becomes its own segment.+ expect(path.split('/')).toHaveLength(4)+ })++ it('refuses empty, dot, and control identifiers without reading metadata', async () => {+ const readMetadata = vi.fn<AnthropicMetadataReader>(async () =>+ anthropicModel(CLAUDE_MODEL, true)+ )+ const rejected = [+ '',+ '.',+ '..',+ ' ..',+ '.. ',+ 'claude sonnet 4',+ 'claude-sonnet-4\t',+ `claude-sonnet-4${String.fromCharCode(0)}`,+ 'claude-sonnet-4ü',+ ]++ for (const model of rejected) {+ await expect(createAnthropicImageCapabilityResolver(model, readMetadata)()).resolves.toEqual({+ status: 'unknown',+ })+ }+ expect(readMetadata).not.toHaveBeenCalled()+ })++ it('reports an explicit false flag as unsupported and caches it', async () => {+ const readMetadata = vi.fn<AnthropicMetadataReader>(async () =>+ anthropicModel(CLAUDE_MODEL, false)+ )+ const resolveCapability = createAnthropicImageCapabilityResolver(CLAUDE_MODEL, readMetadata)++ await expect(resolveCapability()).resolves.toEqual({ status: 'unsupported' })+ await expect(resolveCapability()).resolves.toEqual({ status: 'unsupported' })+ expect(readMetadata).toHaveBeenCalledTimes(1)+ })++ it('returns unknown for absent, mismatched, or malformed metadata', async () => {+ const id = JSON.stringify(CLAUDE_MODEL)+ const bodies = [+ '',+ 'not json',+ '{}',+ '[]',+ 'null',+ '{"type":"model"}',+ '{"type":"error","error":{"type":"not_found_error"}}',+ '{"type":"model","id":"claude-opus-4-1","capabilities":{"image_input":{"supported":true}}}',+ `{"id":${id},"capabilities":{"image_input":{"supported":true}}}`,+ `{"type":"model","id":${id}}`,+ `{"type":"model","id":${id},"capabilities":null}`,+ `{"type":"model","id":${id},"capabilities":{}}`,+ `{"type":"model","id":${id},"capabilities":{"image_input":null}}`,+ `{"type":"model","id":${id},"capabilities":{"image_input":{}}}`,+ `{"type":"model","id":${id},"capabilities":{"image_input":{"supported":"true"}}}`,+ `{"type":"model","id":${id},"capabilities":{"image_input":{"supported":null}}}`,+ ]++ for (const body of bodies) {+ const readMetadata = vi.fn<AnthropicMetadataReader>(+ async () => new Response(body, { status: 200 })+ )+ await expect(+ createAnthropicImageCapabilityResolver(CLAUDE_MODEL, readMetadata)()+ ).resolves.toEqual({ status: 'unknown' })+ }+ })++ it('returns unknown for a non-200 response or a followed redirect and releases the body', async () => {+ const failed = trackedStream()+ const readFailed = vi.fn<AnthropicMetadataReader>(+ async () => new Response(failed.stream, { status: 404 })+ )+ await expect(+ createAnthropicImageCapabilityResolver(CLAUDE_MODEL, readFailed)()+ ).resolves.toEqual({ status: 'unknown' })+ expect(failed.wasCancelled()).toBe(true)++ const redirected = trackedStream()+ const readRedirected = vi.fn<AnthropicMetadataReader>(async () => ({+ status: 200,+ redirected: true,+ body: redirected.stream,+ }))+ await expect(+ createAnthropicImageCapabilityResolver(CLAUDE_MODEL, readRedirected)()+ ).resolves.toEqual({ status: 'unknown' })+ expect(redirected.wasCancelled()).toBe(true)+ })++ it('caps the metadata body at 64 KiB and cancels an oversized read', async () => {+ const oversized = trackedStream(METADATA_BYTES + 1)+ const readOversized = vi.fn<AnthropicMetadataReader>(+ async () => new Response(oversized.stream, { status: 200 })+ )+ await expect(+ createAnthropicImageCapabilityResolver(CLAUDE_MODEL, readOversized)()+ ).resolves.toEqual({ status: 'unknown' })+ expect(oversized.wasCancelled()).toBe(true)++ // A complete body of exactly the cap is still read.+ const base = JSON.stringify({+ type: 'model',+ id: CLAUDE_MODEL,+ capabilities: { image_input: { supported: true } },+ })+ const padded = base.padEnd(METADATA_BYTES, ' ')+ expect(padded.length).toBe(METADATA_BYTES)+ const readAtCap = vi.fn<AnthropicMetadataReader>(+ async () => new Response(padded, { status: 200 })+ )+ await expect(+ createAnthropicImageCapabilityResolver(CLAUDE_MODEL, readAtCap)()+ ).resolves.toMatchObject({ status: 'supported' })+ })++ it('returns unknown when the shared per-call deadline elapses', async () => {+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'Date'] })+ let observed: AbortSignal | undefined+ const readMetadata = vi.fn<AnthropicMetadataReader>((_path, signal) => {+ observed = signal+ return new Promise<ModelMetadataResponse>(() => undefined)+ })+ const pending = createAnthropicImageCapabilityResolver(CLAUDE_MODEL, readMetadata)()++ expect(DEADLINE_MS).toBe(5_000)+ await vi.advanceTimersByTimeAsync(DEADLINE_MS)+ await expect(pending).resolves.toEqual({ status: 'unknown' })+ expect(observed?.aborted).toBe(true)+ })++ it('reports cancellation for an aborted caller and aborts its in-flight read', async () => {+ const controller = new AbortController()+ let observed: AbortSignal | undefined+ const readMetadata = vi.fn<AnthropicMetadataReader>((_path, signal) => {+ observed = signal+ return new Promise<ModelMetadataResponse>(() => undefined)+ })+ const pending = createAnthropicImageCapabilityResolver(+ CLAUDE_MODEL,+ readMetadata+ )(controller.signal)++ controller.abort()+ const failure: unknown = await pending.then(+ () => undefined,+ (reason: unknown) => reason+ )+ expect(failure).toBeInstanceOf(VisualInputError)+ expect(failure).toMatchObject({ code: 'cancelled' })+ expect(observed?.aborted).toBe(true)+ })++ it('fails an already-aborted caller before touching the transport', async () => {+ const controller = new AbortController()+ controller.abort()+ const readMetadata = vi.fn<AnthropicMetadataReader>(async () =>+ anthropicModel(CLAUDE_MODEL, true)+ )++ await expect(+ createAnthropicImageCapabilityResolver(CLAUDE_MODEL, readMetadata)(controller.signal)+ ).rejects.toMatchObject({ code: 'cancelled' })+ expect(readMetadata).not.toHaveBeenCalled()+ })++ it('cancels the in-flight body when the caller aborts', async () => {+ const controller = new AbortController()+ const body = trackedStream()+ const readMetadata = vi.fn<AnthropicMetadataReader>(+ async () => new Response(body.stream, { status: 200 })+ )+ const pending = createAnthropicImageCapabilityResolver(+ CLAUDE_MODEL,+ readMetadata+ )(controller.signal)++ await flushMicrotasks()+ controller.abort()++ await expect(pending).rejects.toMatchObject({ code: 'cancelled' })+ expect(body.wasCancelled()).toBe(true)+ })++ it('gives every call its own signal and never shares the caller signal with the transport', async () => {+ const controller = new AbortController()+ const gate = deferred<void>()+ const signals: AbortSignal[] = []+ const readMetadata = vi.fn<AnthropicMetadataReader>(async (_path, signal) => {+ signals.push(signal)+ await gate.promise+ return anthropicModel(CLAUDE_MODEL, true)+ })+ const resolveCapability = createAnthropicImageCapabilityResolver(CLAUDE_MODEL, readMetadata)+ const first = resolveCapability(controller.signal)+ const second = resolveCapability(controller.signal)++ expect(readMetadata).toHaveBeenCalledTimes(2)+ expect(signals[0]).not.toBe(controller.signal)+ expect(signals[1]).not.toBe(controller.signal)+ expect(signals[0]).not.toBe(signals[1])++ gate.resolve(undefined)+ await expect(first).resolves.toMatchObject({ status: 'supported' })+ await expect(second).resolves.toMatchObject({ status: 'supported' })+ })++ it('serves a cached answer for 60 seconds and refreshes afterwards', async () => {+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'Date'] })+ const readMetadata = vi.fn<AnthropicMetadataReader>(async () =>+ anthropicModel(CLAUDE_MODEL, true)+ )+ const resolveCapability = createAnthropicImageCapabilityResolver(CLAUDE_MODEL, readMetadata)++ await resolveCapability()+ vi.advanceTimersByTime(59_999)+ await resolveCapability()+ expect(readMetadata).toHaveBeenCalledTimes(1)++ vi.advanceTimersByTime(1)+ await resolveCapability()+ expect(readMetadata).toHaveBeenCalledTimes(2)+ })++ it('never caches an unknown answer', async () => {+ const readMetadata = vi.fn<AnthropicMetadataReader>(+ async () => new Response(null, { status: 500 })+ )+ const resolveCapability = createAnthropicImageCapabilityResolver(CLAUDE_MODEL, readMetadata)++ await expect(resolveCapability()).resolves.toEqual({ status: 'unknown' })+ await expect(resolveCapability()).resolves.toEqual({ status: 'unknown' })+ expect(readMetadata).toHaveBeenCalledTimes(2)+ })++ it('binds each resolver to its own model and cache', async () => {+ const readA = vi.fn<AnthropicMetadataReader>(async () =>+ anthropicModel('claude-sonnet-4-5', true)+ )+ const readB = vi.fn<AnthropicMetadataReader>(async () =>+ anthropicModel('claude-opus-4-1', false)+ )+ const resolveA = createAnthropicImageCapabilityResolver('claude-sonnet-4-5', readA)+ const resolveB = createAnthropicImageCapabilityResolver('claude-opus-4-1', readB)++ await expect(resolveA()).resolves.toMatchObject({+ status: 'supported',+ model: 'claude-sonnet-4-5',+ })+ await expect(resolveB()).resolves.toEqual({ status: 'unsupported' })+ await expect(resolveA()).resolves.toMatchObject({ status: 'supported' })+ expect(readA).toHaveBeenCalledTimes(1)+ expect(readB).toHaveBeenCalledTimes(1)+ expect(readA.mock.calls[0][0]).toBe('/v1/models/claude-sonnet-4-5')+ expect(readB.mock.calls[0][0]).toBe('/v1/models/claude-opus-4-1')+ })+})diff --git a/mcp-host/src/visualInput/capabilities.ts b/mcp-host/src/visualInput/capabilities.tsindex 6aa6ad5e6..c6122ffdf 100644--- a/mcp-host/src/visualInput/capabilities.ts+++ b/mcp-host/src/visualInput/capabilities.ts@@ -1,34 +1,92 @@import { type ImageInputCapability, VISUAL_INPUT_LIMITS, VisualInputError } from './policy'-/** The only provider whose model metadata this module interrogates. */+/** The provider name the OpenRouter catalog resolver reports. */export const OPENROUTER_PROVIDER = 'openrouter'/** Fixed, bounded provenance string; upstream text never reaches this field. */export const OPENROUTER_METADATA_EVIDENCE = 'openrouter-model-metadata'-/** The catalog changes slowly, so a resolved answer stays reusable for one minute. */-export const OPENROUTER_CAPABILITY_CACHE_MS = 60_000/**- * Fetches `GET /model/{author}/{slug}` relative to the provider's `/api/v1`- * base. The caller supplies the authenticated transport, so this module reads- * no key, no environment variable, and no socket of its own.+ * The provider name the Anthropic Models API resolver reports. It matches+ * `getProviderType()` on the Claude provider, so an answer carries the same+ * identity the rest of the LLM layer uses.*/-export type OpenRouterMetadataReader = (path: string, signal: AbortSignal) => Promise<Response>+export const ANTHROPIC_PROVIDER = 'claude'+/** Fixed, bounded provenance string; upstream text never reaches this field. */+export const ANTHROPIC_METADATA_EVIDENCE = 'anthropic-model-metadata'++/** Both catalogs change slowly, so a resolved answer stays reusable for one minute. */+const CAPABILITY_CACHE_MS = 60_000++export const OPENROUTER_CAPABILITY_CACHE_MS = CAPABILITY_CACHE_MS+export const ANTHROPIC_CAPABILITY_CACHE_MS = CAPABILITY_CACHE_MS++/**+ * The only parts of one HTTP response this module reads. Native `Response`+ * satisfies it directly, and so does the `{status, redirected, body}` shape a+ * caller builds after bridging a Node stream into a Web stream.+ */+export interface ModelMetadataResponse {+ readonly status: number+ readonly redirected: boolean+ readonly body: ModelMetadataBody | null+}++/**+ * The subset of a Web `ReadableStream` this module consumes, declared+ * structurally because the fetch layer's `Response` typings and Node's+ * `Readable.toWeb` bridge do not always resolve to the same `ReadableStream`+ * declaration. A bounded read only needs `getReader()`, `read()`, `cancel()`,+ * and `releaseLock()`.+ */+export interface ModelMetadataBody {+ getReader(): ModelMetadataBodyReader+ cancel(): Promise<void>+}++export interface ModelMetadataBodyReader {+ read(): Promise<{ done: boolean; value?: Uint8Array }>+ cancel(): Promise<void>+ releaseLock(): void+}++/**+ * Fetches one provider-relative metadata path through the caller's authenticated+ * transport, so this module reads no key, no environment variable, and no socket+ * of its own. The caller resolves the provider base; the path is always relative.+ */+export type MetadataReader = (path: string, signal: AbortSignal) => Promise<ModelMetadataResponse>++/** OpenRouter's `GET /model/{author}/{slug}` reader. */+export type OpenRouterMetadataReader = MetadataReader++/** Anthropic's `GET /v1/models/{model_id}` reader. */+export type AnthropicMetadataReader = MetadataReaderconst UNKNOWN = Object.freeze({ status: 'unknown' as const })const UNSUPPORTED = Object.freeze({ status: 'unsupported' as const })-const ABORTED = Symbol('openrouter-capability-aborted')+const ABORTED = Symbol('image-capability-aborted')++/** The provider-specific half of one resolver: path shape plus documented metadata shape. */+interface ImageCapabilitySource {+ /** Provider-relative request path for one model id, or undefined when it is not addressable. */+ buildPath(model: string): string | undefined+ /** Reads only the documented model shape; an explicit negative is the sole `unsupported`. */+ parse(metadata: string, model: string): ImageInputCapability+}/**- * Resolves whether one `author/slug` model accepts image input, using only the- * injected authenticated reader. Every failure mode other than an explicit- * modality list resolves to `unknown`, so a caller that cannot prove support- * never treats the model as image-capable.+ * Resolves whether one model accepts image input, using only the injected+ * authenticated reader. Every failure mode other than an explicit negative+ * resolves to `unknown`, so a caller that cannot prove support never treats the+ * model as image-capable. Both providers share this engine: one bounded read per+ * uncached call, one absolute per-call deadline, and one private per-resolver cache.*/-export function createOpenRouterImageCapabilityResolver(+function createImageCapabilityResolver(model: string,- readMetadata: OpenRouterMetadataReader+ readMetadata: MetadataReader,+ source: ImageCapabilitySource): (signal?: AbortSignal) => Promise<ImageInputCapability> {- const path = buildModelPath(model)+ const path = source.buildPath(model)let cached: { value: ImageInputCapability; expiresAt: number } | undefinedreturn async function resolveCapability(callerSignal) {@@ -38,9 +96,11 @@ export function createOpenRouterImageCapabilityResolver(const now = Date.now()if (cached !== undefined && cached.expiresAt > now) return cached.value- const outcome = await resolveWithDeadline(model, path, readMetadata, callerSignal)+ const outcome = await resolveWithDeadline(callerSignal, signal =>+ requestCapability(path, readMetadata, signal, metadata => source.parse(metadata, model))+ )if (outcome !== ABORTED && outcome.status !== 'unknown') {- cached = { value: outcome, expiresAt: Date.now() + OPENROUTER_CAPABILITY_CACHE_MS }+ cached = { value: outcome, expiresAt: Date.now() + CAPABILITY_CACHE_MS }}// A caller that aborted never receives a value, even when one arrived first.if (callerSignal?.aborted) throw new VisualInputError('cancelled')@@ -48,13 +108,47 @@ export function createOpenRouterImageCapabilityResolver(}}+/**+ * Resolves whether one `author/slug` OpenRouter model accepts image input, using+ * only the injected authenticated reader.+ */+export function createOpenRouterImageCapabilityResolver(+ model: string,+ readMetadata: OpenRouterMetadataReader+): (signal?: AbortSignal) => Promise<ImageInputCapability> {+ return createImageCapabilityResolver(model, readMetadata, OPENROUTER_SOURCE)+}++/**+ * Resolves whether one Anthropic model accepts image input, reading the+ * documented `capabilities.image_input.supported` flag. Vision is never inferred+ * from the model name: an absent, mismatched, or malformed flag is `unknown`, and+ * only an explicit `false` is `unsupported`.+ */+export function createAnthropicImageCapabilityResolver(+ model: string,+ readMetadata: AnthropicMetadataReader+): (signal?: AbortSignal) => Promise<ImageInputCapability> {+ return createImageCapabilityResolver(model, readMetadata, ANTHROPIC_SOURCE)+}++const OPENROUTER_SOURCE: ImageCapabilitySource = {+ buildPath: buildOpenRouterModelPath,+ parse: openRouterCapabilityFromMetadata,+}++const ANTHROPIC_SOURCE: ImageCapabilitySource = {+ buildPath: buildAnthropicModelPath,+ parse: anthropicCapabilityFromMetadata,+}+/*** OpenRouter model ids are exactly `author/slug`; anything else cannot be* requested at all. Each segment must stay a single printable-ASCII token, and* `encodeURIComponent` escapes the reserved characters that remain, such as the* `:` in `author/model:free`.*/-function buildModelPath(model: string): string | undefined {+function buildOpenRouterModelPath(model: string): string | undefined {const segments = model.split('/')if (segments.length !== 2) return undefinedconst [author, slug] = segments@@ -62,6 +156,18 @@ function buildModelPath(model: string): string | undefined {return `/model/${encodeURIComponent(author)}/${encodeURIComponent(slug)}`}+/**+ * Anthropic model ids address exactly one resource, `GET /v1/models/{model_id}`.+ * The id must stay a single printable-ASCII token, and `encodeURIComponent`+ * escapes `/`, `?`, `#`, and `%` so they cannot introduce another path, query, or+ * fragment. A bare `.` or `..` is refused before encoding, because dots are+ * unreserved characters that `encodeURIComponent` would otherwise leave intact.+ */+function buildAnthropicModelPath(model: string): string | undefined {+ if (!isSafePathSegment(model)) return undefined+ return `/v1/models/${encodeURIComponent(model)}`+}+function isSafePathSegment(segment: string): boolean {if (segment.length === 0 || segment === '.' || segment === '..') return falsefor (const character of segment) {@@ -77,10 +183,8 @@ function isSafePathSegment(segment: string): boolean {* cannot abort each other.*/async function resolveWithDeadline(- model: string,- path: string,- readMetadata: OpenRouterMetadataReader,- callerSignal: AbortSignal | undefined+ callerSignal: AbortSignal | undefined,+ attempt: (signal: AbortSignal) => Promise<ImageInputCapability>): Promise<ImageInputCapability | typeof ABORTED> {const controller = new AbortController()const onCallerAbort = () => controller.abort()@@ -88,10 +192,7 @@ async function resolveWithDeadline(const deadline = setTimeout(() => controller.abort(), VISUAL_INPUT_LIMITS.validationTimeoutMs)try {- return await Promise.race([- requestCapability(model, path, readMetadata, controller.signal),- aborted(controller.signal),- ])+ return await Promise.race([attempt(controller.signal), aborted(controller.signal)])} finally {clearTimeout(deadline)callerSignal?.removeEventListener('abort', onCallerAbort)@@ -113,10 +214,10 @@ function aborted(signal: AbortSignal): Promise<typeof ABORTED> {}async function requestCapability(- model: string,path: string,- readMetadata: OpenRouterMetadataReader,- signal: AbortSignal+ readMetadata: MetadataReader,+ signal: AbortSignal,+ parse: (metadata: string) => ImageInputCapability): Promise<ImageInputCapability> {try {const response = await readMetadata(path, signal)@@ -132,7 +233,7 @@ async function requestCapability(}const metadata = await readBoundedMetadata(response, signal)- return metadata === undefined ? UNKNOWN : capabilityFromMetadata(metadata, model)+ return metadata === undefined ? UNKNOWN : parse(metadata)} catch {// An unreadable transport, status, or body cannot prove support: fail closed.return UNKNOWN@@ -145,7 +246,7 @@ async function requestCapability(* reports `unknown` instead of parsing a truncated or replaced catalog entry.*/async function readBoundedMetadata(- response: Response,+ response: ModelMetadataResponse,signal: AbortSignal): Promise<string | undefined> {const body = response.body@@ -192,20 +293,30 @@ function decodeUtf8(chunks: readonly Uint8Array[], received: number): string | u}/**- * Reads only the documented catalog shape. Anything absent, mistyped, or- * describing a different model leaves the answer `unknown`; an explicit- * modality list without `image` is the single reachable `unsupported`.+ * Parses a metadata body only when it is one JSON object; anything else proves+ * nothing about the requested model.*/-function capabilityFromMetadata(metadata: string, model: string): ImageInputCapability {+function parseJsonObject(metadata: string): Record<string, unknown> | undefined {let payload: unknowntry {payload = JSON.parse(metadata)} catch {- return UNKNOWN+ return undefined}- if (typeof payload !== 'object' || payload === null) return UNKNOWN+ if (typeof payload !== 'object' || payload === null) return undefined+ return payload as Record<string, unknown>+}- const data = (payload as { data?: unknown }).data+/**+ * Reads only OpenRouter's documented catalog shape. Anything absent, mistyped, or+ * describing a different model leaves the answer `unknown`; an explicit modality+ * list without `image` is the single reachable `unsupported`.+ */+function openRouterCapabilityFromMetadata(metadata: string, model: string): ImageInputCapability {+ const payload = parseJsonObject(metadata)+ if (payload === undefined) return UNKNOWN++ const data = payload.dataif (typeof data !== 'object' || data === null) return UNKNOWN// An alias or a neighbouring id is not the requested model; never infer identity.@@ -230,6 +341,38 @@ function capabilityFromMetadata(metadata: string, model: string): ImageInputCapa})}+/**+ * Reads only Anthropic's documented `GET /v1/models/{model_id}` shape. A missing+ * or mistyped `type`, a neighbouring id, a malformed capability tree, or a+ * non-boolean flag all prove nothing and stay `unknown`; the flag's boolean value+ * is the only thing that decides. Nothing is inferred from the model name.+ */+function anthropicCapabilityFromMetadata(metadata: string, model: string): ImageInputCapability {+ const payload = parseJsonObject(metadata)+ if (payload === undefined) return UNKNOWN++ // A neighbouring entry, an alias, or an error envelope is not the requested model.+ if (payload.type !== 'model' || payload.id !== model) return UNKNOWN++ const capabilities = payload.capabilities+ if (typeof capabilities !== 'object' || capabilities === null) return UNKNOWN++ const imageInput = (capabilities as { image_input?: unknown }).image_input+ if (typeof imageInput !== 'object' || imageInput === null) return UNKNOWN++ const supported = (imageInput as { supported?: unknown }).supported+ if (typeof supported !== 'boolean') return UNKNOWN+ if (!supported) return UNSUPPORTED++ // Frozen because the resolver hands this same instance back from its cache.+ return Object.freeze({+ status: 'supported' as const,+ provider: ANTHROPIC_PROVIDER,+ model,+ evidence: ANTHROPIC_METADATA_EVIDENCE,+ })+}+/** Releases a body this module refuses to read; an already-consumed body needs nothing. */function cancelQuietly(stream: { cancel(): Promise<void> } | null): void {if (stream === null) return