python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

fix: reject changed duplicate initialize

Open he-yufeng opened this issue 6 months ago • 2 comments

## Summary

  • reject a second initialize request if it tries to change the session's client parameters
  • keep the first client_params intact so later capability checks cannot be spoofed by a duplicate handshake
  • add a regression test covering the changed-params path

To verify

  • uv run pytest tests/server/test_session.py -q -k "duplicate_initialize or server_session_initialize"
  • uv run ruff check src/mcp/server/session.py tests/server/test_session.py
  • uv run pyright src/mcp/server/session.py tests/server/test_session.py
  • uv run python -m py_compile src/mcp/server/session.py tests/server/test_session.py
  • git diff --check

Closes #2605

he-yufeng avatar May 15 '26 14:05 he-yufeng

Code Review

PR: fix: reject changed duplicate initialize by @he-yufeng

  • ✅ Bug fix

Wallet: 0xdaE5d307339074A24F579dB48e7c639359D94904

Code review for MCP Python SDK

BossChaos avatar May 17 '26 08:05 BossChaos

Rebased this PR onto the current runner/connection architecture instead of keeping the old ServerSession._received_request patch. The duplicate-initialize guard now lives in ServerRunner._handle_initialize(), so a second initialize with changed params returns INVALID_PARAMS and leaves the original connection.client_params intact.

Validation:

  • python -m py_compile src\mcp\server\runner.py tests\server\test_runner.py
  • uv run pytest tests/server/test_runner.py -q -k "initialize" --basetemp .tmp\pytest-2606-20260610 -p no:cacheprovider (16 passed)

he-yufeng avatar Jun 10 '26 13:06 he-yufeng