Skip to content

feat(server_runtime): refuse MCP session tokens older than mcp_token_max_age_seconds - #6782

Closed
d-molinari wants to merge 1 commit into
mainfrom
dmolinari/mcp-token-max-age
Closed

d-molinari wants to merge 1 commit into
mainfrom
dmolinari/mcp-token-max-age

Conversation

@d-molinari

Copy link
Copy Markdown
Contributor

What does this PR do?

Lets a resources server refuse MCP session tokens older than a limit. This is the follow-up promised in #6161's review: URLSafeSerializer carries no timestamp, so a token stays valid as long as the server accepts its session, and code that reads one in a sandbox can use it after its episode.

  • Tokens are signed with URLSafeTimedSerializer, so each carries the second its seed signed it.
  • New option mcp_token_max_age_seconds (needs expose_tools_over_mcp). A request to /mcp whose token is older is answered before the MCP SDK reads it: HTTP 401 with {"error": "invalid_token", "error_description": ...}, the reply the SDK's own bearer authentication gives. With mcp_bearer_token it also carries WWW-Authenticate: Bearer error="invalid_token", ... (RFC 6750) and names no framework. No tool runs.
  • The checkpoint middleware runs before the endpoint. It reads an expired token as one that does not verify, so the 401 comes first. Session routing still routes the token to its session's worker, whose endpoint refuses it.
  • A token signed "after now", which a step back of the host clock makes, is not refused: the age is computed here, not with itsdangerous's own max_age, which refuses negative ages.
  • The tutorial's section on sandboxed endpoints describes the option.

Validation

On Linux (aarch64, Python 3.13), at this commit (uv sync --extra dev):

  • tests/unit_tests/server_runtime/{test_mcp_auto_exposure,test_checkpoint_resources,test_checkpoint_resources_mcp,test_session_routing,test_base_server,test_episode_tokens,test_checkpoint_participant_workers}.py: 565 passed.

  • The new tests on main's nemo_gym/server_runtime: each fails. The two "no limit: any age is served" controls pass on both, as they should.

  • Mutants, each failing its test:

    • no 401 refusal;
    • the challenge sent without bearer tokens;
    • the checkpoint claims reading any age;
    • the option not checked against expose_tools_over_mcp;
    • future tokens refused;
    • the age not read.
  • CI-equivalent gate: core shards 0-3 with coverage (6,454 / 5,750 / 6,318 / 6,246 passed), -m sandbox (2,103 passed), combine_core_coverage.py: 95.10%.

  • Server suites of every component that exposes MCP or reads its token:

    • resources: example_mcp_weather, workplace_assistant, agentic_mme, hle_web_tools, rna_design, openenv, newton_bench, interactive_browser, gdpval, equivalence_llm_judge;
    • environment: single_agent_turn;
    • agents: claude_code_agent, codex_agent, hermes_agent, haystack_agent, pi_sandboxed_agent, opencode_sandboxed_agent, nemo_fabric_agent, harness_agent;
    • officeenv with OfficeEnv 1.10.0.

    All passed except interactive_browser's 6 local_playwright backend tests. They need a Chromium that host lacks, and they fail the same way with main's nemo_gym.

  • tests/e2e/session_routing (NEMO_GYM_MULTIWORKER_E2E=1), 3 runs: 3/3 passed each.

  • pre-commit run --files on the 7 changed files: passed.

Rollouts

N/A: no environment or agent behavior changes unless a config sets the new option, and none in this repo does.

Compatibility and benchmark impact

None for existing configs. The option is off by default, and a token of any age is then served as before. The token format changes (it gains a timestamp), but only the server that signed a token reads it.

Checklist

  • I have read the contributing guidelines.
  • The change is focused; unrelated "drive-by" edits are tracked as separate issues/PRs.
  • Tests added or updated and pass locally, or N/A for docs-only / non-code changes (so CI unit/server checks pass when applicable).
  • Documentation added or updated, or N/A with justification.
  • Pre-commit checks pass locally (run on the changed files).
  • New source files include the required Apache-2.0 SPDX header. N/A: no new files.
  • All commits have DCO sign-off (git commit -s) (so the DCO check passes).

🤖 Generated with Claude Code

…max_age_seconds

MCP session tokens were signed with URLSafeSerializer, which carries no
timestamp, so a token stayed valid as long as the server accepted its
session. Code in a sandbox that reads a token could use it after its
episode.

Tokens are now signed with URLSafeTimedSerializer. A resources server that
sets mcp_token_max_age_seconds answers a request to /mcp whose token is
older with HTTP 401 and the OAuth invalid_token error, before the MCP SDK
reads it; with mcp_bearer_token the reply also carries the RFC 6750
WWW-Authenticate challenge. The checkpoint middleware, which runs first,
reads such a token as one that does not verify and leaves it to the
endpoint. Session routing still sends it to its session's worker, whose
endpoint refuses it.

The option is off by default, and a token is then accepted at any age, as
before. A token signed after now, which a step back of the host clock
makes, is not refused.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Daniele Molinari <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@d-molinari

Copy link
Copy Markdown
Contributor Author

Folded into #6746, which now carries this commit unchanged on top of #6746's two and #6796's, rebased onto main, so the three are reviewed together. Closing.

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.

1 participant