From 8a0c9ca74d0081f09328a2167a6feb6129a35001 Mon Sep 17 00:00:00 2001 From: Hermenix Date: Sun, 11 Oct 2026 22:49:44 +0000 Subject: [PATCH] fix(mcp): declare the HTTP transport's dependencies, and forbid its tests to skip `starlette` and `uvicorn` arrive through the MCP SDK today -- measured, both of them -- and relying on that is exactly the trap this project already wrote down twice. `src/hhstream_mcp/pyproject.toml` declares `anyio` rather than taking it for granted through the SDK because `server.py` uses it by name; `http.py` imports `uvicorn` by name and sits on Starlette's `Request` through the SDK's `TransportSecuritySettings`, so both belong in the metadata on the same argument. An SDK release that moved its HTTP transport behind an optional extra would otherwise break `serve --http` with an ImportError naming somebody else's package. `nix/mcp.nix` carries them for the same reason. The worse half was in the gate. `tests/unit/test_mcp_http.py` opened with `pytest.importorskip("starlette.testclient")`, which is the pattern `checks/unit.nix`'s own comment argues against in as many words: "a file that skipped when the SDK was absent would be green in exactly the build where that mattered". It would not skip today -- `mcp` propagates starlette and httpx -- but the day it did, 43 tests would leave the gate and nothing would say so, because pytest exits 0 when every test was skipped. So the import is unconditional now, `checks/unit.nix` declares `starlette` and `httpx` explicitly, and the check proves them importable before pytest runs, beside the lines that already do that for `hhstream` and the tool table. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01KchcvnbfhhZhUHy83YL3Ya --- checks/unit.nix | 13 +++++++++++++ nix/mcp.nix | 5 +++++ src/hhstream_mcp/pyproject.toml | 12 +++++++++++- tests/unit/test_mcp_http.py | 19 +++++++++++-------- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/checks/unit.nix b/checks/unit.nix index 0d96c9d..7b5a63b 100644 --- a/checks/unit.nix +++ b/checks/unit.nix @@ -32,11 +32,20 @@ let # `mcp.types.Tool` objects is where a malformed schema or a name the spec # refuses would show up, and a file that skipped when the SDK was absent # would be green in exactly the build where that mattered. + # `starlette` and `httpx` beside `mcp`, and for the same reason the comment + # above gives: `tests/unit/test_mcp_http.py` drives the HTTP transport + # through `starlette.testclient`, which needs `httpx`. Both arrive through + # the SDK today -- measured -- and relying on that is the trap. An SDK + # release that moved its HTTP transport behind an optional extra would make + # 43 tests vanish from this gate, and a gate that loses tests silently is + # worse than one that fails. pytestEnv = python3.withPackages (ps: [ ps.pytest ps.pywayland ps.xkbcommon ps.mcp + ps.starlette + ps.httpx ]); # Only the suite itself, so an editor's droppings or a local .pytest_cache @@ -84,6 +93,10 @@ runCommand "hhstream-unit" # so here, as for `hhstream`, the import comes from what Nix installed, and a # packaging mistake is distinguishable from a test failure. python3 -c 'import hhstream_mcp.tools as t; print("mcp tool table:", len(t.TOOLS), "tools")' + # The HTTP transport's stack, proved present here rather than discovered as a + # skip in the report: `test_mcp_http.py` imports `starlette.testclient` at + # module scope and is not allowed to skip. + python3 -c 'import starlette.testclient, hhstream_mcp.http; print("http transport imports")' set +e pytest suite \ diff --git a/nix/mcp.nix b/nix/mcp.nix index a876b06..97749c6 100644 --- a/nix/mcp.nix +++ b/nix/mcp.nix @@ -41,6 +41,11 @@ python3.pkgs.buildPythonApplication { dependencies = [ python3.pkgs.mcp python3.pkgs.anyio + # The HTTP transport's own two, declared for the reason `anyio` is: the SDK + # propagates both today, and `hhstream_mcp.http` imports them by name. See + # the note in src/hhstream_mcp/pyproject.toml. + python3.pkgs.starlette + python3.pkgs.uvicorn # An application is not importable by a dependent until this converts it, # and `hhstream_mcp.invoke` imports `hhstream.cli` directly. (python3.pkgs.toPythonModule hhstream) diff --git a/src/hhstream_mcp/pyproject.toml b/src/hhstream_mcp/pyproject.toml index 6642c88..0e6af41 100644 --- a/src/hhstream_mcp/pyproject.toml +++ b/src/hhstream_mcp/pyproject.toml @@ -29,13 +29,23 @@ requires-python = ">=3.11" # SDK: `server.py` calls `anyio.run` and `anyio.to_thread` directly, and a # dependency used by name belongs in the metadata. # +# `starlette` and `uvicorn` are declared for exactly that reason and no other. +# The SDK pulls both in today -- its streamable-HTTP transport needs them -- so +# leaving them out would work, which is the trap: `http.py` imports +# `uvicorn` and the SDK's `TransportSecuritySettings` sits on Starlette's +# `Request`, and an SDK release that moved either behind an optional extra +# would break `serve --http` with an ImportError naming someone else's package. +# They are only needed by the HTTP transport, which is why they are imported +# inside the functions that use them rather than at module scope: the stdio +# path must neither pay for them nor fail on them. +# # `hhstream` is NOT listed, and it is not optional either: this package imports # `hhstream.cli`, `hhstreamd.protocol` and `hhstream_session` and does not work # without them. It is absent because `hhstream` is not a wheel anyone installs # from an index -- it is this repository's other derivation, and `nix/mcp.nix` # is where the two are joined. A dependency naming a version nobody can # resolve would be worse than the comment you are reading. -dependencies = ["mcp>=1.9", "anyio>=4"] +dependencies = ["mcp>=1.9", "anyio>=4", "starlette>=0.40", "uvicorn>=0.30"] [project.scripts] # Stdio only. One process per client, spawned by the client; no port, no daemon. diff --git a/tests/unit/test_mcp_http.py b/tests/unit/test_mcp_http.py index 082349e..53d1edf 100644 --- a/tests/unit/test_mcp_http.py +++ b/tests/unit/test_mcp_http.py @@ -1,8 +1,10 @@ """The HTTP transport: every way a request is refused, and the one way it is not. -This file imports Starlette as well as the SDK, for the reason -``test_mcp_server.py`` imports the SDK unconditionally: the thing under test is -the wiring, and only a real ASGI stack can judge it. The store itself is +This file imports Starlette as well as the SDK, **unconditionally**, for the +reason ``test_mcp_server.py`` imports the SDK that way: the thing under test is +the wiring, only a real ASGI stack can judge it, and a file that skipped when +its dependency was missing would be green in precisely the build where that +mattered. The store itself is standard library and tested without any of this (``test_mcp_authorizations.py``). @@ -29,11 +31,12 @@ from hhstream_mcp import http as http_transport from hhstream_mcp import tools from hhstream_mcp.invoke import Runner -starlette_testclient = pytest.importorskip( - "starlette.testclient", - reason="the HTTP transport's own dependency; `nix develop` has it", -) -TestClient = starlette_testclient.TestClient +# Imported unconditionally, and not through `importorskip`, for the reason +# `test_mcp_server.py` imports the SDK unconditionally: a file that skipped +# when its dependency was absent would be green in exactly the build where +# that mattered. `checks/unit.nix` declares `starlette` and `httpx` so this +# cannot become a skip, and proves them importable before pytest runs. +from starlette.testclient import TestClient #: The test client's Host, plus what a loopback deployment derives. TEST_HOSTS = ["testserver", *http_transport.default_allowed_hosts("127.0.0.1", 9989)]