@posthog/mcp 0.0.8 → 0.1.16
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.
- package/LICENSE +307 -2
- package/README.md +186 -93
- package/dist/extensions/capture.d.ts +13 -0
- package/dist/extensions/capture.d.ts.map +1 -0
- package/dist/extensions/capture.js +80 -0
- package/dist/extensions/capture.mjs +46 -0
- package/dist/extensions/compatibility.d.ts +23 -0
- package/dist/extensions/compatibility.d.ts.map +1 -0
- package/dist/extensions/compatibility.js +113 -0
- package/dist/extensions/compatibility.mjs +67 -0
- package/dist/extensions/constants.d.ts +39 -0
- package/dist/extensions/constants.d.ts.map +1 -0
- package/dist/extensions/constants.js +86 -0
- package/dist/extensions/constants.mjs +37 -0
- package/dist/extensions/context-parameters.d.ts +31 -0
- package/dist/extensions/context-parameters.d.ts.map +1 -0
- package/dist/extensions/context-parameters.js +93 -0
- package/dist/extensions/context-parameters.mjs +50 -0
- package/dist/extensions/conversation-id.d.ts +52 -0
- package/dist/extensions/conversation-id.d.ts.map +1 -0
- package/dist/extensions/conversation-id.js +164 -0
- package/dist/extensions/conversation-id.mjs +103 -0
- package/dist/extensions/event-types.d.ts +21 -0
- package/dist/extensions/event-types.d.ts.map +1 -0
- package/dist/extensions/event-types.js +47 -0
- package/dist/extensions/event-types.mjs +13 -0
- package/dist/extensions/exceptions.d.ts +13 -0
- package/dist/extensions/exceptions.d.ts.map +1 -0
- package/dist/extensions/exceptions.js +58 -0
- package/dist/extensions/exceptions.mjs +24 -0
- package/dist/extensions/ids.d.ts +13 -0
- package/dist/extensions/ids.d.ts.map +1 -0
- package/dist/extensions/ids.js +55 -0
- package/dist/extensions/ids.mjs +18 -0
- package/dist/extensions/instrument-highlevel.d.ts +3 -0
- package/dist/extensions/instrument-highlevel.d.ts.map +1 -0
- package/dist/extensions/instrument-highlevel.js +229 -0
- package/dist/extensions/instrument-highlevel.mjs +195 -0
- package/dist/extensions/instrument-lowlevel.d.ts +8 -0
- package/dist/extensions/instrument-lowlevel.d.ts.map +1 -0
- package/dist/extensions/instrument-lowlevel.js +85 -0
- package/dist/extensions/instrument-lowlevel.mjs +51 -0
- package/dist/extensions/instrumentation.d.ts +59 -0
- package/dist/extensions/instrumentation.d.ts.map +1 -0
- package/dist/extensions/instrumentation.js +237 -0
- package/dist/extensions/instrumentation.mjs +194 -0
- package/dist/extensions/intent.d.ts +10 -0
- package/dist/extensions/intent.d.ts.map +1 -0
- package/dist/extensions/intent.js +84 -0
- package/dist/extensions/intent.mjs +44 -0
- package/dist/extensions/internal.d.ts +32 -0
- package/dist/extensions/internal.d.ts.map +1 -0
- package/dist/extensions/internal.js +158 -0
- package/dist/extensions/internal.mjs +106 -0
- package/dist/extensions/logger.d.ts +14 -0
- package/dist/extensions/logger.d.ts.map +1 -0
- package/dist/extensions/logger.js +47 -0
- package/dist/extensions/logger.mjs +10 -0
- package/dist/extensions/mcp-payloads.d.ts +5 -0
- package/dist/extensions/mcp-payloads.d.ts.map +1 -0
- package/dist/extensions/mcp-payloads.js +91 -0
- package/dist/extensions/mcp-payloads.mjs +54 -0
- package/dist/extensions/mcp-sdk-compat.d.ts +33 -0
- package/dist/extensions/mcp-sdk-compat.d.ts.map +1 -0
- package/dist/extensions/mcp-sdk-compat.js +112 -0
- package/dist/extensions/mcp-sdk-compat.mjs +60 -0
- package/dist/extensions/posthog-events.d.ts +14 -0
- package/dist/extensions/posthog-events.d.ts.map +1 -0
- package/dist/extensions/posthog-events.js +143 -0
- package/dist/extensions/posthog-events.mjs +109 -0
- package/dist/extensions/sanitization.d.ts +10 -0
- package/dist/extensions/sanitization.d.ts.map +1 -0
- package/dist/extensions/sanitization.js +96 -0
- package/dist/extensions/sanitization.mjs +62 -0
- package/dist/extensions/session.d.ts +20 -0
- package/dist/extensions/session.d.ts.map +1 -0
- package/dist/extensions/session.js +113 -0
- package/dist/extensions/session.mjs +67 -0
- package/dist/extensions/sink.d.ts +44 -0
- package/dist/extensions/sink.d.ts.map +1 -0
- package/dist/extensions/sink.js +102 -0
- package/dist/extensions/sink.mjs +65 -0
- package/dist/extensions/tools.d.ts +9 -0
- package/dist/extensions/tools.d.ts.map +1 -0
- package/dist/extensions/tools.js +79 -0
- package/dist/extensions/tools.mjs +39 -0
- package/dist/extensions/tracing-helpers.d.ts +29 -0
- package/dist/extensions/tracing-helpers.d.ts.map +1 -0
- package/dist/extensions/tracing-helpers.js +59 -0
- package/dist/extensions/tracing-helpers.mjs +16 -0
- package/dist/extensions/truncation.d.ts +25 -0
- package/dist/extensions/truncation.d.ts.map +1 -0
- package/dist/extensions/truncation.js +277 -0
- package/dist/extensions/truncation.mjs +228 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +132 -0
- package/dist/index.mjs +83 -2928
- package/dist/types.d.ts +220 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +18 -0
- package/dist/types.mjs +0 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +36 -0
- package/dist/version.mjs +2 -0
- package/package.json +55 -87
- package/src/extensions/capture.ts +68 -0
- package/src/extensions/compatibility.ts +138 -0
- package/src/extensions/constants.ts +48 -0
- package/src/extensions/context-parameters.ts +119 -0
- package/src/extensions/conversation-id.ts +177 -0
- package/src/extensions/event-types.ts +21 -0
- package/src/extensions/exceptions.ts +83 -0
- package/src/extensions/ids.ts +32 -0
- package/src/extensions/instrument-highlevel.ts +297 -0
- package/src/extensions/instrument-lowlevel.ts +83 -0
- package/src/extensions/instrumentation.ts +415 -0
- package/src/extensions/intent.ts +78 -0
- package/src/extensions/internal.ts +193 -0
- package/src/extensions/logger.ts +27 -0
- package/src/extensions/mcp-payloads.ts +98 -0
- package/src/extensions/mcp-sdk-compat.ts +160 -0
- package/src/extensions/posthog-events.ts +212 -0
- package/src/extensions/sanitization.ts +121 -0
- package/src/extensions/session.ts +117 -0
- package/src/extensions/sink.ts +130 -0
- package/src/extensions/tools.ts +50 -0
- package/src/extensions/tracing-helpers.ts +51 -0
- package/src/extensions/truncation.ts +453 -0
- package/src/index.ts +174 -0
- package/src/types.ts +244 -0
- package/src/version.ts +1 -0
- package/dist/index.cjs +0 -2936
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -264
- package/dist/index.d.mts +0 -263
- package/dist/index.mjs.map +0 -1
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
MAX_BREADTH: ()=>MAX_BREADTH,
|
|
28
|
+
MAX_DEPTH: ()=>MAX_DEPTH,
|
|
29
|
+
MAX_EVENT_BYTES: ()=>MAX_EVENT_BYTES,
|
|
30
|
+
MAX_STRING_LENGTH: ()=>MAX_STRING_LENGTH,
|
|
31
|
+
normalize: ()=>normalize,
|
|
32
|
+
truncateEvent: ()=>truncateEvent
|
|
33
|
+
});
|
|
34
|
+
const MAX_DEPTH = 10;
|
|
35
|
+
const MAX_BREADTH = 100;
|
|
36
|
+
const MAX_STRING_LENGTH = 32768;
|
|
37
|
+
const MAX_EVENT_BYTES = 102400;
|
|
38
|
+
const MAX_USER_INTENT_LENGTH = 2048;
|
|
39
|
+
const MAX_ERROR_MESSAGE_LENGTH = 2048;
|
|
40
|
+
const MAX_RESOURCE_NAME_LENGTH = 256;
|
|
41
|
+
const MAX_METADATA_LENGTH = 256;
|
|
42
|
+
const MAX_STACK_FRAMES = 50;
|
|
43
|
+
const MAX_CONTENT_TEXT_LENGTH = 32768;
|
|
44
|
+
const TRUNCATION_SUFFIX = '...';
|
|
45
|
+
function normalize(input, depth = MAX_DEPTH, maxBreadth = MAX_BREADTH, maxStringLength = MAX_STRING_LENGTH) {
|
|
46
|
+
const memo = new WeakSet();
|
|
47
|
+
return visit(input, depth, maxBreadth, maxStringLength, memo);
|
|
48
|
+
}
|
|
49
|
+
function visit(value, remainingDepth, maxBreadth, maxStringLength, memo) {
|
|
50
|
+
if (null === value) return null;
|
|
51
|
+
if (void 0 === value) return '[undefined]';
|
|
52
|
+
if ('boolean' == typeof value) return value;
|
|
53
|
+
if ('number' == typeof value) {
|
|
54
|
+
if (Number.isNaN(value)) return '[NaN]';
|
|
55
|
+
if (!Number.isFinite(value)) return value > 0 ? '[Infinity]' : '[-Infinity]';
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
if ('bigint' == typeof value) return `[BigInt: ${value}]`;
|
|
59
|
+
if ('string' == typeof value) {
|
|
60
|
+
if (value.length > maxStringLength) return value.slice(0, maxStringLength) + TRUNCATION_SUFFIX;
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
if ('symbol' == typeof value) {
|
|
64
|
+
const desc = value.description;
|
|
65
|
+
return desc ? `[Symbol(${desc})]` : '[Symbol()]';
|
|
66
|
+
}
|
|
67
|
+
if ('function' == typeof value) {
|
|
68
|
+
const name = value.name || '<anonymous>';
|
|
69
|
+
return `[Function: ${name}]`;
|
|
70
|
+
}
|
|
71
|
+
if (value instanceof Date) return Number.isNaN(value.getTime()) ? '[Invalid Date]' : value.toISOString();
|
|
72
|
+
if ('object' == typeof value) {
|
|
73
|
+
if (memo.has(value)) return '[Circular ~]';
|
|
74
|
+
if (remainingDepth <= 0) return Array.isArray(value) ? '[Array]' : '[Object]';
|
|
75
|
+
memo.add(value);
|
|
76
|
+
let result;
|
|
77
|
+
result = Array.isArray(value) ? visitArray(value, remainingDepth - 1, maxBreadth, maxStringLength, memo) : visitObject(value, remainingDepth - 1, maxBreadth, maxStringLength, memo);
|
|
78
|
+
memo.delete(value);
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
return String(value);
|
|
82
|
+
}
|
|
83
|
+
function visitArray(arr, remainingDepth, maxBreadth, maxStringLength, memo) {
|
|
84
|
+
const result = [];
|
|
85
|
+
for(let i = 0; i < arr.length; i++){
|
|
86
|
+
if (i >= maxBreadth) {
|
|
87
|
+
result.push('[MaxProperties ~]');
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
result.push(visit(arr[i], remainingDepth, maxBreadth, maxStringLength, memo));
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
function visitObject(obj, remainingDepth, maxBreadth, maxStringLength, memo) {
|
|
95
|
+
const result = {};
|
|
96
|
+
const keys = Object.keys(obj);
|
|
97
|
+
let count = 0;
|
|
98
|
+
for (const key of keys){
|
|
99
|
+
if (count >= maxBreadth) {
|
|
100
|
+
result['...'] = '[MaxProperties ~]';
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
if (void 0 !== obj[key]) {
|
|
104
|
+
result[key] = visit(obj[key], remainingDepth, maxBreadth, maxStringLength, memo);
|
|
105
|
+
count++;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
function truncateString(str, maxLength) {
|
|
111
|
+
if (null == str) return str;
|
|
112
|
+
if (str.length <= maxLength) return str;
|
|
113
|
+
return str.slice(0, maxLength) + TRUNCATION_SUFFIX;
|
|
114
|
+
}
|
|
115
|
+
function truncateStackFrames(frames) {
|
|
116
|
+
if (!frames || frames.length <= MAX_STACK_FRAMES) return frames;
|
|
117
|
+
const half = Math.floor(MAX_STACK_FRAMES / 2);
|
|
118
|
+
return [
|
|
119
|
+
...frames.slice(0, half),
|
|
120
|
+
...frames.slice(-half)
|
|
121
|
+
];
|
|
122
|
+
}
|
|
123
|
+
function truncateExceptionList(error) {
|
|
124
|
+
if (!Array.isArray(error.$exception_list)) return error;
|
|
125
|
+
return {
|
|
126
|
+
...error,
|
|
127
|
+
$exception_list: error.$exception_list.map((exception)=>{
|
|
128
|
+
const next = {
|
|
129
|
+
...exception
|
|
130
|
+
};
|
|
131
|
+
if ('string' == typeof next.value) next.value = truncateString(next.value, MAX_ERROR_MESSAGE_LENGTH);
|
|
132
|
+
if (next.stacktrace?.frames) next.stacktrace = {
|
|
133
|
+
...next.stacktrace,
|
|
134
|
+
frames: truncateStackFrames(next.stacktrace.frames)
|
|
135
|
+
};
|
|
136
|
+
return next;
|
|
137
|
+
})
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function truncateResponseContent(response) {
|
|
141
|
+
if (null == response || 'object' != typeof response) return response;
|
|
142
|
+
const result = {
|
|
143
|
+
...response
|
|
144
|
+
};
|
|
145
|
+
if (Array.isArray(result.content)) result.content = result.content.map((block)=>{
|
|
146
|
+
if (null != block && 'object' == typeof block && 'type' in block && 'text' in block && block?.type === 'text' && 'string' == typeof block.text && block.text.length > MAX_CONTENT_TEXT_LENGTH) return {
|
|
147
|
+
...block,
|
|
148
|
+
text: block.text.slice(0, MAX_CONTENT_TEXT_LENGTH) + TRUNCATION_SUFFIX
|
|
149
|
+
};
|
|
150
|
+
return block;
|
|
151
|
+
});
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
const textEncoder = new TextEncoder();
|
|
155
|
+
function jsonByteSize(value) {
|
|
156
|
+
return textEncoder.encode(JSON.stringify(value)).length;
|
|
157
|
+
}
|
|
158
|
+
function truncateLargestFields(obj, maxBytes) {
|
|
159
|
+
const result = structuredClone(obj);
|
|
160
|
+
for(let attempt = 0; attempt < 10; attempt++){
|
|
161
|
+
const currentSize = jsonByteSize(result);
|
|
162
|
+
if (currentSize <= maxBytes) break;
|
|
163
|
+
const excess = currentSize - maxBytes;
|
|
164
|
+
const stringPaths = [];
|
|
165
|
+
collectStringPaths(result, [], stringPaths);
|
|
166
|
+
stringPaths.sort((a, b)=>b.length - a.length);
|
|
167
|
+
if (0 === stringPaths.length) break;
|
|
168
|
+
let remaining = excess + 200;
|
|
169
|
+
let truncated = false;
|
|
170
|
+
for (const { path, length } of stringPaths){
|
|
171
|
+
if (remaining <= 0) break;
|
|
172
|
+
const reduction = Math.min(remaining, Math.floor(0.5 * length));
|
|
173
|
+
if (reduction < 10) continue;
|
|
174
|
+
const newLength = length - reduction;
|
|
175
|
+
const currentValue = getNestedValue(result, path);
|
|
176
|
+
if ('string' == typeof currentValue) {
|
|
177
|
+
setNestedValue(result, path, currentValue.slice(0, newLength) + TRUNCATION_SUFFIX);
|
|
178
|
+
remaining -= reduction;
|
|
179
|
+
truncated = true;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (!truncated) break;
|
|
183
|
+
}
|
|
184
|
+
return result;
|
|
185
|
+
}
|
|
186
|
+
function collectStringPaths(obj, currentPath, results) {
|
|
187
|
+
if ('string' == typeof obj && obj.length > 100) return void results.push({
|
|
188
|
+
path: [
|
|
189
|
+
...currentPath
|
|
190
|
+
],
|
|
191
|
+
length: obj.length
|
|
192
|
+
});
|
|
193
|
+
if (Array.isArray(obj)) {
|
|
194
|
+
for (const [i, item] of obj.entries())collectStringPaths(item, [
|
|
195
|
+
...currentPath,
|
|
196
|
+
String(i)
|
|
197
|
+
], results);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (null != obj && 'object' == typeof obj) for (const [key, value] of Object.entries(obj))collectStringPaths(value, [
|
|
201
|
+
...currentPath,
|
|
202
|
+
key
|
|
203
|
+
], results);
|
|
204
|
+
}
|
|
205
|
+
function getNestedValue(obj, path) {
|
|
206
|
+
let current = obj;
|
|
207
|
+
for (const key of path){
|
|
208
|
+
if (null == current || 'object' != typeof current) return;
|
|
209
|
+
current = current[key];
|
|
210
|
+
}
|
|
211
|
+
return current;
|
|
212
|
+
}
|
|
213
|
+
function setNestedValue(obj, path, value) {
|
|
214
|
+
let current = obj;
|
|
215
|
+
for(let i = 0; i < path.length - 1; i++){
|
|
216
|
+
if (null == current || 'object' != typeof current) return;
|
|
217
|
+
current = current[path[i]];
|
|
218
|
+
}
|
|
219
|
+
const finalKey = path.at(-1);
|
|
220
|
+
if (void 0 !== finalKey && null != current && 'object' == typeof current) current[finalKey] = value;
|
|
221
|
+
}
|
|
222
|
+
function truncateToSize(event) {
|
|
223
|
+
if (jsonByteSize(event) <= MAX_EVENT_BYTES) return event;
|
|
224
|
+
for(let depth = MAX_DEPTH - 1; depth >= 1; depth--){
|
|
225
|
+
const reduced = {
|
|
226
|
+
...event
|
|
227
|
+
};
|
|
228
|
+
if (null != reduced.parameters) reduced.parameters = normalize(reduced.parameters, depth);
|
|
229
|
+
if (null != reduced.response) reduced.response = normalize(reduced.response, depth);
|
|
230
|
+
if (null != reduced.identifyActorData) reduced.identifyActorData = normalize(reduced.identifyActorData, depth);
|
|
231
|
+
if (null != reduced.error) reduced.error = normalize(reduced.error, depth);
|
|
232
|
+
if (jsonByteSize(reduced) <= MAX_EVENT_BYTES) return reduced;
|
|
233
|
+
}
|
|
234
|
+
const minimal = {
|
|
235
|
+
...event
|
|
236
|
+
};
|
|
237
|
+
if (null != minimal.parameters) minimal.parameters = normalize(minimal.parameters, 1);
|
|
238
|
+
if (null != minimal.response) minimal.response = normalize(minimal.response, 1);
|
|
239
|
+
if (null != minimal.identifyActorData) minimal.identifyActorData = normalize(minimal.identifyActorData, 1);
|
|
240
|
+
if (null != minimal.error) minimal.error = normalize(minimal.error, 1);
|
|
241
|
+
return truncateLargestFields(minimal, MAX_EVENT_BYTES);
|
|
242
|
+
}
|
|
243
|
+
function truncateEvent(event) {
|
|
244
|
+
const result = {
|
|
245
|
+
...event
|
|
246
|
+
};
|
|
247
|
+
result.userIntent = truncateString(result.userIntent, MAX_USER_INTENT_LENGTH);
|
|
248
|
+
result.resourceName = truncateString(result.resourceName, MAX_RESOURCE_NAME_LENGTH);
|
|
249
|
+
result.serverName = truncateString(result.serverName, MAX_METADATA_LENGTH);
|
|
250
|
+
result.serverVersion = truncateString(result.serverVersion, MAX_METADATA_LENGTH);
|
|
251
|
+
result.clientName = truncateString(result.clientName, MAX_METADATA_LENGTH);
|
|
252
|
+
result.clientVersion = truncateString(result.clientVersion, MAX_METADATA_LENGTH);
|
|
253
|
+
if (null != result.error && 'object' == typeof result.error) result.error = truncateExceptionList(result.error);
|
|
254
|
+
result.response = truncateResponseContent(result.response);
|
|
255
|
+
if (null != result.parameters) result.parameters = normalize(result.parameters);
|
|
256
|
+
if (null != result.response) result.response = normalize(result.response);
|
|
257
|
+
if (null != result.identifyActorData) result.identifyActorData = normalize(result.identifyActorData);
|
|
258
|
+
if (null != result.error) result.error = normalize(result.error);
|
|
259
|
+
return truncateToSize(result);
|
|
260
|
+
}
|
|
261
|
+
exports.MAX_BREADTH = __webpack_exports__.MAX_BREADTH;
|
|
262
|
+
exports.MAX_DEPTH = __webpack_exports__.MAX_DEPTH;
|
|
263
|
+
exports.MAX_EVENT_BYTES = __webpack_exports__.MAX_EVENT_BYTES;
|
|
264
|
+
exports.MAX_STRING_LENGTH = __webpack_exports__.MAX_STRING_LENGTH;
|
|
265
|
+
exports.normalize = __webpack_exports__.normalize;
|
|
266
|
+
exports.truncateEvent = __webpack_exports__.truncateEvent;
|
|
267
|
+
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
268
|
+
"MAX_BREADTH",
|
|
269
|
+
"MAX_DEPTH",
|
|
270
|
+
"MAX_EVENT_BYTES",
|
|
271
|
+
"MAX_STRING_LENGTH",
|
|
272
|
+
"normalize",
|
|
273
|
+
"truncateEvent"
|
|
274
|
+
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
275
|
+
Object.defineProperty(exports, '__esModule', {
|
|
276
|
+
value: true
|
|
277
|
+
});
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
const MAX_DEPTH = 10;
|
|
2
|
+
const MAX_BREADTH = 100;
|
|
3
|
+
const MAX_STRING_LENGTH = 32768;
|
|
4
|
+
const MAX_EVENT_BYTES = 102400;
|
|
5
|
+
const MAX_USER_INTENT_LENGTH = 2048;
|
|
6
|
+
const MAX_ERROR_MESSAGE_LENGTH = 2048;
|
|
7
|
+
const MAX_RESOURCE_NAME_LENGTH = 256;
|
|
8
|
+
const MAX_METADATA_LENGTH = 256;
|
|
9
|
+
const MAX_STACK_FRAMES = 50;
|
|
10
|
+
const MAX_CONTENT_TEXT_LENGTH = 32768;
|
|
11
|
+
const TRUNCATION_SUFFIX = '...';
|
|
12
|
+
function normalize(input, depth = MAX_DEPTH, maxBreadth = MAX_BREADTH, maxStringLength = MAX_STRING_LENGTH) {
|
|
13
|
+
const memo = new WeakSet();
|
|
14
|
+
return visit(input, depth, maxBreadth, maxStringLength, memo);
|
|
15
|
+
}
|
|
16
|
+
function visit(value, remainingDepth, maxBreadth, maxStringLength, memo) {
|
|
17
|
+
if (null === value) return null;
|
|
18
|
+
if (void 0 === value) return '[undefined]';
|
|
19
|
+
if ('boolean' == typeof value) return value;
|
|
20
|
+
if ('number' == typeof value) {
|
|
21
|
+
if (Number.isNaN(value)) return '[NaN]';
|
|
22
|
+
if (!Number.isFinite(value)) return value > 0 ? '[Infinity]' : '[-Infinity]';
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
if ('bigint' == typeof value) return `[BigInt: ${value}]`;
|
|
26
|
+
if ('string' == typeof value) {
|
|
27
|
+
if (value.length > maxStringLength) return value.slice(0, maxStringLength) + TRUNCATION_SUFFIX;
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
if ('symbol' == typeof value) {
|
|
31
|
+
const desc = value.description;
|
|
32
|
+
return desc ? `[Symbol(${desc})]` : '[Symbol()]';
|
|
33
|
+
}
|
|
34
|
+
if ('function' == typeof value) {
|
|
35
|
+
const name = value.name || '<anonymous>';
|
|
36
|
+
return `[Function: ${name}]`;
|
|
37
|
+
}
|
|
38
|
+
if (value instanceof Date) return Number.isNaN(value.getTime()) ? '[Invalid Date]' : value.toISOString();
|
|
39
|
+
if ('object' == typeof value) {
|
|
40
|
+
if (memo.has(value)) return '[Circular ~]';
|
|
41
|
+
if (remainingDepth <= 0) return Array.isArray(value) ? '[Array]' : '[Object]';
|
|
42
|
+
memo.add(value);
|
|
43
|
+
let result;
|
|
44
|
+
result = Array.isArray(value) ? visitArray(value, remainingDepth - 1, maxBreadth, maxStringLength, memo) : visitObject(value, remainingDepth - 1, maxBreadth, maxStringLength, memo);
|
|
45
|
+
memo.delete(value);
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
return String(value);
|
|
49
|
+
}
|
|
50
|
+
function visitArray(arr, remainingDepth, maxBreadth, maxStringLength, memo) {
|
|
51
|
+
const result = [];
|
|
52
|
+
for(let i = 0; i < arr.length; i++){
|
|
53
|
+
if (i >= maxBreadth) {
|
|
54
|
+
result.push('[MaxProperties ~]');
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
result.push(visit(arr[i], remainingDepth, maxBreadth, maxStringLength, memo));
|
|
58
|
+
}
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
function visitObject(obj, remainingDepth, maxBreadth, maxStringLength, memo) {
|
|
62
|
+
const result = {};
|
|
63
|
+
const keys = Object.keys(obj);
|
|
64
|
+
let count = 0;
|
|
65
|
+
for (const key of keys){
|
|
66
|
+
if (count >= maxBreadth) {
|
|
67
|
+
result['...'] = '[MaxProperties ~]';
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
if (void 0 !== obj[key]) {
|
|
71
|
+
result[key] = visit(obj[key], remainingDepth, maxBreadth, maxStringLength, memo);
|
|
72
|
+
count++;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
function truncateString(str, maxLength) {
|
|
78
|
+
if (null == str) return str;
|
|
79
|
+
if (str.length <= maxLength) return str;
|
|
80
|
+
return str.slice(0, maxLength) + TRUNCATION_SUFFIX;
|
|
81
|
+
}
|
|
82
|
+
function truncateStackFrames(frames) {
|
|
83
|
+
if (!frames || frames.length <= MAX_STACK_FRAMES) return frames;
|
|
84
|
+
const half = Math.floor(MAX_STACK_FRAMES / 2);
|
|
85
|
+
return [
|
|
86
|
+
...frames.slice(0, half),
|
|
87
|
+
...frames.slice(-half)
|
|
88
|
+
];
|
|
89
|
+
}
|
|
90
|
+
function truncateExceptionList(error) {
|
|
91
|
+
if (!Array.isArray(error.$exception_list)) return error;
|
|
92
|
+
return {
|
|
93
|
+
...error,
|
|
94
|
+
$exception_list: error.$exception_list.map((exception)=>{
|
|
95
|
+
const next = {
|
|
96
|
+
...exception
|
|
97
|
+
};
|
|
98
|
+
if ('string' == typeof next.value) next.value = truncateString(next.value, MAX_ERROR_MESSAGE_LENGTH);
|
|
99
|
+
if (next.stacktrace?.frames) next.stacktrace = {
|
|
100
|
+
...next.stacktrace,
|
|
101
|
+
frames: truncateStackFrames(next.stacktrace.frames)
|
|
102
|
+
};
|
|
103
|
+
return next;
|
|
104
|
+
})
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
function truncateResponseContent(response) {
|
|
108
|
+
if (null == response || 'object' != typeof response) return response;
|
|
109
|
+
const result = {
|
|
110
|
+
...response
|
|
111
|
+
};
|
|
112
|
+
if (Array.isArray(result.content)) result.content = result.content.map((block)=>{
|
|
113
|
+
if (null != block && 'object' == typeof block && 'type' in block && 'text' in block && block?.type === 'text' && 'string' == typeof block.text && block.text.length > MAX_CONTENT_TEXT_LENGTH) return {
|
|
114
|
+
...block,
|
|
115
|
+
text: block.text.slice(0, MAX_CONTENT_TEXT_LENGTH) + TRUNCATION_SUFFIX
|
|
116
|
+
};
|
|
117
|
+
return block;
|
|
118
|
+
});
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
const textEncoder = new TextEncoder();
|
|
122
|
+
function jsonByteSize(value) {
|
|
123
|
+
return textEncoder.encode(JSON.stringify(value)).length;
|
|
124
|
+
}
|
|
125
|
+
function truncateLargestFields(obj, maxBytes) {
|
|
126
|
+
const result = structuredClone(obj);
|
|
127
|
+
for(let attempt = 0; attempt < 10; attempt++){
|
|
128
|
+
const currentSize = jsonByteSize(result);
|
|
129
|
+
if (currentSize <= maxBytes) break;
|
|
130
|
+
const excess = currentSize - maxBytes;
|
|
131
|
+
const stringPaths = [];
|
|
132
|
+
collectStringPaths(result, [], stringPaths);
|
|
133
|
+
stringPaths.sort((a, b)=>b.length - a.length);
|
|
134
|
+
if (0 === stringPaths.length) break;
|
|
135
|
+
let remaining = excess + 200;
|
|
136
|
+
let truncated = false;
|
|
137
|
+
for (const { path, length } of stringPaths){
|
|
138
|
+
if (remaining <= 0) break;
|
|
139
|
+
const reduction = Math.min(remaining, Math.floor(0.5 * length));
|
|
140
|
+
if (reduction < 10) continue;
|
|
141
|
+
const newLength = length - reduction;
|
|
142
|
+
const currentValue = getNestedValue(result, path);
|
|
143
|
+
if ('string' == typeof currentValue) {
|
|
144
|
+
setNestedValue(result, path, currentValue.slice(0, newLength) + TRUNCATION_SUFFIX);
|
|
145
|
+
remaining -= reduction;
|
|
146
|
+
truncated = true;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (!truncated) break;
|
|
150
|
+
}
|
|
151
|
+
return result;
|
|
152
|
+
}
|
|
153
|
+
function collectStringPaths(obj, currentPath, results) {
|
|
154
|
+
if ('string' == typeof obj && obj.length > 100) return void results.push({
|
|
155
|
+
path: [
|
|
156
|
+
...currentPath
|
|
157
|
+
],
|
|
158
|
+
length: obj.length
|
|
159
|
+
});
|
|
160
|
+
if (Array.isArray(obj)) {
|
|
161
|
+
for (const [i, item] of obj.entries())collectStringPaths(item, [
|
|
162
|
+
...currentPath,
|
|
163
|
+
String(i)
|
|
164
|
+
], results);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (null != obj && 'object' == typeof obj) for (const [key, value] of Object.entries(obj))collectStringPaths(value, [
|
|
168
|
+
...currentPath,
|
|
169
|
+
key
|
|
170
|
+
], results);
|
|
171
|
+
}
|
|
172
|
+
function getNestedValue(obj, path) {
|
|
173
|
+
let current = obj;
|
|
174
|
+
for (const key of path){
|
|
175
|
+
if (null == current || 'object' != typeof current) return;
|
|
176
|
+
current = current[key];
|
|
177
|
+
}
|
|
178
|
+
return current;
|
|
179
|
+
}
|
|
180
|
+
function setNestedValue(obj, path, value) {
|
|
181
|
+
let current = obj;
|
|
182
|
+
for(let i = 0; i < path.length - 1; i++){
|
|
183
|
+
if (null == current || 'object' != typeof current) return;
|
|
184
|
+
current = current[path[i]];
|
|
185
|
+
}
|
|
186
|
+
const finalKey = path.at(-1);
|
|
187
|
+
if (void 0 !== finalKey && null != current && 'object' == typeof current) current[finalKey] = value;
|
|
188
|
+
}
|
|
189
|
+
function truncateToSize(event) {
|
|
190
|
+
if (jsonByteSize(event) <= MAX_EVENT_BYTES) return event;
|
|
191
|
+
for(let depth = MAX_DEPTH - 1; depth >= 1; depth--){
|
|
192
|
+
const reduced = {
|
|
193
|
+
...event
|
|
194
|
+
};
|
|
195
|
+
if (null != reduced.parameters) reduced.parameters = normalize(reduced.parameters, depth);
|
|
196
|
+
if (null != reduced.response) reduced.response = normalize(reduced.response, depth);
|
|
197
|
+
if (null != reduced.identifyActorData) reduced.identifyActorData = normalize(reduced.identifyActorData, depth);
|
|
198
|
+
if (null != reduced.error) reduced.error = normalize(reduced.error, depth);
|
|
199
|
+
if (jsonByteSize(reduced) <= MAX_EVENT_BYTES) return reduced;
|
|
200
|
+
}
|
|
201
|
+
const minimal = {
|
|
202
|
+
...event
|
|
203
|
+
};
|
|
204
|
+
if (null != minimal.parameters) minimal.parameters = normalize(minimal.parameters, 1);
|
|
205
|
+
if (null != minimal.response) minimal.response = normalize(minimal.response, 1);
|
|
206
|
+
if (null != minimal.identifyActorData) minimal.identifyActorData = normalize(minimal.identifyActorData, 1);
|
|
207
|
+
if (null != minimal.error) minimal.error = normalize(minimal.error, 1);
|
|
208
|
+
return truncateLargestFields(minimal, MAX_EVENT_BYTES);
|
|
209
|
+
}
|
|
210
|
+
function truncateEvent(event) {
|
|
211
|
+
const result = {
|
|
212
|
+
...event
|
|
213
|
+
};
|
|
214
|
+
result.userIntent = truncateString(result.userIntent, MAX_USER_INTENT_LENGTH);
|
|
215
|
+
result.resourceName = truncateString(result.resourceName, MAX_RESOURCE_NAME_LENGTH);
|
|
216
|
+
result.serverName = truncateString(result.serverName, MAX_METADATA_LENGTH);
|
|
217
|
+
result.serverVersion = truncateString(result.serverVersion, MAX_METADATA_LENGTH);
|
|
218
|
+
result.clientName = truncateString(result.clientName, MAX_METADATA_LENGTH);
|
|
219
|
+
result.clientVersion = truncateString(result.clientVersion, MAX_METADATA_LENGTH);
|
|
220
|
+
if (null != result.error && 'object' == typeof result.error) result.error = truncateExceptionList(result.error);
|
|
221
|
+
result.response = truncateResponseContent(result.response);
|
|
222
|
+
if (null != result.parameters) result.parameters = normalize(result.parameters);
|
|
223
|
+
if (null != result.response) result.response = normalize(result.response);
|
|
224
|
+
if (null != result.identifyActorData) result.identifyActorData = normalize(result.identifyActorData);
|
|
225
|
+
if (null != result.error) result.error = normalize(result.error);
|
|
226
|
+
return truncateToSize(result);
|
|
227
|
+
}
|
|
228
|
+
export { MAX_BREADTH, MAX_DEPTH, MAX_EVENT_BYTES, MAX_STRING_LENGTH, normalize, truncateEvent };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { PostHog } from 'posthog-node';
|
|
2
|
+
import { deriveSessionIdFromMCPSession } from './extensions/session';
|
|
3
|
+
import type { McpAnalytics, MCPAnalyticsOptions } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Instruments an MCP server so PostHog auto-captures tool calls, tool listings, initialize
|
|
6
|
+
* requests, identity, and exceptions. Returns a handle whose `capture()` method records
|
|
7
|
+
* custom events, so you don't pass the server around after wiring it up.
|
|
8
|
+
*
|
|
9
|
+
* **Idempotent per server instance.** Per-server tracking state lives in a module-level
|
|
10
|
+
* `WeakMap<MCPServerLike, MCPAnalyticsData>` (`internal.ts`); a second `instrument()` call
|
|
11
|
+
* on the same server reuses it rather than double-wrapping handlers.
|
|
12
|
+
*
|
|
13
|
+
* @param server - The MCP server to instrument (low-level `Server` or high-level `McpServer`).
|
|
14
|
+
* @param posthog - A `posthog-node` client you construct and own (matching `@posthog/ai`); call `posthog.shutdown()` on exit to flush.
|
|
15
|
+
* @param options - Optional configuration. See `MCPAnalyticsOptions`.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"
|
|
20
|
+
* import { PostHog } from "posthog-node"
|
|
21
|
+
* import { instrument } from "@posthog/mcp"
|
|
22
|
+
*
|
|
23
|
+
* const posthog = new PostHog(process.env.POSTHOG_PROJECT_TOKEN, { host: "https://us.i.posthog.com" })
|
|
24
|
+
* const server = new McpServer({ name: "my-mcp", version: "1.0.0" })
|
|
25
|
+
* const analytics = instrument(server, posthog)
|
|
26
|
+
*
|
|
27
|
+
* await analytics.capture({ event: "feedback_submitted", properties: { rating: 5 } })
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
declare function instrument(server: unknown, posthog: PostHog, options?: MCPAnalyticsOptions): McpAnalytics;
|
|
31
|
+
export { deriveSessionIdFromMCPSession };
|
|
32
|
+
export { POSTHOG_MCP_ANALYTICS_SOURCE, PostHogMCPAnalyticsEvent, PostHogMCPAnalyticsProperty, } from './extensions/constants';
|
|
33
|
+
export type { BeforeSendFn, CaptureEventData, McpAnalytics, MCPAnalyticsContextOptions, MCPAnalyticsIntentSource, MCPAnalyticsOptions, UserIdentity, } from './types';
|
|
34
|
+
export type IdentifyFunction = MCPAnalyticsOptions['identify'];
|
|
35
|
+
export { instrument };
|
|
36
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAO3C,OAAO,EAAE,6BAA6B,EAAgC,MAAM,sBAAsB,CAAA;AAGlG,OAAO,KAAK,EAGV,YAAY,EAEZ,mBAAmB,EAGpB,MAAM,SAAS,CAAA;AAEhB;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,iBAAS,UAAU,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,GAAE,mBAAwB,GAAG,YAAY,CA6BtG;AAkFD,OAAO,EAAE,6BAA6B,EAAE,CAAA;AACxC,OAAO,EACL,4BAA4B,EAC5B,wBAAwB,EACxB,2BAA2B,GAC5B,MAAM,wBAAwB,CAAA;AAC/B,YAAY,EACV,YAAY,EACZ,gBAAgB,EAChB,YAAY,EACZ,0BAA0B,EAC1B,wBAAwB,EACxB,mBAAmB,EACnB,YAAY,GACb,MAAM,SAAS,CAAA;AAChB,MAAM,MAAM,gBAAgB,GAAG,mBAAmB,CAAC,UAAU,CAAC,CAAA;AAC9D,OAAO,EAAE,UAAU,EAAE,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
instrument: ()=>instrument,
|
|
28
|
+
PostHogMCPAnalyticsEvent: ()=>constants_js_namespaceObject.PostHogMCPAnalyticsEvent,
|
|
29
|
+
POSTHOG_MCP_ANALYTICS_SOURCE: ()=>constants_js_namespaceObject.POSTHOG_MCP_ANALYTICS_SOURCE,
|
|
30
|
+
deriveSessionIdFromMCPSession: ()=>session_js_namespaceObject.deriveSessionIdFromMCPSession,
|
|
31
|
+
PostHogMCPAnalyticsProperty: ()=>constants_js_namespaceObject.PostHogMCPAnalyticsProperty
|
|
32
|
+
});
|
|
33
|
+
const compatibility_js_namespaceObject = require("./extensions/compatibility.js");
|
|
34
|
+
const sink_js_namespaceObject = require("./extensions/sink.js");
|
|
35
|
+
const event_types_js_namespaceObject = require("./extensions/event-types.js");
|
|
36
|
+
const internal_js_namespaceObject = require("./extensions/internal.js");
|
|
37
|
+
const logger_js_namespaceObject = require("./extensions/logger.js");
|
|
38
|
+
const capture_js_namespaceObject = require("./extensions/capture.js");
|
|
39
|
+
const session_js_namespaceObject = require("./extensions/session.js");
|
|
40
|
+
const instrument_lowlevel_js_namespaceObject = require("./extensions/instrument-lowlevel.js");
|
|
41
|
+
const instrument_highlevel_js_namespaceObject = require("./extensions/instrument-highlevel.js");
|
|
42
|
+
const constants_js_namespaceObject = require("./extensions/constants.js");
|
|
43
|
+
function instrument(server, posthog, options = {}) {
|
|
44
|
+
try {
|
|
45
|
+
if (options.logger) (0, logger_js_namespaceObject.setLogger)(options.logger);
|
|
46
|
+
if (!posthog) (0, logger_js_namespaceObject.log)('Warning: No PostHog client passed to instrument(). Events will not be sent anywhere.');
|
|
47
|
+
const validatedServer = (0, compatibility_js_namespaceObject.isCompatibleServerType)(server);
|
|
48
|
+
const lowLevelServer = getLowLevelServer(validatedServer);
|
|
49
|
+
if ((0, internal_js_namespaceObject.getServerTrackingData)(lowLevelServer)) {
|
|
50
|
+
(0, logger_js_namespaceObject.log)('instrument() - Server already instrumented, skipping initialization');
|
|
51
|
+
return createAnalyticsHandle(lowLevelServer);
|
|
52
|
+
}
|
|
53
|
+
const sink = posthog ? new sink_js_namespaceObject.McpEventSink(posthog) : void 0;
|
|
54
|
+
const mcpAnalyticsData = buildTrackingData(lowLevelServer, options, sink);
|
|
55
|
+
(0, internal_js_namespaceObject.setServerTrackingData)(lowLevelServer, mcpAnalyticsData);
|
|
56
|
+
setupTrackedServer(validatedServer, lowLevelServer);
|
|
57
|
+
return createAnalyticsHandle(lowLevelServer);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
(0, logger_js_namespaceObject.log)(`Warning: Failed to instrument server - ${error}`);
|
|
60
|
+
return {
|
|
61
|
+
capture: async ()=>void 0
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function createAnalyticsHandle(lowLevelServer) {
|
|
66
|
+
return {
|
|
67
|
+
capture: (eventData)=>captureCustomEvent(lowLevelServer, eventData)
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function getLowLevelServer(server) {
|
|
71
|
+
return (0, compatibility_js_namespaceObject.isHighLevelServer)(server) ? server.server : server;
|
|
72
|
+
}
|
|
73
|
+
const DEFAULT_OPTIONS = {
|
|
74
|
+
reportMissing: false,
|
|
75
|
+
enableConversationId: false
|
|
76
|
+
};
|
|
77
|
+
function buildTrackingData(lowLevelServer, options, sink) {
|
|
78
|
+
return {
|
|
79
|
+
sink,
|
|
80
|
+
sessionId: (0, session_js_namespaceObject.newSessionId)(),
|
|
81
|
+
lastActivity: new Date(),
|
|
82
|
+
identifiedSessions: new internal_js_namespaceObject.IdentityCache(),
|
|
83
|
+
toolDescriptions: new Map(),
|
|
84
|
+
sessionInfo: (0, session_js_namespaceObject.getSessionInfo)(lowLevelServer, void 0),
|
|
85
|
+
options: {
|
|
86
|
+
...DEFAULT_OPTIONS,
|
|
87
|
+
...options
|
|
88
|
+
},
|
|
89
|
+
sessionSource: 'generated'
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function setupTrackedServer(validatedServer, lowLevelServer) {
|
|
93
|
+
if ((0, compatibility_js_namespaceObject.isHighLevelServer)(validatedServer)) {
|
|
94
|
+
const highLevelServer = validatedServer;
|
|
95
|
+
(0, instrument_highlevel_js_namespaceObject.instrumentHighLevelServer)(highLevelServer);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
(0, instrument_lowlevel_js_namespaceObject.instrumentLowLevelServer)(lowLevelServer);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
(0, logger_js_namespaceObject.log)(`Warning: Failed to setup tool call instrumentation - ${error}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
async function captureCustomEvent(lowLevelServer, eventData) {
|
|
105
|
+
if (!eventData || 'string' != typeof eventData.event || 0 === eventData.event.length) throw new Error('`capture` requires an `event` name, e.g. analytics.capture({ event: "feedback_submitted" })');
|
|
106
|
+
const trackingData = (0, internal_js_namespaceObject.getServerTrackingData)(lowLevelServer);
|
|
107
|
+
if (!trackingData) return;
|
|
108
|
+
const event = {
|
|
109
|
+
sessionId: trackingData.sessionId,
|
|
110
|
+
eventType: event_types_js_namespaceObject.MCPAnalyticsEventType.custom,
|
|
111
|
+
eventName: eventData.event,
|
|
112
|
+
timestamp: new Date(),
|
|
113
|
+
properties: eventData.properties
|
|
114
|
+
};
|
|
115
|
+
await (0, capture_js_namespaceObject.captureEvent)(lowLevelServer, event);
|
|
116
|
+
(0, logger_js_namespaceObject.log)(`Captured event "${eventData.event}" for session ${trackingData.sessionId}`);
|
|
117
|
+
}
|
|
118
|
+
exports.POSTHOG_MCP_ANALYTICS_SOURCE = __webpack_exports__.POSTHOG_MCP_ANALYTICS_SOURCE;
|
|
119
|
+
exports.PostHogMCPAnalyticsEvent = __webpack_exports__.PostHogMCPAnalyticsEvent;
|
|
120
|
+
exports.PostHogMCPAnalyticsProperty = __webpack_exports__.PostHogMCPAnalyticsProperty;
|
|
121
|
+
exports.deriveSessionIdFromMCPSession = __webpack_exports__.deriveSessionIdFromMCPSession;
|
|
122
|
+
exports.instrument = __webpack_exports__.instrument;
|
|
123
|
+
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
124
|
+
"POSTHOG_MCP_ANALYTICS_SOURCE",
|
|
125
|
+
"PostHogMCPAnalyticsEvent",
|
|
126
|
+
"PostHogMCPAnalyticsProperty",
|
|
127
|
+
"deriveSessionIdFromMCPSession",
|
|
128
|
+
"instrument"
|
|
129
|
+
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
130
|
+
Object.defineProperty(exports, '__esModule', {
|
|
131
|
+
value: true
|
|
132
|
+
});
|