Repository navigation
refactor(core): pin request headers per release instead of host policy (ENG-5359) - #31
Conversation
The runtime sends policy.apiVersion as Photon-Version on every upstream request, including retries and Operation polls, and rejects tool input that sets it. The compiler leaves a Photon-Version header parameter out of tool schemas whether it is optional or required. @photon-ai/mcp keeps the date in src/api-version.json and fetches the catalog for the same date. Refs ENG-5359
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
Duplicated code
3 new findings · semantic scan incomplete (inference limit).
|
…y (ENG-5359) Replace the Photon-Version runtime policy with a general compile-time primitive. compilerPolicy.pinnedHeaders names headers the release sends on every upstream request. The compiler validates each pinned value against the operations that declare that header, omits the parameter from tool schemas, and stores the map once in the release so it is part of its identity and change report. One predicate, toolMayNotSetHeader, decides which headers tool input may never set, for reserved and pinned names. @photon-ai/mcp keeps its date next to the catalog and reads it only at build time; the local server needs no version code.
Yukko (yukkodesu)
left a comment
There was a problem hiding this comment.
The overall design is very clean—moving from a vendor-specific runtime policy to compile-time compilerPolicy.pinnedHeaders keeps @photon-ai/mcp-core vendor-agnostic, keeps runtime decoupled from compiler, and integrates cleanly with release identity, bundle verification, semantic change reporting, retries, and operation polling.
Requesting a few changes before merging:
1. Validate and freeze pinnedHeaders in bindVerifiedCapability
In packages/core/src/runtime/runtime-release.ts, bindVerifiedCapability calls deepFreeze(capability) because callers may pass freshly deserialized JSON objects without going through activateRuntimeRelease, but release.pinnedHeaders is forwarded to activateExecution without freezing or validation:
- A freshly deserialized
pinnedHeadersmap remains mutable on the returnedActivatedExecution. - If a caller passes non-canonical keys (e.g.
{ "Photon-Version": "2026-07-01" }),toolMayNotSetHeader(name, request.pinnedHeaders)(Object.hasOwn(pinnedHeaders, name.toLowerCase())) will silently fail to reject caller-supplied headers withforbidden_headerbefore fetch (even thoughheaders.setlater overwrites the value).
Running parseStoredPinnedHeaders(release.pinnedHeaders) inside bindVerifiedCapability enforces canonical lowercase keys, valid non-reserved header names/values, and freezes the map in one step.
(Also worth noting the signature change to bindVerifiedCapability and the new pinnedHeaders field on ActivatedRuntimeRelease / InvocationRequest in the PR description's compatibility notes.)
2. Scope of consumePinnedHeaders on producer-disabled operations
In packages/core/src/compiler/compile.ts, consumePinnedHeaders runs over every operation from listPathOperations(aggregate) before admittedOperations(operations) filters out x-photon-mcp: { enabled: false } operations.
Elsewhere in the compiler, disabled operations are explicitly allowed to carry unsupported transports/methods/schemas without failing compilation (see skips producer-disabled unsupported methods and transports in compiler.test.ts). With the current placement, a disabled endpoint that MCP will never invoke (and that is not a private operationGetter) will break the entire release build if its pinned header parameter has a different schema, uses content, or contains a schema keyword unsupported by closeSchema. Filtering before groupedParameters also masks duplicate_parameter if an operation declares the same pinned header twice.
Consider applying pinned-header validation/removal inside compileCapability (which covers both admitted operations and private operationGetters). If validating disabled operations is intentional as a whole-catalog check, please document and test that behavior explicitly.
3. Release ordering hazard with daily catalog.yml
packages/core/package.json is still 0.1.7 (already published on npm), and packages/mcp/package.json depends on @photon-ai/mcp-core: "workspace:*". Because the new pinnedHeaders field in releaseIdentity is breaking for 0.1.7, if this PR merges while packages/core stays at 0.1.7 and the daily catalog.yml workflow triggers before 0.2.0 is bumped and published:
catalog.ymlwill verify against the workspace core, auto-merge a patch bump of@photon-ai/mcp, and create a newmcp-v*release.release.yml'snpm view "@photon-ai/mcp-core@$core"check will pass against the already-published0.1.7.- The tarball smoke test will then install
@photon-ai/mcp-core@0.1.7from npm and fail oninvalid_runtime_release.
Bumping packages/core/package.json to 0.2.0 (or coordinating the 0.2.0 bump & release immediately on merge) ensures release.yml's guard prevents a mismatched @photon-ai/mcp release.
Minor nits
packages/core/src/runtime/execution/state.ts(pollOperation): usesrequest.execution.pinnedHeadersrather thangetter.pinnedHeaders(and the new test inpackages/core/test/execution.test.tssetspinnedHeadersonly onexecution, leavingoperationGetter.pinnedHeadersas{}). Usinggetter.pinnedHeadersis more consistent withgetter.capabilityandmapResponse(response, getter).packages/core/src/pinned-headers.ts(normalizeHeaderEntries): sorting entries withcompareUtf8beforeObject.fromEntriesmakes the in-memory object key order deterministic, not just the serialized canonical JSON.packages/mcp/scripts/refresh-catalog.mjs: consider assertingtypeof date === "string" && date.length > 0before interpolating into?version=...and request headers.- Please rebase onto latest
main(which picked up #32) before merging so CI verifies against the current catalog.
Review follow-up for the release-pinned headers: - bindVerifiedCapability parses and freezes the stored pin map with the same parser as release activation, so non-canonical keys are rejected. - Pins are checked only on admitted tools and linked private Operation getters. Disabled operations no longer break the build, and a header declared twice still fails with duplicate_parameter. - A pin that no compiled operation declares fails with pinned_header_not_declared. A pin satisfies a typed header when a scalar that serializes to it does, so "2" passes an integer schema. - Hop-by-hop header names join the reserved names. - Parameter compilation moves to compiler/parameters.ts, which takes compiler/schema.ts under 800 lines. Reserved names and pin lookup are separate rules again, and compareUtf8 is exported only from utf8.ts. - Core moves to 0.2.0 so release.yml blocks an @photon-ai/mcp release until that core is on npm. - Polls use the getter's pins and pin maps sort by UTF-8. Both mcp scripts read catalog/api-version.json through one readApiVersion(). Refs ENG-5359
|
Thanks, all three points held up. Addressed in fc24b5e:
Nits: polling uses While in there, a second pass tightened a few things:
On rebasing: I merged nothing into the branch. CI runs on the merge ref with current main, and locally the branch merged with |
The MCP server sent no
Photon-Versionheader, and 110 of 249 production operations exposed it as an optional tool argument, so the model chose whether to send a date. Production will reject requests without it once the switch date is announced. A release now pins the header at compile time: every upstream request carries exactly one value, the compiler checks that value against every operation that declares the header, and tools can no longer set it.photon-version: 2026-07-01(before: none)Photon-Version, optional or required2027-01-01pinned_header_rejected_by_operation: deleteAccount: Photon-Version="2027-01-01""2"on an integer header, or"true"on a boolean headerphoton-versoinpinned_header_not_declared: photon-versoinconnection), duplicated by case, not a token, or value with CR/LF or emptyinvalid_pinned_headerpinnedHeaders: {}Photon-Versionin any caseAuthorization; no upstream request (before: forwarded)pinnedHeaders_changed, decisionreviewpinnedHeadersparseRelease; both hosts regenerate releases on every build{ "Photon-Version": … }, passed tobindVerifiedCapabilityinvalid_pinned_header; a valid map is copied and frozenx-photon-mcp: { enabled: false }) whose pinned header has a different schemaduplicate_parameterImplementation
packages/core/src/headers.ts:reservedHeaderNameis the one rule for headers tool input may never set, and a release may not pin them. This PR adds the hop-by-hop names (connection,expect,keep-alive,te,trailer,transfer-encoding,upgrade), which describe one connection rather than the request. Node 24'sfetchthrows onexpect,keep-alive,transfer-encodingandupgrade, and overwritesconnection.packages/core/src/pinned-headers.tsowns the pin map. It normalizes compile input, parses stored releases (keys must already be canonical;bindVerifiedCapabilityand release activation share that parser), and providespinnedHeaderValue(pins, name), the one case-insensitive lookup that the compiler and the runtime both use. The runtime imports nothing from the compiler.packages/core/src/compiler/parameters.tsturns declared parameters into tool input. It is moved out ofcompiler/schema.ts, which drops from 1018 to 739 lines. Each declared parameter is eitherinputorpinned; a pinned one is checked against its schema and left out of tool input.compileReleasethen fails for any pin that no compiled operation declares.pinnedHeadersis a release-level field, required incompilerPolicy, stored once, covered by the checksum and identity, and reported by the semantic change report. Nothing in core namesPhoton-Version.@photon-ai/mcp: the version lives incatalog/api-version.jsonas{ "version": "2026-07-01" }.refresh-catalog(fetches/openapi.json?version=<version>with the header) andgenerate-releaseread it through onereadApiVersion().src/local.tsis back to its main shape.policy.apiVersion,parseApiVersion,hostSuppliedHeaderName), which bound core to one vendor header and gave the date two owners per host.Compatibility and release
Breaking for
@photon-ai/mcp-corehosts:compilerPolicy.pinnedHeadersis required, and releases compiled without it no longer load.schemaVersionstays 1 because both hosts compile their release at build time, so no stored release crosses the upgrade. Suggested order: core 0.2.0, then@photon-ai/mcp0.2.0 (its release requires that core on npm), then mcp-internal moves to the new core and passes the same field before that switch. Core moves to 0.2.0 here, sorelease.yml, which checks that the core version inpackages/core/package.jsonis on npm, blocks any@photon-ai/mcprelease until core 0.2.0 is published;@photon-ai/mcpstays at its main version for the release flow to bump.API changes for direct core callers:
bindVerifiedCapability(capability, validator)becomesbindVerifiedCapability(capability, { validator, pinnedHeaders }).ActivatedRuntimeRelease,ActivatedExecutionandInvocationRequestgainpinnedHeaders.compileCapabilitytakes{ asyncOperation, extension, pinnedHeaders }as its third argument.compareUtf8is imported fromutf8instead ofrelease.forbidden_header_parameter. The committed catalog declares none.The committed catalog is unchanged and matches
2026-07-01. A PR that changes the version must refresh the catalog, and compile fails if it does not.Validation
On this branch merged with main (
590ddbf):pnpm verifypassed with 595 core and 27 mcp tests, Biome, type checks and both builds; the committed catalog compiled unchanged to 146 tools. Each row of the table above has a test. Run against the previous revision of this PR, the new tests for typed pins, the undeclared pin, the hop-by-hop name and a pinned header without a schema fail; the three cases from the first review (non-canonical stored key, disabled operation with a conflicting pin schema, duplicate declaration) failed before that fix.packages/mcp/test/local.test.tscalls a compiled tool through the MCP SDK client and checks the pinned header on the upstream request. A real MCP client over stdio, run before this revision's refactor, against a local upstream that records raw headers: 8 upstream requests (a read, a write, three retry attempts, an async start and two polls, the polls using the getter's pins) each carried exactly onephoton-version: 2026-07-01; calls settingPhoton-Versionin any casing orAuthorizationwere rejected before any upstream request.release.yml's version guard, unchanged, passes for the published core 0.1.7 and fails for 0.2.0 until it is on npm. Earlier rounds: mcp-internal's full verify passed against this core through a local overlay. Not run: a live call against api.photon.codes with credentials.Refs ENG-5359