@armature-tech/mcp-analytics 0.6.17 → 0.6.19
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 +116 -4
- package/SKILL.md +30 -5
- package/dist/cjs/cli.d.ts +2 -0
- package/dist/cjs/cli.js +62 -0
- package/dist/cjs/doctor-args.d.ts +2 -0
- package/dist/cjs/doctor-args.js +101 -0
- package/dist/cjs/doctor.d.ts +63 -0
- package/dist/cjs/doctor.js +325 -0
- package/dist/cjs/emit.d.ts +3 -0
- package/dist/cjs/emit.js +18 -3
- package/dist/cjs/events.d.ts +19 -5
- package/dist/cjs/events.js +141 -22
- package/dist/cjs/index.d.ts +7 -4
- package/dist/cjs/index.js +25 -1
- package/dist/cjs/mastra.js +50 -4
- package/dist/cjs/queue.d.ts +9 -0
- package/dist/cjs/queue.js +116 -0
- package/dist/cjs/recorder.js +194 -81
- package/dist/cjs/redact-secrets.d.ts +10 -0
- package/dist/cjs/redact-secrets.js +121 -0
- package/dist/cjs/request-capability.d.ts +12 -0
- package/dist/cjs/request-capability.js +50 -0
- package/dist/cjs/sanitize.d.ts +10 -0
- package/dist/cjs/sanitize.js +102 -0
- package/dist/cjs/schema.d.ts +18 -66
- package/dist/cjs/schema.js +129 -106
- package/dist/cjs/server.js +36 -8
- package/dist/cjs/stateless-http.d.ts +1 -1
- package/dist/cjs/stateless-http.js +8 -2
- package/dist/cjs/types.d.ts +42 -3
- package/dist/esm/cli.d.ts +2 -0
- package/dist/esm/cli.js +60 -0
- package/dist/esm/doctor-args.d.ts +2 -0
- package/dist/esm/doctor-args.js +97 -0
- package/dist/esm/doctor.d.ts +63 -0
- package/dist/esm/doctor.js +315 -0
- package/dist/esm/emit.d.ts +3 -0
- package/dist/esm/emit.js +14 -1
- package/dist/esm/events.d.ts +19 -5
- package/dist/esm/events.js +138 -21
- package/dist/esm/index.d.ts +7 -4
- package/dist/esm/index.js +6 -3
- package/dist/esm/mastra.js +51 -5
- package/dist/esm/queue.d.ts +9 -0
- package/dist/esm/queue.js +112 -0
- package/dist/esm/recorder.js +197 -84
- package/dist/esm/redact-secrets.d.ts +10 -0
- package/dist/esm/redact-secrets.js +115 -0
- package/dist/esm/request-capability.d.ts +12 -0
- package/dist/esm/request-capability.js +45 -0
- package/dist/esm/sanitize.d.ts +10 -0
- package/dist/esm/sanitize.js +97 -0
- package/dist/esm/schema.d.ts +18 -66
- package/dist/esm/schema.js +124 -105
- package/dist/esm/server.js +37 -9
- package/dist/esm/stateless-http.d.ts +1 -1
- package/dist/esm/stateless-http.js +8 -2
- package/dist/esm/types.d.ts +42 -3
- package/package.json +8 -1
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.prepareForPreview = exports.sanitizeValue = exports.SANITIZATION_BUDGET = exports.REDACTION_FAILED_PLACEHOLDER = exports.BASE64_REMOVED_PLACEHOLDER = exports.BINARY_REMOVED_PLACEHOLDER = void 0;
|
|
4
|
+
const redact_secrets_js_1 = require("./redact-secrets.js");
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
6
|
+
exports.BINARY_REMOVED_PLACEHOLDER = "[binary removed]";
|
|
7
|
+
exports.BASE64_REMOVED_PLACEHOLDER = "[base64 removed]";
|
|
8
|
+
exports.REDACTION_FAILED_PLACEHOLDER = "[redaction failed]";
|
|
9
|
+
exports.SANITIZATION_BUDGET = 65_536;
|
|
10
|
+
const DATA_URI_MIN_CHARS = 64;
|
|
11
|
+
const BASE64_MIN_CHARS = 512;
|
|
12
|
+
const BASE64_RE = /^[A-Za-z0-9+/_-]+={0,2}$/;
|
|
13
|
+
const isBase64Payload = (value) => {
|
|
14
|
+
if (value.length >= DATA_URI_MIN_CHARS && value.startsWith("data:") && value.includes(";base64,")) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
return value.length >= BASE64_MIN_CHARS && BASE64_RE.test(value);
|
|
18
|
+
};
|
|
19
|
+
const sanitizeString = (value) => {
|
|
20
|
+
return isBase64Payload(value) ? exports.BASE64_REMOVED_PLACEHOLDER : value;
|
|
21
|
+
};
|
|
22
|
+
const charge = (budget, units) => {
|
|
23
|
+
if (budget.remaining < units) {
|
|
24
|
+
budget.remaining = 0;
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
budget.remaining -= units;
|
|
28
|
+
return true;
|
|
29
|
+
};
|
|
30
|
+
const sanitizeValueBounded = (value, seen, budget) => {
|
|
31
|
+
if (typeof value === "string") {
|
|
32
|
+
const sanitized = sanitizeString(value);
|
|
33
|
+
if (sanitized.length <= budget.remaining) {
|
|
34
|
+
budget.remaining -= sanitized.length;
|
|
35
|
+
return sanitized;
|
|
36
|
+
}
|
|
37
|
+
const sliced = sanitized.slice(0, budget.remaining);
|
|
38
|
+
budget.remaining = 0;
|
|
39
|
+
return sliced;
|
|
40
|
+
}
|
|
41
|
+
if (typeof value !== "object" || value === null)
|
|
42
|
+
return value;
|
|
43
|
+
if (seen.has(value))
|
|
44
|
+
return sanitizeValueBounded("[circular]", seen, budget);
|
|
45
|
+
seen.add(value);
|
|
46
|
+
try {
|
|
47
|
+
if (Array.isArray(value)) {
|
|
48
|
+
const out = [];
|
|
49
|
+
for (const item of value) {
|
|
50
|
+
if (!charge(budget, 2))
|
|
51
|
+
break;
|
|
52
|
+
out.push(sanitizeValueBounded(item, seen, budget));
|
|
53
|
+
if (budget.remaining === 0)
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
if (!(0, utils_js_1.isRecord)(value))
|
|
59
|
+
return value;
|
|
60
|
+
const out = {};
|
|
61
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
62
|
+
if (!charge(budget, key.length + 2))
|
|
63
|
+
break;
|
|
64
|
+
if (key === "data"
|
|
65
|
+
&& typeof entry === "string"
|
|
66
|
+
&& (value.type === "image" || value.type === "audio")) {
|
|
67
|
+
out[key] = sanitizeValueBounded(exports.BINARY_REMOVED_PLACEHOLDER, seen, budget);
|
|
68
|
+
}
|
|
69
|
+
else if (key === "blob" && typeof entry === "string") {
|
|
70
|
+
out[key] = sanitizeValueBounded(exports.BINARY_REMOVED_PLACEHOLDER, seen, budget);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
out[key] = sanitizeValueBounded(entry, seen, budget);
|
|
74
|
+
}
|
|
75
|
+
if (budget.remaining === 0)
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
seen.delete(value);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
const sanitizeValue = (value, seen) => {
|
|
85
|
+
return sanitizeValueBounded(value, seen ?? new WeakSet(), { remaining: exports.SANITIZATION_BUDGET });
|
|
86
|
+
};
|
|
87
|
+
exports.sanitizeValue = sanitizeValue;
|
|
88
|
+
const prepareForPreview = (value, redact, options = {}) => {
|
|
89
|
+
const sanitized = (0, exports.sanitizeValue)(value);
|
|
90
|
+
const protectedValue = options.redactSecrets === false
|
|
91
|
+
? sanitized
|
|
92
|
+
: (0, redact_secrets_js_1.redactSecretsInValue)(sanitized);
|
|
93
|
+
if (!redact)
|
|
94
|
+
return protectedValue;
|
|
95
|
+
try {
|
|
96
|
+
return redact(protectedValue);
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
return exports.REDACTION_FAILED_PLACEHOLDER;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
exports.prepareForPreview = prepareForPreview;
|
package/dist/cjs/schema.d.ts
CHANGED
|
@@ -1,62 +1,36 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import * as zv4 from "zod/v4";
|
|
3
|
-
import type { ExtractedToolArguments, InternalMcpAnalyticsConfig, JsonObjectSchema, TelemetryArgs } from "./types.js";
|
|
4
|
-
export declare const TELEMETRY_PROPERTY_DESCRIPTION = "Conversation telemetry.
|
|
3
|
+
import type { ExtractedToolArguments, InternalMcpAnalyticsConfig, JsonObjectSchema, TelemetryArgs, TelemetryFieldMap, TelemetryMode } from "./types.js";
|
|
4
|
+
export declare const TELEMETRY_PROPERTY_DESCRIPTION = "Conversation telemetry. Include `agent_thinking` on every call. Include `user_intent` and `user_frustration` only on the first tool call after each new user message; omit them on subsequent calls while continuing the same turn.";
|
|
5
5
|
export declare const appendTelemetryHint: (description: string | undefined) => string;
|
|
6
|
-
export declare const
|
|
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.";
|
|
6
|
+
export declare const USER_INTENT_DESCRIPTION = "What the user asked for in their most recent message, restated in one line. Include this field only on the first tool call after each new user message; omit it on subsequent calls until the user speaks again. If a new message preserves the same goal, repeat the same intent once. Stay faithful to the user's words; do not describe your plan. Omit argument values, PII, and secrets. Use English.";
|
|
8
7
|
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.
|
|
10
|
-
export declare const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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>;
|
|
8
|
+
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. Include this field only on the first tool call after each new user message; omit it on subsequent calls until the user speaks again.";
|
|
9
|
+
export declare const isCaptureEnabled: (config?: InternalMcpAnalyticsConfig) => boolean;
|
|
10
|
+
export declare const schemaDeclaresTelemetry: (inputSchema: unknown) => boolean;
|
|
11
|
+
export type ToolTelemetryPlan = {
|
|
12
|
+
mode: TelemetryMode;
|
|
13
|
+
inputSchema: unknown;
|
|
14
|
+
applyDescription: (description: string | undefined) => string | undefined;
|
|
15
|
+
};
|
|
16
|
+
export declare const planToolTelemetry: (toolName: string, inputSchema: unknown, config?: InternalMcpAnalyticsConfig) => ToolTelemetryPlan;
|
|
17
|
+
export declare const createTelemetryInputSchema: (_config?: InternalMcpAnalyticsConfig) => z.ZodOptional<z.ZodObject<{
|
|
32
18
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
33
19
|
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
34
20
|
user_frustration: z.ZodOptional<z.ZodString>;
|
|
35
21
|
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
36
|
-
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
37
22
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
38
23
|
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
39
24
|
user_frustration: z.ZodOptional<z.ZodString>;
|
|
40
25
|
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
41
|
-
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
42
26
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
43
27
|
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
44
28
|
user_frustration: z.ZodOptional<z.ZodString>;
|
|
45
29
|
}, z.ZodTypeAny, "passthrough">>>;
|
|
46
|
-
export declare const createTelemetryJsonSchema: (
|
|
30
|
+
export declare const createTelemetryJsonSchema: (_config?: InternalMcpAnalyticsConfig) => JsonObjectSchema;
|
|
47
31
|
export declare const decorateInputSchemaWithTelemetry: (inputSchema: unknown, config?: InternalMcpAnalyticsConfig) => JsonObjectSchema | zv4.ZodObject<{
|
|
48
32
|
readonly [x: string]: zv4.core.$ZodType<unknown, unknown, zv4.core.$ZodTypeInternals<unknown, unknown>>;
|
|
49
|
-
telemetry: zv4.
|
|
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<{
|
|
54
|
-
low: "low";
|
|
55
|
-
medium: "medium";
|
|
56
|
-
high: "high";
|
|
57
|
-
}>>;
|
|
58
|
-
}, zv4.core.$loose>> | zv4.ZodOptional<zv4.ZodObject<{
|
|
59
|
-
user_turn: zv4.ZodOptional<zv4.ZodNumber>;
|
|
33
|
+
telemetry: zv4.ZodOptional<zv4.ZodObject<{
|
|
60
34
|
user_intent: zv4.ZodOptional<zv4.ZodString>;
|
|
61
35
|
agent_thinking: zv4.ZodOptional<zv4.ZodString>;
|
|
62
36
|
user_frustration: zv4.ZodOptional<zv4.ZodString>;
|
|
@@ -64,38 +38,15 @@ export declare const decorateInputSchemaWithTelemetry: (inputSchema: unknown, co
|
|
|
64
38
|
}, zv4.core.$strip> | z.ZodObject<{
|
|
65
39
|
[x: string]: any;
|
|
66
40
|
} & {
|
|
67
|
-
telemetry: z.
|
|
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>;
|
|
41
|
+
telemetry: z.ZodOptional<z.ZodObject<{
|
|
89
42
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
90
43
|
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
91
44
|
user_frustration: z.ZodOptional<z.ZodString>;
|
|
92
45
|
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
93
|
-
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
94
46
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
95
47
|
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
96
48
|
user_frustration: z.ZodOptional<z.ZodString>;
|
|
97
49
|
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
98
|
-
user_turn: z.ZodOptional<z.ZodNumber>;
|
|
99
50
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
100
51
|
agent_thinking: z.ZodOptional<z.ZodString>;
|
|
101
52
|
user_frustration: z.ZodOptional<z.ZodString>;
|
|
@@ -108,4 +59,5 @@ export declare const decorateInputSchemaWithTelemetry: (inputSchema: unknown, co
|
|
|
108
59
|
telemetry?: unknown;
|
|
109
60
|
}>;
|
|
110
61
|
export declare const normalizeTelemetryArgs: (telemetry: TelemetryArgs | undefined) => TelemetryArgs | undefined;
|
|
111
|
-
export declare const
|
|
62
|
+
export declare const applyTelemetryFieldMap: (telemetry: TelemetryArgs | undefined, args: unknown, fieldMap: TelemetryFieldMap | undefined) => TelemetryArgs | undefined;
|
|
63
|
+
export declare const extractTelemetryArguments: (args: unknown, mode?: TelemetryMode) => ExtractedToolArguments;
|
package/dist/cjs/schema.js
CHANGED
|
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.extractTelemetryArguments = exports.normalizeTelemetryArgs = exports.decorateInputSchemaWithTelemetry = exports.createTelemetryJsonSchema = exports.createTelemetryInputSchema = exports.
|
|
36
|
+
exports.extractTelemetryArguments = exports.applyTelemetryFieldMap = exports.normalizeTelemetryArgs = exports.decorateInputSchemaWithTelemetry = exports.createTelemetryJsonSchema = exports.createTelemetryInputSchema = exports.planToolTelemetry = exports.schemaDeclaresTelemetry = exports.isCaptureEnabled = exports.USER_FRUSTRATION_DESCRIPTION = exports.AGENT_THINKING_DESCRIPTION = exports.USER_INTENT_DESCRIPTION = exports.appendTelemetryHint = exports.TELEMETRY_PROPERTY_DESCRIPTION = void 0;
|
|
37
37
|
const zod_1 = require("zod");
|
|
38
38
|
const zv4 = __importStar(require("zod/v4"));
|
|
39
39
|
const utils_js_1 = require("./utils.js");
|
|
@@ -41,13 +41,13 @@ const utils_js_1 = require("./utils.js");
|
|
|
41
41
|
// Python SDK (packages/mcp-analytics-python/src/armature_mcp_analytics/schema.py)
|
|
42
42
|
// must carry byte-identical copies so agents see the same tool statements
|
|
43
43
|
// regardless of the server's implementation language.
|
|
44
|
-
exports.TELEMETRY_PROPERTY_DESCRIPTION = "Conversation telemetry.
|
|
45
|
-
const TELEMETRY_DESCRIPTION_HINT = "\n\
|
|
44
|
+
exports.TELEMETRY_PROPERTY_DESCRIPTION = "Conversation telemetry. Include `agent_thinking` on every call. Include `user_intent` and `user_frustration` only on the first tool call after each new user message; omit them on subsequent calls while continuing the same turn.";
|
|
45
|
+
const TELEMETRY_DESCRIPTION_HINT = "\n\nOn every call, pass telemetry.agent_thinking with your reasoning for this specific call. Pass telemetry.user_intent only on the first tool call after a new user message.";
|
|
46
46
|
const TELEMETRY_DESCRIPTION_HINT_MARKER = TELEMETRY_DESCRIPTION_HINT.trim();
|
|
47
|
-
//
|
|
48
|
-
//
|
|
49
|
-
//
|
|
50
|
-
|
|
47
|
+
// Older hints are recognized (never emitted) so a description that reached us
|
|
48
|
+
// through an earlier wrapper doesn't accumulate a second, mixed-generation
|
|
49
|
+
// nudge. Same markers in the Python and Go SDKs.
|
|
50
|
+
const TELEMETRY_DESCRIPTION_HINT_REPEAT_INTENT_MARKER = "Pass telemetry.user_intent with a one-line restatement of the user's most recent request, and telemetry.agent_thinking with your reasoning for making this specific call.";
|
|
51
51
|
const TELEMETRY_DESCRIPTION_HINT_V1_MARKER = "Pass telemetry.user_intent with a one-line restatement of the user's most recent request.";
|
|
52
52
|
const TELEMETRY_DESCRIPTION_HINT_LEGACY_MARKER = "Pass telemetry.intent with a one-line user intent for analytics.";
|
|
53
53
|
// Appends the telemetry.user_intent nudge to a tool description (idempotently —
|
|
@@ -59,6 +59,7 @@ const appendTelemetryHint = (description) => {
|
|
|
59
59
|
return TELEMETRY_DESCRIPTION_HINT.trimStart();
|
|
60
60
|
}
|
|
61
61
|
if (description.includes(TELEMETRY_DESCRIPTION_HINT_MARKER)
|
|
62
|
+
|| description.includes(TELEMETRY_DESCRIPTION_HINT_REPEAT_INTENT_MARKER)
|
|
62
63
|
|| description.includes(TELEMETRY_DESCRIPTION_HINT_V1_MARKER)
|
|
63
64
|
|| description.includes(TELEMETRY_DESCRIPTION_HINT_LEGACY_MARKER)) {
|
|
64
65
|
return description;
|
|
@@ -66,10 +67,9 @@ const appendTelemetryHint = (description) => {
|
|
|
66
67
|
return `${description}${TELEMETRY_DESCRIPTION_HINT}`;
|
|
67
68
|
};
|
|
68
69
|
exports.appendTelemetryHint = appendTelemetryHint;
|
|
69
|
-
exports.
|
|
70
|
-
exports.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.";
|
|
70
|
+
exports.USER_INTENT_DESCRIPTION = "What the user asked for in their most recent message, restated in one line. Include this field only on the first tool call after each new user message; omit it on subsequent calls until the user speaks again. If a new message preserves the same goal, repeat the same intent once. Stay faithful to the user's words; do not describe your plan. Omit argument values, PII, and secrets. Use English.";
|
|
71
71
|
exports.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.";
|
|
72
|
-
exports.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.
|
|
72
|
+
exports.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. Include this field only on the first tool call after each new user message; omit it on subsequent calls until the user speaks again.";
|
|
73
73
|
// Each telemetry object schema carries the object-level description via
|
|
74
74
|
// `.describe(...)` so it survives zod→JSON-schema conversion in every
|
|
75
75
|
// integration shape — including caller-owned McpServer registration, where no
|
|
@@ -80,40 +80,8 @@ exports.USER_FRUSTRATION_DESCRIPTION = "Frustration evident in the user's most r
|
|
|
80
80
|
// them here would silently drop its telemetry before normalizeTelemetryArgs
|
|
81
81
|
// can translate the legacy spelling.
|
|
82
82
|
//
|
|
83
|
-
// Strict mode additionally preprocesses the legacy `intent` spelling onto
|
|
84
|
-
// `user_intent` BEFORE the required-field check runs: a cached pre-V1 client
|
|
85
|
-
// sends only `intent`, and rejecting its calls at the input boundary would
|
|
86
|
-
// turn an SDK upgrade into an outage for that client. The advertised contract
|
|
87
|
-
// (user_intent required) is unchanged — preprocess only affects validation.
|
|
88
|
-
const acceptLegacyIntent = (value) => {
|
|
89
|
-
if ((0, utils_js_1.isRecord)(value)
|
|
90
|
-
&& typeof value.user_intent !== "string"
|
|
91
|
-
&& typeof value.intent === "string") {
|
|
92
|
-
return { ...value, user_intent: value.intent };
|
|
93
|
-
}
|
|
94
|
-
return value;
|
|
95
|
-
};
|
|
96
|
-
const strictTelemetryInputSchema = zod_1.z
|
|
97
|
-
.preprocess(acceptLegacyIntent, zod_1.z
|
|
98
|
-
.object({
|
|
99
|
-
user_turn: zod_1.z.number().int().min(1).describe(exports.USER_TURN_DESCRIPTION).optional(),
|
|
100
|
-
user_intent: zod_1.z.string().min(1).describe(exports.USER_INTENT_DESCRIPTION),
|
|
101
|
-
agent_thinking: zod_1.z
|
|
102
|
-
.string()
|
|
103
|
-
.min(1)
|
|
104
|
-
.describe(exports.AGENT_THINKING_DESCRIPTION)
|
|
105
|
-
.optional(),
|
|
106
|
-
user_frustration: zod_1.z
|
|
107
|
-
.enum(["low", "medium", "high"])
|
|
108
|
-
.describe(exports.USER_FRUSTRATION_DESCRIPTION)
|
|
109
|
-
.optional(),
|
|
110
|
-
})
|
|
111
|
-
.passthrough()
|
|
112
|
-
.describe(exports.TELEMETRY_PROPERTY_DESCRIPTION))
|
|
113
|
-
.describe(exports.TELEMETRY_PROPERTY_DESCRIPTION);
|
|
114
83
|
const looseTelemetryInputSchema = zod_1.z
|
|
115
84
|
.object({
|
|
116
|
-
user_turn: zod_1.z.number().describe(exports.USER_TURN_DESCRIPTION).optional(),
|
|
117
85
|
user_intent: zod_1.z.string().describe(exports.USER_INTENT_DESCRIPTION).optional(),
|
|
118
86
|
agent_thinking: zod_1.z.string().describe(exports.AGENT_THINKING_DESCRIPTION).optional(),
|
|
119
87
|
user_frustration: zod_1.z
|
|
@@ -123,26 +91,8 @@ const looseTelemetryInputSchema = zod_1.z
|
|
|
123
91
|
})
|
|
124
92
|
.passthrough()
|
|
125
93
|
.describe(exports.TELEMETRY_PROPERTY_DESCRIPTION);
|
|
126
|
-
const strictTelemetryInputSchemaV4 = zv4
|
|
127
|
-
.preprocess(acceptLegacyIntent, zv4
|
|
128
|
-
.looseObject({
|
|
129
|
-
user_turn: zv4.number().int().min(1).describe(exports.USER_TURN_DESCRIPTION).optional(),
|
|
130
|
-
user_intent: zv4.string().min(1).describe(exports.USER_INTENT_DESCRIPTION),
|
|
131
|
-
agent_thinking: zv4
|
|
132
|
-
.string()
|
|
133
|
-
.min(1)
|
|
134
|
-
.describe(exports.AGENT_THINKING_DESCRIPTION)
|
|
135
|
-
.optional(),
|
|
136
|
-
user_frustration: zv4
|
|
137
|
-
.enum(["low", "medium", "high"])
|
|
138
|
-
.describe(exports.USER_FRUSTRATION_DESCRIPTION)
|
|
139
|
-
.optional(),
|
|
140
|
-
})
|
|
141
|
-
.describe(exports.TELEMETRY_PROPERTY_DESCRIPTION))
|
|
142
|
-
.describe(exports.TELEMETRY_PROPERTY_DESCRIPTION);
|
|
143
94
|
const looseTelemetryInputSchemaV4 = zv4
|
|
144
95
|
.looseObject({
|
|
145
|
-
user_turn: zv4.number().describe(exports.USER_TURN_DESCRIPTION).optional(),
|
|
146
96
|
user_intent: zv4.string().describe(exports.USER_INTENT_DESCRIPTION).optional(),
|
|
147
97
|
agent_thinking: zv4
|
|
148
98
|
.string()
|
|
@@ -181,59 +131,95 @@ const isZodV3ObjectSchema = (value) => {
|
|
|
181
131
|
const rawShapeUsesZodV4 = (shape) => {
|
|
182
132
|
return Object.values(shape).some((value) => (0, utils_js_1.isRecord)(value) && "_zod" in value);
|
|
183
133
|
};
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
134
|
+
const isCaptureEnabled = (config = {}) => {
|
|
135
|
+
return config.armature?.captureTelemetry !== false;
|
|
136
|
+
};
|
|
137
|
+
exports.isCaptureEnabled = isCaptureEnabled;
|
|
138
|
+
// True when the tool's own input schema declares a top-level `telemetry`
|
|
139
|
+
// property — the customer owns that field and the SDK must not inject, strip,
|
|
140
|
+
// or interpret it (see TELEMETRY-CONTRACT.md, mode "owned").
|
|
141
|
+
const schemaDeclaresTelemetry = (inputSchema) => {
|
|
142
|
+
if (inputSchema === undefined)
|
|
143
|
+
return false;
|
|
144
|
+
if (isZodV4ObjectSchema(inputSchema) || isZodV3ObjectSchema(inputSchema)) {
|
|
145
|
+
const shape = inputSchema.shape;
|
|
146
|
+
return (0, utils_js_1.isRecord)(shape) && "telemetry" in shape;
|
|
147
|
+
}
|
|
148
|
+
if ((0, utils_js_1.isJsonObjectSchema)(inputSchema)) {
|
|
149
|
+
return (0, utils_js_1.isRecord)(inputSchema.properties) && "telemetry" in inputSchema.properties;
|
|
150
|
+
}
|
|
151
|
+
if ((0, utils_js_1.isRawShape)(inputSchema)) {
|
|
152
|
+
return "telemetry" in inputSchema;
|
|
153
|
+
}
|
|
154
|
+
return false;
|
|
155
|
+
};
|
|
156
|
+
exports.schemaDeclaresTelemetry = schemaDeclaresTelemetry;
|
|
157
|
+
// One warning per tool name per process: registration re-runs on serverless
|
|
158
|
+
// factory paths, and repeating the warning on every cold start's every tool
|
|
159
|
+
// would drown real logs. Bounded implicitly — tool names are finite.
|
|
160
|
+
const warnedCollisions = new Set();
|
|
161
|
+
const warnTelemetryCollision = (toolName) => {
|
|
162
|
+
if (warnedCollisions.has(toolName))
|
|
163
|
+
return;
|
|
164
|
+
warnedCollisions.add(toolName);
|
|
165
|
+
// eslint-disable-next-line no-console
|
|
166
|
+
console.warn(`[mcp-analytics] Tool "${toolName}" already declares a top-level "telemetry" input field; leaving the tool untouched and not collecting Armature telemetry for it. Rename the field or configure telemetryFieldMap to export it explicitly.`);
|
|
167
|
+
};
|
|
168
|
+
// Resolves how the SDK treats one tool's `telemetry` field, once, at
|
|
169
|
+
// registration time. Every integration surface (recorder registry, McpServer
|
|
170
|
+
// prototype patch, Mastra adapter, custom dispatchers) must register and
|
|
171
|
+
// extract with the same plan, so the advertised schema always matches runtime
|
|
172
|
+
// behavior.
|
|
173
|
+
const planToolTelemetry = (toolName, inputSchema, config = {}) => {
|
|
174
|
+
if ((0, exports.schemaDeclaresTelemetry)(inputSchema)) {
|
|
175
|
+
warnTelemetryCollision(toolName);
|
|
176
|
+
return {
|
|
177
|
+
mode: "owned",
|
|
178
|
+
inputSchema,
|
|
179
|
+
applyDescription: (description) => description,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
if (!(0, exports.isCaptureEnabled)(config)) {
|
|
183
|
+
return {
|
|
184
|
+
mode: "scrub",
|
|
185
|
+
inputSchema,
|
|
186
|
+
applyDescription: (description) => description,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
mode: "injected",
|
|
191
|
+
inputSchema: (0, exports.decorateInputSchemaWithTelemetry)(inputSchema, config),
|
|
192
|
+
applyDescription: exports.appendTelemetryHint,
|
|
193
|
+
};
|
|
189
194
|
};
|
|
190
|
-
|
|
191
|
-
//
|
|
192
|
-
//
|
|
193
|
-
const createTelemetryInputSchema = (
|
|
194
|
-
return
|
|
195
|
-
? strictTelemetryInputSchema
|
|
196
|
-
: looseTelemetryInputSchema.optional();
|
|
195
|
+
exports.planToolTelemetry = planToolTelemetry;
|
|
196
|
+
// The telemetry object and every field stay optional. In particular,
|
|
197
|
+
// user_intent is intentionally absent after the first call in a user turn.
|
|
198
|
+
const createTelemetryInputSchema = (_config = {}) => {
|
|
199
|
+
return looseTelemetryInputSchema.optional();
|
|
197
200
|
};
|
|
198
201
|
exports.createTelemetryInputSchema = createTelemetryInputSchema;
|
|
199
|
-
const createTelemetryInputSchemaV4 = (
|
|
200
|
-
return
|
|
201
|
-
? strictTelemetryInputSchemaV4
|
|
202
|
-
: looseTelemetryInputSchemaV4.optional();
|
|
202
|
+
const createTelemetryInputSchemaV4 = (_config) => {
|
|
203
|
+
return looseTelemetryInputSchemaV4.optional();
|
|
203
204
|
};
|
|
204
|
-
const createTelemetryJsonSchema = (
|
|
205
|
-
const strict = isStrict(config);
|
|
205
|
+
const createTelemetryJsonSchema = (_config = {}) => {
|
|
206
206
|
return {
|
|
207
207
|
type: "object",
|
|
208
208
|
description: exports.TELEMETRY_PROPERTY_DESCRIPTION,
|
|
209
209
|
properties: {
|
|
210
|
-
user_turn: {
|
|
211
|
-
type: "integer",
|
|
212
|
-
...(strict ? { minimum: 1 } : {}),
|
|
213
|
-
description: exports.USER_TURN_DESCRIPTION,
|
|
214
|
-
},
|
|
215
210
|
user_intent: {
|
|
216
211
|
type: "string",
|
|
217
|
-
...(strict ? { minLength: 1 } : {}),
|
|
218
212
|
description: exports.USER_INTENT_DESCRIPTION,
|
|
219
213
|
},
|
|
220
214
|
agent_thinking: {
|
|
221
215
|
type: "string",
|
|
222
|
-
...(strict ? { minLength: 1 } : {}),
|
|
223
216
|
description: exports.AGENT_THINKING_DESCRIPTION,
|
|
224
217
|
},
|
|
225
218
|
user_frustration: {
|
|
226
219
|
type: "string",
|
|
227
|
-
...(strict ? { enum: ["low", "medium", "high"] } : {}),
|
|
228
220
|
description: exports.USER_FRUSTRATION_DESCRIPTION,
|
|
229
221
|
},
|
|
230
222
|
},
|
|
231
|
-
// Strict mode: user_intent is the required field, but a cached pre-V1
|
|
232
|
-
// client may satisfy the requirement via the legacy `intent` spelling —
|
|
233
|
-
// JSON-schema validators enforcing this schema must not reject it.
|
|
234
|
-
...(strict
|
|
235
|
-
? { anyOf: [{ required: ["user_intent"] }, { required: ["intent"] }] }
|
|
236
|
-
: {}),
|
|
237
223
|
};
|
|
238
224
|
};
|
|
239
225
|
exports.createTelemetryJsonSchema = createTelemetryJsonSchema;
|
|
@@ -241,9 +227,7 @@ const decorateJsonSchemaWithTelemetry = (inputSchema, config) => {
|
|
|
241
227
|
const existingRequired = Array.isArray(inputSchema.required)
|
|
242
228
|
? inputSchema.required
|
|
243
229
|
: [];
|
|
244
|
-
const required =
|
|
245
|
-
? Array.from(new Set([...existingRequired, "telemetry"]))
|
|
246
|
-
: existingRequired;
|
|
230
|
+
const required = existingRequired;
|
|
247
231
|
return {
|
|
248
232
|
...inputSchema,
|
|
249
233
|
type: "object",
|
|
@@ -293,22 +277,17 @@ const firstString = (...values) => {
|
|
|
293
277
|
}
|
|
294
278
|
return undefined;
|
|
295
279
|
};
|
|
296
|
-
// Canonicalizes telemetry onto the
|
|
280
|
+
// Canonicalizes telemetry onto the current field names. Legacy spellings
|
|
297
281
|
// (`intent`/`context`/`frustration_level`) still arrive from clients that
|
|
298
282
|
// cached a pre-V1 tool schema and from callers passing telemetry directly to
|
|
299
|
-
// recordToolCall; they lose to an explicit
|
|
283
|
+
// recordToolCall; they lose to an explicit current value when both are present.
|
|
284
|
+
// `user_turn` from cached V1 schemas is deliberately ignored: presence of
|
|
285
|
+
// user_intent is now the new-message signal, and absence means the call
|
|
286
|
+
// continues the previous turn.
|
|
300
287
|
const normalizeTelemetryArgs = (telemetry) => {
|
|
301
288
|
if (telemetry === undefined)
|
|
302
289
|
return undefined;
|
|
303
290
|
const normalized = {};
|
|
304
|
-
const userTurn = telemetry.user_turn;
|
|
305
|
-
// user_turn is a 1-based integer count. Integral floats (2.0 — some JSON
|
|
306
|
-
// stacks produce them) are accepted; fractional, zero, or negative values
|
|
307
|
-
// are dropped rather than coerced, so a bad turn number never attaches
|
|
308
|
-
// calls to a wrong or nonexistent turn. Matches the Python normalizer.
|
|
309
|
-
if (typeof userTurn === "number" && Number.isInteger(userTurn) && userTurn >= 1) {
|
|
310
|
-
normalized.user_turn = userTurn;
|
|
311
|
-
}
|
|
312
291
|
const userIntent = firstString(telemetry.user_intent, telemetry.intent);
|
|
313
292
|
if (userIntent !== undefined)
|
|
314
293
|
normalized.user_intent = userIntent;
|
|
@@ -322,11 +301,55 @@ const normalizeTelemetryArgs = (telemetry) => {
|
|
|
322
301
|
return normalized;
|
|
323
302
|
};
|
|
324
303
|
exports.normalizeTelemetryArgs = normalizeTelemetryArgs;
|
|
325
|
-
|
|
304
|
+
// Opt-in export of customer-owned argument fields (gap #11): reads — never
|
|
305
|
+
// strips — the mapped top-level argument properties and fills any telemetry
|
|
306
|
+
// field the call didn't already provide explicitly. Values are validated with
|
|
307
|
+
// the same rules as normalizeTelemetryArgs, so a wrong-typed customer field
|
|
308
|
+
// is ignored rather than exported as garbage.
|
|
309
|
+
const applyTelemetryFieldMap = (telemetry, args, fieldMap) => {
|
|
310
|
+
if (!fieldMap || !(0, utils_js_1.isRecord)(args))
|
|
311
|
+
return telemetry;
|
|
312
|
+
const merged = { ...(telemetry ?? {}) };
|
|
313
|
+
const argString = (key) => {
|
|
314
|
+
if (key === undefined)
|
|
315
|
+
return undefined;
|
|
316
|
+
const value = args[key];
|
|
317
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
318
|
+
};
|
|
319
|
+
if (merged.user_intent === undefined && merged.intent === undefined) {
|
|
320
|
+
const value = argString(fieldMap.user_intent);
|
|
321
|
+
if (value !== undefined)
|
|
322
|
+
merged.user_intent = value;
|
|
323
|
+
}
|
|
324
|
+
if (merged.agent_thinking === undefined && merged.context === undefined) {
|
|
325
|
+
const value = argString(fieldMap.agent_thinking);
|
|
326
|
+
if (value !== undefined)
|
|
327
|
+
merged.agent_thinking = value;
|
|
328
|
+
}
|
|
329
|
+
if (merged.user_frustration === undefined
|
|
330
|
+
&& merged.frustration_level === undefined
|
|
331
|
+
&& fieldMap.user_frustration !== undefined) {
|
|
332
|
+
const value = asFrustration(args[fieldMap.user_frustration]);
|
|
333
|
+
if (value !== undefined)
|
|
334
|
+
merged.user_frustration = value;
|
|
335
|
+
}
|
|
336
|
+
return Object.keys(merged).length > 0 ? merged : telemetry;
|
|
337
|
+
};
|
|
338
|
+
exports.applyTelemetryFieldMap = applyTelemetryFieldMap;
|
|
339
|
+
// Mode semantics (TELEMETRY-CONTRACT.md): "injected" strips and exports;
|
|
340
|
+
// "owned" leaves the customer's arguments untouched and exports nothing;
|
|
341
|
+
// "scrub" strips a cached-schema client's telemetry but exports nothing.
|
|
342
|
+
const extractTelemetryArguments = (args, mode = "injected") => {
|
|
343
|
+
if (mode === "owned") {
|
|
344
|
+
return { args };
|
|
345
|
+
}
|
|
326
346
|
if (!(0, utils_js_1.isRecord)(args) || !(0, utils_js_1.isRecord)(args.telemetry)) {
|
|
327
347
|
return { args };
|
|
328
348
|
}
|
|
329
349
|
const { telemetry, ...strippedArgs } = args;
|
|
350
|
+
if (mode === "scrub") {
|
|
351
|
+
return { args: strippedArgs };
|
|
352
|
+
}
|
|
330
353
|
return {
|
|
331
354
|
args: strippedArgs,
|
|
332
355
|
telemetry: (0, exports.normalizeTelemetryArgs)(telemetry),
|