Repository navigation
feat(core): session secrets, cookie names and MCP endpoint options for servers that sandboxed code calls - #6161
d-molinari wants to merge 2 commits into
Conversation
|
/ok to test d60160b |
|
🌿 Preview your docs: https://nvidia-preview-pr-6161-6341cc906898.docs.buildwithfern.com/nemo/gym |
svcnvidia-nemo-ci
left a comment
There was a problem hiding this comment.
SHIP WITH CARE
Two RISK findings affect the new mcp_server_name option: some accepted names fail during agent seeding, and the shared legacy sandbox seeder ignores the advertised name.
I reviewed all 13 changed files and traced the affected signing, routing, checkpoint, grant, and agent-consumer paths. One delegated report wasn't available, so I completed that topic's source inspection directly. This was a static review; I didn't execute tests or model rollouts, and the PR's validation results remain author-reported.
Inline comments (escaped v1):
-
nemo_gym/server_runtime/tool_access.pyline 47: RISK — Accept the new endpoint names in the MCP grant contractWith
expose_tools_over_mcp: trueandmcp_server_name: _company, the new configuration field validates, but asingle_agent_turnepisode granting those tools fails before agent seeding. _mcp_tool_access copies the advertised name into this field, whose pattern rejects a leading_or-. Every episode using either newly accepted leading character fails instead of carrying the advertised name unchanged.Could you align this consumer with the endpoint's supported alphabet? At this field, the small correction is:
name: str = Field(min_length=1, pattern=r"^[A-Za-z0-9_-][A-Za-z0-9._-]*$")This preserves existing alphanumeric names and interior dots while accepting the new leading characters. Keep
DirectHTTPToolAccessunchanged, and retain rejection of empty names and leading dots. The inspected OpenCode consumer uses the name unchanged as its configuration key; Codex's name predicate also permits these characters.Proposed checks, not executed: parameterize the metadata-to-grant test with
_company,-company, andcompany; each episode should reach agent seeding with that exact name and unchanged Authorization header. Add contract assertions thatcompany.toolsremains accepted while''and.companyremain rejected. Retain the collision cases: equal names on two granted MCP endpoints fail, but direct-HTTP-only Resources access causes no MCP-name collision. Verify the suggested code before adopting it. -
nemo_gym/agents/agent_tools.pyline 67: RISK — Honor the advertised name in the shared sandbox seederThe new option changes seed metadata's
server_name, butseed_mcp_serversstill keys each client configuration entry by the deployment reference. For example, a tool server referenced asstorewithmcp_server_name: companyproduces an entry namedstore, notcompany. PiSandboxedAgent passes these entries into its MCP configuration, and the adapter constructs tool names from those keys, so the model still seesstore_appendrather thancompany_append. The legacy OpenCode path uses the same helper. These sandbox callers therefore ignore the new identity option.Could you use the metadata name for the client entry while retaining the deployment reference for HTTP routing? Immediately before constructing the entry here, a suggested correction is:
listed_as = metadata.get("server_name") or server.name if listed_as in entries: raise ValueError(f"Tool servers advertise the same MCP name {listed_as!r}") entries[listed_as] = { # Keep the existing URL, headers, enabled, and timeout fields. }
The dependent OpenCode change is required: its required-server environment variable must use the same effective entry keys, or the required-server check will reject the renamed connection. Before building that command, derive them as:
rollout_mcp = getattr(request.state, "_ng_opencode_mcp", None) required_mcp_names = ( list(rollout_mcp) if isinstance(rollout_mcp, dict) else [s.name for s in self.config.tool_servers] )
Then serialize
required_mcp_namesin the environment variable. Keepsandbox_server_url(server.name, ...), deployment lookups, and network routing on the real server reference. Preserve the old name when metadata omitsserver_name, unchanged headers/timeouts, and existing static-config precedence. Reject duplicate advertised names within one seeded set rather than silently overwriting an endpoint; separate episodes may still reuse a name.Proposed checks, not executed: stub a seed reply with
server_name='company', URL path/mcp, andAuthorization: Bearer scoped. Seeding referencestoreshould return only keycompany, with the URL still resolved throughstoreand the header unchanged. Withoutserver_name, expect keystore. Two references advertisingcompanyshould fail explicitly. Check that Pi's generated configuration usescompany, and OpenCode's configuration and required-server list both usecompany. Retain the per-run configuration test's assertions that credentials stay out of the receipt and the static configuration remains unchanged. Verify these suggestions before adopting them.
Review generated by codex. This was a full review. Request a full review with /review reset=true.
cmunley1
left a comment
There was a problem hiding this comment.
Looks good overall. With every option off the behavior matches today (same header, key and cookie name, same error text), and the new tests fail when the guarded lines are broken.
RISK: mcp_bearer_token with the default session secret (base_resources_server.py:186, base_server.py:348)
The default get_session_secret() is the public Class___name string, which is also the default cookie name, so it shows up in every Set-Cookie header. With mcp_bearer_token: true and no override, sandboxed code can sign {"sid": <another session>, "tools": null} and get every tool on another session, which is the per-session grant this option exists to enforce.
Suggest refusing the config at startup when mcp_bearer_token is on and get_session_secret() == get_session_middleware_key().
NOTE: tokens never expire (base_resources_server.py:104)
URLSafeSerializer has no timestamp, so only the session fence ends a token. This was already true on main, but this PR is the one that hands tokens to code the model can read. URLSafeTimedSerializer with max_age on load would close it.
40a158b to
2efe9fd
Compare
|
Thanks, both. Fixed in 2efe9fd, and the branch is now rebased onto main (4e3f503):
|
|
/ok to test 2efe9fd |
…r servers that sandboxed code calls A resources server that lends a sandbox is reached by code the model controls, so the session key that every caller can compute from the server's class and name must not sign its cookies and MCP tokens, and its endpoint should look like an ordinary one. - SimpleServer.get_session_secret() signs session cookies and MCP session tokens (default: the session middleware key, so nothing changes). Session routing reads cookies and tokens with it. Its docstring states what an override must keep: one secret in every worker, and a secret that changes ends every open session. - SimpleServer.get_session_cookie_name() names the session cookie (default: the same key). - mcp_server_name: the name the MCP endpoint lists itself under; verify-time normalization strips its namespace. - mcp_bearer_token: tokens ride as Authorization: Bearer, tools/list needs one and lists only the session's tools, a tool outside the grant is an unknown tool, and errors name no framework. - Both options are refused without expose_tools_over_mcp, and a name outside MCP's tool-name alphabet is refused. A single_agent_turn pairing refuses to grant two MCP endpoints listed under one name. Signed-off-by: Daniele Molinari <dmolinari@nvidia.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…er tokens without a secret Review fixes for the MCP endpoint options. - mcp_server_name is also checked against the name pattern of an MCP grant (MCP_TOOL_ACCESS_NAME_PATTERN in tool_access, which MCPToolAccess uses), so a name that an Environment Server cannot grant, one that starts with '_' or '-', is refused at config instead of failing every episode before the agent seed. The grant contract is unchanged. A dot stays refused: Pi's MCP adapter and Codex refuse server names outside [A-Za-z0-9_-]. - seed_mcp_servers keys each entry by the seed metadata's server_name, else the server's name, and refuses two servers listed under one name. URLs, headers and routing still use the server's reference. The sandboxed OpenCode agent's required-server list uses the same keys; the sandboxed Pi agent reads them as given. - An MCP endpoint with mcp_bearer_token refuses to start while get_session_secret() is the public session middleware key, since code that reads one bearer token could sign another session's. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Daniele Molinari <dmolinari@nvidia.com>
2efe9fd to
6341cc9
Compare
|
/ok to test 6341cc9 |
What does this PR do?
Lets a server that code in a sandbox calls keep its sessions and its MCP endpoint to itself. It is the first of three pieces that #5010's review split out: this one, then
single_agent_turn's step loop (#6162, built on this branch), then OfficeEnv's sandboxed server (#5010, built on that one). Every option is off by default, and the defaults are today's behavior.SimpleServer.get_session_secret()is the key that signs session cookies and MCP session tokens, and session routing reads both with it. Its default is today's key,get_session_middleware_key(). The docstring says what an override must keep: one secret in every worker, and a secret that changes ends every open session.SimpleServer.get_session_cookie_name()is the session cookie's name (default: the same key).mcp_server_nameonBaseResourcesServerConfigis the name the MCP endpoint lists itself under in the seed's MCP metadata. Clients name its toolsmcp__<name>__<tool>, and verify-time normalization strips that namespace.mcp_bearer_tokenhands out the session token asAuthorization: Bearer.tools/listthen needs a token and lists only the session's tools, a tool outside the grant is answered as an unknown tool, and errors name no framework.expose_tools_over_mcp.mcp_server_namemust be a name an MCP grant takes (MCP_TOOL_ACCESS_NAME_PATTERN, which starts with a letter or digit) and fit MCP's tool-name alphabet. Asingle_agent_turnpairing that grants two MCP endpoints listed under one name does not start, since their tools would collide; the check is per pairing, so two servers in one run may share a name when no episode is granted both.mcp_bearer_tokenrefuses to start while the server signs its tokens with the default key: that key is public (the default cookie name shows it), so code that reads one token could sign another session's. A server that sets the option overridesget_session_secret().seed_mcp_servers) lists each tool server under the name its endpoint advertises, so the sandboxed Pi and OpenCode agents name its tools that way; two servers advertising one name are refused.Validation
On main
a57f9cd437:-m sandbox): all four shards and the 1,820 sandbox tests pass; combined core coverage 95.18%.gym env test:environment_servers/single_agent_turn299 passed,responses_api_agents/pi_sandboxed_agent63 passed,responses_api_agents/opencode_sandboxed_agent173 passed (its 2 Node tests skip without Node; they pass with it).pre-commitpasses on the changed files,detect-secretsflags only test values in them, and every commit is signed off.Checklist
single_agent_turn's README).git commit -s) (so the DCO check passes).🤖 Generated with Claude Code