Class: PostHog::MCP::Sink Private

Inherits:
Object
  • Object
show all
Defined in:
lib/posthog/mcp/sink.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The capture pipeline: stringify keys -> sanitize -> truncate -> fan out into $mcp_* / $exception payloads -> before_send -> PostHog::Client#capture.

Wraps a host-supplied client and never owns its lifecycle. Errors at any stage are logged and the event dropped, never re-raised into tool code.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Sink

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Sink.



15
16
17
# File 'lib/posthog/mcp/sink.rb', line 15

def initialize(client)
  @client = client
end

Instance Attribute Details

#client ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
# File 'lib/posthog/mcp/sink.rb', line 13

def client
  @client
end

Instance Method Details

#capture(event, options = nil) ⇒ Array<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the payloads handed to the client (for tests).

Parameters:

  • event (Hash) —

    internal event (symbol or string keys)

  • options (Options, nil) (defaults to: nil)

Returns:

  • (Array<Hash>) —

    the payloads handed to the client (for tests)



22
23
24
25
26
27
28
29
30
31
# File 'lib/posthog/mcp/sink.rb', line 22

def capture(event, options = nil)
  processed = process(event, options)
  return [] if processed.nil?

  processed.each { |payload| dispatch(payload) }
  processed
rescue StandardError => e
  Log.debug(options, "Failed to capture PostHog event: #{e.message}")
  []
end

#process(event, options = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the full transform and returns the payloads that survived before_send.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/posthog/mcp/sink.rb', line 34

def process(event, options = nil)
  processed = Sanitization.stringify_keys(event)
  begin
    processed = Sanitization.sanitize_event(processed)
  rescue StandardError => e
    Log.debug(options, "Failed to sanitize event: #{e.message}")
    return nil
  end
  begin
    processed = Truncation.truncate_event(processed)
  rescue StandardError => e
    Log.debug(options, "Failed to truncate event: #{e.message}")
    return nil
  end
  processed['id'] = Ids.new_prefixed_id('evt') unless processed['id'].is_a?(String) && !processed['id'].empty?

  autocapture = options.nil? || options.enable_exception_autocapture
  payloads = EventBuilder.build(processed, enable_exception_autocapture: autocapture)
  apply_before_send(payloads, options)
end