Module: PostHog::MCP::Session Private

Defined in:
lib/posthog/mcp/session.rb

Overview

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

Session id resolution, in priority order: the agent's echoed conversation_id handle first, then our self-encoded session token, then the transport's MCP session id, then this server's own memory (which rolls over after INACTIVITY_TIMEOUT_MINUTES of inactivity).

Class Method Summary collapse

Class Method Details

.derive_session_id_from_conversation(conversation_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.

Deterministic, so every server that sees the same handle derives the same session.



23
24
25
# File 'lib/posthog/mcp/session.rb', line 23

def derive_session_id_from_conversation(conversation_id)
  Ids.deterministic_prefixed_id('ses', conversation_id)
end

.derive_session_id_from_mcp_session(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.



18
19
20
# File 'lib/posthog/mcp/session.rb', line 18

def derive_session_id_from_mcp_session(mcp_session_id)
  Ids.deterministic_prefixed_id('ses', mcp_session_id)
end

.new_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/session.rb', line 14

def new_session_id
  Ids.new_prefixed_id('ses')
end

.resolve(data, mcp_session_id, token: nil, conversation_id: nil) ⇒ Array(String, String)

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 [session_id, source] where source is one of conversation, token, mcp, generated.

Returns:

  • (Array(String, String)) —

    [session_id, source] where source is one of conversation, token, mcp, generated



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/posthog/mcp/session.rb', line 29

def resolve(data, mcp_session_id, token: nil, conversation_id: nil)
  return [derive_session_id_from_conversation(conversation_id), 'conversation'] if present?(conversation_id)

  data.synchronize do
    now = Time.now

    if token
      data.session_id = token.session_id
      data.session_source = 'token'
      data.last_activity = now
      return [data.session_id, 'token']
    end

    if present?(mcp_session_id)
      data.session_id = derive_session_id_from_mcp_session(mcp_session_id)
      data.last_mcp_session_id = mcp_session_id
      data.session_source = 'mcp'
      data.last_activity = now
      return [data.session_id, 'mcp']
    end

    if data.session_source == 'mcp' && data.last_mcp_session_id
      data.last_activity = now
      return [data.session_id, 'mcp']
    end

    stale = (now - data.last_activity) > (INACTIVITY_TIMEOUT_MINUTES * 60)
    if data.session_source != 'generated' || stale || data.session_id.nil?
      data.session_id = new_session_id
      data.session_source = 'generated'
    end
    data.last_activity = now
    [data.session_id, 'generated']
  end
end