diff --git a/.sampo/changesets/mcp-fastmcp2-input-keys.md b/.sampo/changesets/mcp-fastmcp2-input-keys.md new file mode 100644 index 000000000..9d47767f1 --- /dev/null +++ b/.sampo/changesets/mcp-fastmcp2-input-keys.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +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 00bd4ef4d..e6af0f65a 100644 --- a/posthog/mcp/_instrument_lowlevel.py +++ b/posthog/mcp/_instrument_lowlevel.py @@ -706,9 +706,13 @@ 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 - ] + list_tools = getattr(high_level, "list_tools", None) + 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 d8f18b86b..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"}} @@ -699,3 +702,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_and_trusts_no_schema(): + 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 None + assert listings == []