@armature-tech/mcp-analytics 0.6.18 → 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 +99 -4
- package/SKILL.md +29 -4
- 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 +18 -5
- package/dist/cjs/events.js +127 -41
- package/dist/cjs/index.d.ts +6 -4
- package/dist/cjs/index.js +15 -1
- package/dist/cjs/mastra.js +42 -0
- package/dist/cjs/queue.d.ts +9 -0
- package/dist/cjs/queue.js +116 -0
- package/dist/cjs/recorder.js +157 -70
- 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 +5 -1
- package/dist/cjs/sanitize.js +51 -37
- package/dist/cjs/server.js +20 -0
- package/dist/cjs/types.d.ts +25 -1
- 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 +18 -5
- package/dist/esm/events.js +124 -40
- package/dist/esm/index.d.ts +6 -4
- package/dist/esm/index.js +5 -3
- package/dist/esm/mastra.js +42 -0
- package/dist/esm/queue.d.ts +9 -0
- package/dist/esm/queue.js +112 -0
- package/dist/esm/recorder.js +159 -72
- 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 +5 -1
- package/dist/esm/sanitize.js +50 -36
- package/dist/esm/server.js +20 -0
- package/dist/esm/types.d.ts +25 -1
- package/package.json +6 -1
package/dist/cjs/events.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.normalizeStartedAt = exports.normalizeRequestId = exports.normalizeSessionId = exports.buildSessionInitBatch = exports.buildBatch = exports.buildSessionInitEvent = exports.buildToolCallEvent = exports.buildEventId = exports.buildActorId = void 0;
|
|
3
|
+
exports.normalizeStartedAt = exports.normalizeRequestId = exports.normalizeSessionId = exports.buildSessionInitBatch = exports.buildBatch = exports.buildSessionInitEvent = exports.finalizeToolCallEvent = exports.buildToolCallEvent = exports.buildActorIdentityEvent = exports.buildEventId = exports.buildActorId = void 0;
|
|
4
4
|
const node_crypto_1 = require("node:crypto");
|
|
5
5
|
const schema_js_1 = require("./schema.js");
|
|
6
6
|
const sanitize_js_1 = require("./sanitize.js");
|
|
7
|
+
const redact_secrets_js_1 = require("./redact-secrets.js");
|
|
7
8
|
const utils_js_1 = require("./utils.js");
|
|
8
9
|
const trimOrUndefined = (value) => {
|
|
9
10
|
if (typeof value !== "string")
|
|
@@ -31,33 +32,95 @@ const buildEventId = ({ actorId, requestId, kind, }) => {
|
|
|
31
32
|
return (0, utils_js_1.sha256Hex)(`${actorId} ${kind} ${requestId}`);
|
|
32
33
|
};
|
|
33
34
|
exports.buildEventId = buildEventId;
|
|
35
|
+
const buildActorIdentityEvent = ({ actorId, identifier, startedAt, }) => {
|
|
36
|
+
return {
|
|
37
|
+
event_id: (0, exports.buildEventId)({ actorId, requestId: identifier, kind: "actor_identity" }),
|
|
38
|
+
kind: "actor_identity",
|
|
39
|
+
actor_id: actorId,
|
|
40
|
+
session_id_hint: null,
|
|
41
|
+
started_at: startedAt,
|
|
42
|
+
finished_at: startedAt,
|
|
43
|
+
duration_ms: 0,
|
|
44
|
+
ok: true,
|
|
45
|
+
error: null,
|
|
46
|
+
metadata: { identifier },
|
|
47
|
+
script_source: null,
|
|
48
|
+
script_source_truncated: false,
|
|
49
|
+
result_preview: null,
|
|
50
|
+
result_truncated: false,
|
|
51
|
+
calls: [],
|
|
52
|
+
logs: [],
|
|
53
|
+
search_calls: [],
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
exports.buildActorIdentityEvent = buildActorIdentityEvent;
|
|
34
57
|
const buildToolCallSource = (toolName, input) => {
|
|
35
58
|
return `MCP tool call: ${toolName}\n\nInput:\n${(0, utils_js_1.stringifyPreview)(input)}`;
|
|
36
59
|
};
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
60
|
+
const prepareTelemetry = (telemetry, redact, redactSecrets) => {
|
|
61
|
+
const normalized = (0, schema_js_1.normalizeTelemetryArgs)(telemetry);
|
|
62
|
+
if (!normalized)
|
|
63
|
+
return undefined;
|
|
64
|
+
const protectedTelemetry = {
|
|
65
|
+
...normalized,
|
|
66
|
+
...(typeof normalized.user_intent === "string"
|
|
67
|
+
? {
|
|
68
|
+
user_intent: redactSecrets
|
|
69
|
+
? (0, redact_secrets_js_1.redactSecretsInString)(normalized.user_intent)
|
|
70
|
+
: normalized.user_intent,
|
|
71
|
+
}
|
|
72
|
+
: {}),
|
|
73
|
+
...(typeof normalized.agent_thinking === "string"
|
|
74
|
+
? {
|
|
75
|
+
agent_thinking: redactSecrets
|
|
76
|
+
? (0, redact_secrets_js_1.redactSecretsInString)(normalized.agent_thinking)
|
|
77
|
+
: normalized.agent_thinking,
|
|
78
|
+
}
|
|
79
|
+
: {}),
|
|
80
|
+
};
|
|
81
|
+
if (!redact)
|
|
82
|
+
return protectedTelemetry;
|
|
43
83
|
try {
|
|
44
|
-
return (0, schema_js_1.normalizeTelemetryArgs)(redact(
|
|
84
|
+
return (0, schema_js_1.normalizeTelemetryArgs)(redact(protectedTelemetry));
|
|
45
85
|
}
|
|
46
86
|
catch {
|
|
47
87
|
return undefined;
|
|
48
88
|
}
|
|
49
89
|
};
|
|
50
|
-
const
|
|
51
|
-
if (errorMessage === undefined
|
|
52
|
-
return
|
|
90
|
+
const prepareErrorMessage = (errorMessage, redact, redactSecrets) => {
|
|
91
|
+
if (errorMessage === undefined)
|
|
92
|
+
return undefined;
|
|
93
|
+
const protectedMessage = redactSecrets
|
|
94
|
+
? (0, redact_secrets_js_1.redactSecretsInString)(errorMessage)
|
|
95
|
+
: errorMessage;
|
|
96
|
+
if (!redact)
|
|
97
|
+
return protectedMessage;
|
|
53
98
|
try {
|
|
54
|
-
const redacted = redact(
|
|
99
|
+
const redacted = redact(protectedMessage);
|
|
55
100
|
return typeof redacted === "string" ? redacted : (0, utils_js_1.stringifyPreview)(redacted);
|
|
56
101
|
}
|
|
57
102
|
catch {
|
|
58
103
|
return sanitize_js_1.REDACTION_FAILED_PLACEHOLDER;
|
|
59
104
|
}
|
|
60
105
|
};
|
|
106
|
+
const prepareToolCallCandidate = ({ toolName, telemetry, input, output, status, durationMs, errorMessage, sessionId, redact, redactSecrets = true, }) => {
|
|
107
|
+
const preparedTelemetry = prepareTelemetry(telemetry, redact, redactSecrets);
|
|
108
|
+
return {
|
|
109
|
+
kind: "tool_call",
|
|
110
|
+
toolName,
|
|
111
|
+
status,
|
|
112
|
+
durationMs,
|
|
113
|
+
...(sessionId ? { sessionId } : {}),
|
|
114
|
+
input: (0, sanitize_js_1.prepareForPreview)(input, redact, { redactSecrets }),
|
|
115
|
+
...(output === undefined
|
|
116
|
+
? {}
|
|
117
|
+
: { output: (0, sanitize_js_1.prepareForPreview)(output, redact, { redactSecrets }) }),
|
|
118
|
+
...(errorMessage === undefined
|
|
119
|
+
? {}
|
|
120
|
+
: { errorMessage: prepareErrorMessage(errorMessage, redact, redactSecrets) }),
|
|
121
|
+
...(preparedTelemetry === undefined ? {} : { telemetry: preparedTelemetry }),
|
|
122
|
+
};
|
|
123
|
+
};
|
|
61
124
|
// Marks an event as synthetic traffic from an Armature workflow run so
|
|
62
125
|
// Session Analytics can exclude it. Spread first in the event literal —
|
|
63
126
|
// absent when the call did not originate from a workflow run.
|
|
@@ -66,35 +129,26 @@ const workflowStamp = (workflowRunId) => {
|
|
|
66
129
|
return {};
|
|
67
130
|
return { is_workflow: true, workflow_run_id: workflowRunId };
|
|
68
131
|
};
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
// error string, and the telemetry text.
|
|
74
|
-
const safeInput = (0, sanitize_js_1.prepareForPreview)(input, redact);
|
|
75
|
-
const safeOutput = output === undefined
|
|
76
|
-
? undefined
|
|
77
|
-
: (0, sanitize_js_1.prepareForPreview)(output, redact);
|
|
78
|
-
const safeErrorMessage = redactErrorMessage(errorMessage, redact);
|
|
79
|
-
const inputPreview = (0, utils_js_1.truncateUtf8)((0, utils_js_1.stringifyPreview)(safeInput), utils_js_1.MAX_PREVIEW_BYTES);
|
|
80
|
-
const source = (0, utils_js_1.truncateUtf8)(buildToolCallSource(toolName, safeInput), utils_js_1.MAX_SOURCE_BYTES);
|
|
81
|
-
const resultPreview = safeOutput === undefined
|
|
132
|
+
const assembleToolCallEvent = (candidate, input) => {
|
|
133
|
+
const inputPreview = (0, utils_js_1.truncateUtf8)((0, utils_js_1.stringifyPreview)(candidate.input), utils_js_1.MAX_PREVIEW_BYTES);
|
|
134
|
+
const source = (0, utils_js_1.truncateUtf8)(buildToolCallSource(candidate.toolName, candidate.input), utils_js_1.MAX_SOURCE_BYTES);
|
|
135
|
+
const resultPreview = candidate.output === undefined
|
|
82
136
|
? null
|
|
83
|
-
: (0, utils_js_1.truncateUtf8)((0, utils_js_1.stringifyPreview)(
|
|
84
|
-
const t =
|
|
137
|
+
: (0, utils_js_1.truncateUtf8)((0, utils_js_1.stringifyPreview)(candidate.output), utils_js_1.MAX_PREVIEW_BYTES);
|
|
138
|
+
const t = (0, schema_js_1.normalizeTelemetryArgs)(candidate.telemetry);
|
|
85
139
|
return {
|
|
86
|
-
...workflowStamp(workflowRunId),
|
|
87
|
-
event_id: (0, exports.buildEventId)({ actorId, requestId, kind: "tool_call" }),
|
|
140
|
+
...workflowStamp(input.workflowRunId),
|
|
141
|
+
event_id: (0, exports.buildEventId)({ actorId: input.actorId, requestId: input.requestId, kind: "tool_call" }),
|
|
88
142
|
kind: "tool_call",
|
|
89
|
-
actor_id: actorId,
|
|
90
|
-
session_id_hint: sessionId ?? null,
|
|
91
|
-
started_at: startedAt,
|
|
92
|
-
finished_at: finishedAt,
|
|
93
|
-
duration_ms: durationMs,
|
|
94
|
-
ok: status === "ok",
|
|
95
|
-
error:
|
|
143
|
+
actor_id: input.actorId,
|
|
144
|
+
session_id_hint: candidate.sessionId ?? null,
|
|
145
|
+
started_at: input.startedAt,
|
|
146
|
+
finished_at: input.finishedAt,
|
|
147
|
+
duration_ms: candidate.durationMs,
|
|
148
|
+
ok: candidate.status === "ok",
|
|
149
|
+
error: candidate.errorMessage ?? null,
|
|
96
150
|
metadata: {
|
|
97
|
-
tool_name: toolName,
|
|
151
|
+
tool_name: candidate.toolName,
|
|
98
152
|
user_intent: t?.user_intent ?? null,
|
|
99
153
|
agent_thinking: t?.agent_thinking ?? null,
|
|
100
154
|
user_frustration: t?.user_frustration ?? null,
|
|
@@ -104,6 +158,7 @@ const buildToolCallEvent = ({ toolName, telemetry, input, output, status, durati
|
|
|
104
158
|
context: t?.agent_thinking ?? null,
|
|
105
159
|
frustration_level: t?.user_frustration ?? null,
|
|
106
160
|
input_preview: inputPreview.value,
|
|
161
|
+
...(input.capabilityRequest ? { capability_request: true } : {}),
|
|
107
162
|
},
|
|
108
163
|
script_source: source.value,
|
|
109
164
|
script_source_truncated: source.truncated,
|
|
@@ -114,7 +169,32 @@ const buildToolCallEvent = ({ toolName, telemetry, input, output, status, durati
|
|
|
114
169
|
search_calls: [],
|
|
115
170
|
};
|
|
116
171
|
};
|
|
172
|
+
const buildToolCallEvent = (input) => {
|
|
173
|
+
return assembleToolCallEvent(prepareToolCallCandidate(input), input);
|
|
174
|
+
};
|
|
117
175
|
exports.buildToolCallEvent = buildToolCallEvent;
|
|
176
|
+
const finalizeToolCallEvent = async (input) => {
|
|
177
|
+
let candidate = prepareToolCallCandidate(input);
|
|
178
|
+
if (input.redactEvent) {
|
|
179
|
+
try {
|
|
180
|
+
const redacted = await input.redactEvent(candidate);
|
|
181
|
+
if (redacted === null)
|
|
182
|
+
return null;
|
|
183
|
+
candidate = redacted;
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
candidate = {
|
|
187
|
+
...candidate,
|
|
188
|
+
input: sanitize_js_1.REDACTION_FAILED_PLACEHOLDER,
|
|
189
|
+
output: sanitize_js_1.REDACTION_FAILED_PLACEHOLDER,
|
|
190
|
+
errorMessage: sanitize_js_1.REDACTION_FAILED_PLACEHOLDER,
|
|
191
|
+
telemetry: undefined,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return assembleToolCallEvent(candidate, input);
|
|
196
|
+
};
|
|
197
|
+
exports.finalizeToolCallEvent = finalizeToolCallEvent;
|
|
118
198
|
const buildSessionInitEvent = ({ actorId, sessionId, startedAt, extra, clientInfo, workflowRunId, }) => {
|
|
119
199
|
return {
|
|
120
200
|
...workflowStamp(workflowRunId),
|
|
@@ -153,8 +233,10 @@ const buildSessionInitEvent = ({ actorId, sessionId, startedAt, extra, clientInf
|
|
|
153
233
|
};
|
|
154
234
|
};
|
|
155
235
|
exports.buildSessionInitEvent = buildSessionInitEvent;
|
|
156
|
-
const buildBatch = ({ event, extra, actorId, startedAt, sessionInitKeys, clientInfo, workflowRunId, }) => {
|
|
236
|
+
const buildBatch = ({ event, identityEvent, extra, actorId, startedAt, sessionInitKeys, clientInfo, workflowRunId, }) => {
|
|
157
237
|
const events = [];
|
|
238
|
+
if (identityEvent)
|
|
239
|
+
events.push(identityEvent);
|
|
158
240
|
if (extra?.sessionId) {
|
|
159
241
|
const key = `${actorId}:${extra.sessionId}`;
|
|
160
242
|
if (!sessionInitKeys.has(key)) {
|
|
@@ -173,14 +255,18 @@ const buildBatch = ({ event, extra, actorId, startedAt, sessionInitKeys, clientI
|
|
|
173
255
|
return { schema_version: utils_js_1.SCHEMA_VERSION, events };
|
|
174
256
|
};
|
|
175
257
|
exports.buildBatch = buildBatch;
|
|
176
|
-
const buildSessionInitBatch = ({ actorId, sessionId, startedAt, extra, sessionInitKeys, clientInfo, workflowRunId, }) => {
|
|
258
|
+
const buildSessionInitBatch = ({ actorId, sessionId, startedAt, extra, sessionInitKeys, clientInfo, workflowRunId, identityEvent, }) => {
|
|
177
259
|
const key = `${actorId}:${sessionId}`;
|
|
178
|
-
if (sessionInitKeys.has(key))
|
|
179
|
-
return
|
|
260
|
+
if (sessionInitKeys.has(key)) {
|
|
261
|
+
return identityEvent
|
|
262
|
+
? { schema_version: utils_js_1.SCHEMA_VERSION, events: [identityEvent] }
|
|
263
|
+
: null;
|
|
264
|
+
}
|
|
180
265
|
sessionInitKeys.add(key);
|
|
181
266
|
return {
|
|
182
267
|
schema_version: utils_js_1.SCHEMA_VERSION,
|
|
183
268
|
events: [
|
|
269
|
+
...(identityEvent ? [identityEvent] : []),
|
|
184
270
|
(0, exports.buildSessionInitEvent)({
|
|
185
271
|
actorId,
|
|
186
272
|
sessionId,
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
export type { ActorIdResolver, ActorIdResolverInput, AnalyticsEventKind, AnalyticsIngestBatch, AnalyticsIngestEvent, AnalyticsRecorder, ExtractedToolArguments, HeaderBag, InstrumentToolCallEvent, JsonObjectSchema, McpAnalyticsConfig, McpClientInfo, McpServerInfo, RecordSessionInitEvent, RecordToolCallEvent, RedactFunction, RegisteredToolHandler, RequestExtra, TelemetryArgs, TelemetryEmitter, TelemetryFieldMap, TelemetryMode, ToolCallHandler, ToolDefinition, ToolHandlerContext, ToolRegistration, WithMcpAnalyticsResult, } from "./types.js";
|
|
1
|
+
export type { ActorIdResolver, ActorIdResolverInput, ActorIdentifierResolver, AnalyticsEventKind, AnalyticsIngestBatch, AnalyticsIngestEvent, AnalyticsRecorder, ExtractedToolArguments, HeaderBag, InstrumentToolCallEvent, JsonObjectSchema, McpAnalyticsConfig, McpClientInfo, McpServerInfo, RecordSessionInitEvent, RecordToolCallEvent, RedactableToolCall, RedactEventHook, RedactFunction, RegisteredToolHandler, RequestExtra, TelemetryArgs, TelemetryEmitter, TelemetryFieldMap, TelemetryMode, ToolCallHandler, ToolDefinition, ToolHandlerContext, ToolRegistration, WithMcpAnalyticsResult, } from "./types.js";
|
|
2
2
|
export { appendTelemetryHint, applyTelemetryFieldMap, createTelemetryInputSchema, createTelemetryJsonSchema, decorateInputSchemaWithTelemetry, extractTelemetryArguments, isCaptureEnabled, normalizeTelemetryArgs, planToolTelemetry, schemaDeclaresTelemetry, type ToolTelemetryPlan, } from "./schema.js";
|
|
3
|
-
export { BASE64_REMOVED_PLACEHOLDER, BINARY_REMOVED_PLACEHOLDER, prepareForPreview, REDACTION_FAILED_PLACEHOLDER, sanitizeValue, } from "./sanitize.js";
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
3
|
+
export { BASE64_REMOVED_PLACEHOLDER, BINARY_REMOVED_PLACEHOLDER, prepareForPreview, REDACTION_FAILED_PLACEHOLDER, sanitizeValue, SANITIZATION_BUDGET, } from "./sanitize.js";
|
|
4
|
+
export { normalizeSensitiveFieldName, redactSecretsInString, redactSecretsInValue, SECRET_PATTERN_RULES, SENSITIVE_FIELD_NAMES, type SecretPatternRule, } from "./redact-secrets.js";
|
|
5
|
+
export { createPrivacyQueue, PRIVACY_QUEUE_BATCH_SIZE, PRIVACY_QUEUE_CAPACITY, type PrivacyQueue, type PrivacyQueueFinalizer, } from "./queue.js";
|
|
6
|
+
export { buildActorId, buildActorIdentityEvent, buildEventId, buildSessionInitEvent, buildToolCallEvent, finalizeToolCallEvent, normalizeSessionId, type BuildToolCallEventInput, } from "./events.js";
|
|
7
|
+
export { defaultMcpAnalyticsConfig, emitTelemetryEvent, postTelemetryEvent, reportEmitError, } from "./emit.js";
|
|
6
8
|
export { createAnalyticsRecorder } from "./recorder.js";
|
|
7
9
|
export { buildStatelessSessionId, parseStatelessSessionClientInfo, resolveStatelessHttpSession, type StatelessHttpSession, } from "./stateless-http.js";
|
|
8
10
|
export { createMcpAnalyticsServer, withMcpAnalytics } from "./server.js";
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.instrumentMcpServerTools = exports.withMcpAnalytics = exports.createMcpAnalyticsServer = exports.resolveStatelessHttpSession = exports.parseStatelessSessionClientInfo = exports.buildStatelessSessionId = exports.createAnalyticsRecorder = exports.postTelemetryEvent = exports.emitTelemetryEvent = exports.defaultMcpAnalyticsConfig = exports.normalizeSessionId = exports.buildToolCallEvent = exports.buildSessionInitEvent = exports.buildEventId = exports.buildActorId = exports.sanitizeValue = exports.REDACTION_FAILED_PLACEHOLDER = exports.prepareForPreview = exports.BINARY_REMOVED_PLACEHOLDER = exports.BASE64_REMOVED_PLACEHOLDER = exports.schemaDeclaresTelemetry = exports.planToolTelemetry = exports.normalizeTelemetryArgs = exports.isCaptureEnabled = exports.extractTelemetryArguments = exports.decorateInputSchemaWithTelemetry = exports.createTelemetryJsonSchema = exports.createTelemetryInputSchema = exports.applyTelemetryFieldMap = exports.appendTelemetryHint = void 0;
|
|
3
|
+
exports.instrumentMcpServerTools = exports.withMcpAnalytics = exports.createMcpAnalyticsServer = exports.resolveStatelessHttpSession = exports.parseStatelessSessionClientInfo = exports.buildStatelessSessionId = exports.createAnalyticsRecorder = exports.reportEmitError = exports.postTelemetryEvent = exports.emitTelemetryEvent = exports.defaultMcpAnalyticsConfig = exports.normalizeSessionId = exports.finalizeToolCallEvent = exports.buildToolCallEvent = exports.buildSessionInitEvent = exports.buildEventId = exports.buildActorIdentityEvent = exports.buildActorId = exports.PRIVACY_QUEUE_CAPACITY = exports.PRIVACY_QUEUE_BATCH_SIZE = exports.createPrivacyQueue = exports.SENSITIVE_FIELD_NAMES = exports.SECRET_PATTERN_RULES = exports.redactSecretsInValue = exports.redactSecretsInString = exports.normalizeSensitiveFieldName = exports.SANITIZATION_BUDGET = exports.sanitizeValue = exports.REDACTION_FAILED_PLACEHOLDER = exports.prepareForPreview = exports.BINARY_REMOVED_PLACEHOLDER = exports.BASE64_REMOVED_PLACEHOLDER = exports.schemaDeclaresTelemetry = exports.planToolTelemetry = exports.normalizeTelemetryArgs = exports.isCaptureEnabled = exports.extractTelemetryArguments = exports.decorateInputSchemaWithTelemetry = exports.createTelemetryJsonSchema = exports.createTelemetryInputSchema = exports.applyTelemetryFieldMap = exports.appendTelemetryHint = void 0;
|
|
4
4
|
var schema_js_1 = require("./schema.js");
|
|
5
5
|
Object.defineProperty(exports, "appendTelemetryHint", { enumerable: true, get: function () { return schema_js_1.appendTelemetryHint; } });
|
|
6
6
|
Object.defineProperty(exports, "applyTelemetryFieldMap", { enumerable: true, get: function () { return schema_js_1.applyTelemetryFieldMap; } });
|
|
@@ -18,16 +18,30 @@ Object.defineProperty(exports, "BINARY_REMOVED_PLACEHOLDER", { enumerable: true,
|
|
|
18
18
|
Object.defineProperty(exports, "prepareForPreview", { enumerable: true, get: function () { return sanitize_js_1.prepareForPreview; } });
|
|
19
19
|
Object.defineProperty(exports, "REDACTION_FAILED_PLACEHOLDER", { enumerable: true, get: function () { return sanitize_js_1.REDACTION_FAILED_PLACEHOLDER; } });
|
|
20
20
|
Object.defineProperty(exports, "sanitizeValue", { enumerable: true, get: function () { return sanitize_js_1.sanitizeValue; } });
|
|
21
|
+
Object.defineProperty(exports, "SANITIZATION_BUDGET", { enumerable: true, get: function () { return sanitize_js_1.SANITIZATION_BUDGET; } });
|
|
22
|
+
var redact_secrets_js_1 = require("./redact-secrets.js");
|
|
23
|
+
Object.defineProperty(exports, "normalizeSensitiveFieldName", { enumerable: true, get: function () { return redact_secrets_js_1.normalizeSensitiveFieldName; } });
|
|
24
|
+
Object.defineProperty(exports, "redactSecretsInString", { enumerable: true, get: function () { return redact_secrets_js_1.redactSecretsInString; } });
|
|
25
|
+
Object.defineProperty(exports, "redactSecretsInValue", { enumerable: true, get: function () { return redact_secrets_js_1.redactSecretsInValue; } });
|
|
26
|
+
Object.defineProperty(exports, "SECRET_PATTERN_RULES", { enumerable: true, get: function () { return redact_secrets_js_1.SECRET_PATTERN_RULES; } });
|
|
27
|
+
Object.defineProperty(exports, "SENSITIVE_FIELD_NAMES", { enumerable: true, get: function () { return redact_secrets_js_1.SENSITIVE_FIELD_NAMES; } });
|
|
28
|
+
var queue_js_1 = require("./queue.js");
|
|
29
|
+
Object.defineProperty(exports, "createPrivacyQueue", { enumerable: true, get: function () { return queue_js_1.createPrivacyQueue; } });
|
|
30
|
+
Object.defineProperty(exports, "PRIVACY_QUEUE_BATCH_SIZE", { enumerable: true, get: function () { return queue_js_1.PRIVACY_QUEUE_BATCH_SIZE; } });
|
|
31
|
+
Object.defineProperty(exports, "PRIVACY_QUEUE_CAPACITY", { enumerable: true, get: function () { return queue_js_1.PRIVACY_QUEUE_CAPACITY; } });
|
|
21
32
|
var events_js_1 = require("./events.js");
|
|
22
33
|
Object.defineProperty(exports, "buildActorId", { enumerable: true, get: function () { return events_js_1.buildActorId; } });
|
|
34
|
+
Object.defineProperty(exports, "buildActorIdentityEvent", { enumerable: true, get: function () { return events_js_1.buildActorIdentityEvent; } });
|
|
23
35
|
Object.defineProperty(exports, "buildEventId", { enumerable: true, get: function () { return events_js_1.buildEventId; } });
|
|
24
36
|
Object.defineProperty(exports, "buildSessionInitEvent", { enumerable: true, get: function () { return events_js_1.buildSessionInitEvent; } });
|
|
25
37
|
Object.defineProperty(exports, "buildToolCallEvent", { enumerable: true, get: function () { return events_js_1.buildToolCallEvent; } });
|
|
38
|
+
Object.defineProperty(exports, "finalizeToolCallEvent", { enumerable: true, get: function () { return events_js_1.finalizeToolCallEvent; } });
|
|
26
39
|
Object.defineProperty(exports, "normalizeSessionId", { enumerable: true, get: function () { return events_js_1.normalizeSessionId; } });
|
|
27
40
|
var emit_js_1 = require("./emit.js");
|
|
28
41
|
Object.defineProperty(exports, "defaultMcpAnalyticsConfig", { enumerable: true, get: function () { return emit_js_1.defaultMcpAnalyticsConfig; } });
|
|
29
42
|
Object.defineProperty(exports, "emitTelemetryEvent", { enumerable: true, get: function () { return emit_js_1.emitTelemetryEvent; } });
|
|
30
43
|
Object.defineProperty(exports, "postTelemetryEvent", { enumerable: true, get: function () { return emit_js_1.postTelemetryEvent; } });
|
|
44
|
+
Object.defineProperty(exports, "reportEmitError", { enumerable: true, get: function () { return emit_js_1.reportEmitError; } });
|
|
31
45
|
var recorder_js_1 = require("./recorder.js");
|
|
32
46
|
Object.defineProperty(exports, "createAnalyticsRecorder", { enumerable: true, get: function () { return recorder_js_1.createAnalyticsRecorder; } });
|
|
33
47
|
var stateless_http_js_1 = require("./stateless-http.js");
|
package/dist/cjs/mastra.js
CHANGED
|
@@ -4,6 +4,8 @@ exports.createMastraAnalytics = exports.wrapMastraTools = exports.wrapMastraTool
|
|
|
4
4
|
const recorder_js_1 = require("./recorder.js");
|
|
5
5
|
const schema_js_1 = require("./schema.js");
|
|
6
6
|
const utils_js_1 = require("./utils.js");
|
|
7
|
+
const zod_1 = require("zod");
|
|
8
|
+
const request_capability_js_1 = require("./request-capability.js");
|
|
7
9
|
const readMcpExtra = (mastraContext) => {
|
|
8
10
|
if (!(0, utils_js_1.isRecord)(mastraContext))
|
|
9
11
|
return undefined;
|
|
@@ -141,9 +143,49 @@ const wrapOneTool = (toolKey, tool, recorder, config, resolveExtra) => {
|
|
|
141
143
|
// passes the constraint without a cast.
|
|
142
144
|
const wrapMastraToolsWithRecorder = (tools, recorder, config = {}, options = {}) => {
|
|
143
145
|
const out = {};
|
|
146
|
+
const requestCapabilityEnabled = (0, request_capability_js_1.isRequestCapabilityEnabled)(config);
|
|
147
|
+
if (requestCapabilityEnabled !== recorder.hasTool(request_capability_js_1.REQUEST_CAPABILITY_TOOL_NAME)) {
|
|
148
|
+
throw new Error("request_capability configuration does not match the supplied analytics recorder.");
|
|
149
|
+
}
|
|
150
|
+
if (requestCapabilityEnabled
|
|
151
|
+
&& Object.entries(tools).some(([key, tool]) => key === request_capability_js_1.REQUEST_CAPABILITY_TOOL_NAME
|
|
152
|
+
|| tool.id === request_capability_js_1.REQUEST_CAPABILITY_TOOL_NAME)) {
|
|
153
|
+
throw new Error(`Tool name "${request_capability_js_1.REQUEST_CAPABILITY_TOOL_NAME}" is reserved while armature.requestCapability is enabled.`);
|
|
154
|
+
}
|
|
144
155
|
for (const [key, tool] of Object.entries(tools)) {
|
|
145
156
|
out[key] = wrapOneTool(key, tool, recorder, config, options.resolveExtra);
|
|
146
157
|
}
|
|
158
|
+
if (requestCapabilityEnabled) {
|
|
159
|
+
out[request_capability_js_1.REQUEST_CAPABILITY_TOOL_NAME] = {
|
|
160
|
+
id: request_capability_js_1.REQUEST_CAPABILITY_TOOL_NAME,
|
|
161
|
+
description: request_capability_js_1.REQUEST_CAPABILITY_DESCRIPTION,
|
|
162
|
+
inputSchema: zod_1.z.object({
|
|
163
|
+
capability: zod_1.z
|
|
164
|
+
.string()
|
|
165
|
+
.min(1)
|
|
166
|
+
.max(1000)
|
|
167
|
+
.describe(request_capability_js_1.REQUEST_CAPABILITY_ARGUMENT_DESCRIPTION),
|
|
168
|
+
}),
|
|
169
|
+
execute: async (inputData, mastraContext) => {
|
|
170
|
+
const extra = mergeExtra((0, exports.defaultMastraResolveExtra)(mastraContext), options.resolveExtra?.(mastraContext));
|
|
171
|
+
const result = await recorder.dispatch(request_capability_js_1.REQUEST_CAPABILITY_TOOL_NAME, inputData, {
|
|
172
|
+
ctx: mastraContext,
|
|
173
|
+
extra,
|
|
174
|
+
sessionId: extra?.sessionId,
|
|
175
|
+
authInfo: extra?.authInfo,
|
|
176
|
+
headers: extra?.requestInfo?.headers,
|
|
177
|
+
});
|
|
178
|
+
const text = result.content.find((content) => content.type === "text")?.text;
|
|
179
|
+
if (result.isError) {
|
|
180
|
+
throw new Error(text ?? "capability request failed");
|
|
181
|
+
}
|
|
182
|
+
// Mastra's MCPServer turns raw tool output into CallToolResult content.
|
|
183
|
+
// Returning the dispatcher's CallToolResult here would JSON-stringify
|
|
184
|
+
// and double-wrap it as text.
|
|
185
|
+
return text ?? "Capability request acknowledged.";
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
}
|
|
147
189
|
return out;
|
|
148
190
|
};
|
|
149
191
|
exports.wrapMastraToolsWithRecorder = wrapMastraToolsWithRecorder;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AnalyticsIngestEvent, McpAnalyticsConfig } from "./types.js";
|
|
2
|
+
export declare const PRIVACY_QUEUE_CAPACITY = 1000;
|
|
3
|
+
export declare const PRIVACY_QUEUE_BATCH_SIZE = 20;
|
|
4
|
+
export type PrivacyQueueFinalizer = () => AnalyticsIngestEvent[] | null | Promise<AnalyticsIngestEvent[] | null>;
|
|
5
|
+
export type PrivacyQueue = {
|
|
6
|
+
enqueue: (finalize: PrivacyQueueFinalizer) => Promise<void>;
|
|
7
|
+
flush: () => Promise<void>;
|
|
8
|
+
};
|
|
9
|
+
export declare const createPrivacyQueue: (config: McpAnalyticsConfig) => PrivacyQueue;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPrivacyQueue = exports.PRIVACY_QUEUE_BATCH_SIZE = exports.PRIVACY_QUEUE_CAPACITY = void 0;
|
|
4
|
+
const emit_js_1 = require("./emit.js");
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
6
|
+
exports.PRIVACY_QUEUE_CAPACITY = 1_000;
|
|
7
|
+
exports.PRIVACY_QUEUE_BATCH_SIZE = 20;
|
|
8
|
+
let warnedServerlessWithoutSchedule = false;
|
|
9
|
+
const warn = (message, error) => {
|
|
10
|
+
// eslint-disable-next-line no-console
|
|
11
|
+
console.warn(`[mcp-analytics] ${message}`, ...(error === undefined ? [] : [error]));
|
|
12
|
+
};
|
|
13
|
+
const createPrivacyQueue = (config) => {
|
|
14
|
+
const pending = [];
|
|
15
|
+
let running = null;
|
|
16
|
+
let dropped = 0;
|
|
17
|
+
let warnedDropped = false;
|
|
18
|
+
const isEnabled = () => config.armature?.enabled !== false;
|
|
19
|
+
if (isEnabled()
|
|
20
|
+
&& config.armature?.delivery !== "await"
|
|
21
|
+
&& !config.armature?.schedule
|
|
22
|
+
&& !warnedServerlessWithoutSchedule
|
|
23
|
+
&& ((0, utils_js_1.readEnv)("AWS_LAMBDA_FUNCTION_NAME") || (0, utils_js_1.readEnv)("VERCEL"))) {
|
|
24
|
+
warnedServerlessWithoutSchedule = true;
|
|
25
|
+
warn("background delivery may be frozen by this serverless runtime; use delivery: \"await\" or armature.schedule");
|
|
26
|
+
}
|
|
27
|
+
const emit = async (batch) => {
|
|
28
|
+
const emitter = config.armature?.emit
|
|
29
|
+
?? (async (value) => {
|
|
30
|
+
await (0, emit_js_1.postTelemetryEvent)(value, config);
|
|
31
|
+
});
|
|
32
|
+
try {
|
|
33
|
+
await emitter(batch);
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
(0, emit_js_1.reportEmitError)(error, batch, config);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const drain = async () => {
|
|
40
|
+
while (pending.length > 0) {
|
|
41
|
+
const items = pending.splice(0, exports.PRIVACY_QUEUE_BATCH_SIZE);
|
|
42
|
+
const events = [];
|
|
43
|
+
for (const finalize of items) {
|
|
44
|
+
try {
|
|
45
|
+
const finalized = await finalize();
|
|
46
|
+
if (finalized)
|
|
47
|
+
events.push(...finalized);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
warn("privacy queue candidate failed and was dropped:", error);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (events.length > 0) {
|
|
54
|
+
await emit({ schema_version: utils_js_1.SCHEMA_VERSION, events });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const start = (deferred) => {
|
|
59
|
+
if (running)
|
|
60
|
+
return running;
|
|
61
|
+
const gate = deferred
|
|
62
|
+
? new Promise((resolve) => {
|
|
63
|
+
if (typeof setImmediate === "function")
|
|
64
|
+
setImmediate(resolve);
|
|
65
|
+
else
|
|
66
|
+
queueMicrotask(resolve);
|
|
67
|
+
})
|
|
68
|
+
: Promise.resolve();
|
|
69
|
+
const task = gate
|
|
70
|
+
.then(drain)
|
|
71
|
+
.finally(() => {
|
|
72
|
+
if (running === task)
|
|
73
|
+
running = null;
|
|
74
|
+
if (pending.length > 0 && isEnabled())
|
|
75
|
+
scheduleBackground();
|
|
76
|
+
});
|
|
77
|
+
running = task;
|
|
78
|
+
return task;
|
|
79
|
+
};
|
|
80
|
+
const scheduleBackground = () => {
|
|
81
|
+
const work = start(true);
|
|
82
|
+
try {
|
|
83
|
+
config.armature?.schedule?.(work);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
warn("schedule hook threw; background work remains active:", error);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
const flush = async () => {
|
|
90
|
+
if (!isEnabled())
|
|
91
|
+
return;
|
|
92
|
+
while (running || pending.length > 0) {
|
|
93
|
+
await (running ?? start(false));
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const enqueue = (finalize) => {
|
|
97
|
+
if (!isEnabled())
|
|
98
|
+
return Promise.resolve();
|
|
99
|
+
if (pending.length >= exports.PRIVACY_QUEUE_CAPACITY) {
|
|
100
|
+
pending.shift();
|
|
101
|
+
dropped += 1;
|
|
102
|
+
if (!warnedDropped) {
|
|
103
|
+
warnedDropped = true;
|
|
104
|
+
warn(`privacy queue overflow; dropped ${dropped} oldest candidate(s)`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
pending.push(finalize);
|
|
108
|
+
if (config.armature?.delivery === "await")
|
|
109
|
+
return flush();
|
|
110
|
+
if (!running)
|
|
111
|
+
scheduleBackground();
|
|
112
|
+
return Promise.resolve();
|
|
113
|
+
};
|
|
114
|
+
return { enqueue, flush };
|
|
115
|
+
};
|
|
116
|
+
exports.createPrivacyQueue = createPrivacyQueue;
|