mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 18:41:07 +08:00
## Description Supersedes #2658 (`tejas/mcp-2x-port`, a draft with conflicts against `main` since 2026-10-20). This is that PR's net diff rebased onto `main` as one commit (originally `fc1ba891`, now `7e0094d6`; see below), crediting Tejas Chopra and Jerrett Davis as co-authors, with three changes needed on current `main`. Both MCP servers (`headroom/ccr/mcp_server.py`, `headroom/memory/mcp_server.py`) move from SDK 1's decorator API to SDK 2's constructor callbacks, as #2658 did. The `<2.0.0` cap from #2642 is then lifted. Why now: current Claude Code tries the modern `server/discover` handshake first. On SDK 1, `headroom mcp serve` rejects it and logs `Failed to validate request: 31 validation errors for ClientRequest` on stderr in every session. Claude Code then falls back to legacy `initialize`, so the tools still work. On SDK 2 the modern handshake succeeds and nothing is logged. Changes relative to #2658: 1. **The floor is `mcp>=2.2.0,<3.0.0`, not `>=2.0.0`.** The GitHub advisories mark `mcp` `>=2.0.0, <2.2.0` as affected by CVE-2026-59951 (the reason for #4239's `>=1.30.0` floor), CVE-2026-116684, CVE-2026-116685 and CVE-2026-116686. #2658's lock resolved 2.1.1. The contract test keeps `main`'s CVE guard for the 2.x line and rejects 3.x (see #2977). 2. **`tests/test_ccr_mcp_http.py` is ported.** #1773 added it after #2658 branched, and it fails on SDK 2: `streamable_http_client` now takes an `httpx2.AsyncClient`, yields `(read, write)` without the session-id getter, and its types are snake_case (`protocol_version`). `headroom/ccr/mcp_http.py` itself needs no change. 3. **The lock is regenerated** with uv 0.9.30 (`uv lock --upgrade-package mcp`, still `revision = 3`). **Rebased onto main (`7e0094d6`), 2026-11-03.** The branch is now two linear commits, with no merge commits: - `ce4d6442` feat(mcp)!: port MCP servers to SDK 2.x (was `39c574c2`) - `274f0672` fix(mcp): attribute SDK 2 clients by client_info, not clientInfo (was `c7900848`) The previous head was the merge `4c1b774f` (main `5c785e69` into the branch), which carried the resolution against #4525 (GHSA-8g49-pg6g-xgrf, confining `headroom_read` to the workspace). The rebase replays the port directly on top of #4525 instead. What it changed: - `headroom/ccr/mcp_server.py` conflicted with #4525 in two places: the imports (#4525's `errno` next to the port's `contextvars`) and the tool list, where #4525's workspace-scoped `headroom_read` schema met the port's move from the `@list_tools` decorator to `_on_list_tools`. The resolved file keeps both: SDK 2 callbacks and #4525's confinement and `HEADROOM_MCP_READ_ROOTS` description. It is byte-identical to the file in the previous merge head `4c1b774f`. - `uv.lock` conflicted in one hunk with #4614's `mem0ai` bump: kept this PR's `mcp>=2.2.0,<3.0.0` for the `mcp` and `proxy` extras and #4614's `mem0ai>=2.0.18,<3.0`. `uv lock --check` resolves 253 packages. - Every other file this PR touches is unchanged from `4c1b774f`. The only difference in `pyproject.toml` and `uv.lock` against the previous head is #4614's own diff, line for line. The Strands blocker from the July review no longer applies: `strands-agents` 1.63.0 declares `mcp<2.4,>=1.23.0`, and the lock resolves `mcp` 2.3.0 with it. Dependency justification (CONTRIBUTING, new dep or version bump): | Package | Change | Source | |---|---|---| | `mcp` | 1.30.0 → 2.3.0 | modelcontextprotocol/python-sdk | | `mcp-types` | new, 2.3.0 | same SDK; `mcp` 2.3.0 pins it `==2.3.0` | | `httpx2`, `httpcore2` | new, 2.16.0 | pydantic/httpx2 (Tom Christie); SDK 2's HTTP client | | `httpx2-jsfetch` | new, 1.0 | only on `sys_platform == 'emscripten'`; not installed elsewhere | | `strands-agents` | 1.62.0 → 1.63.0 | first release that accepts `mcp` 2.2+ | | `httpx-sse` | removed | SDK 1 only | | `idna` | 3.15 → 3.20 | required by `httpx2` | ## Type of Change - [ ] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [x] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Performance improvement - [ ] Code refactoring (no functional changes) ## Changes Made - `headroom/ccr/mcp_server.py`, `headroom/memory/mcp_server.py`: register handlers as `on_list_tools` / `on_call_tool` constructor callbacks, return `ListToolsResult` / `CallToolResult`, and read the request context from the callback argument. These are #2658's changes, including Jerrett's memory-backend cleanup hook. - `pyproject.toml`: `mcp>=2.2.0,<3.0.0` in the `proxy` and `mcp` extras. - `uv.lock`: relocked as described above. - `tests/test_mcp_dependency_contract.py`: both extras must admit 2.2.0 and reject 1.30.0, 2.1.1 and 3.0.0. - `tests/test_mcp_sdk2_contract.py`, `tests/_mcp_stub.py`, `tests/test_mcp_stub.py`, `tests/test_memory/test_mcp_server.py`: from #2658. - `tests/test_ccr_mcp_http.py`: ported to the SDK 2 client. It reads the session id from the `mcp-session-id` response header. - `headroom/ccr/mcp_server.py` `_current_client`: read `client_info` (SDK 2) instead of `clientInfo` (SDK 1), so `headroom_compress` savings events name the client again. `tests/test_mcp_sdk2_contract.py` now builds the SDK's own `InitializeRequestParams`; its old stand-in used `clientInfo` and stayed green while every client was recorded as `unknown`. Before and after, with an SDK 2.3 client named `attribution-probe` over stdio: the ledger recorded `unknown` on both `server/discover` and `initialize`, and now records `attribution-probe` on both (found by Devin's review). ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [ ] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [x] Manual testing performed ### Test Output After the rebase, on `274f0672`: ```text # Python 3.11.17, mcp 2.3.0, strands-agents 1.63.0 $ pytest -q tests/test_mcp_dependency_contract.py tests/test_mcp_sdk2_contract.py tests/test_mcp_stub.py \ tests/test_ccr_mcp_http.py tests/test_memory/test_mcp_server.py \ <every test module that references mcp_server, GHSA or HEADROOM_MCP_READ_ROOTS, incl. tests/test_ccr_mcp_server.py> 840 passed, 2 skipped $ uv lock --check Resolved 253 packages $ ruff check && ruff format --check on every changed .py file (ruff 0.17.2); git diff --check origin/main clean ``` The wider 40-file mcp/strands run below was not repeated after the rebase. Before the rebase: ```text # Python 3.11.17, uv sync --frozen --extra mcp --extra strands --extra proxy --extra otel (mcp 2.3.0, strands-agents 1.63.0) $ pytest -q <every test module that references mcp, strands or the MCP servers: 40 files> 1163 passed, 31 skipped $ pytest -q tests/test_ccr_mcp_http.py tests/test_mcp_dependency_contract.py tests/test_mcp_sdk2_contract.py 9 passed $ pytest -q tests/integrations/test_strands tests/test_strands_tokenizer.py 80 passed, 25 skipped $ uv run --with uv==0.9.30 python -m uv lock --check Resolved 253 packages $ ruff check && ruff format --check on every changed .py file (ruff 0.16.8); git diff --check origin/main clean # [all] export against the union of the extras it includes: no package missing ``` ## Real Behavior Proof - Environment: macOS 27 (arm64), Python 3.11.17; `main` `fc1ba891` (SDK 1.30.0) and this branch (SDK 2.3.0); temporary `HOME`; plus this branch merged into my local runtime branch and installed as the `headroom` uv tool. - Exact command / steps: an SDK 2.3 stdio client starts `headroom mcp serve`, calls `ClientSession.discover()` (`server/discover`, as Claude Code does first), falls back to `initialize()` if that is rejected, then lists tools and calls `headroom_stats`. The server's stderr is captured. - Observed result: on `main`, `server/discover` is rejected with -32602 and the legacy `initialize` is used; stderr has 94 lines, starting `WARNING: Failed to validate request: 31 validation errors for ClientRequest`; 3 tools are listed and the call succeeds. On this branch the modern `server/discover` is accepted and stderr has 0 lines; 3 tools are listed and the call succeeds. The installed build behaves the same. - Not tested: Windows; the memory MCP server against a live client (unit and contract tests only); Strands agents against a live MCP server (`tests/integrations/test_strands` and `tests/test_strands_tokenizer.py`: 80 passed, 25 skipped); a real network client against the Streamable HTTP transport (the ASGI contract test only). ## Runtime Rollout Safety - Rollout-managed feature(s): none - Minimum rollout channel: n/a, dependency and server API change - Stable/default behavior changed: yes; the MCP servers require SDK 2.2+ and speak the modern handshake; the tools and their JSON results are unchanged - Kill switch / disable path: none; pin `headroom-ai` to the previous release to stay on SDK 1 - Unsafe override required: no - Qualification impact: environments that pin `mcp<2` alongside `headroom-ai[mcp]` or `[proxy]` no longer resolve - Rollback path: revert this commit, which restores the `<2.0.0` cap ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] I did **not** edit `CHANGELOG.md` — it is generated by release-please from my Conventional Commit PR title (a CI guard enforces this) ## Screenshots (if applicable) N/A ## Additional Notes Proposed on #2658 (https://github.com/headroomlabs-ai/headroom/pull/2658#issuecomment-6562744017). If maintainers would rather keep #2658, `ce4d6442` and `274f0672` (previously `39c574c2` and `c7900848`) can be cherry-picked onto `tejas/mcp-2x-port` instead, and I'll close this. `mypy headroom/ccr/mcp_server.py headroom/memory/mcp_server.py headroom/ccr/mcp_http.py` with SDK 2.3 installed reports one error, in `headroom/providers/model_metadata.py:79`, which `main` reports too; none in the changed files. Full `mypy headroom` was not run. No docs change: the MCP tool names, arguments and results are unchanged. --------- Co-authored-by: Kenneth Wong <17061293+KennethWKZ@users.noreply.github.com> Co-authored-by: Tejas Chopra <chopratejas@gmail.com> Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
119 lines
4.2 KiB
Python
119 lines
4.2 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import sys
|
|
from types import ModuleType
|
|
|
|
import pytest
|
|
|
|
from tests import _mcp_stub as mcp_stub
|
|
|
|
|
|
def test_build_mcp_sdk_stub_exposes_minimum_sdk_contract() -> None:
|
|
modules = mcp_stub._build_mcp_sdk_stub()
|
|
|
|
# MCP 2.x: handlers are constructor callbacks, not decorators.
|
|
async def on_list_tools(ctx, params=None): # pragma: no cover - identity only
|
|
return None
|
|
|
|
async def on_call_tool(ctx, params): # pragma: no cover - identity only
|
|
return None
|
|
|
|
server = modules["mcp.server"].Server(
|
|
"headroom", on_list_tools=on_list_tools, on_call_tool=on_call_tool
|
|
)
|
|
assert server.name == "headroom"
|
|
assert server.handlers == {"on_list_tools": on_list_tools, "on_call_tool": on_call_tool}
|
|
assert server.create_initialization_options() == {}
|
|
|
|
# The 1.x decorator surface must be gone: keeping it would let a caller still
|
|
# on decorators pass its tests while the real 2.x SDK rejects it.
|
|
assert not hasattr(server, "list_tools")
|
|
assert not hasattr(server, "call_tool")
|
|
|
|
tool = modules["mcp.types"].Tool(name="search")
|
|
text = modules["mcp.types"].TextContent(text="payload")
|
|
assert tool.kwargs == {"name": "search"}
|
|
assert text.kwargs == {"text": "payload"}
|
|
|
|
tools_result = modules["mcp.types"].ListToolsResult(tools=[tool])
|
|
call_result = modules["mcp.types"].CallToolResult(content=[text])
|
|
assert tools_result.tools == [tool]
|
|
assert call_result.content == [text]
|
|
|
|
with pytest.raises(RuntimeError, match="should not run"):
|
|
asyncio.run(modules["mcp.server.stdio"].stdio_server())
|
|
|
|
|
|
def test_import_module_with_mcp_stub_imports_target_and_cleans_up(monkeypatch) -> None:
|
|
original_mcp = ModuleType("mcp")
|
|
monkeypatch.setitem(sys.modules, "mcp", original_mcp)
|
|
for name in mcp_stub._MCP_MODULE_NAMES[1:]:
|
|
sys.modules.pop(name, None)
|
|
|
|
imported = ModuleType("fake_target")
|
|
|
|
def fake_import_module(module_name: str) -> ModuleType:
|
|
assert module_name == "fake_target"
|
|
for name in mcp_stub._MCP_MODULE_NAMES:
|
|
assert name in sys.modules
|
|
return imported
|
|
|
|
monkeypatch.setattr(mcp_stub.importlib, "import_module", fake_import_module)
|
|
|
|
result = mcp_stub.import_module_with_mcp_stub("fake_target")
|
|
|
|
assert result is imported
|
|
assert sys.modules["mcp"] is original_mcp
|
|
for name in mcp_stub._MCP_MODULE_NAMES[1:]:
|
|
assert name not in sys.modules
|
|
|
|
|
|
def test_import_module_with_mcp_stub_reimports_target_and_restores_originals(monkeypatch) -> None:
|
|
original_modules = {}
|
|
for name in mcp_stub._MCP_MODULE_NAMES:
|
|
module = ModuleType(f"original::{name}")
|
|
original_modules[name] = module
|
|
monkeypatch.setitem(sys.modules, name, module)
|
|
|
|
existing = ModuleType("fake_target")
|
|
monkeypatch.setitem(sys.modules, "fake_target", existing)
|
|
imported = ModuleType("fake_target")
|
|
|
|
def fake_import_module(module_name: str) -> ModuleType:
|
|
assert module_name == "fake_target"
|
|
assert "fake_target" not in sys.modules
|
|
for name in mcp_stub._MCP_MODULE_NAMES:
|
|
assert sys.modules[name] is not original_modules[name]
|
|
return imported
|
|
|
|
monkeypatch.setattr(mcp_stub.importlib, "import_module", fake_import_module)
|
|
|
|
result = mcp_stub.import_module_with_mcp_stub("fake_target")
|
|
|
|
assert result is imported
|
|
assert sys.modules["fake_target"] is existing
|
|
for name, module in original_modules.items():
|
|
assert sys.modules[name] is module
|
|
|
|
|
|
def test_import_module_with_mcp_stub_cleans_up_dotted_target_attribute(monkeypatch) -> None:
|
|
parent = ModuleType("fakepkg")
|
|
monkeypatch.setitem(sys.modules, "fakepkg", parent)
|
|
for name in mcp_stub._MCP_MODULE_NAMES:
|
|
sys.modules.pop(name, None)
|
|
|
|
imported = ModuleType("fakepkg.fake_target")
|
|
|
|
def fake_import_module(module_name: str) -> ModuleType:
|
|
assert module_name == "fakepkg.fake_target"
|
|
parent.fake_target = imported
|
|
return imported
|
|
|
|
monkeypatch.setattr(mcp_stub.importlib, "import_module", fake_import_module)
|
|
|
|
result = mcp_stub.import_module_with_mcp_stub("fakepkg.fake_target")
|
|
|
|
assert result is imported
|
|
assert not hasattr(parent, "fake_target")
|