Skip to content
Open
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/somber-runesinger-tuonetar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: patch
---

Report the Python package version in MCP event metadata and request headers so SDK Health can assess the installed package.
8 changes: 4 additions & 4 deletions posthog/mcp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ analytics = instrument(server, posthog)
Install is just `pip install posthog`. `instrument()` needs the MCP SDK at runtime,
but anyone wrapping a server already has it.

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

## Stateless / multi-pod servers

Expand Down
4 changes: 2 additions & 2 deletions posthog/mcp/_lib_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from __future__ import annotations

from ..client import Client
from ..version import VERSION

from .constants import POSTHOG_MCP_LIB_NAME
from .version import __version__


def apply_mcp_lib_identity(client: Client) -> None:
"""Relabel every event sent by ``client`` as coming from ``posthog.mcp``."""
set_identity = getattr(client, "_set_library_identity", None)
if set_identity is not None:
set_identity(POSTHOG_MCP_LIB_NAME, __version__)
set_identity(POSTHOG_MCP_LIB_NAME, VERSION)
4 changes: 2 additions & 2 deletions posthog/test/mcp/test_no_crash.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

from posthog.client import Client
from posthog.mcp import instrument
from posthog.mcp.version import __version__ as MCP_VERSION
from posthog.test.mcp._helpers import MCP_MAJOR, FakeClient
from posthog.version import VERSION


async def test_unsupported_server_returns_noop_handle():
Expand Down Expand Up @@ -75,7 +75,7 @@ def before_send(event):
client.capture("after instrumentation")

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


@pytest.mark.parametrize(
Expand Down
16 changes: 8 additions & 8 deletions posthog/test/mcp/test_posthog_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

from posthog.capture_mode import CaptureMode
from posthog.mcp import PostHogMCP
from posthog.mcp.version import __version__ as MCP_VERSION
from posthog.test.mcp._helpers import (
events_named as _events,
flush_background as _flush,
)
from posthog.version import VERSION


def make_client():
Expand Down Expand Up @@ -73,7 +73,7 @@ def before_send(event):
assert {event["event"] for event in captured} == {"$mcp_tool_call", "$exception"}
assert all(
event["properties"]["$lib"] == "posthog-python-mcp"
and event["properties"]["$lib_version"] == MCP_VERSION
and event["properties"]["$lib_version"] == VERSION
for event in captured
)

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

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


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

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


def test_mcp_library_identity_reaches_feature_flag_requests():
Expand All @@ -112,7 +112,7 @@ def test_mcp_library_identity_reaches_feature_flag_requests():
client.evaluate_flags("user_1")

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


Expand All @@ -130,7 +130,7 @@ def test_mcp_library_identity_reaches_feature_flag_definition_requests():
client.load_feature_flags()

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

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

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


Expand Down