Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/mcp-fastmcp2-input-keys.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 7 additions & 3 deletions posthog/mcp/_instrument_lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 28 additions & 1 deletion posthog/test/mcp/test_fastmcp_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}
Expand Down Expand Up @@ -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 == []
Loading