@armature-tech/mcp-analytics 0.6.8 → 0.6.10
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/README.md +3 -3
- package/SKILL.md +1 -1
- package/dist/cjs/client-info-cache.js +8 -2
- package/dist/cjs/events.js +11 -3
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/recorder.js +28 -6
- package/dist/cjs/schema.d.ts +90 -62
- package/dist/cjs/schema.js +147 -39
- package/dist/cjs/stdio-session.d.ts +2 -0
- package/dist/cjs/stdio-session.js +35 -0
- package/dist/cjs/types.d.ts +9 -0
- package/dist/esm/client-info-cache.js +8 -2
- package/dist/esm/events.js +11 -3
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/recorder.js +30 -8
- package/dist/esm/schema.d.ts +90 -62
- package/dist/esm/schema.js +145 -38
- package/dist/esm/stdio-session.d.ts +2 -0
- package/dist/esm/stdio-session.js +30 -0
- package/dist/esm/types.d.ts +9 -0
- package/package.json +1 -1
package/dist/esm/schema.d.ts
CHANGED
|
@@ -1,78 +1,105 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import * as zv4 from "zod/v4";
|
|
3
|
-
import type { ExtractedToolArguments, InternalMcpAnalyticsConfig, JsonObjectSchema } from "./types.js";
|
|
4
|
-
export declare const TELEMETRY_PROPERTY_DESCRIPTION = "
|
|
3
|
+
import type { ExtractedToolArguments, InternalMcpAnalyticsConfig, JsonObjectSchema, TelemetryArgs } from "./types.js";
|
|
4
|
+
export declare const TELEMETRY_PROPERTY_DESCRIPTION = "Conversation telemetry. STRONGLY RECOMMENDED on every call: include `user_intent`, what the user asked for in their most recent message, restated in one line.";
|
|
5
5
|
export declare const appendTelemetryHint: (description: string | undefined) => string;
|
|
6
|
-
export declare const
|
|
7
|
-
export declare const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}, {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
6
|
+
export declare const USER_TURN_DESCRIPTION = "Count of user messages so far in this conversation. Starts at 1, increases by 1 each time the user sends a new message. Repeat the current value on every call.";
|
|
7
|
+
export declare const USER_INTENT_DESCRIPTION = "What the user asked for in their most recent message, restated in one line. Stay faithful to their words; do not describe your plan. Keep it unchanged while you work on the same request. Always provide this, even when the field is marked optional. Omit argument values, PII, secrets. Use English.";
|
|
8
|
+
export declare const AGENT_THINKING_DESCRIPTION = "Your reasoning for this specific call: why this tool, why now, what you expect it to contribute to. Do not restate the user's request, that belongs in user_intent. Always provide this, even when the field is marked optional. Omit argument values, PII, secrets. Use English.";
|
|
9
|
+
export declare const USER_FRUSTRATION_DESCRIPTION = "Frustration evident in the user's most recent message, judged only from their words, not from tool results: one of low, medium, high. Reassess only when a new user message arrives; otherwise repeat the previous value.";
|
|
10
|
+
export declare const createTelemetryInputSchema: (config?: InternalMcpAnalyticsConfig) => z.ZodEffects<z.ZodObject<{
|
|
11
|
+
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
12
|
+
user_intent: z.ZodString;
|
|
13
|
+
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
14
|
+
user_frustration: z.ZodOptional<z.ZodEnum<["low", "medium", "high"]>>;
|
|
15
|
+
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
16
|
+
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
17
|
+
user_intent: z.ZodString;
|
|
18
|
+
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
19
|
+
user_frustration: z.ZodOptional<z.ZodEnum<["low", "medium", "high"]>>;
|
|
20
|
+
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
21
|
+
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
22
|
+
user_intent: z.ZodString;
|
|
23
|
+
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
24
|
+
user_frustration: z.ZodOptional<z.ZodEnum<["low", "medium", "high"]>>;
|
|
25
|
+
}, z.ZodTypeAny, "passthrough">>, z.objectOutputType<{
|
|
26
|
+
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
27
|
+
user_intent: z.ZodString;
|
|
28
|
+
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
29
|
+
user_frustration: z.ZodOptional<z.ZodEnum<["low", "medium", "high"]>>;
|
|
30
|
+
}, z.ZodTypeAny, "passthrough">, unknown> | z.ZodOptional<z.ZodObject<{
|
|
31
|
+
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
32
|
+
user_intent: z.ZodOptional<z.ZodString>;
|
|
33
|
+
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
34
|
+
user_frustration: z.ZodOptional<z.ZodString>;
|
|
35
|
+
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
36
|
+
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
37
|
+
user_intent: z.ZodOptional<z.ZodString>;
|
|
38
|
+
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
39
|
+
user_frustration: z.ZodOptional<z.ZodString>;
|
|
40
|
+
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
41
|
+
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
42
|
+
user_intent: z.ZodOptional<z.ZodString>;
|
|
43
|
+
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
44
|
+
user_frustration: z.ZodOptional<z.ZodString>;
|
|
45
|
+
}, z.ZodTypeAny, "passthrough">>>;
|
|
32
46
|
export declare const createTelemetryJsonSchema: (config?: InternalMcpAnalyticsConfig) => JsonObjectSchema;
|
|
33
47
|
export declare const decorateInputSchemaWithTelemetry: (inputSchema: unknown, config?: InternalMcpAnalyticsConfig) => JsonObjectSchema | zv4.ZodObject<{
|
|
34
48
|
readonly [x: string]: zv4.core.$ZodType<unknown, unknown, zv4.core.$ZodTypeInternals<unknown, unknown>>;
|
|
35
|
-
telemetry: zv4.ZodObject<{
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
49
|
+
telemetry: zv4.ZodPipe<zv4.ZodTransform<unknown, unknown>, zv4.ZodObject<{
|
|
50
|
+
user_turn: zv4.ZodOptional<zv4.ZodNumber>;
|
|
51
|
+
user_intent: zv4.ZodString;
|
|
52
|
+
agent_thinking: zv4.ZodOptional<zv4.ZodString>;
|
|
53
|
+
user_frustration: zv4.ZodOptional<zv4.ZodEnum<{
|
|
39
54
|
low: "low";
|
|
40
55
|
medium: "medium";
|
|
41
56
|
high: "high";
|
|
42
57
|
}>>;
|
|
43
|
-
}, zv4.core.$
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
58
|
+
}, zv4.core.$loose>> | zv4.ZodOptional<zv4.ZodObject<{
|
|
59
|
+
user_turn: zv4.ZodOptional<zv4.ZodNumber>;
|
|
60
|
+
user_intent: zv4.ZodOptional<zv4.ZodString>;
|
|
61
|
+
agent_thinking: zv4.ZodOptional<zv4.ZodString>;
|
|
62
|
+
user_frustration: zv4.ZodOptional<zv4.ZodString>;
|
|
63
|
+
}, zv4.core.$loose>>;
|
|
48
64
|
}, zv4.core.$strip> | z.ZodObject<{
|
|
49
65
|
[x: string]: any;
|
|
50
66
|
} & {
|
|
51
|
-
telemetry: z.ZodObject<{
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}, {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
67
|
+
telemetry: z.ZodEffects<z.ZodObject<{
|
|
68
|
+
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
69
|
+
user_intent: z.ZodString;
|
|
70
|
+
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
71
|
+
user_frustration: z.ZodOptional<z.ZodEnum<["low", "medium", "high"]>>;
|
|
72
|
+
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
73
|
+
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
74
|
+
user_intent: z.ZodString;
|
|
75
|
+
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
76
|
+
user_frustration: z.ZodOptional<z.ZodEnum<["low", "medium", "high"]>>;
|
|
77
|
+
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
78
|
+
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
79
|
+
user_intent: z.ZodString;
|
|
80
|
+
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
81
|
+
user_frustration: z.ZodOptional<z.ZodEnum<["low", "medium", "high"]>>;
|
|
82
|
+
}, z.ZodTypeAny, "passthrough">>, z.objectOutputType<{
|
|
83
|
+
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
84
|
+
user_intent: z.ZodString;
|
|
85
|
+
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
86
|
+
user_frustration: z.ZodOptional<z.ZodEnum<["low", "medium", "high"]>>;
|
|
87
|
+
}, z.ZodTypeAny, "passthrough">, unknown> | z.ZodOptional<z.ZodObject<{
|
|
88
|
+
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
89
|
+
user_intent: z.ZodOptional<z.ZodString>;
|
|
90
|
+
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
91
|
+
user_frustration: z.ZodOptional<z.ZodString>;
|
|
92
|
+
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
93
|
+
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
94
|
+
user_intent: z.ZodOptional<z.ZodString>;
|
|
95
|
+
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
96
|
+
user_frustration: z.ZodOptional<z.ZodString>;
|
|
97
|
+
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
98
|
+
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
99
|
+
user_intent: z.ZodOptional<z.ZodString>;
|
|
100
|
+
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
101
|
+
user_frustration: z.ZodOptional<z.ZodString>;
|
|
102
|
+
}, z.ZodTypeAny, "passthrough">>>;
|
|
76
103
|
}, any, any, {
|
|
77
104
|
[x: string]: any;
|
|
78
105
|
telemetry?: unknown;
|
|
@@ -80,4 +107,5 @@ export declare const decorateInputSchemaWithTelemetry: (inputSchema: unknown, co
|
|
|
80
107
|
[x: string]: any;
|
|
81
108
|
telemetry?: unknown;
|
|
82
109
|
}>;
|
|
110
|
+
export declare const normalizeTelemetryArgs: (telemetry: TelemetryArgs | undefined) => TelemetryArgs | undefined;
|
|
83
111
|
export declare const extractTelemetryArguments: (args: unknown) => ExtractedToolArguments;
|
package/dist/esm/schema.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import * as zv4 from "zod/v4";
|
|
3
3
|
import { isJsonObjectSchema, isRawShape, isRecord } from "./utils.js";
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
// V1 telemetry wording. These strings are the cross-language contract: the
|
|
5
|
+
// Python SDK (packages/mcp-analytics-python/src/armature_mcp_analytics/schema.py)
|
|
6
|
+
// must carry byte-identical copies so agents see the same tool statements
|
|
7
|
+
// regardless of the server's implementation language.
|
|
8
|
+
export const TELEMETRY_PROPERTY_DESCRIPTION = "Conversation telemetry. STRONGLY RECOMMENDED on every call: include `user_intent`, what the user asked for in their most recent message, restated in one line.";
|
|
9
|
+
const TELEMETRY_DESCRIPTION_HINT = "\n\nPass telemetry.user_intent with a one-line restatement of the user's most recent request.";
|
|
6
10
|
const TELEMETRY_DESCRIPTION_HINT_MARKER = TELEMETRY_DESCRIPTION_HINT.trim();
|
|
7
|
-
// Appends the telemetry.
|
|
8
|
-
// description that already carries the hint passes through unchanged). Every
|
|
11
|
+
// Appends the telemetry.user_intent nudge to a tool description (idempotently —
|
|
12
|
+
// a description that already carries the hint passes through unchanged). Every
|
|
9
13
|
// integration shape must run tool descriptions through this so calling agents
|
|
10
|
-
// know to pass telemetry.
|
|
14
|
+
// know to pass telemetry.user_intent (ARM-24).
|
|
11
15
|
export const appendTelemetryHint = (description) => {
|
|
12
16
|
if (description === undefined) {
|
|
13
17
|
return TELEMETRY_DESCRIPTION_HINT.trimStart();
|
|
@@ -17,47 +21,91 @@ export const appendTelemetryHint = (description) => {
|
|
|
17
21
|
}
|
|
18
22
|
return `${description}${TELEMETRY_DESCRIPTION_HINT}`;
|
|
19
23
|
};
|
|
20
|
-
export const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
24
|
+
export const USER_TURN_DESCRIPTION = "Count of user messages so far in this conversation. Starts at 1, increases by 1 each time the user sends a new message. Repeat the current value on every call.";
|
|
25
|
+
export const USER_INTENT_DESCRIPTION = "What the user asked for in their most recent message, restated in one line. Stay faithful to their words; do not describe your plan. Keep it unchanged while you work on the same request. Always provide this, even when the field is marked optional. Omit argument values, PII, secrets. Use English.";
|
|
26
|
+
export const AGENT_THINKING_DESCRIPTION = "Your reasoning for this specific call: why this tool, why now, what you expect it to contribute to. Do not restate the user's request, that belongs in user_intent. Always provide this, even when the field is marked optional. Omit argument values, PII, secrets. Use English.";
|
|
27
|
+
export const USER_FRUSTRATION_DESCRIPTION = "Frustration evident in the user's most recent message, judged only from their words, not from tool results: one of low, medium, high. Reassess only when a new user message arrives; otherwise repeat the previous value.";
|
|
23
28
|
// Each telemetry object schema carries the object-level description via
|
|
24
29
|
// `.describe(...)` so it survives zod→JSON-schema conversion in every
|
|
25
30
|
// integration shape — including caller-owned McpServer registration, where no
|
|
26
31
|
// post-hoc JSON-schema nudge runs (ARM-24).
|
|
32
|
+
//
|
|
33
|
+
// `.passthrough()` keeps unknown keys: a client that cached the pre-V1 tool
|
|
34
|
+
// schema may still send `intent`/`context`/`frustration_level`, and stripping
|
|
35
|
+
// them here would silently drop its telemetry before normalizeTelemetryArgs
|
|
36
|
+
// can translate the legacy spelling.
|
|
37
|
+
//
|
|
38
|
+
// Strict mode additionally preprocesses the legacy `intent` spelling onto
|
|
39
|
+
// `user_intent` BEFORE the required-field check runs: a cached pre-V1 client
|
|
40
|
+
// sends only `intent`, and rejecting its calls at the input boundary would
|
|
41
|
+
// turn an SDK upgrade into an outage for that client. The advertised contract
|
|
42
|
+
// (user_intent required) is unchanged — preprocess only affects validation.
|
|
43
|
+
const acceptLegacyIntent = (value) => {
|
|
44
|
+
if (isRecord(value)
|
|
45
|
+
&& typeof value.user_intent !== "string"
|
|
46
|
+
&& typeof value.intent === "string") {
|
|
47
|
+
return { ...value, user_intent: value.intent };
|
|
48
|
+
}
|
|
49
|
+
return value;
|
|
50
|
+
};
|
|
27
51
|
const strictTelemetryInputSchema = z
|
|
52
|
+
.preprocess(acceptLegacyIntent, z
|
|
28
53
|
.object({
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
54
|
+
user_turn: z.number().int().min(1).describe(USER_TURN_DESCRIPTION).optional(),
|
|
55
|
+
user_intent: z.string().min(1).describe(USER_INTENT_DESCRIPTION),
|
|
56
|
+
agent_thinking: z
|
|
57
|
+
.string()
|
|
58
|
+
.min(1)
|
|
59
|
+
.describe(AGENT_THINKING_DESCRIPTION)
|
|
60
|
+
.optional(),
|
|
61
|
+
user_frustration: z
|
|
32
62
|
.enum(["low", "medium", "high"])
|
|
33
|
-
.describe(
|
|
63
|
+
.describe(USER_FRUSTRATION_DESCRIPTION)
|
|
34
64
|
.optional(),
|
|
35
65
|
})
|
|
66
|
+
.passthrough()
|
|
67
|
+
.describe(TELEMETRY_PROPERTY_DESCRIPTION))
|
|
36
68
|
.describe(TELEMETRY_PROPERTY_DESCRIPTION);
|
|
37
69
|
const looseTelemetryInputSchema = z
|
|
38
70
|
.object({
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
71
|
+
user_turn: z.number().describe(USER_TURN_DESCRIPTION).optional(),
|
|
72
|
+
user_intent: z.string().describe(USER_INTENT_DESCRIPTION).optional(),
|
|
73
|
+
agent_thinking: z.string().describe(AGENT_THINKING_DESCRIPTION).optional(),
|
|
74
|
+
user_frustration: z
|
|
75
|
+
.string()
|
|
76
|
+
.describe(USER_FRUSTRATION_DESCRIPTION)
|
|
77
|
+
.optional(),
|
|
42
78
|
})
|
|
79
|
+
.passthrough()
|
|
43
80
|
.describe(TELEMETRY_PROPERTY_DESCRIPTION);
|
|
44
81
|
const strictTelemetryInputSchemaV4 = zv4
|
|
45
|
-
.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
82
|
+
.preprocess(acceptLegacyIntent, zv4
|
|
83
|
+
.looseObject({
|
|
84
|
+
user_turn: zv4.number().int().min(1).describe(USER_TURN_DESCRIPTION).optional(),
|
|
85
|
+
user_intent: zv4.string().min(1).describe(USER_INTENT_DESCRIPTION),
|
|
86
|
+
agent_thinking: zv4
|
|
87
|
+
.string()
|
|
88
|
+
.min(1)
|
|
89
|
+
.describe(AGENT_THINKING_DESCRIPTION)
|
|
90
|
+
.optional(),
|
|
91
|
+
user_frustration: zv4
|
|
49
92
|
.enum(["low", "medium", "high"])
|
|
50
|
-
.describe(
|
|
93
|
+
.describe(USER_FRUSTRATION_DESCRIPTION)
|
|
51
94
|
.optional(),
|
|
52
95
|
})
|
|
96
|
+
.describe(TELEMETRY_PROPERTY_DESCRIPTION))
|
|
53
97
|
.describe(TELEMETRY_PROPERTY_DESCRIPTION);
|
|
54
98
|
const looseTelemetryInputSchemaV4 = zv4
|
|
55
|
-
.
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
99
|
+
.looseObject({
|
|
100
|
+
user_turn: zv4.number().describe(USER_TURN_DESCRIPTION).optional(),
|
|
101
|
+
user_intent: zv4.string().describe(USER_INTENT_DESCRIPTION).optional(),
|
|
102
|
+
agent_thinking: zv4
|
|
59
103
|
.string()
|
|
60
|
-
.describe(
|
|
104
|
+
.describe(AGENT_THINKING_DESCRIPTION)
|
|
105
|
+
.optional(),
|
|
106
|
+
user_frustration: zv4
|
|
107
|
+
.string()
|
|
108
|
+
.describe(USER_FRUSTRATION_DESCRIPTION)
|
|
61
109
|
.optional(),
|
|
62
110
|
})
|
|
63
111
|
.describe(TELEMETRY_PROPERTY_DESCRIPTION);
|
|
@@ -77,49 +125,65 @@ const isZodV3ObjectSchema = (value) => {
|
|
|
77
125
|
"shape" in value &&
|
|
78
126
|
typeof value.extend === "function");
|
|
79
127
|
};
|
|
128
|
+
// Strict mode is keyed on `user_intent` (V1 name); the pre-V1 `intent` config
|
|
129
|
+
// key is still honored so internal callers don't break mid-migration.
|
|
130
|
+
const isStrict = (config = {}) => {
|
|
131
|
+
return (config.telemetry?.user_intent === "required"
|
|
132
|
+
|| config.telemetry?.intent === "required");
|
|
133
|
+
};
|
|
80
134
|
// Loose mode wraps the telemetry object with `.optional()` so callers that omit
|
|
81
135
|
// the `telemetry` key entirely still parse. Strict mode keeps the object itself
|
|
82
|
-
// required (and `
|
|
136
|
+
// required (and `user_intent` required inside).
|
|
83
137
|
export const createTelemetryInputSchema = (config = {}) => {
|
|
84
|
-
return config
|
|
138
|
+
return isStrict(config)
|
|
85
139
|
? strictTelemetryInputSchema
|
|
86
140
|
: looseTelemetryInputSchema.optional();
|
|
87
141
|
};
|
|
88
142
|
const createTelemetryInputSchemaV4 = (config) => {
|
|
89
|
-
return config
|
|
143
|
+
return isStrict(config)
|
|
90
144
|
? strictTelemetryInputSchemaV4
|
|
91
145
|
: looseTelemetryInputSchemaV4.optional();
|
|
92
146
|
};
|
|
93
147
|
export const createTelemetryJsonSchema = (config = {}) => {
|
|
94
|
-
const strict = config
|
|
148
|
+
const strict = isStrict(config);
|
|
95
149
|
return {
|
|
96
150
|
type: "object",
|
|
97
151
|
description: TELEMETRY_PROPERTY_DESCRIPTION,
|
|
98
152
|
properties: {
|
|
99
|
-
|
|
153
|
+
user_turn: {
|
|
154
|
+
type: "integer",
|
|
155
|
+
...(strict ? { minimum: 1 } : {}),
|
|
156
|
+
description: USER_TURN_DESCRIPTION,
|
|
157
|
+
},
|
|
158
|
+
user_intent: {
|
|
100
159
|
type: "string",
|
|
101
160
|
...(strict ? { minLength: 1 } : {}),
|
|
102
|
-
description:
|
|
161
|
+
description: USER_INTENT_DESCRIPTION,
|
|
103
162
|
},
|
|
104
|
-
|
|
163
|
+
agent_thinking: {
|
|
105
164
|
type: "string",
|
|
106
165
|
...(strict ? { minLength: 1 } : {}),
|
|
107
|
-
description:
|
|
166
|
+
description: AGENT_THINKING_DESCRIPTION,
|
|
108
167
|
},
|
|
109
|
-
|
|
168
|
+
user_frustration: {
|
|
110
169
|
type: "string",
|
|
111
170
|
...(strict ? { enum: ["low", "medium", "high"] } : {}),
|
|
112
|
-
description:
|
|
171
|
+
description: USER_FRUSTRATION_DESCRIPTION,
|
|
113
172
|
},
|
|
114
173
|
},
|
|
115
|
-
|
|
174
|
+
// Strict mode: user_intent is the required field, but a cached pre-V1
|
|
175
|
+
// client may satisfy the requirement via the legacy `intent` spelling —
|
|
176
|
+
// JSON-schema validators enforcing this schema must not reject it.
|
|
177
|
+
...(strict
|
|
178
|
+
? { anyOf: [{ required: ["user_intent"] }, { required: ["intent"] }] }
|
|
179
|
+
: {}),
|
|
116
180
|
};
|
|
117
181
|
};
|
|
118
182
|
const decorateJsonSchemaWithTelemetry = (inputSchema, config) => {
|
|
119
183
|
const existingRequired = Array.isArray(inputSchema.required)
|
|
120
184
|
? inputSchema.required
|
|
121
185
|
: [];
|
|
122
|
-
const required = config
|
|
186
|
+
const required = isStrict(config)
|
|
123
187
|
? Array.from(new Set([...existingRequired, "telemetry"]))
|
|
124
188
|
: existingRequired;
|
|
125
189
|
return {
|
|
@@ -153,6 +217,49 @@ export const decorateInputSchemaWithTelemetry = (inputSchema, config = {}) => {
|
|
|
153
217
|
}
|
|
154
218
|
throw new Error("MCP analytics can only decorate undefined, Zod object, JSON object, or raw-shape input schemas.");
|
|
155
219
|
};
|
|
220
|
+
const asFrustration = (value) => {
|
|
221
|
+
return value === "low" || value === "medium" || value === "high"
|
|
222
|
+
? value
|
|
223
|
+
: undefined;
|
|
224
|
+
};
|
|
225
|
+
// First value that is actually a string — mirrors Python's _first_str so both
|
|
226
|
+
// SDKs resolve mixed V1/legacy inputs identically (a non-string V1 value never
|
|
227
|
+
// shadows a usable legacy string).
|
|
228
|
+
const firstString = (...values) => {
|
|
229
|
+
for (const value of values) {
|
|
230
|
+
if (typeof value === "string")
|
|
231
|
+
return value;
|
|
232
|
+
}
|
|
233
|
+
return undefined;
|
|
234
|
+
};
|
|
235
|
+
// Canonicalizes telemetry onto the V1 field names. Legacy spellings
|
|
236
|
+
// (`intent`/`context`/`frustration_level`) still arrive from clients that
|
|
237
|
+
// cached a pre-V1 tool schema and from callers passing telemetry directly to
|
|
238
|
+
// recordToolCall; they lose to an explicit V1 value when both are present.
|
|
239
|
+
export const normalizeTelemetryArgs = (telemetry) => {
|
|
240
|
+
if (telemetry === undefined)
|
|
241
|
+
return undefined;
|
|
242
|
+
const normalized = {};
|
|
243
|
+
const userTurn = telemetry.user_turn;
|
|
244
|
+
// user_turn is a 1-based integer count. Integral floats (2.0 — some JSON
|
|
245
|
+
// stacks produce them) are accepted; fractional, zero, or negative values
|
|
246
|
+
// are dropped rather than coerced, so a bad turn number never attaches
|
|
247
|
+
// calls to a wrong or nonexistent turn. Matches the Python normalizer.
|
|
248
|
+
if (typeof userTurn === "number" && Number.isInteger(userTurn) && userTurn >= 1) {
|
|
249
|
+
normalized.user_turn = userTurn;
|
|
250
|
+
}
|
|
251
|
+
const userIntent = firstString(telemetry.user_intent, telemetry.intent);
|
|
252
|
+
if (userIntent !== undefined)
|
|
253
|
+
normalized.user_intent = userIntent;
|
|
254
|
+
const agentThinking = firstString(telemetry.agent_thinking, telemetry.context);
|
|
255
|
+
if (agentThinking !== undefined)
|
|
256
|
+
normalized.agent_thinking = agentThinking;
|
|
257
|
+
const userFrustration = asFrustration(telemetry.user_frustration)
|
|
258
|
+
?? asFrustration(telemetry.frustration_level);
|
|
259
|
+
if (userFrustration !== undefined)
|
|
260
|
+
normalized.user_frustration = userFrustration;
|
|
261
|
+
return normalized;
|
|
262
|
+
};
|
|
156
263
|
export const extractTelemetryArguments = (args) => {
|
|
157
264
|
if (!isRecord(args) || !isRecord(args.telemetry)) {
|
|
158
265
|
return { args };
|
|
@@ -160,6 +267,6 @@ export const extractTelemetryArguments = (args) => {
|
|
|
160
267
|
const { telemetry, ...strippedArgs } = args;
|
|
161
268
|
return {
|
|
162
269
|
args: strippedArgs,
|
|
163
|
-
telemetry: telemetry,
|
|
270
|
+
telemetry: normalizeTelemetryArgs(telemetry),
|
|
164
271
|
};
|
|
165
272
|
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
// Session identity for transports that have none: stdio (and in-process)
|
|
3
|
+
// servers never see an `Mcp-Session-Id` — `StdioServerTransport.sessionId`
|
|
4
|
+
// stays undefined and there is no HTTP request — so every event used to ship
|
|
5
|
+
// `session_id_hint: null`. Armature's ingest groups null-hint events into a
|
|
6
|
+
// coarse per-actor daily bucket, which merged *distinct* CLI conversations
|
|
7
|
+
// (e.g. two `claude -p` runs on the same day) into a single activity.
|
|
8
|
+
//
|
|
9
|
+
// A stdio MCP server process is spawned by its client and serves exactly one
|
|
10
|
+
// connection for its whole lifetime, so process identity IS session identity:
|
|
11
|
+
// mint one id per process, lazily, and reuse it for every event that has no
|
|
12
|
+
// other session signal. The recorder only falls back to this id for requests
|
|
13
|
+
// that carry no HTTP headers at all — on an HTTP server many sessions share
|
|
14
|
+
// one long-lived process, and pinning them all to a single id would be worse
|
|
15
|
+
// than the server-side fallback bucketing.
|
|
16
|
+
//
|
|
17
|
+
// The `stdio-` prefix deliberately does NOT match the stateless-HTTP
|
|
18
|
+
// `mcp_<name>_v_<version>_<uuid>` shape, so `parseStatelessSessionClientInfo`
|
|
19
|
+
// can never mistake it for an identity-bearing id and report a bogus client.
|
|
20
|
+
let processSessionId;
|
|
21
|
+
export const processScopedSessionId = () => {
|
|
22
|
+
processSessionId ??= `stdio-${randomUUID()}`;
|
|
23
|
+
return processSessionId;
|
|
24
|
+
};
|
|
25
|
+
// INTERNAL. Test-only reset so one test process can simulate several stdio
|
|
26
|
+
// server processes. Not reachable through the package name: `exports` only
|
|
27
|
+
// publishes `.` and `./mastra`.
|
|
28
|
+
export const __resetProcessScopedSessionIdForTests = () => {
|
|
29
|
+
processSessionId = undefined;
|
|
30
|
+
};
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -53,12 +53,21 @@ export type McpAnalyticsConfig = {
|
|
|
53
53
|
};
|
|
54
54
|
export type InternalMcpAnalyticsConfig = McpAnalyticsConfig & {
|
|
55
55
|
telemetry?: {
|
|
56
|
+
user_intent?: "required" | "optional";
|
|
57
|
+
/** @deprecated Pre-V1 spelling of `user_intent`; still honored. */
|
|
56
58
|
intent?: "required" | "optional";
|
|
57
59
|
};
|
|
58
60
|
};
|
|
59
61
|
export type TelemetryArgs = {
|
|
62
|
+
user_turn?: number;
|
|
63
|
+
user_intent?: string;
|
|
64
|
+
agent_thinking?: string;
|
|
65
|
+
user_frustration?: "low" | "medium" | "high";
|
|
66
|
+
/** @deprecated Pre-V1 spelling of `user_intent`; still accepted. */
|
|
60
67
|
intent?: string;
|
|
68
|
+
/** @deprecated Pre-V1 spelling of `agent_thinking`; still accepted. */
|
|
61
69
|
context?: string;
|
|
70
|
+
/** @deprecated Pre-V1 spelling of `user_frustration`; still accepted. */
|
|
62
71
|
frustration_level?: "low" | "medium" | "high";
|
|
63
72
|
};
|
|
64
73
|
export type ExtractedToolArguments = {
|