Skip to content

Commit db8ecb8

Browse files
authored
fix(mcp): report the Python package version (#921)
1 parent c7584fd commit db8ecb8

5 files changed

Lines changed: 21 additions & 16 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
pypi/posthog: patch
3+
---
4+
5+
Report the Python package version in MCP event metadata and request headers so SDK Health can assess the installed package.

‎posthog/mcp/README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ analytics = instrument(server, posthog)
1616
Install is just `pip install posthog`. `instrument()` needs the MCP SDK at runtime,
1717
but anyone wrapping a server already has it.
1818

19-
MCP analytics events report `$lib: "posthog-python-mcp"`. Because `$lib` is a
20-
client-level identity, `instrument()` relabels every event sent by the client passed
21-
to it. Use a client dedicated to MCP analytics if the application also captures
22-
unrelated events.
19+
MCP analytics events report `$lib: "posthog-python-mcp"` and the installed `posthog` package version in `$lib_version`.
20+
Request headers use the same identity and package version, so SDK Health can compare MCP traffic with Python SDK releases.
21+
Because `$lib` is a client-level identity, `instrument()` relabels every event sent by the client passed to it.
22+
Use a client dedicated to MCP analytics if the application also captures unrelated events.
2323

2424
## Stateless / multi-pod servers
2525

‎posthog/mcp/_lib_identity.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from __future__ import annotations
44

55
from ..client import Client
6+
from ..version import VERSION
67

78
from .constants import POSTHOG_MCP_LIB_NAME
8-
from .version import __version__
99

1010

1111
def apply_mcp_lib_identity(client: Client) -> None:
1212
"""Relabel every event sent by ``client`` as coming from ``posthog.mcp``."""
1313
set_identity = getattr(client, "_set_library_identity", None)
1414
if set_identity is not None:
15-
set_identity(POSTHOG_MCP_LIB_NAME, __version__)
15+
set_identity(POSTHOG_MCP_LIB_NAME, VERSION)

‎posthog/test/mcp/test_no_crash.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
from posthog.client import Client
1313
from posthog.mcp import instrument
14-
from posthog.mcp.version import __version__ as MCP_VERSION
1514
from posthog.test.mcp._helpers import MCP_MAJOR, FakeClient
15+
from posthog.version import VERSION
1616

1717

1818
async def test_unsupported_server_returns_noop_handle():
@@ -75,7 +75,7 @@ def before_send(event):
7575
client.capture("after instrumentation")
7676

7777
assert captured[0]["properties"]["$lib"] == "posthog-python-mcp"
78-
assert captured[0]["properties"]["$lib_version"] == MCP_VERSION
78+
assert captured[0]["properties"]["$lib_version"] == VERSION
7979

8080

8181
@pytest.mark.parametrize(

‎posthog/test/mcp/test_posthog_mcp.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
from posthog.capture_mode import CaptureMode
66
from posthog.mcp import PostHogMCP
7-
from posthog.mcp.version import __version__ as MCP_VERSION
87
from posthog.test.mcp._helpers import (
98
events_named as _events,
109
flush_background as _flush,
1110
)
11+
from posthog.version import VERSION
1212

1313

1414
def make_client():
@@ -73,7 +73,7 @@ def before_send(event):
7373
assert {event["event"] for event in captured} == {"$mcp_tool_call", "$exception"}
7474
assert all(
7575
event["properties"]["$lib"] == "posthog-python-mcp"
76-
and event["properties"]["$lib_version"] == MCP_VERSION
76+
and event["properties"]["$lib_version"] == VERSION
7777
for event in captured
7878
)
7979

@@ -86,7 +86,7 @@ def test_mcp_library_identity_reaches_capture_v0_header():
8686
client.capture("$mcp_custom")
8787

8888
assert post.call_args.kwargs["headers"]["User-Agent"] == (
89-
f"posthog-python-mcp/{MCP_VERSION}"
89+
f"posthog-python-mcp/{VERSION}"
9090
)
9191

9292

@@ -95,10 +95,10 @@ def test_mcp_library_identity_reaches_capture_v1_header():
9595
with mock.patch("posthog.client._send_v1_batch") as send:
9696
client.capture("$mcp_custom")
9797

98-
assert send.call_args.kwargs["sdk_info"] == f"posthog-python-mcp/{MCP_VERSION}"
98+
assert send.call_args.kwargs["sdk_info"] == f"posthog-python-mcp/{VERSION}"
9999
event = send.call_args.args[2][0]
100100
assert event["properties"]["$lib"] == "posthog-python-mcp"
101-
assert event["properties"]["$lib_version"] == MCP_VERSION
101+
assert event["properties"]["$lib_version"] == VERSION
102102

103103

104104
def test_mcp_library_identity_reaches_feature_flag_requests():
@@ -112,7 +112,7 @@ def test_mcp_library_identity_reaches_feature_flag_requests():
112112
client.evaluate_flags("user_1")
113113

114114
assert post.call_args.kwargs["headers"]["User-Agent"] == (
115-
f"posthog-python-mcp/{MCP_VERSION}"
115+
f"posthog-python-mcp/{VERSION}"
116116
)
117117

118118

@@ -130,7 +130,7 @@ def test_mcp_library_identity_reaches_feature_flag_definition_requests():
130130
client.load_feature_flags()
131131

132132
assert get.call_args.kwargs["headers"]["User-Agent"] == (
133-
f"posthog-python-mcp/{MCP_VERSION}"
133+
f"posthog-python-mcp/{VERSION}"
134134
)
135135
client.shutdown()
136136

@@ -144,7 +144,7 @@ def test_mcp_library_identity_reaches_remote_config_requests():
144144
assert client.get_remote_config_payload("flag-key") == "payload"
145145

146146
assert get.call_args.kwargs["headers"]["User-Agent"] == (
147-
f"posthog-python-mcp/{MCP_VERSION}"
147+
f"posthog-python-mcp/{VERSION}"
148148
)
149149

150150

0 commit comments

Comments
 (0)