Class: PostHog::MCP::TrackingData Private

Inherits:
Object
  • Object
show all
Defined in:
lib/posthog/mcp/tracking_data.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.

All per-server analytics state. Lives on the instrumented MCP::Server instance (never in module-level storage), guarded by its own mutex.

Constant Summary collapse

MAX_INITIALIZED_SESSIONS =

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

1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options:, sink:, server_name: nil, server_version: nil) ⇒ TrackingData

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 TrackingData.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/posthog/mcp/tracking_data.rb', line 17

def initialize(options:, sink:, server_name: nil, server_version: nil)
  @options = options
  @sink = sink
  @server_name = server_name
  @server_version = server_version
  @mutex = Mutex.new
  @session_id = nil
  @session_source = 'generated'
  @last_mcp_session_id = nil
  @last_activity = Time.now
  @warned_no_stateless_session = false
  # Set once a request arrives over HTTP: from then on the server-wide session
  # is never a safe fallback for a capture that lost its request scope.
  @http_transport_seen = false
  @warned_unscoped_capture = false
  # The `get_more_tools` class {Tools.register} added to the server, if any.
  @virtual_tool = nil
  @identified_sessions = IdentityCache.new
  @tool_descriptions = {}
  @tool_categories = {}
  # Which tools got `_mcp_instructions` declared at tools/list. Only those
  # may be mirrored into; absent fails closed.
  @tool_output_instructions = {}
  # Which injected argument names the analytics layer owns per tool (i.e.
  # the tool does not declare them itself).
  @tool_owned_params = {}
  @initialized_sessions = {}
end

Instance Attribute Details

#http_transport_seen ⇒ 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.



14
15
16
# File 'lib/posthog/mcp/tracking_data.rb', line 14

def http_transport_seen
  @http_transport_seen
end

#identified_sessions ⇒ 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.



12
13
14
# File 'lib/posthog/mcp/tracking_data.rb', line 12

def identified_sessions
  @identified_sessions
end

#last_activity ⇒ 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.



14
15
16
# File 'lib/posthog/mcp/tracking_data.rb', line 14

def last_activity
  @last_activity
end

#last_mcp_session_id ⇒ 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.



14
15
16
# File 'lib/posthog/mcp/tracking_data.rb', line 14

def last_mcp_session_id
  @last_mcp_session_id
end

#options ⇒ 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.



12
13
14
# File 'lib/posthog/mcp/tracking_data.rb', line 12

def options
  @options
end

#server_name ⇒ 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.



12
13
14
# File 'lib/posthog/mcp/tracking_data.rb', line 12

def server_name
  @server_name
end

#server_version ⇒ 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.



12
13
14
# File 'lib/posthog/mcp/tracking_data.rb', line 12

def server_version
  @server_version
end

#session_id ⇒ 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.



14
15
16
# File 'lib/posthog/mcp/tracking_data.rb', line 14

def session_id
  @session_id
end

#session_source ⇒ 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.



14
15
16
# File 'lib/posthog/mcp/tracking_data.rb', line 14

def session_source
  @session_source
end

#sink ⇒ 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.



12
13
14
# File 'lib/posthog/mcp/tracking_data.rb', line 12

def sink
  @sink
end

#tool_categories ⇒ 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.



12
13
14
# File 'lib/posthog/mcp/tracking_data.rb', line 12

def tool_categories
  @tool_categories
end

#tool_descriptions ⇒ 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.



12
13
14
# File 'lib/posthog/mcp/tracking_data.rb', line 12

def tool_descriptions
  @tool_descriptions
end

#tool_output_instructions ⇒ 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.



12
13
14
# File 'lib/posthog/mcp/tracking_data.rb', line 12

def tool_output_instructions
  @tool_output_instructions
end

#tool_owned_params ⇒ 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.



12
13
14
# File 'lib/posthog/mcp/tracking_data.rb', line 12

def tool_owned_params
  @tool_owned_params
end

#virtual_tool ⇒ 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.



14
15
16
# File 'lib/posthog/mcp/tracking_data.rb', line 14

def virtual_tool
  @virtual_tool
end

#warned_no_stateless_session ⇒ 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.



14
15
16
# File 'lib/posthog/mcp/tracking_data.rb', line 14

def warned_no_stateless_session
  @warned_no_stateless_session
end

#warned_unscoped_capture ⇒ 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.



14
15
16
# File 'lib/posthog/mcp/tracking_data.rb', line 14

def warned_unscoped_capture
  @warned_unscoped_capture
end

Instance Method Details

#claim_session_initialized(session_id) ⇒ 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.

Claim the one lazy initialize event a session gets. Check and mark are a single critical section, so when concurrent first requests race only the caller that inserted the key sees true and emits $mcp_initialize.



69
70
71
72
73
74
75
76
77
# File 'lib/posthog/mcp/tracking_data.rb', line 69

def claim_session_initialized(session_id)
  synchronize do
    next false if @initialized_sessions.key?(session_id)

    @initialized_sessions[session_id] = true
    @initialized_sessions.shift while @initialized_sessions.length > MAX_INITIALIZED_SESSIONS
    true
  end
end

#mark_session_initialized(session_id) ⇒ 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.



54
55
56
57
58
59
60
# File 'lib/posthog/mcp/tracking_data.rb', line 54

def mark_session_initialized(session_id)
  synchronize do
    @initialized_sessions.delete(session_id)
    @initialized_sessions[session_id] = true
    @initialized_sessions.shift while @initialized_sessions.length > MAX_INITIALIZED_SESSIONS
  end
end

#remember_tool(name, description: nil, category: nil, owned_params: nil, output_instructions: 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.



79
80
81
82
83
84
85
86
# File 'lib/posthog/mcp/tracking_data.rb', line 79

def remember_tool(name, description: nil, category: nil, owned_params: nil, output_instructions: nil)
  synchronize do
    @tool_descriptions[name] = description if description.is_a?(String) && !description.empty?
    @tool_categories[name] = category if category.is_a?(String) && !category.empty?
    @tool_owned_params[name] = owned_params unless owned_params.nil?
    @tool_output_instructions[name] = output_instructions unless output_instructions.nil?
  end
end

#session_initialized?(session_id) ⇒ Boolean

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:

  • (Boolean)


62
63
64
# File 'lib/posthog/mcp/tracking_data.rb', line 62

def session_initialized?(session_id)
  synchronize { @initialized_sessions.key?(session_id) }
end

#synchronize(&block) ⇒ 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.



46
47
48
49
50
51
52
# File 'lib/posthog/mcp/tracking_data.rb', line 46

def synchronize(&block)
  if @mutex.owned?
    yield
  else
    @mutex.synchronize(&block)
  end
end