@posthog/mcp 0.10.6 → 0.10.8
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/dist/extensions/analytics-parameters.d.ts +7 -2
- package/dist/extensions/analytics-parameters.d.ts.map +1 -1
- package/dist/extensions/analytics-parameters.js +4 -2
- package/dist/extensions/analytics-parameters.mjs +4 -2
- package/dist/extensions/capture.d.ts +2 -2
- package/dist/extensions/capture.d.ts.map +1 -1
- package/dist/extensions/capture.js +2 -2
- package/dist/extensions/capture.mjs +2 -2
- package/dist/extensions/conversation-id.d.ts +37 -8
- package/dist/extensions/conversation-id.d.ts.map +1 -1
- package/dist/extensions/conversation-id.js +8 -13
- package/dist/extensions/conversation-id.mjs +8 -13
- package/dist/extensions/instrument-highlevel.d.ts.map +1 -1
- package/dist/extensions/instrument-highlevel.js +1 -1
- package/dist/extensions/instrument-highlevel.mjs +1 -1
- package/dist/extensions/instrumentation.d.ts.map +1 -1
- package/dist/extensions/instrumentation.js +86 -42
- package/dist/extensions/instrumentation.mjs +88 -44
- package/dist/extensions/internal.d.ts +10 -2
- package/dist/extensions/internal.d.ts.map +1 -1
- package/dist/extensions/internal.js +25 -9
- package/dist/extensions/internal.mjs +19 -6
- package/dist/extensions/output-instructions.d.ts +70 -0
- package/dist/extensions/output-instructions.d.ts.map +1 -0
- package/dist/extensions/output-instructions.js +131 -0
- package/dist/extensions/output-instructions.mjs +82 -0
- package/dist/extensions/session.d.ts +26 -5
- package/dist/extensions/session.d.ts.map +1 -1
- package/dist/extensions/session.js +35 -15
- package/dist/extensions/session.mjs +33 -16
- package/dist/types.d.ts +23 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +3 -3
- package/src/extensions/analytics-parameters.ts +15 -2
- package/src/extensions/capture.ts +13 -6
- package/src/extensions/conversation-id.ts +51 -29
- package/src/extensions/instrument-highlevel.ts +3 -1
- package/src/extensions/instrumentation.ts +151 -60
- package/src/extensions/internal.ts +30 -7
- package/src/extensions/output-instructions.ts +231 -0
- package/src/extensions/session.ts +128 -29
- package/src/types.ts +23 -1
- package/src/version.ts +1 -1
|
@@ -0,0 +1,131 @@
|
|
|
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
|
+
mirrorInstructionsIntoStructuredContent: ()=>mirrorInstructionsIntoStructuredContent,
|
|
28
|
+
addInstructionsToOutputSchema: ()=>addInstructionsToOutputSchema,
|
|
29
|
+
addInstructionsToOutputSchemas: ()=>addInstructionsToOutputSchemas,
|
|
30
|
+
MCP_INSTRUCTIONS_KEY: ()=>MCP_INSTRUCTIONS_KEY,
|
|
31
|
+
buildConversationInstructions: ()=>buildConversationInstructions,
|
|
32
|
+
canDeclareOutputInstructions: ()=>canDeclareOutputInstructions
|
|
33
|
+
});
|
|
34
|
+
const external_logger_js_namespaceObject = require("./logger.js");
|
|
35
|
+
const MCP_INSTRUCTIONS_KEY = '_mcp_instructions';
|
|
36
|
+
const INSTRUCTIONS_FIELD_DESCRIPTION = 'Server-issued handles for this conversation, and what to do with them. Read and follow.';
|
|
37
|
+
const CONVERSATION_ID_FIELD_DESCRIPTION = 'Echo this exact value as the conversation_id argument on every subsequent tool call.';
|
|
38
|
+
function canDeclareOutputInstructions(outputSchema) {
|
|
39
|
+
if (!outputSchema || 'object' != typeof outputSchema) return false;
|
|
40
|
+
if (isSchemaObjectLike(outputSchema)) return false;
|
|
41
|
+
const schema = outputSchema;
|
|
42
|
+
if (schema.$ref || schema.oneOf || schema.allOf || schema.anyOf) return false;
|
|
43
|
+
const { properties } = schema;
|
|
44
|
+
if (void 0 !== properties && ('object' != typeof properties || null === properties || Array.isArray(properties))) return false;
|
|
45
|
+
return !properties || !Object.prototype.hasOwnProperty.call(properties, MCP_INSTRUCTIONS_KEY);
|
|
46
|
+
}
|
|
47
|
+
function isZodLike(entry) {
|
|
48
|
+
return !!entry && 'object' == typeof entry && ('_def' in entry || '_zod' in entry || '~standard' in entry);
|
|
49
|
+
}
|
|
50
|
+
function looksLikeJsonSchema(candidate) {
|
|
51
|
+
if ('string' == typeof candidate.type || 'string' == typeof candidate.$ref) return true;
|
|
52
|
+
if (Array.isArray(candidate.oneOf) || Array.isArray(candidate.allOf) || Array.isArray(candidate.anyOf)) return true;
|
|
53
|
+
const { properties } = candidate;
|
|
54
|
+
return !!properties && 'object' == typeof properties && !isZodLike(properties);
|
|
55
|
+
}
|
|
56
|
+
function isSchemaObjectLike(value) {
|
|
57
|
+
const candidate = value;
|
|
58
|
+
if (isZodLike(candidate)) return true;
|
|
59
|
+
if (looksLikeJsonSchema(candidate)) return false;
|
|
60
|
+
return Object.values(candidate).some(isZodLike);
|
|
61
|
+
}
|
|
62
|
+
function addInstructionsToOutputSchema(tool, logger = external_logger_js_namespaceObject.log) {
|
|
63
|
+
const toolName = tool.name || 'unknown';
|
|
64
|
+
const original = tool.outputSchema;
|
|
65
|
+
if (!original || 'object' != typeof original) return tool;
|
|
66
|
+
if (!canDeclareOutputInstructions(original)) {
|
|
67
|
+
const schema = original;
|
|
68
|
+
logger(schema.properties && Object.prototype.hasOwnProperty.call(schema.properties, MCP_INSTRUCTIONS_KEY) ? `WARN: Tool "${toolName}" already declares '${MCP_INSTRUCTIONS_KEY}' in its output schema. Leaving it alone.` : `WARN: Tool "${toolName}" has a complex output schema (oneOf/allOf/anyOf/$ref). Skipping '${MCP_INSTRUCTIONS_KEY}' declaration; its session handle stays content-only.`);
|
|
69
|
+
return tool;
|
|
70
|
+
}
|
|
71
|
+
const modifiedTool = {
|
|
72
|
+
...tool
|
|
73
|
+
};
|
|
74
|
+
const outputSchema = JSON.parse(JSON.stringify(original));
|
|
75
|
+
if (!outputSchema.properties) outputSchema.properties = {};
|
|
76
|
+
outputSchema.properties[MCP_INSTRUCTIONS_KEY] = {
|
|
77
|
+
type: 'object',
|
|
78
|
+
description: INSTRUCTIONS_FIELD_DESCRIPTION,
|
|
79
|
+
properties: {
|
|
80
|
+
conversation_id: {
|
|
81
|
+
type: 'string',
|
|
82
|
+
description: CONVERSATION_ID_FIELD_DESCRIPTION
|
|
83
|
+
},
|
|
84
|
+
instructions: {
|
|
85
|
+
type: 'string'
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
modifiedTool.outputSchema = outputSchema;
|
|
90
|
+
return modifiedTool;
|
|
91
|
+
}
|
|
92
|
+
function addInstructionsToOutputSchemas(tools, logger = external_logger_js_namespaceObject.log) {
|
|
93
|
+
return tools.map((tool)=>addInstructionsToOutputSchema(tool, logger));
|
|
94
|
+
}
|
|
95
|
+
const ECHO_INSTRUCTION = 'Send this conversation_id as an argument on every subsequent tool call in this conversation.';
|
|
96
|
+
function buildConversationInstructions(conversationId) {
|
|
97
|
+
return {
|
|
98
|
+
conversation_id: conversationId,
|
|
99
|
+
instructions: ECHO_INSTRUCTION
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function mirrorInstructionsIntoStructuredContent(result, conversationId) {
|
|
103
|
+
if (!result || 'object' != typeof result) return result;
|
|
104
|
+
const structuredContent = result.structuredContent;
|
|
105
|
+
if (!structuredContent || 'object' != typeof structuredContent || Array.isArray(structuredContent)) return result;
|
|
106
|
+
if (Object.prototype.hasOwnProperty.call(structuredContent, MCP_INSTRUCTIONS_KEY)) return result;
|
|
107
|
+
return {
|
|
108
|
+
...result,
|
|
109
|
+
structuredContent: {
|
|
110
|
+
...structuredContent,
|
|
111
|
+
[MCP_INSTRUCTIONS_KEY]: buildConversationInstructions(conversationId)
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
exports.MCP_INSTRUCTIONS_KEY = __webpack_exports__.MCP_INSTRUCTIONS_KEY;
|
|
116
|
+
exports.addInstructionsToOutputSchema = __webpack_exports__.addInstructionsToOutputSchema;
|
|
117
|
+
exports.addInstructionsToOutputSchemas = __webpack_exports__.addInstructionsToOutputSchemas;
|
|
118
|
+
exports.buildConversationInstructions = __webpack_exports__.buildConversationInstructions;
|
|
119
|
+
exports.canDeclareOutputInstructions = __webpack_exports__.canDeclareOutputInstructions;
|
|
120
|
+
exports.mirrorInstructionsIntoStructuredContent = __webpack_exports__.mirrorInstructionsIntoStructuredContent;
|
|
121
|
+
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
122
|
+
"MCP_INSTRUCTIONS_KEY",
|
|
123
|
+
"addInstructionsToOutputSchema",
|
|
124
|
+
"addInstructionsToOutputSchemas",
|
|
125
|
+
"buildConversationInstructions",
|
|
126
|
+
"canDeclareOutputInstructions",
|
|
127
|
+
"mirrorInstructionsIntoStructuredContent"
|
|
128
|
+
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
129
|
+
Object.defineProperty(exports, '__esModule', {
|
|
130
|
+
value: true
|
|
131
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { log } from "./logger.mjs";
|
|
2
|
+
const MCP_INSTRUCTIONS_KEY = '_mcp_instructions';
|
|
3
|
+
const INSTRUCTIONS_FIELD_DESCRIPTION = 'Server-issued handles for this conversation, and what to do with them. Read and follow.';
|
|
4
|
+
const CONVERSATION_ID_FIELD_DESCRIPTION = 'Echo this exact value as the conversation_id argument on every subsequent tool call.';
|
|
5
|
+
function canDeclareOutputInstructions(outputSchema) {
|
|
6
|
+
if (!outputSchema || 'object' != typeof outputSchema) return false;
|
|
7
|
+
if (isSchemaObjectLike(outputSchema)) return false;
|
|
8
|
+
const schema = outputSchema;
|
|
9
|
+
if (schema.$ref || schema.oneOf || schema.allOf || schema.anyOf) return false;
|
|
10
|
+
const { properties } = schema;
|
|
11
|
+
if (void 0 !== properties && ('object' != typeof properties || null === properties || Array.isArray(properties))) return false;
|
|
12
|
+
return !properties || !Object.prototype.hasOwnProperty.call(properties, MCP_INSTRUCTIONS_KEY);
|
|
13
|
+
}
|
|
14
|
+
function isZodLike(entry) {
|
|
15
|
+
return !!entry && 'object' == typeof entry && ('_def' in entry || '_zod' in entry || '~standard' in entry);
|
|
16
|
+
}
|
|
17
|
+
function looksLikeJsonSchema(candidate) {
|
|
18
|
+
if ('string' == typeof candidate.type || 'string' == typeof candidate.$ref) return true;
|
|
19
|
+
if (Array.isArray(candidate.oneOf) || Array.isArray(candidate.allOf) || Array.isArray(candidate.anyOf)) return true;
|
|
20
|
+
const { properties } = candidate;
|
|
21
|
+
return !!properties && 'object' == typeof properties && !isZodLike(properties);
|
|
22
|
+
}
|
|
23
|
+
function isSchemaObjectLike(value) {
|
|
24
|
+
const candidate = value;
|
|
25
|
+
if (isZodLike(candidate)) return true;
|
|
26
|
+
if (looksLikeJsonSchema(candidate)) return false;
|
|
27
|
+
return Object.values(candidate).some(isZodLike);
|
|
28
|
+
}
|
|
29
|
+
function addInstructionsToOutputSchema(tool, logger = log) {
|
|
30
|
+
const toolName = tool.name || 'unknown';
|
|
31
|
+
const original = tool.outputSchema;
|
|
32
|
+
if (!original || 'object' != typeof original) return tool;
|
|
33
|
+
if (!canDeclareOutputInstructions(original)) {
|
|
34
|
+
const schema = original;
|
|
35
|
+
logger(schema.properties && Object.prototype.hasOwnProperty.call(schema.properties, MCP_INSTRUCTIONS_KEY) ? `WARN: Tool "${toolName}" already declares '${MCP_INSTRUCTIONS_KEY}' in its output schema. Leaving it alone.` : `WARN: Tool "${toolName}" has a complex output schema (oneOf/allOf/anyOf/$ref). Skipping '${MCP_INSTRUCTIONS_KEY}' declaration; its session handle stays content-only.`);
|
|
36
|
+
return tool;
|
|
37
|
+
}
|
|
38
|
+
const modifiedTool = {
|
|
39
|
+
...tool
|
|
40
|
+
};
|
|
41
|
+
const outputSchema = JSON.parse(JSON.stringify(original));
|
|
42
|
+
if (!outputSchema.properties) outputSchema.properties = {};
|
|
43
|
+
outputSchema.properties[MCP_INSTRUCTIONS_KEY] = {
|
|
44
|
+
type: 'object',
|
|
45
|
+
description: INSTRUCTIONS_FIELD_DESCRIPTION,
|
|
46
|
+
properties: {
|
|
47
|
+
conversation_id: {
|
|
48
|
+
type: 'string',
|
|
49
|
+
description: CONVERSATION_ID_FIELD_DESCRIPTION
|
|
50
|
+
},
|
|
51
|
+
instructions: {
|
|
52
|
+
type: 'string'
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
modifiedTool.outputSchema = outputSchema;
|
|
57
|
+
return modifiedTool;
|
|
58
|
+
}
|
|
59
|
+
function addInstructionsToOutputSchemas(tools, logger = log) {
|
|
60
|
+
return tools.map((tool)=>addInstructionsToOutputSchema(tool, logger));
|
|
61
|
+
}
|
|
62
|
+
const ECHO_INSTRUCTION = 'Send this conversation_id as an argument on every subsequent tool call in this conversation.';
|
|
63
|
+
function buildConversationInstructions(conversationId) {
|
|
64
|
+
return {
|
|
65
|
+
conversation_id: conversationId,
|
|
66
|
+
instructions: ECHO_INSTRUCTION
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function mirrorInstructionsIntoStructuredContent(result, conversationId) {
|
|
70
|
+
if (!result || 'object' != typeof result) return result;
|
|
71
|
+
const structuredContent = result.structuredContent;
|
|
72
|
+
if (!structuredContent || 'object' != typeof structuredContent || Array.isArray(structuredContent)) return result;
|
|
73
|
+
if (Object.prototype.hasOwnProperty.call(structuredContent, MCP_INSTRUCTIONS_KEY)) return result;
|
|
74
|
+
return {
|
|
75
|
+
...result,
|
|
76
|
+
structuredContent: {
|
|
77
|
+
...structuredContent,
|
|
78
|
+
[MCP_INSTRUCTIONS_KEY]: buildConversationInstructions(conversationId)
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
export { MCP_INSTRUCTIONS_KEY, addInstructionsToOutputSchema, addInstructionsToOutputSchemas, buildConversationInstructions, canDeclareOutputInstructions, mirrorInstructionsIntoStructuredContent };
|
|
@@ -6,12 +6,33 @@ export declare function newSessionId(): string;
|
|
|
6
6
|
*/
|
|
7
7
|
export declare function deriveSessionIdFromMCPSession(mcpSessionId: string): string;
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
9
|
+
* Derives the SDK session id from the agent's conversation handle. Deterministic
|
|
10
|
+
* and unsalted on purpose: two pods that never met must agree on the session, and
|
|
11
|
+
* the 2026-07-28 revision leaves them no shared state to agree through.
|
|
12
|
+
*
|
|
13
|
+
* Hashed rather than used verbatim so an MCP session can never collide with a
|
|
14
|
+
* Session Replay id — a bare uuidv7 would render a "View recording" button that
|
|
15
|
+
* resolves to nothing.
|
|
16
|
+
*
|
|
17
|
+
* Exported because this *is* the cross-SDK contract: posthog-python has to
|
|
18
|
+
* reproduce it byte for byte or the same conversation splits into two sessions
|
|
19
|
+
* depending on which SDK served the call.
|
|
13
20
|
*/
|
|
14
|
-
export declare function
|
|
21
|
+
export declare function deriveSessionIdFromConversation(conversationId: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* Resolves the session id for a request. Three steps, first match wins:
|
|
24
|
+
*
|
|
25
|
+
* 1. the agent carried a `conversation_id` tool argument — 2026-07-28
|
|
26
|
+
* 2. the request carried a session id — 2025-11-25
|
|
27
|
+
* 3. nothing was carried, so reuse this instance's own id — stdio
|
|
28
|
+
*
|
|
29
|
+
* The split mirrors the two protocol revisions. 2026-07-28 removed protocol-level
|
|
30
|
+
* sessions, so the only thing that can carry a session across calls is the agent
|
|
31
|
+
* itself (1). 2025-11-25 kept the session on the connection, so the request
|
|
32
|
+
* carries it (2). 3 is for stdio, where neither applies — there is no
|
|
33
|
+
* HTTP request to carry anything, and no agent handle unless the host opted in.
|
|
34
|
+
*/
|
|
35
|
+
export declare function getSessionId(server: MCPServerLike, extra?: CompatibleRequestHandlerExtra, conversationId?: string): string;
|
|
15
36
|
/**
|
|
16
37
|
* Builds the session metadata stamped onto an event. The caller passes the
|
|
17
38
|
* session id resolved for *this* request so identity attribution can't be
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/extensions/session.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,6BAA6B,EAC7B,gBAAgB,EAChB,aAAa,EAEb,WAAW,EACZ,MAAM,UAAU,CAAA;
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/extensions/session.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,6BAA6B,EAC7B,gBAAgB,EAChB,aAAa,EAEb,WAAW,EACZ,MAAM,UAAU,CAAA;AAOjB,wBAAgB,YAAY,IAAI,MAAM,CAErC;AAED;;;GAGG;AACH,wBAAgB,6BAA6B,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAE1E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,+BAA+B,CAAC,cAAc,EAAE,MAAM,GAAG,MAAM,CAE9E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAC1B,MAAM,EAAE,aAAa,EACrB,KAAK,CAAC,EAAE,6BAA6B,EACrC,cAAc,CAAC,EAAE,MAAM,GACtB,MAAM,CA0CR;AA4ED;;;;GAIG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,aAAa,EACrB,IAAI,EAAE,gBAAgB,GAAG,SAAS,EAClC,SAAS,CAAC,EAAE,MAAM,GACjB,WAAW,CAmCb"}
|
|
@@ -24,6 +24,7 @@ var __webpack_require__ = {};
|
|
|
24
24
|
var __webpack_exports__ = {};
|
|
25
25
|
__webpack_require__.r(__webpack_exports__);
|
|
26
26
|
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
deriveSessionIdFromConversation: ()=>deriveSessionIdFromConversation,
|
|
27
28
|
getSessionId: ()=>getSessionId,
|
|
28
29
|
deriveSessionIdFromMCPSession: ()=>deriveSessionIdFromMCPSession,
|
|
29
30
|
getSessionInfo: ()=>getSessionInfo,
|
|
@@ -40,27 +41,42 @@ function newSessionId() {
|
|
|
40
41
|
function deriveSessionIdFromMCPSession(mcpSessionId) {
|
|
41
42
|
return (0, external_ids_js_namespaceObject.deterministicPrefixedId)('ses', mcpSessionId);
|
|
42
43
|
}
|
|
43
|
-
function
|
|
44
|
+
function deriveSessionIdFromConversation(conversationId) {
|
|
45
|
+
return (0, external_ids_js_namespaceObject.deterministicPrefixedId)('ses', conversationId);
|
|
46
|
+
}
|
|
47
|
+
function getSessionId(server, extra, conversationId) {
|
|
44
48
|
const data = (0, external_internal_js_namespaceObject.getServerTrackingData)(server);
|
|
45
49
|
if (!data) throw new Error('Server tracking data not found');
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
data.sessionInfo.clientVersion = token.clientVersion;
|
|
53
|
-
data.sessionInfo.protocolVersion = token.protocolVersion;
|
|
54
|
-
sessionId = token.sessionId;
|
|
55
|
-
} else if (extra?.sessionId) {
|
|
56
|
-
data.sessionSource = 'mcp';
|
|
57
|
-
sessionId = deriveSessionIdFromMCPSession(extra.sessionId);
|
|
58
|
-
} else sessionId = getSessionIdFromMemory(data);
|
|
50
|
+
if (conversationId) {
|
|
51
|
+
applyTokenClientIdentity(data, extra);
|
|
52
|
+
return deriveSessionIdFromConversation(conversationId);
|
|
53
|
+
}
|
|
54
|
+
const carriedByRequest = readSessionIdFromRequest(data, extra);
|
|
55
|
+
const sessionId = carriedByRequest ?? getSessionIdFromMemory(data);
|
|
59
56
|
data.sessionId = sessionId;
|
|
60
57
|
data.lastActivity = new Date();
|
|
61
58
|
(0, external_internal_js_namespaceObject.setServerTrackingData)(server, data);
|
|
62
59
|
return sessionId;
|
|
63
60
|
}
|
|
61
|
+
function applyTokenClientIdentity(data, extra) {
|
|
62
|
+
const token = (0, external_session_token_js_namespaceObject.decodeSessionId)((0, external_session_token_js_namespaceObject.readMcpSessionHeader)(extra?.requestInfo?.headers));
|
|
63
|
+
if (!token) return;
|
|
64
|
+
data.sessionInfo.clientName = token.clientName;
|
|
65
|
+
data.sessionInfo.clientVersion = token.clientVersion;
|
|
66
|
+
data.sessionInfo.protocolVersion = token.protocolVersion;
|
|
67
|
+
return token;
|
|
68
|
+
}
|
|
69
|
+
function readSessionIdFromRequest(data, extra) {
|
|
70
|
+
const token = applyTokenClientIdentity(data, extra);
|
|
71
|
+
if (token) {
|
|
72
|
+
data.sessionSource = 'token';
|
|
73
|
+
return token.sessionId;
|
|
74
|
+
}
|
|
75
|
+
if (extra?.sessionId) {
|
|
76
|
+
data.sessionSource = 'mcp';
|
|
77
|
+
return deriveSessionIdFromMCPSession(extra.sessionId);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
64
80
|
function getSessionIdFromMemory(data) {
|
|
65
81
|
const timeoutMs = 60 * external_constants_js_namespaceObject.INACTIVITY_TIMEOUT_IN_MINUTES * 1000;
|
|
66
82
|
const isStale = Date.now() - data.lastActivity.getTime() > timeoutMs;
|
|
@@ -90,13 +106,17 @@ function getSessionInfo(server, data, sessionId) {
|
|
|
90
106
|
if (!data) return sessionInfo;
|
|
91
107
|
data.sessionInfo = sessionInfo;
|
|
92
108
|
(0, external_internal_js_namespaceObject.setServerTrackingData)(server, data);
|
|
93
|
-
return
|
|
109
|
+
return {
|
|
110
|
+
...sessionInfo
|
|
111
|
+
};
|
|
94
112
|
}
|
|
113
|
+
exports.deriveSessionIdFromConversation = __webpack_exports__.deriveSessionIdFromConversation;
|
|
95
114
|
exports.deriveSessionIdFromMCPSession = __webpack_exports__.deriveSessionIdFromMCPSession;
|
|
96
115
|
exports.getSessionId = __webpack_exports__.getSessionId;
|
|
97
116
|
exports.getSessionInfo = __webpack_exports__.getSessionInfo;
|
|
98
117
|
exports.newSessionId = __webpack_exports__.newSessionId;
|
|
99
118
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
119
|
+
"deriveSessionIdFromConversation",
|
|
100
120
|
"deriveSessionIdFromMCPSession",
|
|
101
121
|
"getSessionId",
|
|
102
122
|
"getSessionInfo",
|
|
@@ -9,27 +9,42 @@ function newSessionId() {
|
|
|
9
9
|
function deriveSessionIdFromMCPSession(mcpSessionId) {
|
|
10
10
|
return deterministicPrefixedId('ses', mcpSessionId);
|
|
11
11
|
}
|
|
12
|
-
function
|
|
12
|
+
function deriveSessionIdFromConversation(conversationId) {
|
|
13
|
+
return deterministicPrefixedId('ses', conversationId);
|
|
14
|
+
}
|
|
15
|
+
function getSessionId(server, extra, conversationId) {
|
|
13
16
|
const data = getServerTrackingData(server);
|
|
14
17
|
if (!data) throw new Error('Server tracking data not found');
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
data.sessionInfo.clientVersion = token.clientVersion;
|
|
22
|
-
data.sessionInfo.protocolVersion = token.protocolVersion;
|
|
23
|
-
sessionId = token.sessionId;
|
|
24
|
-
} else if (extra?.sessionId) {
|
|
25
|
-
data.sessionSource = 'mcp';
|
|
26
|
-
sessionId = deriveSessionIdFromMCPSession(extra.sessionId);
|
|
27
|
-
} else sessionId = getSessionIdFromMemory(data);
|
|
18
|
+
if (conversationId) {
|
|
19
|
+
applyTokenClientIdentity(data, extra);
|
|
20
|
+
return deriveSessionIdFromConversation(conversationId);
|
|
21
|
+
}
|
|
22
|
+
const carriedByRequest = readSessionIdFromRequest(data, extra);
|
|
23
|
+
const sessionId = carriedByRequest ?? getSessionIdFromMemory(data);
|
|
28
24
|
data.sessionId = sessionId;
|
|
29
25
|
data.lastActivity = new Date();
|
|
30
26
|
setServerTrackingData(server, data);
|
|
31
27
|
return sessionId;
|
|
32
28
|
}
|
|
29
|
+
function applyTokenClientIdentity(data, extra) {
|
|
30
|
+
const token = decodeSessionId(readMcpSessionHeader(extra?.requestInfo?.headers));
|
|
31
|
+
if (!token) return;
|
|
32
|
+
data.sessionInfo.clientName = token.clientName;
|
|
33
|
+
data.sessionInfo.clientVersion = token.clientVersion;
|
|
34
|
+
data.sessionInfo.protocolVersion = token.protocolVersion;
|
|
35
|
+
return token;
|
|
36
|
+
}
|
|
37
|
+
function readSessionIdFromRequest(data, extra) {
|
|
38
|
+
const token = applyTokenClientIdentity(data, extra);
|
|
39
|
+
if (token) {
|
|
40
|
+
data.sessionSource = 'token';
|
|
41
|
+
return token.sessionId;
|
|
42
|
+
}
|
|
43
|
+
if (extra?.sessionId) {
|
|
44
|
+
data.sessionSource = 'mcp';
|
|
45
|
+
return deriveSessionIdFromMCPSession(extra.sessionId);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
33
48
|
function getSessionIdFromMemory(data) {
|
|
34
49
|
const timeoutMs = 60 * INACTIVITY_TIMEOUT_IN_MINUTES * 1000;
|
|
35
50
|
const isStale = Date.now() - data.lastActivity.getTime() > timeoutMs;
|
|
@@ -59,6 +74,8 @@ function getSessionInfo(server, data, sessionId) {
|
|
|
59
74
|
if (!data) return sessionInfo;
|
|
60
75
|
data.sessionInfo = sessionInfo;
|
|
61
76
|
setServerTrackingData(server, data);
|
|
62
|
-
return
|
|
77
|
+
return {
|
|
78
|
+
...sessionInfo
|
|
79
|
+
};
|
|
63
80
|
}
|
|
64
|
-
export { deriveSessionIdFromMCPSession, getSessionId, getSessionInfo, newSessionId };
|
|
81
|
+
export { deriveSessionIdFromConversation, deriveSessionIdFromMCPSession, getSessionId, getSessionInfo, newSessionId };
|
package/dist/types.d.ts
CHANGED
|
@@ -46,7 +46,21 @@ export interface MCPAnalyticsOptions {
|
|
|
46
46
|
* detected under the same name.
|
|
47
47
|
*/
|
|
48
48
|
missingCapabilityToolName?: string;
|
|
49
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* Opt in to session correlation for the MCP **2026-07-28** revision, which removed
|
|
51
|
+
* protocol-level sessions: no `initialize`, no `mcp-session-id` header, and a fresh
|
|
52
|
+
* server instance per HTTP request. With none of those left to anchor on, the only
|
|
53
|
+
* thing that can carry a session across calls is the agent itself.
|
|
54
|
+
*
|
|
55
|
+
* Turning this on injects a `conversation_id` parameter into every tool, mints one
|
|
56
|
+
* on the first call, asks the agent to echo it back, and derives `$session_id` from
|
|
57
|
+
* that handle — so calls correlate across reconnects, restarts, and per-request
|
|
58
|
+
* instances.
|
|
59
|
+
*
|
|
60
|
+
* Off by default, and fully inert when off: no parameter is injected, no schema is
|
|
61
|
+
* touched, no prompt-back is appended, and `$session_id` resolves exactly as it did
|
|
62
|
+
* before (the request's own session id, else this instance's).
|
|
63
|
+
*/
|
|
50
64
|
enableConversationId?: boolean;
|
|
51
65
|
/**
|
|
52
66
|
* Emit a `$exception` event alongside any failed tool call. Defaults to `true`.
|
|
@@ -96,6 +110,8 @@ export type RegisteredTool = {
|
|
|
96
110
|
/** MCP tool `_meta` block (spec-allowed arbitrary metadata, e.g. `category`). */
|
|
97
111
|
_meta?: Record<string, unknown>;
|
|
98
112
|
inputSchema?: unknown;
|
|
113
|
+
/** Present when the tool was registered with a declared output schema. */
|
|
114
|
+
outputSchema?: unknown;
|
|
99
115
|
update?: (...args: unknown[]) => unknown;
|
|
100
116
|
} & ({
|
|
101
117
|
callback: ToolCallback;
|
|
@@ -243,6 +259,12 @@ export interface SessionInfo {
|
|
|
243
259
|
export interface AnalyticsParameterOwnership {
|
|
244
260
|
context: boolean;
|
|
245
261
|
conversationId: boolean;
|
|
262
|
+
/**
|
|
263
|
+
* True when we declared `_mcp_instructions` on this tool's advertised output
|
|
264
|
+
* schema, so writing that key into `structuredContent` will validate. False
|
|
265
|
+
* for tools with no output schema, or one we could not extend.
|
|
266
|
+
*/
|
|
267
|
+
outputInstructions: boolean;
|
|
246
268
|
}
|
|
247
269
|
export interface MCPAnalyticsData {
|
|
248
270
|
sink: McpEventSink | undefined;
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAA;AACxE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAClD,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAA;AACrE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAA;AAC1D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAA;AACtE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAEnD,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AAEhD,+FAA+F;AAC/F,MAAM,MAAM,eAAe,GAAG,aAAa,CAAC,eAAe,CAAA;AAC3D,qEAAqE;AACrE,MAAM,MAAM,UAAU,GAAG,aAAa,CAAC,UAAU,CAAA;AAEjD,MAAM,WAAW,oBAAoB;IACnC,SAAS,CAAC,EAAE,UAAU,CAAA;IACtB,IAAI,CAAC,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D,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,gFAAgF;AAChF,MAAM,WAAW,oBAAqB,SAAQ,gBAAgB;IAC5D;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;IACpB,2EAA2E;IAC3E,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,yEAAyE;IACzE,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,gDAAgD;IAChD,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,6DAA6D;IAC7D,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,kFAAkF;IAClF,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,0FAA0F;IAC1F,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,sDAAsD;AACtD,MAAM,WAAW,sBAAsB;IACrC;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,GAAG,0BAA0B,CAAA;IAC9C;;;;OAIG;IACH,aAAa,CAAC,EAAE,OAAO,CAAA;CACxB;AAED;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB;IAC/B,sEAAsE;IACtE,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,4EAA4E;IAC5E,YAAY,CAAC,EAAE,wBAAwB,CAAA;IACvC,uEAAuE;IACvE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC9B,6DAA6D;IAC7D,mBAAmB,EAAE,OAAO,CAAA;CAC7B;AAED,gGAAgG;AAChG,MAAM,WAAW,4BAA6B,SAAQ,gBAAgB;IACpE;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,2EAA2E;IAC3E,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB"}
|
package/dist/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const version = "0.10.
|
|
1
|
+
export declare const version = "0.10.8";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
package/dist/version.js
CHANGED
|
@@ -26,7 +26,7 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
26
26
|
__webpack_require__.d(__webpack_exports__, {
|
|
27
27
|
version: ()=>version
|
|
28
28
|
});
|
|
29
|
-
const version = '0.10.
|
|
29
|
+
const version = '0.10.8';
|
|
30
30
|
exports.version = __webpack_exports__.version;
|
|
31
31
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
32
32
|
"version"
|
package/dist/version.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const version = '0.10.
|
|
1
|
+
const version = '0.10.8';
|
|
2
2
|
export { version };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@posthog/mcp",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.8",
|
|
4
4
|
"bugs": {
|
|
5
5
|
"url": "https://github.com/PostHog/posthog-js/issues"
|
|
6
6
|
},
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"module": "dist/index.mjs",
|
|
29
29
|
"types": "dist/index.d.ts",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@posthog/core": "^1.46.
|
|
31
|
+
"@posthog/core": "^1.46.9"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@babel/preset-env": "^7.27.1",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"@types/node": "^20.0.0",
|
|
40
40
|
"jest": "29.7.0",
|
|
41
41
|
"zod": "^3.25.0",
|
|
42
|
-
"posthog-node": "^5.
|
|
42
|
+
"posthog-node": "^5.48.1",
|
|
43
43
|
"@posthog-tooling/tsconfig-base": "1.1.1"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { AnalyticsParameterOwnership } from '../types'
|
|
2
2
|
import { getObjectShape, isZodRawShapeCompat } from './mcp-sdk-compat'
|
|
3
|
+
import { canDeclareOutputInstructions } from './output-instructions'
|
|
3
4
|
|
|
4
5
|
const JSON_SCHEMA_KEYWORDS = [
|
|
5
6
|
'$defs',
|
|
@@ -68,14 +69,26 @@ export function analyticsOwnsParameter(inputSchema: unknown, parameterName: stri
|
|
|
68
69
|
return canInjectAnalyticsParameter(schema, parameterName)
|
|
69
70
|
}
|
|
70
71
|
|
|
71
|
-
export function getAnalyticsParameterOwnership(
|
|
72
|
+
export function getAnalyticsParameterOwnership(
|
|
73
|
+
inputSchema: unknown,
|
|
74
|
+
outputSchema?: unknown
|
|
75
|
+
): AnalyticsParameterOwnership {
|
|
72
76
|
return {
|
|
73
77
|
context: analyticsOwnsParameter(inputSchema, 'context'),
|
|
74
78
|
conversationId: analyticsOwnsParameter(inputSchema, 'conversation_id'),
|
|
79
|
+
outputInstructions: canDeclareOutputInstructions(outputSchema),
|
|
75
80
|
}
|
|
76
81
|
}
|
|
77
82
|
|
|
78
|
-
|
|
83
|
+
/**
|
|
84
|
+
* Removes the arguments we injected, leaving the host's own. Input-side only, so
|
|
85
|
+
* it takes just those two flags rather than the whole ownership record — callers
|
|
86
|
+
* that have no output-schema context should not have to invent a value for it.
|
|
87
|
+
*/
|
|
88
|
+
export function stripOwnedAnalyticsArguments(
|
|
89
|
+
args: unknown,
|
|
90
|
+
ownership: Pick<AnalyticsParameterOwnership, 'context' | 'conversationId'>
|
|
91
|
+
): unknown {
|
|
79
92
|
let cleanedArgs = args
|
|
80
93
|
if (ownership.context && cleanedArgs && typeof cleanedArgs === 'object' && 'context' in cleanedArgs) {
|
|
81
94
|
const { context: _context, ...rest } = cleanedArgs
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// Copyright (c) 2025 AgentCat, Inc. (formerly MCPcat)
|
|
4
4
|
// Licensed under the MIT License: https://github.com/agentcathq/agentcat-typescript-sdk/blob/main/LICENSE
|
|
5
5
|
|
|
6
|
-
import type { MCPServerLike, McpEvent } from '../types'
|
|
6
|
+
import type { MCPServerLike, McpEvent, SessionInfo } from '../types'
|
|
7
7
|
import { MCPAnalyticsEventType } from './event-types'
|
|
8
8
|
import { getServerTrackingData } from './internal'
|
|
9
9
|
import type { LoggerFn } from './logger'
|
|
@@ -19,7 +19,12 @@ import { getSessionInfo } from './session'
|
|
|
19
19
|
* Auto-capture callers (tool calls, listings, identify) intentionally ignore the
|
|
20
20
|
* return value, keeping the tool path isolated from analytics latency/errors.
|
|
21
21
|
*/
|
|
22
|
-
export function captureEvent(
|
|
22
|
+
export function captureEvent(
|
|
23
|
+
server: MCPServerLike,
|
|
24
|
+
eventInput: McpEvent,
|
|
25
|
+
logger: LoggerFn,
|
|
26
|
+
requestAttribution?: SessionInfo
|
|
27
|
+
): Promise<void> | undefined {
|
|
23
28
|
const data = getServerTrackingData(server)
|
|
24
29
|
if (!data) {
|
|
25
30
|
logger('Warning: Server tracking data not found. Event will not be published.')
|
|
@@ -31,7 +36,10 @@ export function captureEvent(server: MCPServerLike, eventInput: McpEvent, logger
|
|
|
31
36
|
return
|
|
32
37
|
}
|
|
33
38
|
|
|
34
|
-
|
|
39
|
+
// Instrumented request paths pass an immutable snapshot resolved before any
|
|
40
|
+
// user callback or handler can yield to a concurrent request. Custom capture
|
|
41
|
+
// has no request context, so it intentionally falls back to current state.
|
|
42
|
+
const sessionInfo = requestAttribution ?? getSessionInfo(server, data, eventInput.sessionId)
|
|
35
43
|
|
|
36
44
|
const duration =
|
|
37
45
|
eventInput.duration || (eventInput.timestamp ? Date.now() - eventInput.timestamp.getTime() : undefined)
|
|
@@ -48,9 +56,8 @@ export function captureEvent(server: MCPServerLike, eventInput: McpEvent, logger
|
|
|
48
56
|
sdkVersion: sessionInfo.sdkVersion,
|
|
49
57
|
serverName: sessionInfo.serverName,
|
|
50
58
|
serverVersion: sessionInfo.serverVersion,
|
|
51
|
-
// Prefer
|
|
52
|
-
//
|
|
53
|
-
// request from another client can't misattribute this one.
|
|
59
|
+
// Prefer client metadata stamped onto this event from the request's `_meta`
|
|
60
|
+
// over sessionInfo, so concurrent stateless requests stay isolated.
|
|
54
61
|
clientName: eventInput.clientName ?? sessionInfo.clientName,
|
|
55
62
|
clientVersion: eventInput.clientVersion ?? sessionInfo.clientVersion,
|
|
56
63
|
identifyActorGivenId: sessionInfo.identifyActorGivenId,
|