Skip to content

refactor(core): pin request headers per release instead of host policy (ENG-5359) - #31

Merged
Unic (YuniqueUnic) merged 3 commits into
mainfrom
unic/eng-5359-mcp-photon-version
Nov 21, 2026
Merged

Unic (YuniqueUnic) merged 3 commits into
mainfrom
unic/eng-5359-mcp-photon-version

Conversation

@YuniqueUnic

@YuniqueUnic Unic (YuniqueUnic) commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

The MCP server sent no Photon-Version header, 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.

// host: packages/mcp/scripts/generate-release.mjs (version from catalog/api-version.json)
compilerPolicy: { operationPollLimit: 3, pinnedHeaders: { "photon-version": await readApiVersion() } }

// packages/core/src/runtime/invoke.ts
if (reservedHeaderName.test(name) || pinnedHeaderValue(request.pinnedHeaders, name) !== undefined) throw new InvocationError("forbidden_header");
for (const [name, value] of Object.entries(request.pinnedHeaders)) headers.set(name, value);
Input or condition Result
Any upstream request, including retries and Operation polls Exactly one photon-version: 2026-07-01 (before: none)
Operation declares Photon-Version, optional or required Omitted from the tool schema (before: optional tool argument)
Pinned value not allowed by an operation's schema, e.g. 2027-01-01 Compile fails: pinned_header_rejected_by_operation: deleteAccount: Photon-Version="2027-01-01"
Pin "2" on an integer header, or "true" on a boolean header Accepted: a scalar that serializes to the pin satisfies the schema, the same rule tool input follows
Pin that no compiled operation declares, e.g. a misspelled photon-versoin Compile fails: pinned_header_not_declared: photon-versoin
Pinned name reserved (including hop-by-hop names such as connection), duplicated by case, not a token, or value with CR/LF or empty Compile fails: invalid_pinned_header
pinnedHeaders: {} Compiles; nothing extra is sent
Tool input names Photon-Version in any case Rejected exactly like Authorization; no upstream request (before: forwarded)
Pinned map changes between releases pinnedHeaders_changed, decision review
Release document without pinnedHeaders Rejected by parseRelease; both hosts regenerate releases on every build
Stored pin map with a non-canonical key, e.g. { "Photon-Version": … }, passed to bindVerifiedCapability Rejected with invalid_pinned_header; a valid map is copied and frozen
Disabled operation (x-photon-mcp: { enabled: false }) whose pinned header has a different schema Skipped, because MCP never calls it. Linked private Operation getters are still checked
Pinned header declared twice on an admitted operation duplicate_parameter

Implementation

  • packages/core/src/headers.ts: reservedHeaderName is 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's fetch throws on expect, keep-alive, transfer-encoding and upgrade, and overwrites connection.
  • packages/core/src/pinned-headers.ts owns the pin map. It normalizes compile input, parses stored releases (keys must already be canonical; bindVerifiedCapability and release activation share that parser), and provides pinnedHeaderValue(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.ts turns declared parameters into tool input. It is moved out of compiler/schema.ts, which drops from 1018 to 739 lines. Each declared parameter is either input or pinned; a pinned one is checked against its schema and left out of tool input. compileRelease then fails for any pin that no compiled operation declares.
  • pinnedHeaders is a release-level field, required in compilerPolicy, stored once, covered by the checksum and identity, and reported by the semantic change report. Nothing in core names Photon-Version.
  • @photon-ai/mcp: the version lives in catalog/api-version.json as { "version": "2026-07-01" }. refresh-catalog (fetches /openapi.json?version=<version> with the header) and generate-release read it through one readApiVersion(). src/local.ts is back to its main shape.
  • Replaces this PR's first design (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-core hosts: compilerPolicy.pinnedHeaders is required, and releases compiled without it no longer load. schemaVersion stays 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/mcp 0.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, so release.yml, which checks that the core version in packages/core/package.json is on npm, blocks any @photon-ai/mcp release until core 0.2.0 is published; @photon-ai/mcp stays at its main version for the release flow to bump.

API changes for direct core callers:

  • bindVerifiedCapability(capability, validator) becomes bindVerifiedCapability(capability, { validator, pinnedHeaders }).
  • ActivatedRuntimeRelease, ActivatedExecution and InvocationRequest gain pinnedHeaders.
  • compileCapability takes { asyncOperation, extension, pinnedHeaders } as its third argument.
  • compareUtf8 is imported from utf8 instead of release.
  • Hop-by-hop header parameters follow the reserved-header rule: an optional one is left out of tool input, and a required one fails with 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 verify passed 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.ts calls 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 one photon-version: 2026-07-01; calls setting Photon-Version in any casing or Authorization were 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

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
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cef116ac-404c-4e86-8599-746b158288c2
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Duplicated code

Exact duplication Before 1e5cff9 After fc24b5e
Duplicated lines 150 (1.85%) 136 (1.62%)
Clone pairs 11 10
Scanned files 31 35

3 new findings · semantic scan incomplete (inference limit).

@YuniqueUnic Unic (YuniqueUnic) changed the title feat(core): pin Photon-Version on every upstream request (ENG-5359) refactor(core): pin request headers per release instead of host policy (ENG-5359) Nov 19, 2026
…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.

@yukkodesu Yukko (yukkodesu) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 pinnedHeaders map remains mutable on the returned ActivatedExecution.
  • 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 with forbidden_header before fetch (even though headers.set later 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:

  1. catalog.yml will verify against the workspace core, auto-merge a patch bump of @photon-ai/mcp, and create a new mcp-v* release.
  2. release.yml's npm view "@photon-ai/mcp-core@$core" check will pass against the already-published 0.1.7.
  3. The tarball smoke test will then install @photon-ai/mcp-core@0.1.7 from npm and fail on invalid_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): uses request.execution.pinnedHeaders rather than getter.pinnedHeaders (and the new test in packages/core/test/execution.test.ts sets pinnedHeaders only on execution, leaving operationGetter.pinnedHeaders as {}). Using getter.pinnedHeaders is more consistent with getter.capability and mapResponse(response, getter).
  • packages/core/src/pinned-headers.ts (normalizeHeaderEntries): sorting entries with compareUtf8 before Object.fromEntries makes the in-memory object key order deterministic, not just the serialized canonical JSON.
  • packages/mcp/scripts/refresh-catalog.mjs: consider asserting typeof date === "string" && date.length > 0 before 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
@YuniqueUnic

Copy link
Copy Markdown
Collaborator Author

Thanks, all three points held up. Addressed in fc24b5e:

  1. bindVerifiedCapability now runs the stored pin map through parseStoredPinnedHeaders, the same parser release activation uses, so a non-canonical key such as Photon-Version is rejected with invalid_pinned_header and the returned execution holds a frozen copy. The signature change and the new pinnedHeaders fields are in the PR description's compatibility notes.
  2. Pins are checked only on admitted tools and linked private Operation getters, so a disabled operation with a conflicting pin schema now compiles, and a header declared twice on an admitted operation still fails with duplicate_parameter. Both have tests.
  3. packages/core is now 0.2.0, so release.yml's npm check blocks any @photon-ai/mcp release until core 0.2.0 is published. I checked the guard: it passes for 0.1.7 and fails for 0.2.0 today.

Nits: polling uses getter.pinnedHeaders (the test sets different pins on the getter and the initiating operation, so it fails on the old code), normalizeHeaderEntries sorts with compareUtf8, and both mcp scripts read catalog/api-version.json through one readApiVersion(), which rejects a missing, empty or padded value before any fetch.

While in there, a second pass tightened a few things:

  • A pin that no compiled operation declares fails with pinned_header_not_declared, so a misspelled name can't compile and go out on every request.
  • A pin passes a typed header when a scalar that serializes to it does, so "2" passes an integer schema, matching tool input.
  • A pinned header with no schema reports missing_parameter_schema instead of a schema mismatch.
  • Hop-by-hop names (connection, transfer-encoding and so on) are reserved. Node 24's fetch throws on four of them and overwrites connection, so a pin could never be sent as written.
  • The parameter code moved to compiler/parameters.ts, which takes compiler/schema.ts from 1018 to 739 lines. Each declared parameter is now explicitly input or pinned. toolMayNotSetHeader is gone: reservedHeaderName and pinnedHeaderValue are separate rules, and the compiler and runtime use the same lookup.
  • compareUtf8 is exported only from utf8.ts instead of through an alias in release.ts. The api-version field is now version, since versions are opaque strings.
  • The pin tests moved to test/pinned-headers.test.ts, with the cases that only exercised the JSON Schema library removed.

On rebasing: I merged nothing into the branch. CI runs on the merge ref with current main, and locally the branch merged with 590ddbf passes pnpm verify (595 core, 27 mcp) and compiles the committed catalog unchanged to 146 tools.

@YuniqueUnic
Unic (YuniqueUnic) merged commit 582a35a into main Nov 21, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants