Skip to content

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

TypeScript

fix(mcp-host): resolve provider non-secret env from per-Host env

Jose Gabriel Ramirez Encinasf3218482d12e
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index d589c42bd..1928bc87d 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.556",
+ "version": "0.1.557",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.556",
+ "version": "0.1.557",
"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 4ea1e8ea3..2e9f5111a 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.556",
+ "version": "0.1.557",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/config/configStore.ts b/mcp-host/src/config/configStore.ts
index 7f18dc5e9..19bd52b3c 100644
--- a/mcp-host/src/config/configStore.ts
+++ b/mcp-host/src/config/configStore.ts
@@ -24,7 +24,8 @@
*
* Updates fire `onChange` with `{llmKeyChanged, envChanged}` so subscribers
* can skip work that doesn't concern them (e.g. shell-tool spawn cares about
- * envChanged; LLM provider factory cares about llmKeyChanged).
+ * envChanged; LLM provider factory cares about llmKeyChanged, plus envChanged
+ * when the provider's declared non-secret env — e.g. AWS_REGION — changed).
*
* Reconnect strategy: watches that disconnect are restarted with exponential
* backoff; on "too old" the watch falls back to a fresh list and resumes.
diff --git a/mcp-host/src/llm/__tests__/providerEnv.hostEnv.test.ts b/mcp-host/src/llm/__tests__/providerEnv.hostEnv.test.ts
new file mode 100644
index 000000000..cce7dea9c
--- /dev/null
+++ b/mcp-host/src/llm/__tests__/providerEnv.hostEnv.test.ts
@@ -0,0 +1,249 @@
+/**
+ * Non-secret provider env set under Control UI Host → Environment lands in the
+ * per-Host `host-<ref>-env` ConfigMap / `-env-secret` Secret, which HCC does NOT
+ * `envFrom` into the pod — mcp-host only sees it through ConfigStore. These
+ * tests prove the drivers resolve it from there (mirroring main.ts's
+ * `hostProviderEnv` wiring), that only declared names flow, and that a Host →
+ * Environment edit is detected for the provider rebuild.
+ */
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { ConfigStore } from '../../config/configStore'
+import { buildBedrockClientConfig } from '../drivers/bedrockConverse'
+import { buildVertexClientOptions } from '../drivers/googleGenerative'
+import { createLLMProvider } from '../index'
+import {
+ createProviderEnvChangeDetector,
+ processEnvLookup,
+ providerEnvLookup,
+} from '../providerEnv'
+import type { LlmProvider } from '../registryCore'
+
+const ENV_NAMES = [
+ 'AWS_REGION',
+ 'VERTEX_PROJECT_ID',
+ 'VERTEX_LOCATION',
+ 'AZURE_OPENAI_ENDPOINT',
+ 'AZURE_OPENAI_API_VERSION',
+] as const
+
+interface FakeWatchHandle {
+ emit: (type: string, obj: unknown) => void
+}
+
+function makeFakeWatch() {
+ const active = new Map<string, FakeWatchHandle>()
+ return {
+ active,
+ watch: vi.fn(
+ async (
+ path: string,
+ params: { fieldSelector?: string },
+ onEvent: (type: string, obj: unknown) => void
+ ) => {
+ active.set(`${path}|${params.fieldSelector ?? ''}`, { emit: onEvent })
+ return { abort: () => {} }
+ }
+ ),
+ }
+}
+
+function makeFakeCoreApi(
+ secrets: Record<string, Record<string, string>>,
+ configMaps: Record<string, Record<string, string>>
+) {
+ const notFound = () => {
+ const err: { code?: number } = { code: 404 }
+ throw err
+ }
+ return {
+ readNamespacedSecret: vi.fn(async (req: { name: string }) =>
+ secrets[req.name] ? { data: secrets[req.name] } : notFound()
+ ),
+ readNamespacedConfigMap: vi.fn(async (req: { name: string }) =>
+ configMaps[req.name] ? { data: configMaps[req.name] } : notFound()
+ ),
+ }
+}
+
+const SA_JSON_NO_PROJECT = JSON.stringify({
+ client_email: '[email protected]',
+ private_key: '-----BEGIN PRIVATE KEY-----\nexample\n-----END PRIVATE KEY-----\n',
+})
+
+describe('per-Host env (Host → Environment) reaches the provider drivers', () => {
+ let store: ConfigStore | null = null
+ const saved: Record<string, string | undefined> = {}
+ let watch: ReturnType<typeof makeFakeWatch>
+
+ beforeEach(() => {
+ for (const n of ENV_NAMES) {
+ saved[n] = process.env[n]
+ delete process.env[n]
+ }
+ watch = makeFakeWatch()
+ })
+
+ afterEach(() => {
+ store?.stop()
+ store = null
+ for (const n of ENV_NAMES) {
+ if (saved[n] === undefined) delete process.env[n]
+ else process.env[n] = saved[n]
+ }
+ })
+
+ async function startStore(
+ provider: LlmProvider,
+ llmSecret: Record<string, string>,
+ hostEnv: Record<string, string>,
+ hostEnvSecret?: Record<string, string>
+ ): Promise<ConfigStore> {
+ const secrets: Record<string, Record<string, string>> = { 'chatllm-api-keys': llmSecret }
+ if (hostEnvSecret) secrets['host-trader-env-secret'] = hostEnvSecret
+ store = new ConfigStore({
+ namespace: 'mcp-host',
+ hostRef: 'trader',
+ llmSecretRef: 'chatllm-api-keys',
+ provider,
+ coreApi: makeFakeCoreApi(secrets, { 'host-trader-env': hostEnv }) as never,
+ watch: watch as never,
+ })
+ await store.start()
+ return store
+ }
+
+ /** Mirrors main.ts `hostProviderEnv` + `createLLMProvider` wiring. */
+ function buildFromStore(s: ConfigStore, provider: LlmProvider, model: string) {
+ const llm = s.llmCredentials()!
+ return createLLMProvider(
+ { [llm.provider]: llm.credentials },
+ { provider, name: model },
+ { env: providerEnvLookup(provider, name => s.get(name)) }
+ )
+ }
+
+ it('bedrock: AWS_REGION set only in host-<ref>-env constructs the provider', async () => {
+ const s = await startStore(
+ 'bedrock',
+ { 'aws-access-key-id': 'AKIAEXAMPLE', 'aws-secret-access-key': 'secret-example' },
+ { AWS_REGION: 'eu-west-1' }
+ )
+ expect(buildFromStore(s, 'bedrock', 'anthropic.claude-3-5-sonnet')).not.toBeNull()
+ expect(
+ buildBedrockClientConfig(
+ { 'aws-access-key-id': 'AKIAEXAMPLE', 'aws-secret-access-key': 'secret-example' },
+ providerEnvLookup('bedrock', name => s.get(name))
+ ).region
+ ).toBe('eu-west-1')
+ })
+
+ it('bedrock: without an env source the region is still only read from process.env', async () => {
+ const s = await startStore(
+ 'bedrock',
+ { 'aws-access-key-id': 'AKIAEXAMPLE', 'aws-secret-access-key': 'secret-example' },
+ { AWS_REGION: 'eu-west-1' }
+ )
+ const llm = s.llmCredentials()!
+ expect(
+ createLLMProvider({ [llm.provider]: llm.credentials }, { provider: 'bedrock', name: 'm' })
+ ).toBeNull()
+ })
+
+ it('vertex: VERTEX_PROJECT_ID/VERTEX_LOCATION from host env (secret tier wins over CM)', async () => {
+ const s = await startStore(
+ 'vertex',
+ { 'vertex-service-account-json': SA_JSON_NO_PROJECT },
+ { VERTEX_PROJECT_ID: 'proj-cm', VERTEX_LOCATION: 'europe-west4' },
+ { VERTEX_PROJECT_ID: 'proj-secret' }
+ )
+ const opts = buildVertexClientOptions(
+ { 'vertex-service-account-json': SA_JSON_NO_PROJECT },
+ providerEnvLookup('vertex', name => s.get(name))
+ )
+ expect(opts.project).toBe('proj-secret')
+ expect(opts.location).toBe('europe-west4')
+ expect(buildFromStore(s, 'vertex', 'gemini-2.5-pro')).not.toBeNull()
+ })
+
+ it('azure: AZURE_OPENAI_ENDPOINT from host env constructs the provider', async () => {
+ const s = await startStore(
+ 'azure',
+ { 'azure-openai-api-key': 'az-key' },
+ { AZURE_OPENAI_ENDPOINT: 'https://my-resource.openai.azure.com/' }
+ )
+ expect(buildFromStore(s, 'azure', 'my-deployment')).not.toBeNull()
+ })
+
+ it('falls back to process.env when the host env does not set the name', async () => {
+ process.env.AWS_REGION = 'us-east-2'
+ const s = await startStore(
+ 'bedrock',
+ { 'aws-access-key-id': 'AKIAEXAMPLE', 'aws-secret-access-key': 'secret-example' },
+ {}
+ )
+ expect(
+ buildBedrockClientConfig(
+ { 'aws-access-key-id': 'AKIAEXAMPLE', 'aws-secret-access-key': 'secret-example' },
+ providerEnvLookup('bedrock', name => s.get(name))
+ ).region
+ ).toBe('us-east-2')
+ })
+
+ it('only the provider-declared non-secret names resolve through the lookup', async () => {
+ const s = await startStore(
+ 'bedrock',
+ { 'aws-access-key-id': 'AKIAEXAMPLE', 'aws-secret-access-key': 'secret-example' },
+ { AWS_REGION: 'eu-west-1', VERTEX_PROJECT_ID: 'proj' },
+ { GITHUB_TOKEN: 'ghp_example' }
+ )
+ const env = providerEnvLookup('bedrock', name => s.get(name))
+ expect(env('AWS_REGION')).toBe('eu-west-1')
+ expect(env('GITHUB_TOKEN')).toBeUndefined()
+ expect(env('AWS_SECRET_ACCESS_KEY')).toBeUndefined()
+ expect(env('VERTEX_PROJECT_ID')).toBeUndefined()
+ // Never written into the process environment (would reach child processes).
+ expect(process.env.AWS_REGION).toBeUndefined()
+ })
+
+ it('a Host → Environment edit of AWS_REGION is detected; unrelated env edits are not', async () => {
+ const s = await startStore(
+ 'bedrock',
+ { 'aws-access-key-id': 'AKIAEXAMPLE', 'aws-secret-access-key': 'secret-example' },
+ { AWS_REGION: 'eu-west-1', LOG_LEVEL: 'info' }
+ )
+ const changed = createProviderEnvChangeDetector('bedrock', name => s.get(name))
+ const rebuilds: string[] = []
+ s.onChange(({ envChanged }) => {
+ if (envChanged && changed()) {
+ rebuilds.push(
+ buildBedrockClientConfig(
+ { 'aws-access-key-id': 'AKIAEXAMPLE', 'aws-secret-access-key': 'secret-example' },
+ providerEnvLookup('bedrock', name => s.get(name))
+ ).region
+ )
+ }
+ })
+ const cmWatch = [...watch.active.entries()].find(([k]) =>
+ k.endsWith('metadata.name=host-trader-env')
+ )
+ expect(cmWatch).toBeDefined()
+ const emit = cmWatch![1].emit
+
+ emit('MODIFIED', {
+ metadata: { name: 'host-trader-env' },
+ data: { AWS_REGION: 'eu-west-1', LOG_LEVEL: 'debug' },
+ })
+ expect(rebuilds).toEqual([])
+
+ emit('MODIFIED', {
+ metadata: { name: 'host-trader-env' },
+ data: { AWS_REGION: 'ap-southeast-2', LOG_LEVEL: 'debug' },
+ })
+ expect(rebuilds).toEqual(['ap-southeast-2'])
+ })
+
+ it('processEnvLookup reads the pod environment (dev-mode default)', () => {
+ process.env.AWS_REGION = 'us-west-2'
+ expect(processEnvLookup('AWS_REGION')).toBe('us-west-2')
+ })
+})
diff --git a/mcp-host/src/llm/azure.ts b/mcp-host/src/llm/azure.ts
index 1705415ef..697eb16b0 100644
--- a/mcp-host/src/llm/azure.ts
+++ b/mcp-host/src/llm/azure.ts
@@ -18,11 +18,14 @@
*/
import OpenAI from 'openai'
import { OpenAIProvider } from './openai'
+import { type ProviderEnvLookup, processEnvLookup } from './providerEnv'
import type { LlmProvider } from './registryCore'
export class AzureOpenAIProvider extends OpenAIProvider {
- constructor(apiKey: string, model: string) {
- const endpoint = process.env.AZURE_OPENAI_ENDPOINT?.trim()
+ constructor(apiKey: string, model: string, env: ProviderEnvLookup = processEnvLookup) {
+ // Non-secret per-Host env (`host-<ref>-env` via ConfigStore in a Host pod;
+ // process.env by default) — see ./providerEnv.
+ const endpoint = env('AZURE_OPENAI_ENDPOINT')?.trim()
if (!endpoint) {
// Fail-closed: without the per-resource endpoint there is no correct host
// to reach. Throwing lets createLLMProvider return null so the R5 failover
@@ -53,7 +56,7 @@ export class AzureOpenAIProvider extends OpenAIProvider {
// Optional; only the classic (non-v1) path needs `api-version` (a query
// param there). Read for forward-compat and thread it as a default query so
// a switch back to the classic surface keeps working; harmless on v1.
- const apiVersion = process.env.AZURE_OPENAI_API_VERSION?.trim()
+ const apiVersion = env('AZURE_OPENAI_API_VERSION')?.trim()
const client = new OpenAI({
apiKey,
baseURL,
diff --git a/mcp-host/src/llm/drivers/bedrockConverse.ts b/mcp-host/src/llm/drivers/bedrockConverse.ts
index 6c6e0c593..9125838f5 100644
--- a/mcp-host/src/llm/drivers/bedrockConverse.ts
+++ b/mcp-host/src/llm/drivers/bedrockConverse.ts
@@ -25,6 +25,7 @@ import {
} from '../../core/types'
import type { ProviderCredentials } from '../../types'
import { classifyByHttpStatus, classifyUnknown } from '../errorClassification'
+import { type ProviderEnvLookup, processEnvLookup } from '../providerEnv'
import type { LlmProvider } from '../registryCore'
import type { ClassifiedError, SingleTurnProvider } from '../types'
@@ -362,19 +363,23 @@ export interface BedrockClientConfig {
}
/**
- * Pure: derive the Bedrock client config from the credential bag + pod env.
- * Both AWS key slots are required (also validated upstream in
- * `createLLMProvider`); `AWS_REGION` is required non-secret pod env
- * (`host-<ref>-env`). Throws on a missing region / key. Separated from
+ * Pure: derive the Bedrock client config from the credential bag + the
+ * non-secret provider env. Both AWS key slots are required (also validated
+ * upstream in `createLLMProvider`); `AWS_REGION` is required non-secret env,
+ * resolved through `env` (the per-Host `host-<ref>-env` via ConfigStore in a
+ * Host pod, `process.env` by default). Throws on a missing region / key. Separated from
* {@link buildBedrockConverseDriver} so the wiring is testable without the SDK.
*/
-export function buildBedrockClientConfig(credentials: ProviderCredentials): BedrockClientConfig {
+export function buildBedrockClientConfig(
+ credentials: ProviderCredentials,
+ env: ProviderEnvLookup = processEnvLookup
+): BedrockClientConfig {
const accessKeyId = credentials['aws-access-key-id']
const secretAccessKey = credentials['aws-secret-access-key']
if (!accessKeyId || !secretAccessKey) {
throw new Error('bedrock: missing AWS access-key-id / secret-access-key')
}
- const region = process.env.AWS_REGION
+ const region = env('AWS_REGION')?.trim()
if (!region) throw new Error('bedrock: AWS_REGION is required')
return { region, credentials: { accessKeyId, secretAccessKey } }
}
@@ -388,9 +393,10 @@ export function buildBedrockClientConfig(credentials: ProviderCredentials): Bedr
*/
export function buildBedrockConverseDriver(
credentials: ProviderCredentials,
- model: string
+ model: string,
+ env: ProviderEnvLookup = processEnvLookup
): SingleTurnProvider {
- const clientConfig = buildBedrockClientConfig(credentials)
+ const clientConfig = buildBedrockClientConfig(credentials, env)
// Lazy CommonJS require — only reached for a Bedrock Host.
diff --git a/mcp-host/src/llm/drivers/googleGenerative.ts b/mcp-host/src/llm/drivers/googleGenerative.ts
index 3700212ed..eea8313b3 100644
--- a/mcp-host/src/llm/drivers/googleGenerative.ts
+++ b/mcp-host/src/llm/drivers/googleGenerative.ts
@@ -26,6 +26,7 @@ import {
} from '../../core/types'
import type { ProviderCredentials } from '../../types'
import { classifyUnknown } from '../errorClassification'
+import { type ProviderEnvLookup, processEnvLookup } from '../providerEnv'
import type { LlmProvider } from '../registryCore'
import type { ClassifiedError, SingleTurnProvider } from '../types'
@@ -361,7 +362,9 @@ export interface VertexClientOptions {
}
/**
- * Pure: derive the Vertex client options from the credential bag + pod env,
+ * Pure: derive the Vertex client options from the credential bag + the
+ * non-secret provider env (`env`: per-Host `host-<ref>-env` via ConfigStore in a
+ * Host pod, `process.env` by default),
* constructing the auth credentials IN MEMORY from the service-account JSON —
* never via a `GOOGLE_APPLICATION_CREDENTIALS` file. `VERTEX_PROJECT_ID`
* (falling back to the JSON's `project_id`) is required; `VERTEX_LOCATION`
@@ -370,7 +373,10 @@ export interface VertexClientOptions {
* {@link buildGoogleGenerativeDriver} so all the wiring is testable without the
* SDK.
*/
-export function buildVertexClientOptions(credentials: ProviderCredentials): VertexClientOptions {
+export function buildVertexClientOptions(
+ credentials: ProviderCredentials,
+ env: ProviderEnvLookup = processEnvLookup
+): VertexClientOptions {
const rawJson = credentials['vertex-service-account-json']
if (!rawJson) throw new Error('vertex: missing service-account JSON credential')
@@ -385,9 +391,9 @@ export function buildVertexClientOptions(credentials: ProviderCredentials): Vert
throw new Error('vertex: service-account JSON missing client_email/private_key')
}
- const projectId = process.env.VERTEX_PROJECT_ID || sa.project_id
+ const projectId = env('VERTEX_PROJECT_ID')?.trim() || sa.project_id
if (!projectId) throw new Error('vertex: VERTEX_PROJECT_ID is required')
- const location = process.env.VERTEX_LOCATION || 'us-central1'
+ const location = env('VERTEX_LOCATION')?.trim() || 'us-central1'
return {
vertexai: true,
@@ -409,9 +415,10 @@ export function buildVertexClientOptions(credentials: ProviderCredentials): Vert
*/
export function buildGoogleGenerativeDriver(
credentials: ProviderCredentials,
- model: string
+ model: string,
+ env: ProviderEnvLookup = processEnvLookup
): SingleTurnProvider {
- const options = buildVertexClientOptions(credentials)
+ const options = buildVertexClientOptions(credentials, env)
// Lazy CommonJS require — only reached for a Vertex Host.
diff --git a/mcp-host/src/llm/index.ts b/mcp-host/src/llm/index.ts
index 244b76807..7dd03e53d 100644
--- a/mcp-host/src/llm/index.ts
+++ b/mcp-host/src/llm/index.ts
@@ -14,6 +14,7 @@ import { readLiveGrokPolicyBinding, resolveGrokAttemptPolicy } from './grokPolic
import type { GrokAttemptContext } from './grokSubscription'
import { OpenAIProvider } from './openai'
import { ProviderAttemptAuthorizer, resolveCodexAuthorizeUrl } from './providerAttemptAuthorizer'
+import type { ProviderEnvLookup } from './providerEnv'
import { makeProvider } from './registry'
import { ALL_PROVIDERS, descriptorFor, isLlmProvider } from './registryCore'
// Re-export the transport interfaces (moved to ./types to break the registry
@@ -93,6 +94,12 @@ function createCodexRuntimeDeps(captured?: CodexAttemptContext) {
export type CreateLlmProviderOptions = {
capturedCodexAttemptContext?: CodexAttemptContext
capturedGrokAttemptContext?: GrokAttemptContext
+ /**
+ * Non-secret provider env source (see ./providerEnv). Omitted → process.env.
+ * mcp-host's Host runtime passes the per-Host env from ConfigStore so values
+ * set under Control UI Host → Environment reach the drivers.
+ */
+ env?: ProviderEnvLookup
}
/**
@@ -130,16 +137,14 @@ export function createLLMProvider(
// never throw here (their empty-key case is already handled above), so their
// behaviour is byte-identical.
try {
- return makeProvider(
- provider,
- credentials,
- modelName,
- provider === 'codex-subscription'
+ return makeProvider(provider, credentials, modelName, {
+ env: options?.env,
+ ...(provider === 'codex-subscription'
? { codex: createCodexRuntimeDeps(options?.capturedCodexAttemptContext) }
: provider === 'grok-subscription'
? { grok: createGrokRuntimeDeps(options?.capturedGrokAttemptContext) }
- : undefined
- )
+ : {}),
+ })
} catch (err) {
logger.error({}, 'Failed to construct LLM provider')
return null
diff --git a/mcp-host/src/llm/providerEnv.ts b/mcp-host/src/llm/providerEnv.ts
new file mode 100644
index 000000000..d48e6dc7f
--- /dev/null
+++ b/mcp-host/src/llm/providerEnv.ts
@@ -0,0 +1,68 @@
+/**
+ * Non-secret provider env source (AWS_REGION, VERTEX_PROJECT_ID/VERTEX_LOCATION,
+ * AZURE_OPENAI_ENDPOINT/AZURE_OPENAI_API_VERSION, …).
+ *
+ * Operators set these under Control UI Host → Environment, which writes the
+ * per-Host `host-<ref>-env` ConfigMap / `host-<ref>-env-secret` Secret. HCC does
+ * NOT `envFrom` those into the pod; mcp-host reads them through its watched
+ * ConfigStore. The drivers therefore take a lookup function instead of reading
+ * `process.env` directly.
+ *
+ * The values are deliberately NOT injected into `process.env`: that would leak
+ * per-Host (possibly secret-tier) entries into every child process (shell tool,
+ * recipe sub-processes). The lookup stays in-process.
+ */
+import { type LlmProvider, descriptorFor } from './registryCore'
+
+/** Resolve a non-secret provider env var by name; undefined when unset. */
+export type ProviderEnvLookup = (name: string) => string | undefined
+
+/** Default source: the pod's own environment (dev mode, recipe pods, tests). */
+export const processEnvLookup: ProviderEnvLookup = name => process.env[name]
+
+/** Names of the non-secret env vars `provider` declares in its descriptor. */
+export function providerNonSecretEnvNames(provider: LlmProvider): readonly string[] {
+ return descriptorFor(provider).nonSecretEnv.map(v => v.envName)
+}
+
+/**
+ * Restrict `source` to the non-secret env names `provider` declares. Any other
+ * name resolves to undefined, so a driver can never read an arbitrary per-Host
+ * entry (e.g. a credential) through this channel.
+ */
+export function providerEnvLookup(
+ provider: LlmProvider,
+ source: ProviderEnvLookup
+): ProviderEnvLookup {
+ const allowed = new Set(providerNonSecretEnvNames(provider))
+ return name => (allowed.has(name) ? source(name) : undefined)
+}
+
+/**
+ * Stable fingerprint of the declared non-secret env values for `provider`, used
+ * to detect a Host → Environment edit that should rebuild the live provider.
+ */
+export function providerEnvFingerprint(provider: LlmProvider, source: ProviderEnvLookup): string {
+ return JSON.stringify(
+ providerNonSecretEnvNames(provider).map(name => [name, source(name) ?? null])
+ )
+}
+
+/**
+ * Change detector for the live-provider rebuild on a Host → Environment edit.
+ * Captures the declared non-secret values of `provider` now; each call returns
+ * true only when they differ from the previous observation, so unrelated
+ * per-Host env edits (e.g. a GITHUB_TOKEN rotation) do not rebuild the provider.
+ */
+export function createProviderEnvChangeDetector(
+ provider: LlmProvider,
+ source: ProviderEnvLookup
+): () => boolean {
+ let last = providerEnvFingerprint(provider, source)
+ return () => {
+ const next = providerEnvFingerprint(provider, source)
+ if (next === last) return false
+ last = next
+ return true
+ }
+}
diff --git a/mcp-host/src/llm/registry.ts b/mcp-host/src/llm/registry.ts
index 8e622a101..58466eff3 100644
--- a/mcp-host/src/llm/registry.ts
+++ b/mcp-host/src/llm/registry.ts
@@ -25,12 +25,19 @@ import { buildGoogleGenerativeDriver } from './drivers/googleGenerative'
import { type GrokSubscriptionDeps, GrokSubscriptionProvider } from './grokSubscription'
import { OpenAIProvider } from './openai'
import { OpenAICompatibleProvider } from './openaiCompatible'
+import { type ProviderEnvLookup, processEnvLookup } from './providerEnv'
import { type LlmProvider, descriptorFor, primarySlot } from './registryCore'
import type { SingleTurnProvider } from './types'
export type MakeProviderOptions = {
codex?: CodexSubscriptionDeps
grok?: GrokSubscriptionDeps
+ /**
+ * Source for the provider's declared non-secret env (AWS_REGION, Vertex
+ * project/location, Azure endpoint/api-version). Defaults to process.env; a
+ * Host pod passes the per-Host env from ConfigStore (see ./providerEnv).
+ */
+ env?: ProviderEnvLookup
}
export {
@@ -69,6 +76,7 @@ export function makeProvider(
options?: MakeProviderOptions
): SingleTurnProvider {
const d = descriptorFor(provider)
+ const env = options?.env ?? processEnvLookup
switch (provider) {
case 'openai':
return new OpenAIProvider(credentials[primarySlot(d).dataKey], model)
@@ -76,10 +84,10 @@ export function makeProvider(
return new ClaudeProvider(credentials[primarySlot(d).dataKey], model)
case 'vertex':
// Own-SDK arm — lazy require lives inside the driver builder.
- return buildGoogleGenerativeDriver(credentials, model ?? d.defaultModel ?? '')
+ return buildGoogleGenerativeDriver(credentials, model ?? d.defaultModel ?? '', env)
case 'bedrock':
// Own-SDK arm — lazy require lives inside the driver builder.
- return buildBedrockConverseDriver(credentials, model ?? d.defaultModel ?? '')
+ return buildBedrockConverseDriver(credentials, model ?? d.defaultModel ?? '', env)
case 'azure':
// Light-driver arm: OpenAI wire shape but per-resource host
// (AZURE_OPENAI_ENDPOINT) + `api-key` header + deployment-name-as-model, so
@@ -87,7 +95,8 @@ export function makeProvider(
// below. Fails closed (throws) if the operator omitted the endpoint.
return new AzureOpenAIProvider(
credentials[primarySlot(d).dataKey],
- model ?? d.defaultModel ?? ''
+ model ?? d.defaultModel ?? '',
+ env
)
case 'codex-subscription':
if (process.env.MCP_HOST_CODEX_SUBSCRIPTION_ENABLED !== 'true') {
diff --git a/mcp-host/src/main.ts b/mcp-host/src/main.ts
index e47331bd2..9ee3cab51 100644
--- a/mcp-host/src/main.ts
+++ b/mcp-host/src/main.ts
@@ -82,6 +82,11 @@ import { hostPrimaryLlmBindingChanged } from './llm/hostLlmBinding'
import { type ImageInputResolver } from './llm/imageInput'
import { PromptCache } from './llm/promptCache'
import { clerumPromptCacheInvalidationsTotal } from './llm/promptCacheMetrics'
+import {
+ type ProviderEnvLookup,
+ createProviderEnvChangeDetector,
+ providerEnvLookup,
+} from './llm/providerEnv'
import { ALL_PROVIDERS, type LlmProvider, descriptorFor, isLlmProvider } from './llm/registryCore'
import { logger } from './logger'
import {
@@ -422,6 +427,20 @@ function apiKeysFromConfigStore(store: ConfigStore): ApiKeys {
return { [llm.provider]: llm.credentials }
}
+/**
+ * The non-secret provider env for `provider` (AWS_REGION, Vertex project/
+ * location, Azure endpoint/api-version), resolved from the live ConfigStore —
+ * per-Host `host-<ref>-env` / `-env-secret` first, then process.env. Restricted
+ * to the provider's declared non-secret names and kept in-process (never
+ * written to process.env, so it cannot reach a tool subprocess). Undefined
+ * without a ConfigStore (dev mode) → drivers read process.env.
+ */
+function hostProviderEnv(provider: string | undefined): ProviderEnvLookup | undefined {
+ const store = configStore
+ if (!store || !provider || !isLlmProvider(provider)) return undefined
+ return providerEnvLookup(provider, name => store.get(name))
+}
+
// ─── R5 — provider fallback ────────────────────────────────────────────────
/** Structured WARN on every failover switch (no secrets). */
@@ -518,7 +537,8 @@ function buildFallbackProvider(entry: FallbackEntry): SingleTurnProvider | null
if (!creds) return null
return createLLMProvider(
{ [entry.provider]: creds },
- { provider: entry.provider as LlmProvider, name: entry.model }
+ { provider: entry.provider as LlmProvider, name: entry.model },
+ { env: hostProviderEnv(entry.provider) }
)
}
@@ -605,7 +625,11 @@ function buildProviderForModel(model: string): SingleTurnProvider | null {
const modelCfg = currentHost?.spec.model
if (!modelCfg?.provider) return null
const keys = configStore ? apiKeysFromConfigStore(configStore) : currentKeys
- return createLLMProvider(keys, { provider: modelCfg.provider, name: model })
+ return createLLMProvider(
+ keys,
+ { provider: modelCfg.provider, name: model },
+ { env: hostProviderEnv(modelCfg.provider) }
+ )
}
/**
@@ -678,20 +702,35 @@ async function ensureConfigStore(host: HostCRD): Promise<ConfigStore> {
// lives in control-api/WRC. Re-checked on every allowlist reload below.
signalHostModelAllowlist(store, host.spec.model)
+ // Host → Environment edits (host-<ref>-env CM / -env-secret) fire only
+ // `envChanged`. Rebuild the provider when one of ITS declared non-secret env
+ // values (AWS_REGION, VERTEX_*, AZURE_OPENAI_*) actually changed; unrelated
+ // env edits keep the live provider. The store is rebuilt on a provider change,
+ // so the detector is per (store, provider).
+ const providerEnvChanged =
+ provider && isLlmProvider(provider)
+ ? createProviderEnvChangeDetector(provider, name => store.get(name))
+ : () => false
+
// Hot reload: rebuild the LLM provider on key rotation. The agent receives
// a swapped provider via setLLMProvider; in-flight requests finish on the
// old client (already dispatched), the next task uses the new one.
- store.onChange(({ llmKeyChanged, allowlistChanged }) => {
+ store.onChange(({ llmKeyChanged, envChanged, allowlistChanged }) => {
// R3.7 — re-evaluate the Host model against the freshly-loaded allowlist.
if (allowlistChanged && currentHost) signalHostModelAllowlist(store, currentHost.spec.model)
- if (!llmKeyChanged || !currentHost) return
+ // Always consult the detector on envChanged so its baseline tracks the
+ // latest values even when a key rotation triggers the rebuild instead.
+ const providerConfigChanged = envChanged && providerEnvChanged()
+ if ((!llmKeyChanged && !providerConfigChanged) || !currentHost) return
const keys = apiKeysFromConfigStore(store)
currentKeys = keys
- const next = createLLMProvider(keys, currentHost.spec.model)
+ const next = createLLMProvider(keys, currentHost.spec.model, {
+ env: hostProviderEnv(currentHost.spec.model?.provider),
+ })
if (!next) {
logger.error(
{},
- '[Main] LLM provider rebuild failed after key rotation — Host may be degraded'
+ '[Main] LLM provider rebuild failed after LLM config change — Host may be degraded'
)
return
}
@@ -705,7 +744,10 @@ async function ensureConfigStore(host: HostCRD): Promise<ConfigStore> {
failoverEngine?.clearCooldown()
if (agent) {
agent.setLLMProvider(next, currentHost.spec.model?.name)
- logger.info({}, '[Main] LLM provider rebuilt and re-attached to agent (key rotated)')
+ logger.info(
+ { llmKeyChanged, providerConfigChanged },
+ '[Main] LLM provider rebuilt and re-attached to agent (LLM config changed)'
+ )
} else {
logger.info({}, '[Main] LLM provider rebuilt (agent not ready yet, will use on init)')
}
@@ -737,7 +779,9 @@ async function initializeProvider(
logger.info({ model: host.spec.model }, '[Main] Model config:')
logger.info({ contextRef: host.spec.contextRef }, '[Main] Context ref:')
- currentProvider = createLLMProvider(keys, host.spec.model)
+ currentProvider = createLLMProvider(keys, host.spec.model, {
+ env: hostProviderEnv(host.spec.model?.provider),
+ })
bootFallbackEntry = null
// R5.10 — boot with fallback (Q8 ratified): if the PRIMARY is inconstructible