Skip to content

fix(mcp): validate secrets after parsing - #4099

Merged
simonrosenberg merged 1 commit into
mainfrom
fix-mcp-secret-json-validation
Jul 13, 2026
Merged

simonrosenberg merged 1 commit into
mainfrom
fix-mcp-secret-json-validation

Conversation

@simonrosenberg

@simonrosenberg simonrosenberg commented Jul 13, 2026 •

Copy link
Copy Markdown
Member

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 SecretStr objects, which JSON-mode validation
then rejects as non-string input.

Summary

  • Normalize every MCP secret after Pydantic parses its declared SecretStr type.
  • Cover env/header maps and API key, bearer, basic, header, and OAuth credentials.
  • Preserve encryption plus empty, whitespace, and redacted-value filtering.
  • Exercise the real encrypted conversation metadata save/restart boundary.

Issue Number

Closes #4096.

How to Test

Run the regression and persistence tests:

uv run pytest -q tests/sdk/mcp/test_mcp_config_secrets.py \
  tests/agent_server/test_event_service.py::TestEventServiceSaveMeta::test_save_meta_round_trips_agent_definition_mcp_secrets

Local result: 3 passed.

Run the two complete CI test domains:

CI=true uv run python -m pytest -q -n 4 \
  --basetemp=/tmp/sdk-4096-validation tests/sdk
CI=true uv run python -m pytest -q -n 4 tests/agent_server

Local results:

  • SDK: 5401 passed, 9 skipped, 13 xfailed
  • Agent server: 1463 passed

Run the repository-wide quality gate:

uv run pre-commit run --all-files --show-diff-on-failure

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:

  1. Started a real ConversationService with four agent definitions and
    web-researcher at index 3.
  2. Persisted its Tavily environment placeholder through encrypted meta.json.
  3. Closed the service and constructed a new ConversationService from the same
    persistence directory and cipher.
  4. Verified the conversation loaded and the decrypted placeholder remained
    ${TAVILY_API_KEY}.

Observed output:

agent_definitions=4
web_researcher_index=3
meta_secret=encrypted
restart=loaded

Video/Screenshots

Not applicable; this is a server-side persistence fix.

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

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

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python amd64, arm64 nikolaik/python-nodejs:python3.13-nodejs22-slim Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:d52dc56-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-d52dc56-python \
  ghcr.io/openhands/agent-server:d52dc56-python

All tags pushed for this build

ghcr.io/openhands/agent-server:d52dc56-golang-amd64
ghcr.io/openhands/agent-server:d52dc562962dc3eadc3db2bdb439df80207e171c-golang-amd64
ghcr.io/openhands/agent-server:fix-mcp-secret-json-validation-golang-amd64
ghcr.io/openhands/agent-server:d52dc56-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:d52dc56-golang-arm64
ghcr.io/openhands/agent-server:d52dc562962dc3eadc3db2bdb439df80207e171c-golang-arm64
ghcr.io/openhands/agent-server:fix-mcp-secret-json-validation-golang-arm64
ghcr.io/openhands/agent-server:d52dc56-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:d52dc56-java-amd64
ghcr.io/openhands/agent-server:d52dc562962dc3eadc3db2bdb439df80207e171c-java-amd64
ghcr.io/openhands/agent-server:fix-mcp-secret-json-validation-java-amd64
ghcr.io/openhands/agent-server:d52dc56-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:d52dc56-java-arm64
ghcr.io/openhands/agent-server:d52dc562962dc3eadc3db2bdb439df80207e171c-java-arm64
ghcr.io/openhands/agent-server:fix-mcp-secret-json-validation-java-arm64
ghcr.io/openhands/agent-server:d52dc56-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:d52dc56-python-amd64
ghcr.io/openhands/agent-server:d52dc562962dc3eadc3db2bdb439df80207e171c-python-amd64
ghcr.io/openhands/agent-server:fix-mcp-secret-json-validation-python-amd64
ghcr.io/openhands/agent-server:d52dc56-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-amd64
ghcr.io/openhands/agent-server:d52dc56-python-arm64
ghcr.io/openhands/agent-server:d52dc562962dc3eadc3db2bdb439df80207e171c-python-arm64
ghcr.io/openhands/agent-server:fix-mcp-secret-json-validation-python-arm64
ghcr.io/openhands/agent-server:d52dc56-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-arm64
ghcr.io/openhands/agent-server:d52dc56-golang
ghcr.io/openhands/agent-server:d52dc562962dc3eadc3db2bdb439df80207e171c-golang
ghcr.io/openhands/agent-server:fix-mcp-secret-json-validation-golang
ghcr.io/openhands/agent-server:d52dc56-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:d52dc56-java
ghcr.io/openhands/agent-server:d52dc562962dc3eadc3db2bdb439df80207e171c-java
ghcr.io/openhands/agent-server:fix-mcp-secret-json-validation-java
ghcr.io/openhands/agent-server:d52dc56-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:d52dc56-python
ghcr.io/openhands/agent-server:d52dc562962dc3eadc3db2bdb439df80207e171c-python
ghcr.io/openhands/agent-server:fix-mcp-secret-json-validation-python
ghcr.io/openhands/agent-server:d52dc56-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim

About Multi-Architecture Support

  • Each variant tag (e.g., d52dc56-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., d52dc56-python-amd64) are also available if needed

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

Copy link
Copy Markdown
Contributor

Python API breakage checks — ✅ PASSED

Result: ✅ PASSED

Action log

@simonrosenberg
simonrosenberg marked this pull request as ready for review July 13, 2026 07:22
@github-actions

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-sdk/openhands/sdk/mcp
   config.py3405683%89, 126, 152, 176, 318–321, 325–328, 332–334, 360–363, 367, 381, 385, 389, 394, 464, 466, 472, 484, 489–492, 498–500, 506, 543, 585–589, 591, 593–595, 597–600, 602–604, 611, 614, 616
TOTAL36286732279% 

@simonrosenberg
simonrosenberg requested a review from enyst July 13, 2026 07:25

@all-hands-bot all-hands-bot 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.

✅ 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.

@simonrosenberg simonrosenberg self-assigned this Jul 13, 2026
@simonrosenberg
simonrosenberg requested a review from neubig July 13, 2026 07:40

@enyst enyst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

@simonrosenberg
simonrosenberg merged commit c8e7225 into main Jul 13, 2026
46 of 47 checks passed
@simonrosenberg
simonrosenberg deleted the fix-mcp-secret-json-validation branch July 13, 2026 10:55
@simonrosenberg

Copy link
Copy Markdown
Member Author

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...

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.

[Bug]: agent-server crash / Input should be a valid string [type=string_type, input_value=SecretStr('**********'), input_type=SecretStr]

3 participants