From 7689e2d44fe5579c1cacdcdc55f27e6a9e003ea4 Mon Sep 17 00:00:00 2001 From: Lucas Faria Date: Fri, 6 Nov 2026 17:14:58 -0300 Subject: [PATCH 1/3] fix(mcp): read the middleware-applied tool listing on FastMCP 2.x What: _listed_input_schema now falls back to FastMCP 2.x's _list_tools_middleware when the server has no list_tools. Why: 2.x has no list_tools, so the schema lookup raised, was logged and dropped, and every argument looked undeclared. $mcp_input_keys came back as ['[redacted]'] on 2.x servers with middleware. Tested: pytest posthog/test/mcp on fastmcp 2.14.7 (661 passed, 14 skipped) and 3.2.0 (665 passed, 10 skipped); ruff format/check and mypy-baseline clean. --- .sampo/changesets/mcp-fastmcp2-input-keys.md | 5 +++++ posthog/mcp/_instrument_lowlevel.py | 8 +++++--- 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 .sampo/changesets/mcp-fastmcp2-input-keys.md diff --git a/.sampo/changesets/mcp-fastmcp2-input-keys.md b/.sampo/changesets/mcp-fastmcp2-input-keys.md new file mode 100644 index 000000000..4a71cd7f5 --- /dev/null +++ b/.sampo/changesets/mcp-fastmcp2-input-keys.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +Fixed `$mcp_input_keys` recording `["[redacted]"]` on FastMCP 2.x servers that use middleware. The server's advertised schema was never read there, so every argument name looked undeclared. diff --git a/posthog/mcp/_instrument_lowlevel.py b/posthog/mcp/_instrument_lowlevel.py index 00bd4ef4d..2677c2622 100644 --- a/posthog/mcp/_instrument_lowlevel.py +++ b/posthog/mcp/_instrument_lowlevel.py @@ -706,9 +706,11 @@ async def _listed_input_schema( """Return the schema that middleware advertises for the current request.""" try: version = _requested_tool_version(meta) - candidates = [ - tool for tool in await high_level.list_tools() if tool.name == name - ] + # FastMCP 2.x names the middleware-applied listing `_list_tools_middleware`. + list_tools = getattr(high_level, "list_tools", None) or getattr( + high_level, "_list_tools_middleware" + ) + candidates = [tool for tool in await list_tools() if tool.name == name] if version is not None: candidates = [ tool From 9b9eed447089ea936b0ad76a1b67b9f181928890 Mon Sep 17 00:00:00 2001 From: Lucas Faria Date: Fri, 6 Nov 2026 17:23:13 -0300 Subject: [PATCH 2/3] fix(mcp): read the FastMCP 2.x tool registry, not its middleware listing Review: the 2.x fallback ran _list_tools_middleware, so a request middleware such as SlidingWindowRateLimitingMiddleware spent a quota slot on the schema lookup and could fail a call that succeeded before. 2.x now reads get_tools(), which runs no middleware; 3.x keeps list_tools(). Tested: new test_schema_lookup_on_fastmcp_2_runs_no_request_middleware fails without the fix and passes with it; pytest posthog/test/mcp on fastmcp 2.14.7 (661 passed; the 1 failure is test_stdio_handshake_then_calls, which fails the same way locally without this change) and fastmcp 3.2.0 (665 passed); ruff clean. --- posthog/mcp/_instrument_lowlevel.py | 13 ++++++++----- posthog/test/mcp/test_fastmcp_v2.py | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/posthog/mcp/_instrument_lowlevel.py b/posthog/mcp/_instrument_lowlevel.py index 2677c2622..b767bb6b5 100644 --- a/posthog/mcp/_instrument_lowlevel.py +++ b/posthog/mcp/_instrument_lowlevel.py @@ -706,11 +706,14 @@ async def _listed_input_schema( """Return the schema that middleware advertises for the current request.""" try: version = _requested_tool_version(meta) - # FastMCP 2.x names the middleware-applied listing `_list_tools_middleware`. - list_tools = getattr(high_level, "list_tools", None) or getattr( - high_level, "_list_tools_middleware" - ) - candidates = [tool for tool in await list_tools() if tool.name == name] + list_tools = getattr(high_level, "list_tools", None) + if list_tools is not None: + listed = await list_tools() + else: + # FastMCP 2.x only offers its listing through request middleware, + # which can consume rate limits, so read the registry instead. + listed = (await high_level.get_tools()).values() + candidates = [tool for tool in listed if tool.name == name] if version is not None: candidates = [ tool diff --git a/posthog/test/mcp/test_fastmcp_v2.py b/posthog/test/mcp/test_fastmcp_v2.py index d8f18b86b..54279b5f9 100644 --- a/posthog/test/mcp/test_fastmcp_v2.py +++ b/posthog/test/mcp/test_fastmcp_v2.py @@ -699,3 +699,27 @@ async def flaky_get_tool(name, *args, **kwargs): assert "real tool ran" in str(out.root.content[0].text) assert _events(client, "$mcp_missing_capability") == [] assert any("delegating the call to your server" in m for m in messages) + + +@pytest.mark.skipif( + hasattr(FastMCP, "list_tools"), reason="FastMCP 2.x has no public list_tools" +) +async def test_schema_lookup_on_fastmcp_2_runs_no_request_middleware(): + from fastmcp.server.middleware import Middleware + + from posthog.mcp._instrument_lowlevel import _listed_input_schema + + listings = [] + + class CountListings(Middleware): + async def on_list_tools(self, context, call_next): + listings.append(context) + return await call_next(context) + + server = make_server() + server.add_middleware(CountListings()) + + schema = await _listed_input_schema(server, "add", None) + + assert schema is not None and set(schema["properties"]) >= {"a", "b"} + assert listings == [] From 4f115d9326a07d5e6b761d50709604389207520f Mon Sep 17 00:00:00 2001 From: Lucas Faria Date: Fri, 6 Nov 2026 18:32:39 -0300 Subject: [PATCH 3/3] fix(mcp): don't read the advertised schema on FastMCP 2.x Review on the previous commit: reading the registry with get_tools() can disagree with what middleware advertises (a replaced tool, a prefixed mount sharing a root name), so trusting it records undeclared argument names. Running the middleware listing instead spends rate limits. FastMCP 2.x has no safe source for the advertised schema, so the lookup now returns None there, which keeps argument names redacted, runs no middleware, and stops the error logged on every call. The 2.x middleware test now expects the redaction. Tested: pytest posthog/test/mcp on fastmcp 2.14.7 (661 passed; the only failure is test_stdio_handshake_then_calls, which fails locally without this change) and fastmcp 3.2.0 (665 passed); ruff clean. --- .sampo/changesets/mcp-fastmcp2-input-keys.md | 2 +- posthog/mcp/_instrument_lowlevel.py | 13 ++++++------- posthog/test/mcp/test_fastmcp_v2.py | 9 ++++++--- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/.sampo/changesets/mcp-fastmcp2-input-keys.md b/.sampo/changesets/mcp-fastmcp2-input-keys.md index 4a71cd7f5..9d47767f1 100644 --- a/.sampo/changesets/mcp-fastmcp2-input-keys.md +++ b/.sampo/changesets/mcp-fastmcp2-input-keys.md @@ -2,4 +2,4 @@ pypi/posthog: patch --- -Fixed `$mcp_input_keys` recording `["[redacted]"]` on FastMCP 2.x servers that use middleware. The server's advertised schema was never read there, so every argument name looked undeclared. +On FastMCP 2.x servers with middleware, `posthog.mcp` no longer tries to read the advertised tool schema on each call. FastMCP 2.x can only list tools through request middleware, which can spend rate limits, so the lookup failed and logged an error on every call. Argument names there stay redacted in `$mcp_input_keys`, as before. diff --git a/posthog/mcp/_instrument_lowlevel.py b/posthog/mcp/_instrument_lowlevel.py index b767bb6b5..e6af0f65a 100644 --- a/posthog/mcp/_instrument_lowlevel.py +++ b/posthog/mcp/_instrument_lowlevel.py @@ -707,13 +707,12 @@ async def _listed_input_schema( try: version = _requested_tool_version(meta) list_tools = getattr(high_level, "list_tools", None) - if list_tools is not None: - listed = await list_tools() - else: - # FastMCP 2.x only offers its listing through request middleware, - # which can consume rate limits, so read the registry instead. - listed = (await high_level.get_tools()).values() - candidates = [tool for tool in listed if tool.name == name] + if list_tools is None: + # FastMCP 2.x only lists through request middleware, which can + # spend rate limits, and its registry can disagree with what that + # middleware advertises, so the advertised schema is unknown. + return None + candidates = [tool for tool in await list_tools() if tool.name == name] if version is not None: candidates = [ tool diff --git a/posthog/test/mcp/test_fastmcp_v2.py b/posthog/test/mcp/test_fastmcp_v2.py index 54279b5f9..cb1a44817 100644 --- a/posthog/test/mcp/test_fastmcp_v2.py +++ b/posthog/test/mcp/test_fastmcp_v2.py @@ -220,7 +220,10 @@ def fresh(): event = _events(client, "$mcp_tool_call")[0]["properties"] assert event["$mcp_llm_model"] == "gpt-5" assert event["$mcp_llm_model_source"] == "client_metadata" - assert event["$mcp_input_keys"] == ["a", "b"] + # FastMCP 2.x can only list through request middleware, so the advertised + # schema is unknown there and the undeclared names stay redacted. + expected_keys = ["a", "b"] if hasattr(FastMCP, "list_tools") else ["[redacted]"] + assert event["$mcp_input_keys"] == expected_keys _OWN_MODEL = {"llm_model": {"type": "string"}} @@ -704,7 +707,7 @@ async def flaky_get_tool(name, *args, **kwargs): @pytest.mark.skipif( hasattr(FastMCP, "list_tools"), reason="FastMCP 2.x has no public list_tools" ) -async def test_schema_lookup_on_fastmcp_2_runs_no_request_middleware(): +async def test_schema_lookup_on_fastmcp_2_runs_no_request_middleware_and_trusts_no_schema(): from fastmcp.server.middleware import Middleware from posthog.mcp._instrument_lowlevel import _listed_input_schema @@ -721,5 +724,5 @@ async def on_list_tools(self, context, call_next): schema = await _listed_input_schema(server, "add", None) - assert schema is not None and set(schema["properties"]) >= {"a", "b"} + assert schema is None assert listings == []