Skip to content

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

TypeScript

feat(mcp-host): bound Grok authorize by the Grok contract and send its V2 envelope

Alfredo Lopez4071579d7b99
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 126c0d67a..d32a2ca19 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.445",
+ "version": "0.1.446",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.445",
+ "version": "0.1.446",
"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 e189791ca..0f9eb847c 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.445",
+ "version": "0.1.446",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/llm/grokLlmProxyClient.ts b/mcp-host/src/llm/grokLlmProxyClient.ts
index f405f35ad..4353b1576 100644
--- a/mcp-host/src/llm/grokLlmProxyClient.ts
+++ b/mcp-host/src/llm/grokLlmProxyClient.ts
@@ -1,3 +1,9 @@
+import {
+ SCHEMA_VERSION_V2,
+ buildGrokProxyEnvelope,
+ parseGrokCompletionRequest,
+} from '@clerum/grok-provider-attempt-contract'
+
export const GROK_PROXY_COMPLETIONS_PATH = '/internal/runtime/v1/grok/completions'
/**
@@ -93,6 +99,41 @@ export class GrokLlmProxyClient {
},
retryOnUnauthorized: boolean
): Promise<GrokProxyStreamResult> {
+ let body: unknown = {
+ executionTicket: input.executionTicket,
+ requestHash: input.requestHash,
+ request: input.request,
+ ...(input.deadlineMs !== undefined ? { deadlineMs: input.deadlineMs } : {}),
+ }
+ // A V2 request carries its deadline inside the hashed request, and
+ // grok-llm-proxy refuses an outer one. The envelope is the contract's own,
+ // so a request the proxy would refuse never leaves this process (#784).
+ if ((input.request as { schemaVersion?: string } | null)?.schemaVersion === SCHEMA_VERSION_V2) {
+ const parsed = parseGrokCompletionRequest(input.request)
+ if (!parsed.ok) throw new GrokProxyError('invalid_request', parsed.message, false)
+ if (input.deadlineMs !== undefined && input.deadlineMs !== parsed.value.deadlineMs) {
+ throw new GrokProxyError(
+ 'invalid_request',
+ 'Grok deadline must match the authorized request',
+ false
+ )
+ }
+ const envelope = buildGrokProxyEnvelope({
+ executionTicket: input.executionTicket,
+ requestHash: input.requestHash,
+ request: parsed.value,
+ })
+ if (!envelope.ok) {
+ const code =
+ envelope.code === 'limit'
+ ? 'payload_too_large'
+ : envelope.code === 'request_hash_mismatch'
+ ? 'request_hash_mismatch'
+ : 'invalid_request'
+ throw new GrokProxyError(code, envelope.message, false)
+ }
+ body = envelope.value
+ }
const jwt = this.options.readPlatformJwt()
const fetchFn = this.options.fetchFn ?? fetch
const response = await fetchFn(this.options.runtimeUrl, {
@@ -101,12 +142,7 @@ export class GrokLlmProxyClient {
authorization: `Bearer ${jwt}`,
'content-type': 'application/json',
},
- body: JSON.stringify({
- executionTicket: input.executionTicket,
- requestHash: input.requestHash,
- request: input.request,
- ...(input.deadlineMs !== undefined ? { deadlineMs: input.deadlineMs } : {}),
- }),
+ body: JSON.stringify(body),
signal: input.signal,
})
if (!response.ok) {
diff --git a/mcp-host/src/llm/providerAttemptAuthorizer.ts b/mcp-host/src/llm/providerAttemptAuthorizer.ts
index 9c3f27ea7..32a66e6ad 100644
--- a/mcp-host/src/llm/providerAttemptAuthorizer.ts
+++ b/mcp-host/src/llm/providerAttemptAuthorizer.ts
@@ -1,3 +1,8 @@
+import {
+ LIMITS as GROK_LIMITS,
+ PROVIDER_ID as GROK_PROVIDER_ID,
+ requestBodyLimitBytes as grokRequestBodyLimitBytes,
+} from '@clerum/grok-provider-attempt-contract'
import {
ENVELOPE_ALLOWANCE_BYTES,
LIMITS,
@@ -7,6 +12,29 @@ import {
export const AUTHORIZE_PATH = '/api/v1/mcp-host/llm/provider-attempts/authorize'
+type RequestContract = {
+ label: 'Codex' | 'Grok'
+ maxRequestBodyBytes: number
+ requestBodyLimitBytes: (request: unknown) => number
+}
+
+/**
+ * The contract that bounds `request`. The authorize route is shared, and
+ * control-api picks the Grok contract by `request.provider` (#784); checking a
+ * Grok request against the Codex caps here would refuse a Grok V2 body that
+ * control-api accepts.
+ */
+function requestContract(request: unknown): RequestContract {
+ if ((request as { provider?: unknown } | null)?.provider === GROK_PROVIDER_ID) {
+ return {
+ label: 'Grok',
+ maxRequestBodyBytes: GROK_LIMITS.maxRequestBodyBytes,
+ requestBodyLimitBytes: grokRequestBodyLimitBytes,
+ }
+ }
+ return { label: 'Codex', maxRequestBodyBytes: LIMITS.maxRequestBodyBytes, requestBodyLimitBytes }
+}
+
/**
* Room for the authorize envelope around the contract-capped `request`: ids,
* revisions, hashes and recipe names, a few hundred bytes in practice. The
@@ -90,17 +118,18 @@ export class ProviderAttemptAuthorizer {
expiresAt: string
}> {
const serialized = JSON.stringify(body)
- const requestLimit = requestBodyLimitBytes(body.request)
+ const contract = requestContract(body.request)
+ const requestLimit = contract.requestBodyLimitBytes(body.request)
// The larger of the two budgets wins, as in control-api's authorizer: the
// non-image cap plus the envelope allowance, or the V2 visual envelope.
const bodyLimit = Math.max(
requestLimit,
- LIMITS.maxRequestBodyBytes + AUTHORIZE_ENVELOPE_ALLOWANCE_BYTES
+ contract.maxRequestBodyBytes + AUTHORIZE_ENVELOPE_ALLOWANCE_BYTES
)
if (Buffer.byteLength(serialized, 'utf8') > bodyLimit) {
throw new CodexAuthorizeError(
'payload_too_large',
- `Codex request exceeds ${requestLimit / (1024 * 1024)} MiB; use fewer or smaller images, or reduce context`
+ `${contract.label} request exceeds ${requestLimit / (1024 * 1024)} MiB; use fewer or smaller images, or reduce context`
)
}
const jwt = this.options.readPlatformJwt()
@@ -135,7 +164,7 @@ export class ProviderAttemptAuthorizer {
throw new CodexAuthorizeError(
code,
code === 'payload_too_large'
- ? 'Codex request is too large; use fewer or smaller images, or reduce context'
+ ? `${contract.label} request is too large; use fewer or smaller images, or reduce context`
: `authorize failed with ${response.status}`
)
}