posthog-ruby 3.35.5 → 3.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/posthog/mcp/README.md +1 -0
- data/lib/posthog/mcp/analytics.rb +53 -13
- data/lib/posthog/mcp/client_conversation.rb +62 -0
- data/lib/posthog/mcp/constants.rb +1 -0
- data/lib/posthog/mcp/conversation_id.rb +2 -2
- data/lib/posthog/mcp/event_builder.rb +4 -0
- data/lib/posthog/mcp/instrumentation.rb +105 -20
- data/lib/posthog/mcp/request_scope.rb +2 -1
- data/lib/posthog/mcp/session.rb +36 -17
- data/lib/posthog/mcp/tracking_data.rb +5 -4
- data/lib/posthog/mcp.rb +1 -0
- data/lib/posthog/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fa1af5a1007154eb69081c96f31c9b81b0070a6b3f1ff02075ef6f8f9ece512c
|
|
4
|
+
data.tar.gz: 60f1f2b27c6f492d3241ef6942f5873305268f350072d1f93614d67dcbc3765a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3b9ccc3390cbbf623d7266ca537cfaeda7f14160fd5814ac6bd1b5613aa05558ce1a8dbdd150e732a7f42633d1018acbe75ebbb997c3821f5c758304b51c3dfe
|
|
7
|
+
data.tar.gz: bdbd3873deedb2e1d674d9560c3915cffbc6df0d9ab3717a734aed12b46f78ace41beef3c84081e8adaa3071a35c838bd207bef4b97c3000cc8d13eda0cc8101
|
data/lib/posthog/mcp/README.md
CHANGED
|
@@ -43,6 +43,7 @@ These are the few things that differ from what the docs describe for the other S
|
|
|
43
43
|
| `mcp.rb` (parent dir) | `PostHog::MCP.instrument` and the public helpers |
|
|
44
44
|
| `server_extension.rb` | Prepended onto `MCP::Server` and the Streamable HTTP transport |
|
|
45
45
|
| `instrumentation.rb` | Per-request dispatch: intent, identity, sessions, recording |
|
|
46
|
+
| `session.rb`, `client_conversation.rb` | Session resolution and the clients whose `_meta` conversation ids it reads |
|
|
46
47
|
| `client.rb` | `PostHog::MCP::Client` for custom dispatchers |
|
|
47
48
|
| `rack_middleware.rb` | `Mcp-Session-Id` tokens for a custom Rack stack |
|
|
48
49
|
| `sanitization.rb`, `truncation.rb` | Redaction and payload budgets |
|
|
@@ -9,6 +9,9 @@ module PostHog
|
|
|
9
9
|
class Analytics
|
|
10
10
|
FLUSH_TIMEOUT_SECONDS = 10
|
|
11
11
|
private_constant :FLUSH_TIMEOUT_SECONDS
|
|
12
|
+
MAX_HELD_EVENTS = 1000
|
|
13
|
+
MAX_HELD_BYTES = 4 * 1024 * 1024
|
|
14
|
+
private_constant :MAX_HELD_EVENTS, :MAX_HELD_BYTES
|
|
12
15
|
|
|
13
16
|
# @api private
|
|
14
17
|
def initialize(server)
|
|
@@ -39,14 +42,14 @@ module PostHog
|
|
|
39
42
|
return if data.nil?
|
|
40
43
|
|
|
41
44
|
scope = RequestScope.current
|
|
42
|
-
scope = nil unless scope.is_a?(Hash)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
scope = nil unless scope.is_a?(Hash) && scope[:owner].equal?(data)
|
|
46
|
+
input = { 'event_type' => EventType::CUSTOM, 'event_name' => event, 'timestamp' => Time.now.utc,
|
|
47
|
+
'properties' => properties }
|
|
48
|
+
return if hold(data, scope, input)
|
|
49
|
+
|
|
50
|
+
session_id, source = current_session(data, scope)
|
|
51
|
+
Instrumentation.capture_event(data, input.merge('session_id' => session_id, 'session_source' => source),
|
|
52
|
+
actor: scoped_actor(scope))
|
|
50
53
|
nil
|
|
51
54
|
end
|
|
52
55
|
|
|
@@ -80,13 +83,50 @@ module PostHog
|
|
|
80
83
|
# created before the request. Fail closed with a standalone session
|
|
81
84
|
# rather than filing the event under another caller's identity; over stdio a
|
|
82
85
|
# server only ever talks to one client, so the fallback stays.
|
|
83
|
-
def
|
|
84
|
-
|
|
85
|
-
return
|
|
86
|
-
return data.session_id unless data.http_transport_seen
|
|
86
|
+
def current_session(data, scope)
|
|
87
|
+
return scope.values_at(:session_id, :session_source) if scope&.[](:session_id)
|
|
88
|
+
return [data.session_id, data.session_source] unless data.http_transport_seen
|
|
87
89
|
|
|
88
90
|
warn_unscoped_capture(data)
|
|
89
|
-
Session.new_session_id
|
|
91
|
+
[Session.new_session_id, 'generated']
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# The sink's own sanitize and truncate steps run at capture, so a held event
|
|
95
|
+
# is a snapshot no later change to its properties reaches, and its size is bounded.
|
|
96
|
+
# {Sink#process} runs both steps again on release; they are idempotent, so the second pass is deliberate.
|
|
97
|
+
def hold(data, scope, input)
|
|
98
|
+
return false unless scope&.key?(:held)
|
|
99
|
+
|
|
100
|
+
snapshot = Truncation.truncate_event(Sanitization.sanitize_event(
|
|
101
|
+
Sanitization.stringify_keys(input.merge('duration' => 0))
|
|
102
|
+
))
|
|
103
|
+
bytes = JSON.generate(snapshot['properties'] || {}).bytesize
|
|
104
|
+
data.synchronize do
|
|
105
|
+
held = scope[:held]
|
|
106
|
+
next false unless held
|
|
107
|
+
|
|
108
|
+
held_bytes = scope.fetch(:held_bytes, 0) + bytes
|
|
109
|
+
if held.size < MAX_HELD_EVENTS && held_bytes <= MAX_HELD_BYTES
|
|
110
|
+
held << snapshot
|
|
111
|
+
scope[:held_bytes] = held_bytes
|
|
112
|
+
next true
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
warn_overflow(data, scope)
|
|
116
|
+
false
|
|
117
|
+
end
|
|
118
|
+
rescue StandardError => e
|
|
119
|
+
Log.debug(data.options, "analytics.capture could not hold its event, sending it now: #{e.message}")
|
|
120
|
+
false
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def warn_overflow(data, scope)
|
|
124
|
+
return if scope[:overflowed_held]
|
|
125
|
+
|
|
126
|
+
scope[:overflowed_held] = true
|
|
127
|
+
Log.warn(data.options, "Warning: a tool call captured more than #{MAX_HELD_EVENTS} custom events or " \
|
|
128
|
+
"#{MAX_HELD_BYTES / 1024 / 1024} MiB of them before its conversation handle was " \
|
|
129
|
+
'delivered; sending the rest with the session the call has so far')
|
|
90
130
|
end
|
|
91
131
|
|
|
92
132
|
# The identity the in-flight request resolved, which {Instrumentation} pinned
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PostHog
|
|
4
|
+
module MCP
|
|
5
|
+
# {ROWS} is the table of the MCP analytics spec's "Session identity"
|
|
6
|
+
# requirement, row for row, in its order. posthog-js and posthog-python keep
|
|
7
|
+
# the same table.
|
|
8
|
+
#
|
|
9
|
+
# @api private
|
|
10
|
+
module ClientConversation
|
|
11
|
+
Row = Struct.new(:key, :id, :user_agent, :client_names, :handle_free, keyword_init: true) do
|
|
12
|
+
def recognizes?(user_agent, client_name)
|
|
13
|
+
return true if user_agent.is_a?(String) && self.user_agent.match?(user_agent)
|
|
14
|
+
|
|
15
|
+
client_name.is_a?(String) && client_names.any? { |name| name.casecmp?(client_name) }
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
UUID = /\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/
|
|
20
|
+
NIL_UUID = '00000000-0000-0000-0000-000000000000'
|
|
21
|
+
PRINTABLE_ASCII = /\A[\x20-\x7e]{1,128}\z/
|
|
22
|
+
|
|
23
|
+
uuid = lambda do |value|
|
|
24
|
+
lowered = value.downcase
|
|
25
|
+
lowered if UUID.match?(lowered) && lowered != NIL_UUID
|
|
26
|
+
end
|
|
27
|
+
printable = ->(value) { value if PRINTABLE_ASCII.match?(value) }
|
|
28
|
+
|
|
29
|
+
ROWS = [
|
|
30
|
+
Row.new(key: 'openai/session', id: printable, handle_free: true,
|
|
31
|
+
user_agent: %r{\Aopenai-mcp/[^\s(]*\s*\(\s*chatgpt\s*[;,)]}i, client_names: []),
|
|
32
|
+
Row.new(key: 'openai/session', id: printable, handle_free: false,
|
|
33
|
+
user_agent: %r{\Aopenai-mcp/}i, client_names: []),
|
|
34
|
+
Row.new(key: 'sessionId', id: uuid, handle_free: false,
|
|
35
|
+
user_agent: %r{\Acodex-mcp-client/}i, client_names: ['codex-mcp-client']),
|
|
36
|
+
Row.new(key: 'vscode.conversationId', id: uuid, handle_free: false,
|
|
37
|
+
user_agent: %r{\AVisual Studio Code(?: - Insiders)?/}i,
|
|
38
|
+
client_names: ['Visual Studio Code', 'Visual Studio Code - Insiders']),
|
|
39
|
+
Row.new(key: 'com.anthropic/session', id: printable, handle_free: false,
|
|
40
|
+
user_agent: %r{\A(?:Claude-User\z|claude-code/)}i, client_names: ['Anthropic/ClaudeAI', 'claude-code']),
|
|
41
|
+
Row.new(key: 'gen_ai.conversation.id', id: printable, handle_free: false,
|
|
42
|
+
user_agent: %r{\ACursor/}i, client_names: %w[Cursor cursor-vscode])
|
|
43
|
+
].freeze
|
|
44
|
+
|
|
45
|
+
module_function
|
|
46
|
+
|
|
47
|
+
def recognize(user_agent, client_name)
|
|
48
|
+
ROWS.find { |row| row.recognizes?(user_agent, client_name) }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def redacted_keys(user_agent, client_name)
|
|
52
|
+
ROWS.select { |row| row.recognizes?(user_agent, client_name) }.map(&:key).uniq
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def read(row, meta)
|
|
56
|
+
value = row && SchemaMutation.fetch(meta, row.key)
|
|
57
|
+
id = row.id.call(value) if value.is_a?(String) && value.valid_encoding?
|
|
58
|
+
id && "#{row.key}:#{id}"
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -69,6 +69,7 @@ module PostHog
|
|
|
69
69
|
SERVER_NAME = '$mcp_server_name'
|
|
70
70
|
SERVER_VERSION = '$mcp_server_version'
|
|
71
71
|
SESSION_ID = '$session_id'
|
|
72
|
+
SESSION_SOURCE = '$mcp_session_source'
|
|
72
73
|
SOURCE = '$mcp_source'
|
|
73
74
|
TOOL_CATEGORY = '$mcp_tool_category'
|
|
74
75
|
TOOL_DESCRIPTION = '$mcp_tool_description'
|
|
@@ -38,13 +38,13 @@ module PostHog
|
|
|
38
38
|
# own schema is passed application data, which must not anchor analytics:
|
|
39
39
|
# two users sharing such a value would be stitched into one conversation.
|
|
40
40
|
# @return [Array(String, Boolean), Array(nil, false)] `[conversation_id, minted]`
|
|
41
|
-
def resolve(enabled, supplied, tool_name, missing_capability_tool_name)
|
|
41
|
+
def resolve(enabled, supplied, tool_name, missing_capability_tool_name, mint: true)
|
|
42
42
|
return [nil, false] if !enabled || tool_name == missing_capability_tool_name
|
|
43
43
|
|
|
44
44
|
value = normalize(supplied)
|
|
45
45
|
return [value.downcase, false] if value && MINTED_CONVERSATION_ID.match?(value)
|
|
46
46
|
|
|
47
|
-
[Ids.uuid_v7, true]
|
|
47
|
+
mint ? [Ids.uuid_v7, true] : [nil, false]
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
def prompt_back?(result)
|
|
@@ -8,6 +8,8 @@ module PostHog
|
|
|
8
8
|
#
|
|
9
9
|
# @api private
|
|
10
10
|
module EventBuilder
|
|
11
|
+
WIRE_SESSION_SOURCES = { 'token' => 'transport', 'mcp' => 'transport' }.freeze
|
|
12
|
+
|
|
11
13
|
module_function
|
|
12
14
|
|
|
13
15
|
# @return [Array<Hash>] payloads `{'event', 'distinct_id', 'properties', 'timestamp'}`
|
|
@@ -44,6 +46,8 @@ module PostHog
|
|
|
44
46
|
def add_session_id(event, properties)
|
|
45
47
|
session_id = event['session_id']
|
|
46
48
|
properties[Property::SESSION_ID] = session_id if session_id.is_a?(String) && !session_id.empty?
|
|
49
|
+
source = event['session_source']
|
|
50
|
+
properties[Property::SESSION_SOURCE] = WIRE_SESSION_SOURCES.fetch(source, source) if source
|
|
47
51
|
end
|
|
48
52
|
|
|
49
53
|
def add_conversation_id(event, properties)
|
|
@@ -41,6 +41,7 @@ module PostHog
|
|
|
41
41
|
# Passed as `actor:` by a caller that has no request identity to offer, and
|
|
42
42
|
# is distinct from an explicit `nil` (a request that resolved to nobody).
|
|
43
43
|
UNRESOLVED_ACTOR = Object.new.freeze
|
|
44
|
+
INVOCATION_KEYS = %i[owner session_id session_source actor held held_bytes overflowed_held].freeze
|
|
44
45
|
|
|
45
46
|
class << self
|
|
46
47
|
def tracked?(method)
|
|
@@ -105,37 +106,56 @@ module PostHog
|
|
|
105
106
|
@token = SessionToken.decode(header_session_id)
|
|
106
107
|
@data.http_transport_seen = true if http?
|
|
107
108
|
@identified_in_request = {}
|
|
109
|
+
@session_sources = {}
|
|
108
110
|
end
|
|
109
111
|
|
|
110
112
|
# Runs the wrapped handler and records the request.
|
|
111
113
|
def dispatch(&handler)
|
|
112
114
|
@start = self.class.monotonic_now
|
|
113
|
-
return dispatch_kind(&handler) if @scope
|
|
115
|
+
return dispatch_kind(&handler) if @scope && !enclosing_invocation?
|
|
114
116
|
|
|
115
117
|
# Only the Streamable HTTP transport publishes a scope. Opening one for
|
|
116
118
|
# every other transport too (stdio, a custom dispatcher) means the session
|
|
117
119
|
# settled before the tool body runs is the one {Analytics#capture} reads
|
|
118
120
|
# inside it, whatever the request is anchored on.
|
|
119
|
-
|
|
121
|
+
#
|
|
122
|
+
# A dispatch a tool body makes synchronously gets its own scope too. The
|
|
123
|
+
# enclosing invocation's session, identity and held events belong to it
|
|
124
|
+
# alone, and a thread it spawned still holds that scope, so the nested
|
|
125
|
+
# call must never write to it.
|
|
126
|
+
enclosing = @scope
|
|
127
|
+
RequestScope.with(headers: @headers, transport: enclosing ? enclosing[:transport] : :other) do |scope|
|
|
120
128
|
@scope = scope
|
|
121
129
|
dispatch_kind(&handler)
|
|
130
|
+
ensure
|
|
131
|
+
enclosing[:mint] = scope[:mint] if enclosing && scope[:mint]
|
|
122
132
|
end
|
|
123
133
|
end
|
|
124
134
|
|
|
125
135
|
private
|
|
126
136
|
|
|
127
137
|
def dispatch_kind(&)
|
|
138
|
+
@client_conversation = safely { ClientConversation.read(client_row, envelope_meta) } if @kind == :tools_call
|
|
139
|
+
safely { drop_client_conversation_key }
|
|
128
140
|
case @kind
|
|
129
141
|
when :tools_call then dispatch_tool_call(&)
|
|
130
142
|
when :tools_list then dispatch_tools_list(&)
|
|
131
143
|
when :initialize then dispatch_initialize(&)
|
|
132
144
|
else dispatch_generic(&)
|
|
133
145
|
end
|
|
146
|
+
ensure
|
|
147
|
+
safely { release_custom_events(@scope[:session_id]) }
|
|
148
|
+
Session.end_tool_call(@data) if @kind == :tools_call && !http?
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def enclosing_invocation?
|
|
152
|
+
INVOCATION_KEYS.any? { |key| @scope.key?(key) }
|
|
134
153
|
end
|
|
135
154
|
|
|
136
155
|
# --- dispatchers -------------------------------------------------------
|
|
137
156
|
|
|
138
157
|
def dispatch_tool_call
|
|
158
|
+
Session.start_tool_call(@data) unless http?
|
|
139
159
|
name = fetch(@params, :name)
|
|
140
160
|
arguments = fetch(@params, :arguments)
|
|
141
161
|
arguments = nil unless arguments.is_a?(Hash)
|
|
@@ -146,11 +166,13 @@ module PostHog
|
|
|
146
166
|
# Resolution reads the stripped values, never `original_arguments`: only an
|
|
147
167
|
# argument this layer injected is ours to interpret. A `conversation_id` or
|
|
148
168
|
# `context` the tool declares itself stays application data and is left to it.
|
|
169
|
+
handle_free = safely(false) { client_row&.handle_free }
|
|
170
|
+
request = request_with_arguments(original_arguments)
|
|
149
171
|
conversation_id, minted = safely([nil, false]) do
|
|
150
|
-
ConversationId.resolve(@options.enable_conversation_id
|
|
151
|
-
missing_name
|
|
172
|
+
ConversationId.resolve(@options.enable_conversation_id && !@client_conversation,
|
|
173
|
+
stripped[ConversationId::PARAM_NAME], name, missing_name,
|
|
174
|
+
mint: !handle_free && !carries_session?)
|
|
152
175
|
end
|
|
153
|
-
request = request_with_arguments(original_arguments)
|
|
154
176
|
|
|
155
177
|
if virtual_tool?(name)
|
|
156
178
|
# Run the gem's own handler so validation, in-flight tracking and
|
|
@@ -172,7 +194,7 @@ module PostHog
|
|
|
172
194
|
return result
|
|
173
195
|
end
|
|
174
196
|
|
|
175
|
-
safely { prime_session(request, minted ? nil : conversation_id) }
|
|
197
|
+
safely { prime_session(request, minted ? nil : conversation_id, hold_custom_events: minted) }
|
|
176
198
|
begin
|
|
177
199
|
result = yield
|
|
178
200
|
rescue StandardError => e
|
|
@@ -191,7 +213,7 @@ module PostHog
|
|
|
191
213
|
error_source = result
|
|
192
214
|
unless no_response?(result) || conversation_id.nil?
|
|
193
215
|
safely do
|
|
194
|
-
if @data.tool_output_instructions[name]
|
|
216
|
+
if @data.tool_output_instructions[name] && !handle_free
|
|
195
217
|
result, delivered = ConversationId.mirror_instructions(result, conversation_id)
|
|
196
218
|
end
|
|
197
219
|
if minted
|
|
@@ -205,6 +227,7 @@ module PostHog
|
|
|
205
227
|
safely do
|
|
206
228
|
cid = minted && !delivered ? nil : conversation_id
|
|
207
229
|
session_id = prepare_request(request, conversation_id: cid)
|
|
230
|
+
release_custom_events(session_id)
|
|
208
231
|
record_tool_call(session_id, name, request, result: result, conversation_id: cid,
|
|
209
232
|
stripped: stripped, error_source: error_source)
|
|
210
233
|
end
|
|
@@ -228,7 +251,8 @@ module PostHog
|
|
|
228
251
|
if tools.is_a?(Array)
|
|
229
252
|
names = tools.map { |tool| fetch(tool, :name) }.compact
|
|
230
253
|
empty = tools.empty?
|
|
231
|
-
|
|
254
|
+
advertise_handle = !client_row&.handle_free
|
|
255
|
+
mutated = tools.map { |tool| mutate_tool(tool, advertise_handle) }
|
|
232
256
|
result = result.merge(SchemaMutation.key_for(result, :tools) => mutated)
|
|
233
257
|
end
|
|
234
258
|
end
|
|
@@ -405,6 +429,7 @@ module PostHog
|
|
|
405
429
|
event = {
|
|
406
430
|
'event_type' => event_type,
|
|
407
431
|
'session_id' => session_id,
|
|
432
|
+
'session_source' => @session_sources[session_id],
|
|
408
433
|
'duration' => duration_ms,
|
|
409
434
|
'client_name' => nil,
|
|
410
435
|
'client_version' => nil,
|
|
@@ -454,16 +479,43 @@ module PostHog
|
|
|
454
479
|
# recorded under. A minted handle is not known to have reached the agent
|
|
455
480
|
# until the call returns, so it stays out of here. {#prepare_request} runs
|
|
456
481
|
# again after the call; the second run is idempotent.
|
|
457
|
-
def prime_session(request, conversation_id)
|
|
482
|
+
def prime_session(request, conversation_id, hold_custom_events: false)
|
|
458
483
|
session_id = prepare_request(request, conversation_id: conversation_id)
|
|
459
|
-
@
|
|
484
|
+
@data.synchronize do
|
|
485
|
+
@scope[:owner] = @data
|
|
486
|
+
@scope[:session_id] = session_id
|
|
487
|
+
@scope[:session_source] = @session_sources[session_id]
|
|
488
|
+
@scope[:held] = [] if hold_custom_events
|
|
489
|
+
end
|
|
490
|
+
end
|
|
491
|
+
|
|
492
|
+
def release_custom_events(session_id)
|
|
493
|
+
return unless @scope.key?(:held)
|
|
494
|
+
|
|
495
|
+
held = @data.synchronize do
|
|
496
|
+
next nil unless @scope.key?(:held)
|
|
497
|
+
|
|
498
|
+
@scope[:session_id] = session_id
|
|
499
|
+
@scope[:session_source] = @session_sources[session_id]
|
|
500
|
+
@scope[:actor] = actor_for(session_id)
|
|
501
|
+
@scope.delete(:held)
|
|
502
|
+
end
|
|
503
|
+
actor = actor_for(session_id)
|
|
504
|
+
held&.each do |input|
|
|
505
|
+
self.class.capture_event(@data, input.merge('session_id' => session_id,
|
|
506
|
+
'session_source' => @session_sources[session_id]),
|
|
507
|
+
actor: actor)
|
|
508
|
+
end
|
|
460
509
|
end
|
|
461
510
|
|
|
462
511
|
# Resolve the session id and run identify.
|
|
463
512
|
def prepare_request(request, conversation_id: nil, token: nil)
|
|
464
513
|
token ||= @token
|
|
465
514
|
session_id, source = Session.resolve(@data, mcp_session_id(token), token: token,
|
|
466
|
-
conversation_id: conversation_id
|
|
515
|
+
conversation_id: conversation_id,
|
|
516
|
+
client_conversation: @client_conversation,
|
|
517
|
+
request_session_id: request_session_id)
|
|
518
|
+
@session_sources[session_id] = source
|
|
467
519
|
warn_stateless_session_not_wired if source == 'generated' && http?
|
|
468
520
|
|
|
469
521
|
# A tool call prepares twice: once before the body to pin the session, and
|
|
@@ -478,7 +530,10 @@ module PostHog
|
|
|
478
530
|
# overwritten while the handler ran.
|
|
479
531
|
@identified_in_request[session_id] = actor
|
|
480
532
|
@scope[:actor] = actor if @scope.is_a?(Hash)
|
|
481
|
-
|
|
533
|
+
if identify_event
|
|
534
|
+
self.class.capture_event(@data, identify_event.merge('session_source' => source),
|
|
535
|
+
actor: actor)
|
|
536
|
+
end
|
|
482
537
|
end
|
|
483
538
|
session_id
|
|
484
539
|
end
|
|
@@ -501,14 +556,28 @@ module PostHog
|
|
|
501
556
|
payload
|
|
502
557
|
end
|
|
503
558
|
|
|
559
|
+
def carries_session?
|
|
560
|
+
!@token.nil? || !mcp_session_id(@token).nil?
|
|
561
|
+
end
|
|
562
|
+
|
|
563
|
+
def request_session_id
|
|
564
|
+
@request_session_id ||= (Session.new_session_id if http?)
|
|
565
|
+
end
|
|
566
|
+
|
|
567
|
+
def client_row
|
|
568
|
+
return @client_row if defined?(@client_row)
|
|
569
|
+
|
|
570
|
+
@client_row = ClientConversation.recognize(@headers['user-agent'], client_identity(@request)[0])
|
|
571
|
+
end
|
|
572
|
+
|
|
504
573
|
def warn_stateless_session_not_wired
|
|
505
574
|
return if @data.warned_no_stateless_session
|
|
506
575
|
|
|
507
576
|
@data.warned_no_stateless_session = true
|
|
508
577
|
Log.warn(
|
|
509
578
|
@options,
|
|
510
|
-
'Warning: an MCP request arrived over streamable HTTP with no session id, so PostHog
|
|
511
|
-
'
|
|
579
|
+
'Warning: an MCP request arrived over streamable HTTP with no session id, so PostHog gave it a ' \
|
|
580
|
+
'$session_id of its own, shared with no other request. In stateless mode the ' \
|
|
512
581
|
'client must replay the Mcp-Session-Id header PostHog::MCP mints at initialize; for a custom Rack ' \
|
|
513
582
|
'stack add PostHog::MCP::RackMiddleware. Conversation ids, which are enabled by default, also anchor ' \
|
|
514
583
|
'the session without any middleware. See https://posthog.com/docs/mcp-analytics/installation#ruby.'
|
|
@@ -521,9 +590,12 @@ module PostHog
|
|
|
521
590
|
SessionToken.read_header(@headers)
|
|
522
591
|
end
|
|
523
592
|
|
|
524
|
-
# The transport's own session id (never our token).
|
|
593
|
+
# The transport's own session id (never our token). A 2026-07-28 request
|
|
594
|
+
# carries none: the session the gem gives it is assigned afresh to each request.
|
|
525
595
|
def mcp_session_id(token)
|
|
526
|
-
|
|
596
|
+
if @session.respond_to?(:session_id) && self.class.legacy_era?(protocol_version)
|
|
597
|
+
transport_id = @session.session_id
|
|
598
|
+
end
|
|
527
599
|
return transport_id if transport_id.is_a?(String) && !transport_id.empty?
|
|
528
600
|
|
|
529
601
|
token ? nil : header_session_id
|
|
@@ -539,7 +611,8 @@ module PostHog
|
|
|
539
611
|
end
|
|
540
612
|
|
|
541
613
|
def client_identity(request)
|
|
542
|
-
|
|
614
|
+
# One instance serves every stateless HTTP client, so its handshake may be another client's.
|
|
615
|
+
info = envelope_client_info || session_client || (server_client unless http?)
|
|
543
616
|
info = fetch(request_params(request), :clientInfo) if info.nil? && @kind == :initialize
|
|
544
617
|
name = info.is_a?(Hash) ? fetch(info, :name) : nil
|
|
545
618
|
version = info.is_a?(Hash) ? fetch(info, :version) : nil
|
|
@@ -596,6 +669,18 @@ module PostHog
|
|
|
596
669
|
params.is_a?(Hash) ? params : {}
|
|
597
670
|
end
|
|
598
671
|
|
|
672
|
+
# A recognized client's conversation id only groups the call; its value is never sent.
|
|
673
|
+
# Every matching row's key is dropped, not just the selected row's, once for every method,
|
|
674
|
+
# before any callback or event reads the request.
|
|
675
|
+
def drop_client_conversation_key
|
|
676
|
+
meta = envelope_meta
|
|
677
|
+
keys = ClientConversation.redacted_keys(@headers['user-agent'], client_identity(@request)[0])
|
|
678
|
+
return if keys.empty? || !meta
|
|
679
|
+
|
|
680
|
+
@params = @params.merge(SchemaMutation.key_for(@params, :_meta) => meta.except(*keys, *keys.map(&:to_sym)))
|
|
681
|
+
@request = @request.merge(SchemaMutation.key_for(@request, :params) => @params)
|
|
682
|
+
end
|
|
683
|
+
|
|
599
684
|
def request_with_arguments(arguments)
|
|
600
685
|
params = @params.merge(SchemaMutation.key_for(@params, :arguments) => arguments)
|
|
601
686
|
@request.merge(SchemaMutation.key_for(@request, :params) => params)
|
|
@@ -673,7 +758,7 @@ module PostHog
|
|
|
673
758
|
stripped
|
|
674
759
|
end
|
|
675
760
|
|
|
676
|
-
def mutate_tool(tool)
|
|
761
|
+
def mutate_tool(tool, advertise_handle)
|
|
677
762
|
return tool unless tool.is_a?(Hash)
|
|
678
763
|
|
|
679
764
|
name = fetch(tool, :name)
|
|
@@ -691,7 +776,7 @@ module PostHog
|
|
|
691
776
|
if @options.enable_conversation_id
|
|
692
777
|
updated = SchemaMutation.add_conversation_id_parameter(schema, tool_name: name, options: @options)
|
|
693
778
|
owned << ConversationId::PARAM_NAME unless updated.equal?(schema)
|
|
694
|
-
schema = updated
|
|
779
|
+
schema = updated if advertise_handle
|
|
695
780
|
end
|
|
696
781
|
if @options.capture_model_enabled?
|
|
697
782
|
updated = SchemaMutation.add_model_parameter(
|
|
@@ -703,7 +788,7 @@ module PostHog
|
|
|
703
788
|
|
|
704
789
|
mutated = tool.merge(SchemaMutation.key_for(tool, :inputSchema) => schema)
|
|
705
790
|
declared = nil
|
|
706
|
-
if @options.enable_conversation_id
|
|
791
|
+
if @options.enable_conversation_id && advertise_handle
|
|
707
792
|
output_schema = fetch(tool, :outputSchema)
|
|
708
793
|
new_output, declared = SchemaMutation.add_output_instructions(output_schema, tool_name: name,
|
|
709
794
|
options: @options)
|
|
@@ -18,7 +18,8 @@ module PostHog
|
|
|
18
18
|
module_function
|
|
19
19
|
|
|
20
20
|
# @return [Hash, nil] `{headers:, transport:, mint:, session_id:}` for the in-flight HTTP request,
|
|
21
|
-
# plus `
|
|
21
|
+
# plus `owner:` (the server's tracking data that pinned the session, the only
|
|
22
|
+
# handle {Analytics} lets read it) and `actor:` once {Instrumentation} has resolved identity for it.
|
|
22
23
|
# `session_id` is the `$session_id` {Instrumentation} settled on before running the
|
|
23
24
|
# tool body, and `actor` the identity it resolved, so a custom event captured inside
|
|
24
25
|
# the tool is attributed to this request - to the right session and the right person -
|
data/lib/posthog/mcp/session.rb
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
module PostHog
|
|
4
4
|
module MCP
|
|
5
|
-
# Session id resolution, in priority order: the
|
|
6
|
-
# agent's echoed `conversation_id` handle
|
|
7
|
-
#
|
|
8
|
-
#
|
|
5
|
+
# Session id resolution, in priority order: the client's own conversation id,
|
|
6
|
+
# the agent's echoed `conversation_id` handle, our self-encoded session token,
|
|
7
|
+
# the transport's MCP session id, a session of its own for a request that
|
|
8
|
+
# carries none over a transport shared by every client (stateless HTTP), and
|
|
9
|
+
# last this server's own memory (which rolls over after
|
|
10
|
+
# {INACTIVITY_TIMEOUT_MINUTES} of inactivity).
|
|
9
11
|
#
|
|
10
12
|
# @api private
|
|
11
13
|
module Session
|
|
@@ -25,17 +27,18 @@ module PostHog
|
|
|
25
27
|
end
|
|
26
28
|
|
|
27
29
|
# @return [Array(String, String)] `[session_id, source]` where source is one of
|
|
28
|
-
# `conversation`, `token`, `mcp`, `generated`
|
|
29
|
-
def resolve(data, mcp_session_id, token: nil, conversation_id: nil
|
|
30
|
+
# `client_metadata`, `conversation`, `token`, `mcp`, `generated`
|
|
31
|
+
def resolve(data, mcp_session_id, token: nil, conversation_id: nil, client_conversation: nil,
|
|
32
|
+
request_session_id: nil)
|
|
33
|
+
if present?(client_conversation)
|
|
34
|
+
return [derive_session_id_from_conversation(client_conversation), 'client_metadata']
|
|
35
|
+
end
|
|
30
36
|
return [derive_session_id_from_conversation(conversation_id), 'conversation'] if present?(conversation_id)
|
|
31
37
|
|
|
32
38
|
data.synchronize do
|
|
33
|
-
now = Time.now
|
|
34
|
-
|
|
35
39
|
if token
|
|
36
40
|
data.session_id = token.session_id
|
|
37
41
|
data.session_source = 'token'
|
|
38
|
-
data.last_activity = now
|
|
39
42
|
return [data.session_id, 'token']
|
|
40
43
|
end
|
|
41
44
|
|
|
@@ -43,25 +46,41 @@ module PostHog
|
|
|
43
46
|
data.session_id = derive_session_id_from_mcp_session(mcp_session_id)
|
|
44
47
|
data.last_mcp_session_id = mcp_session_id
|
|
45
48
|
data.session_source = 'mcp'
|
|
46
|
-
data.last_activity = now
|
|
47
49
|
return [data.session_id, 'mcp']
|
|
48
50
|
end
|
|
49
51
|
|
|
50
|
-
|
|
51
|
-
data.last_activity = now
|
|
52
|
-
return [data.session_id, 'mcp']
|
|
53
|
-
end
|
|
52
|
+
return [request_session_id, 'generated'] if present?(request_session_id)
|
|
54
53
|
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
return [data.session_id, 'mcp'] if data.session_source == 'mcp' && data.last_mcp_session_id
|
|
55
|
+
|
|
56
|
+
if data.session_source != 'generated'
|
|
57
57
|
data.session_id = new_session_id
|
|
58
58
|
data.session_source = 'generated'
|
|
59
59
|
end
|
|
60
|
-
data.last_activity = now
|
|
61
60
|
[data.session_id, 'generated']
|
|
62
61
|
end
|
|
63
62
|
end
|
|
64
63
|
|
|
64
|
+
# A desktop app can keep one stdio process alive across many chats.
|
|
65
|
+
def start_tool_call(data)
|
|
66
|
+
data.synchronize do
|
|
67
|
+
data.session_id = new_session_id if data.session_source == 'generated' && idle?(data)
|
|
68
|
+
data.tool_calls_in_flight += 1
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def end_tool_call(data)
|
|
73
|
+
data.synchronize do
|
|
74
|
+
data.tool_calls_in_flight -= 1
|
|
75
|
+
data.last_tool_call_end = Time.now
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def idle?(data)
|
|
80
|
+
data.tool_calls_in_flight.zero? && !data.last_tool_call_end.nil? &&
|
|
81
|
+
Time.now - data.last_tool_call_end >= INACTIVITY_TIMEOUT_MINUTES * 60
|
|
82
|
+
end
|
|
83
|
+
|
|
65
84
|
def present?(value)
|
|
66
85
|
value.is_a?(String) && !value.empty?
|
|
67
86
|
end
|
|
@@ -9,8 +9,8 @@ module PostHog
|
|
|
9
9
|
class TrackingData
|
|
10
10
|
attr_reader :options, :sink, :server_name, :server_version, :identified_sessions,
|
|
11
11
|
:tool_descriptions, :tool_categories, :tool_output_instructions, :tool_owned_params
|
|
12
|
-
attr_accessor :session_id, :session_source, :last_mcp_session_id, :
|
|
13
|
-
:virtual_tool, :http_transport_seen, :warned_unscoped_capture
|
|
12
|
+
attr_accessor :session_id, :session_source, :last_mcp_session_id, :last_tool_call_end, :tool_calls_in_flight,
|
|
13
|
+
:warned_no_stateless_session, :virtual_tool, :http_transport_seen, :warned_unscoped_capture
|
|
14
14
|
|
|
15
15
|
def initialize(options:, sink:, server_name: nil, server_version: nil)
|
|
16
16
|
@options = options
|
|
@@ -18,10 +18,11 @@ module PostHog
|
|
|
18
18
|
@server_name = server_name
|
|
19
19
|
@server_version = server_version
|
|
20
20
|
@mutex = Mutex.new
|
|
21
|
-
@session_id =
|
|
21
|
+
@session_id = Session.new_session_id
|
|
22
22
|
@session_source = 'generated'
|
|
23
23
|
@last_mcp_session_id = nil
|
|
24
|
-
@
|
|
24
|
+
@last_tool_call_end = nil
|
|
25
|
+
@tool_calls_in_flight = 0
|
|
25
26
|
@warned_no_stateless_session = false
|
|
26
27
|
# Set once a request arrives over HTTP: from then on the server-wide session
|
|
27
28
|
# is never a safe fallback for a capture that lost its request scope.
|
data/lib/posthog/mcp.rb
CHANGED
|
@@ -14,6 +14,7 @@ require 'posthog/mcp/exceptions'
|
|
|
14
14
|
require 'posthog/mcp/sanitization'
|
|
15
15
|
require 'posthog/mcp/truncation'
|
|
16
16
|
require 'posthog/mcp/conversation_id'
|
|
17
|
+
require 'posthog/mcp/client_conversation'
|
|
17
18
|
require 'posthog/mcp/intent'
|
|
18
19
|
require 'posthog/mcp/schema_mutation'
|
|
19
20
|
require 'posthog/mcp/event_builder'
|
data/lib/posthog/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: posthog-ruby
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.36.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- PostHog
|
|
@@ -57,6 +57,7 @@ files:
|
|
|
57
57
|
- lib/posthog/mcp/README.md
|
|
58
58
|
- lib/posthog/mcp/analytics.rb
|
|
59
59
|
- lib/posthog/mcp/client.rb
|
|
60
|
+
- lib/posthog/mcp/client_conversation.rb
|
|
60
61
|
- lib/posthog/mcp/constants.rb
|
|
61
62
|
- lib/posthog/mcp/conversation_id.rb
|
|
62
63
|
- lib/posthog/mcp/event_builder.rb
|