Repository navigation
fix(mcp): validate secrets after parsing - #4099
Conversation
Co-authored-by: openhands <openhands@all-hands.dev>
Python API breakage checks — ✅ PASSEDResult: ✅ PASSED |
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
all-hands-bot
left a comment
There was a problem hiding this comment.
✅ QA Report: PASS
The MCP secret JSON validation fix works: I reproduced the base-branch validation failure and confirmed the PR reloads encrypted MCP secrets through both SDK JSON validation and EventService metadata reload.
Does this PR achieve its stated goal?
Yes. The goal was to let persisted conversations/agent definitions containing MCP secrets load through Pydantic's JSON validation path after encrypted serialization. On origin/main, the same encrypted JSON reload fails because JSON validation receives SecretStr objects where strings are expected; on PR commit d52dc562962dc3eadc3db2bdb439df80207e171c, the SDK round trip and EventService save_meta/load_meta boundary both load successfully while keeping the stored payload free of plaintext secrets.
| Phase | Result |
|---|---|
| Environment Setup | ✅ make build completed successfully and installed the uv workspace environment. |
| CI Status | 🟡 23 successful, 6 pending Docker build/push jobs, 2 skipped, 0 failing when checked. |
| Functional Verification | ✅ Base bug reproduced; PR fixed SDK encrypted JSON reload and EventService metadata reload. |
Functional Verification
Test 1: SDK encrypted AgentDefinition JSON round trip across MCP secret fields
Step 1 — Reproduce on base without the fix:
Ran git switch --detach origin/main && PYTHONPATH=openhands-sdk:openhands-agent-server:openhands-tools:openhands-workspace uv run python /tmp/qa_mcp_secret_json.py:
payload_length=3017
payload_contains_plain_secret= False
roundtrip=validation_error
10 validation errors for AgentDefinition
mcp_config.stdio.env.ENV_TOKEN
Input should be a valid string [type=string_type, input_value=SecretStr('**********'), input_type=SecretStr]
mcp_config.stdio.headers.X-Top-Level
Input should be a valid string [type=string_type, input_value=SecretStr('**********'), input_type=SecretStr]
mcp_config.api-key.auth.api_key.value
Input should be a valid string [type=string_type, input_value=SecretStr('**********'), input_type=SecretStr]
This confirms the reported bug exists on base: encrypted payloads do not expose plaintext, but JSON-mode validation rejects the decrypted MCP secret values because they are already SecretStr objects.
Step 2 — Apply the PR's changes:
Checked out d52dc562962dc3eadc3db2bdb439df80207e171c.
Step 3 — Re-run with the fix in place:
Ran git switch --detach d52dc562962dc3eadc3db2bdb439df80207e171c && PYTHONPATH=openhands-sdk:openhands-agent-server:openhands-tools:openhands-workspace uv run python /tmp/qa_mcp_secret_json.py:
payload_length=3017
payload_contains_plain_secret= False
roundtrip=loaded
restored_matches_original= True
filtered_config= {'stdio': {'command': 'echo', 'env': {'KEEP': 'env-value'}, 'headers': {'KEEP': 'header-value'}}, 'api-key': {'url': 'https://example.com/api-key', 'auth': {'strategy': 'api_key'}}}
This confirms the SDK path now reloads env/header maps and scalar auth secrets from encrypted JSON, restores the original MCP config, and still drops empty, whitespace-only, and redacted secret values.
Test 2: EventService encrypted conversation metadata save/load boundary
Step 1 — Reproduce on base without the fix:
Ran git switch --detach origin/main && PYTHONPATH=openhands-sdk:openhands-agent-server:openhands-tools:openhands-workspace OPENHANDS_SUPPRESS_BANNER=1 uv run python /tmp/qa_event_service_agent_meta.py:
service_save_meta=ok
meta_contains_plain_placeholder= False
service_load_meta=validation_error
1 validation error for StoredConversation
agent_definitions.0.mcp_config.tavily.env.TAVILY_API_KEY
Input should be a valid string [type=string_type, input_value=SecretStr('**********'), input_type=SecretStr]
This reproduces the production-style persistence failure: save_meta writes encrypted metadata, but reloading the saved conversation fails at the agent-definition MCP env secret.
Step 2 — Apply the PR's changes:
Checked out d52dc562962dc3eadc3db2bdb439df80207e171c.
Step 3 — Re-run with the fix in place:
Ran git switch --detach d52dc562962dc3eadc3db2bdb439df80207e171c && PYTHONPATH=openhands-sdk:openhands-agent-server:openhands-tools:openhands-workspace OPENHANDS_SUPPRESS_BANNER=1 uv run python /tmp/qa_event_service_agent_meta.py:
service_save_meta=ok
meta_contains_plain_placeholder= False
service_load_meta=loaded
restored_env_value= ${TAVILY_API_KEY}
This confirms the real metadata boundary now reloads and decrypts the MCP placeholder correctly without storing it in plaintext.
Issues Found
None from functional QA.
This review was created by an AI agent (OpenHands) on behalf of the user.
enyst
left a comment
There was a problem hiding this comment.
Thank you!
I wonder why did the agent ever choose object instead of SecretStr. Maybe we’re missing a categorical instruction in AGENTS.md to use strong types wherever possible, wdyt?
I agree that gpt 5.6 seems to enjoy loose typing... |
HUMAN:
Fix secrets pydantic validation
AGENT:
Why
Persisted conversations containing agent definitions with MCP secrets cannot be
loaded through Pydantic's JSON validation path. The MCP validators run before
core parsing but return final
SecretStrobjects, which JSON-mode validationthen rejects as non-string input.
Summary
SecretStrtype.Issue Number
Closes #4096.
How to Test
Run the regression and persistence tests:
Local result:
3 passed.Run the two complete CI test domains:
Local results:
5401 passed, 9 skipped, 13 xfailed1463 passedRun the repository-wide quality gate:
All hooks passed, including Ruff formatting/linting, pycodestyle, Pyright,
import dependency rules, and tool registration checks.
I also ran a production-path restart reproduction outside pytest:
ConversationServicewith four agent definitions andweb-researcherat index 3.meta.json.ConversationServicefrom the samepersistence directory and cipher.
${TAVILY_API_KEY}.Observed output:
Video/Screenshots
Not applicable; this is a server-side persistence fix.
Type
Notes
This fixes the validation-phase mismatch rather than special-casing MCP env
maps. It therefore also covers scalar authentication and OAuth secrets that
would otherwise fail at the same JSON reload boundary.
Agent Server images for this PR
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdknikolaik/python-nodejs:python3.13-nodejs22-slimgolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:d52dc56-pythonRun
All tags pushed for this build
About Multi-Architecture Support
d52dc56-python) is a multi-arch manifest supporting both amd64 and arm64d52dc56-python-amd64) are also available if needed