Add MCP Analytics support (PostHog\MCP helpers, plus a laravel/mcp integration)

Aperta
#310 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
Più di una settimana
Idoneità per principianti
18/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
laravel, php
Ambito
analytics, backend

Direzione di ricerca

This is a design proposal for a new PostHog\MCP namespace in the PHP SDK, not a scoped change. Start from the existing capture client in the package and the proposed MCP::captureToolCall signature, then decide the sessionId and redaction rules against the other SDKs. Done would need agreement from maintainers on the API surface and a first version covering only $mcp_tool_call.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement feature/mcp-analytics
Problem Statement

We run TryPost, an open-source social media scheduler built on Laravel. It exposes an MCP server with about 100 tools through the official laravel/mcp package, used daily from ChatGPT, Claude and Codex. We'd like to use MCP Analytics, but there is no PHP SDK for it: the install guide lists TypeScript, Python, Go, Ruby and Rust, and PHP only appears on the other languages page as "send the events yourself".

The product works the same whatever sends the events (it reads the event name and $mcp_source), so a manual capture('$mcp_tool_call', [...]) does work. What's missing is the part the other SDKs handle for you, and where mistakes are easy and silent:

  • Building the $mcp_* properties on every call ($mcp_source, $mcp_tool_name, $mcp_is_error, $mcp_error_type, $mcp_duration_ms, $mcp_server_name, client name and version, protocol version, …). A typo in posthog_mcp_analytics makes the calls invisible with no error.
  • Deriving $session_id the same way the other SDKs do (ses_ + the first 32 hex characters of a SHA-256), consistent across replicas.
  • Redacting and truncating $mcp_parameters, $mcp_response, $mcp_intent and error messages with the same rules the other SDKs apply. An agent can paste a credential into a tool argument.
  • The context intent argument: adding it to each advertised tool schema and stripping it before the tool runs.
  • Sending $process_person_profile: false for anonymous calls.

PHP is a big share of MCP servers today (Laravel ships laravel/mcp officially, and there's the official modelcontextprotocol/php-sdk for Symfony and plain PHP), so we probably aren't the only ones who read the install page and assumed the product isn't for them.

Solution Brainstorm

A PostHog\MCP namespace in this package (no new dependencies), with framework-agnostic per-call helpers, since PHP MCP servers don't share one server object to wrap. Roughly:

use PostHog\MCP;

// tools/list: optionally add the `context` intent argument (and get_more_tools)
$tools = MCP::prepareToolList($tools, context: true, reportMissing: false);

// tools/call: strip the injected argument before the tool runs
$prepared = MCP::prepareToolCall($name, $arguments);

MCP::captureToolCall(
    distinctId: $userId,
    tool: $name,
    durationMs: $durationMs,
    isError: $isError,
    errorType: 'validation',
    sessionId: MCP::sessionId($transportSessionId),
    intent: $prepared->intent,
    client: ['name' => 'claude-ai', 'version' => '1.0', 'user_agent' => $ua],
    server: ['name' => 'TryPost', 'version' => '1.0.0'],
    parameters: null, // opt-in, sanitised like the other SDKs
    groups: ['account' => $accountId],
);

Then, as follow-ups:

  • A laravel/mcp integration in posthog/posthog-laravel (a CallTool method subclass or a middleware), so a Laravel server opts in with one line of config. laravel/mcp lets a server replace its tools/call handler ($this->addMethod('tools/call', ...)), which is the natural hook.
  • A session strategy for stateless servers. laravel/mcp runs over stateless HTTP with no Mcp-Session-Id, which is the gap described in PostHog/posthog#119784. A helper that groups calls by an inactivity window keyed on distinct_id + client, without changing tool schemas or results, would cover it (the ChatGPT and Claude directories discourage telemetry ids in tool results).

A first version with $mcp_tool_call only would already unlock the dashboards, tool quality and sessions views; $mcp_tools_list, $mcp_initialize and $mcp_missing_capability could follow.

Related: PostHog/posthog-elixir#209 asks for the same helper in Elixir, and PostHog/posthog#119784 covers stateless sessions.

Happy to test a pre-release against our server.

Lingua principale
PHP
Stelle
32
Fork
36
Merge medio
1g 18h
PR unite (30g)
44

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di PostHog/posthog-php

Tutte le issue di PostHog/posthog-php

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.