Documentation
¶
Overview ¶
Package posthogmcp builds and enqueues canonical PostHog analytics events for Model Context Protocol activity without depending on a particular MCP framework.
Applications own the PostHog client lifecycle and pass completed tool calls to Analytics. Tool parameters, responses, intent, and error messages are sanitized and bounded before they are queued. A call that names an unregistered tool, an input_required round the client receives, and a report of a missing capability, are not tool calls: Analytics.CaptureUnknownTool, Analytics.CaptureInputRequired, and Analytics.CaptureMissingCapability send their own events. Parameter or response JSON exceeding 1 MiB after media redaction is replaced with an omission marker.
MCP events carry $lib posthog-go-mcp. A client in CaptureModeAnalyticsV1 sends one library name per request, taken from the SDK, so there its MCP events report posthog-go.
Index ¶
- type Analytics
- func (a *Analytics) CaptureInputRequired(ctx context.Context, event InputRequired) error
- func (a *Analytics) CaptureMissingCapability(ctx context.Context, event MissingCapability) error
- func (a *Analytics) CaptureToolCall(ctx context.Context, call ToolCall) error
- func (a *Analytics) CaptureUnknownTool(ctx context.Context, event UnknownTool) error
- type EventContext
- type InputRequired
- type IntentSource
- type MissingCapability
- type ModelSource
- type Option
- type ToolCall
- type UnknownTool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Analytics ¶
type Analytics struct {
// contains filtered or unexported fields
}
Analytics constructs and enqueues canonical PostHog MCP analytics events. It does not own the lifecycle of the PostHog client.
func New ¶
func New(client posthog.EnqueueClient, opts ...Option) *Analytics
New creates an MCP analytics recorder using client.
func (*Analytics) CaptureInputRequired ¶ added in v1.30.0
func (a *Analytics) CaptureInputRequired(ctx context.Context, event InputRequired) error
CaptureInputRequired validates, transforms, and enqueues one $mcp_input_required event, carrying the methods of the round's requests and never their content.
func (*Analytics) CaptureMissingCapability ¶ added in v1.32.0
func (a *Analytics) CaptureMissingCapability(ctx context.Context, event MissingCapability) error
CaptureMissingCapability validates, transforms, and enqueues one $mcp_missing_capability event, with the virtual tool's name as $mcp_resource_name. It is not a tool call, and enqueues no $mcp_tool_call or $exception.
func (*Analytics) CaptureToolCall ¶
CaptureToolCall validates, transforms, and enqueues one completed MCP tool call. When exception autocapture is enabled, all messages are built before either is enqueued. Enqueue failures are joined after every configured message has been attempted.
A posthog.RequestContext attached to ctx supplies the distinct and session IDs the call leaves empty, and its properties sit under call.Properties. They go through the same reserved-key and sanitization rules as the call's own.
Example ¶
package main
import (
"context"
"log"
"time"
posthog "github.com/posthog/posthog-go"
"github.com/posthog/posthog-go/posthogmcp"
)
func main() {
client := posthog.New("phc_project_key")
defer client.Close()
analytics := posthogmcp.New(client)
err := analytics.CaptureToolCall(context.Background(), posthogmcp.ToolCall{
ToolName: "search_docs",
DistinctID: "user_123",
Duration: 42 * time.Millisecond,
})
if err != nil {
log.Printf("capture MCP analytics: %v", err)
}
}
Output:
func (*Analytics) CaptureUnknownTool ¶ added in v1.30.0
func (a *Analytics) CaptureUnknownTool(ctx context.Context, event UnknownTool) error
CaptureUnknownTool validates, transforms, and enqueues one $mcp_unknown_tool event. It is not a tool call, and enqueues no $mcp_tool_call or $exception.
type EventContext ¶ added in v1.30.0
type EventContext struct {
// DistinctID falls back to the RequestContext's, then SessionID, then "anonymous".
DistinctID string
// SessionID is captured as $session_id. A valid ConversationID replaces it.
SessionID string
// Groups is captured as $groups.
Groups posthog.Groups
// SetProperties is captured as $set, and needs an explicit DistinctID.
SetProperties posthog.Properties
ServerName string
ServerVersion string
ClientName string
ClientVersion string
ProtocolVersion string
ConversationID string
ClientUserAgent string
VendorClient string
// Properties adds custom event metadata. $mcp_* and identity control keys
// are reserved.
Properties posthog.Properties
// Timestamp is the time of the event. When zero, the PostHog client stamps
// the time it enqueues the event.
Timestamp time.Time
}
EventContext is what $mcp_unknown_tool, $mcp_input_required, and $mcp_missing_capability carry besides their own fields. Each field is captured, redacted, and bounded like the ToolCall field of the same name.
type InputRequired ¶ added in v1.30.0
type InputRequired struct {
EventContext
// ToolName is required and not blank.
ToolName string
// Duration is how long this round took, and must not be negative.
Duration time.Duration
// Methods is the method of each input request, in the order of the
// requests' keys and duplicates kept, captured as
// $mcp_input_request_methods. Never pass the requests or the answers.
Methods []string
}
InputRequired describes one input_required round a client received. The call is not complete: the retry that completes it is a ToolCall.
type IntentSource ¶
type IntentSource string
IntentSource identifies how an MCP tool-call intent was obtained.
const ( // IntentSourceContextParameter means the MCP client supplied the intent in a // tool context parameter. IntentSourceContextParameter IntentSource = "context_parameter" // IntentSourceInferred means the host application inferred the intent. IntentSourceInferred IntentSource = "inferred" )
type MissingCapability ¶ added in v1.32.0
type MissingCapability struct {
EventContext
// ToolName is the virtual tool's name, captured as $mcp_resource_name. It is
// required and not blank.
ToolName string
// Intent is the agent's report, captured as $mcp_intent with source
// context_parameter, redacted and bounded like ToolCall.Intent.
Intent string
// LLMModel is captured as $mcp_llm_model, like ToolCall.LLMModel.
LLMModel string
// LLMModelSource is like ToolCall.LLMModelSource.
LLMModelSource ModelSource
}
MissingCapability describes one report an agent made through the virtual tool that asks for a capability the server lacks.
type ModelSource ¶ added in v1.29.0
type ModelSource string
ModelSource identifies how an MCP tool-call LLM model name was obtained.
const ( // ModelSourceClientMetadata means the MCP client's own metadata named the // model. ModelSourceClientMetadata ModelSource = "client_metadata" // ModelSourceSelfReported means the calling agent reported the model. ModelSourceSelfReported ModelSource = "self_reported" )
type Option ¶
type Option func(*config)
Option configures Analytics.
func WithExceptionAutocapture ¶
WithExceptionAutocapture controls whether failed tool calls also enqueue a PostHog exception. It is enabled by default.
type ToolCall ¶
type ToolCall struct {
// ToolName is required and not blank. It is captured as $mcp_tool_name and
// $mcp_resource_name.
ToolName string
// ToolDescription is captured as $mcp_tool_description.
ToolDescription string
// ToolCategory is captured as $mcp_tool_category.
ToolCategory string
// DistinctID is the event's distinct ID. When empty, the ID falls back to
// the RequestContext's, then SessionID, then "anonymous", and no person
// profile is processed.
DistinctID string
// SessionID is captured as $session_id and is the distinct ID fallback. A
// valid ConversationID replaces it with the session derived from the handle.
SessionID string
// Groups is captured as $groups on both events.
Groups posthog.Groups
// SetProperties is captured as $set on $mcp_tool_call. It needs an explicit
// DistinctID and is dropped without one.
SetProperties posthog.Properties
// ServerName is captured as $mcp_server_name on both events.
ServerName string
// ServerVersion is captured as $mcp_server_version on both events.
ServerVersion string
// ClientName is captured as $mcp_client_name on both events.
ClientName string
// ClientVersion is captured as $mcp_client_version on both events.
ClientVersion string
// ProtocolVersion is captured as $mcp_protocol_version on both events.
ProtocolVersion string
// ConversationID is captured as $mcp_conversation_id on both events, since it
// joins tool calls into a conversation. Only a UUIDv7-shaped value is kept,
// lowercased, and anything else is dropped.
ConversationID string
// ClientUserAgent is the HTTP User-Agent header of the request, captured as
// $mcp_client_user_agent on $mcp_tool_call with credentials redacted.
ClientUserAgent string
// VendorClient is the client vendor the transport reported, captured as
// $mcp_vendor_client on $mcp_tool_call.
VendorClient string
// LLMModel is the model that made the call, captured as $mcp_llm_model on
// $mcp_tool_call. A blank value or "unknown" is not recorded.
LLMModel string
// LLMModelSource says where LLMModel came from and defaults to
// ModelSourceSelfReported. It is only recorded, and only validated, with a model.
LLMModelSource ModelSource
// Intent is the agent's stated reason for the call. When it arrives as a
// tool argument, remove that argument from Parameters: Parameters only has
// credentials redacted, while Intent also has personal data redacted.
Intent string
// IntentSource says how Intent was obtained and defaults to
// IntentSourceContextParameter. It is only recorded with an Intent.
IntentSource IntentSource
// Parameters is captured as $mcp_parameters as given, like the manual
// capture APIs in the Python and TypeScript SDKs. Automatic
// instrumentation passes the JSON-RPC request here, as
// {"request": {"method": "tools/call", "params": {"name": ..., "arguments": ...}}}.
Parameters any
// Response is captured as $mcp_response with credentials and media content
// redacted. A response too large to process is replaced by a marker.
Response any
// Duration is captured as $mcp_duration_ms and must not be negative.
Duration time.Duration
// IsError marks a failed call that has no Go error, such as an MCP result
// with isError set. A non-nil Error implies IsError.
IsError bool
// Error is the failure, if any. When set, it is also captured as a
// $exception unless exception autocapture is disabled.
Error error
// ErrorType is a coarse category captured as $mcp_error_type, such as
// "validation" or "timeout". When empty it is the Go type of Error, such as
// fs.PathError, else "Error". The $exception event always carries the Go
// type of Error.
ErrorType string
// Properties adds custom event metadata. $mcp_* and identity control keys
// are reserved; use the corresponding ToolCall fields instead.
Properties posthog.Properties
// Timestamp is the time of the event. When zero, the PostHog client stamps
// the time it enqueues the event.
Timestamp time.Time
}
ToolCall describes one completed MCP tool invocation.
type UnknownTool ¶ added in v1.30.0
type UnknownTool struct {
EventContext
// ToolName is the name the agent sent, captured as $mcp_tool_name and
// redacted as free text. It is required and not blank.
ToolName string
}
UnknownTool describes a call that named a tool the server has not registered.