@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
package/dist/cjs/events.js
CHANGED
|
@@ -1,8 +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
|
+
const sanitize_js_1 = require("./sanitize.js");
|
|
7
|
+
const redact_secrets_js_1 = require("./redact-secrets.js");
|
|
6
8
|
const utils_js_1 = require("./utils.js");
|
|
7
9
|
const trimOrUndefined = (value) => {
|
|
8
10
|
if (typeof value !== "string")
|
|
@@ -30,9 +32,95 @@ const buildEventId = ({ actorId, requestId, kind, }) => {
|
|
|
30
32
|
return (0, utils_js_1.sha256Hex)(`${actorId} ${kind} ${requestId}`);
|
|
31
33
|
};
|
|
32
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;
|
|
33
57
|
const buildToolCallSource = (toolName, input) => {
|
|
34
58
|
return `MCP tool call: ${toolName}\n\nInput:\n${(0, utils_js_1.stringifyPreview)(input)}`;
|
|
35
59
|
};
|
|
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;
|
|
83
|
+
try {
|
|
84
|
+
return (0, schema_js_1.normalizeTelemetryArgs)(redact(protectedTelemetry));
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
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;
|
|
98
|
+
try {
|
|
99
|
+
const redacted = redact(protectedMessage);
|
|
100
|
+
return typeof redacted === "string" ? redacted : (0, utils_js_1.stringifyPreview)(redacted);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return sanitize_js_1.REDACTION_FAILED_PLACEHOLDER;
|
|
104
|
+
}
|
|
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
|
+
};
|
|
36
124
|
// Marks an event as synthetic traffic from an Armature workflow run so
|
|
37
125
|
// Session Analytics can exclude it. Spread first in the event literal —
|
|
38
126
|
// absent when the call did not originate from a workflow run.
|
|
@@ -41,27 +129,26 @@ const workflowStamp = (workflowRunId) => {
|
|
|
41
129
|
return {};
|
|
42
130
|
return { is_workflow: true, workflow_run_id: workflowRunId };
|
|
43
131
|
};
|
|
44
|
-
const
|
|
45
|
-
const inputPreview = (0, utils_js_1.truncateUtf8)((0, utils_js_1.stringifyPreview)(input), utils_js_1.MAX_PREVIEW_BYTES);
|
|
46
|
-
const source = (0, utils_js_1.truncateUtf8)(buildToolCallSource(toolName, input), utils_js_1.MAX_SOURCE_BYTES);
|
|
47
|
-
const resultPreview = output === 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
|
|
48
136
|
? null
|
|
49
|
-
: (0, utils_js_1.truncateUtf8)((0, utils_js_1.stringifyPreview)(output), utils_js_1.MAX_PREVIEW_BYTES);
|
|
50
|
-
const t = (0, schema_js_1.normalizeTelemetryArgs)(telemetry);
|
|
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);
|
|
51
139
|
return {
|
|
52
|
-
...workflowStamp(workflowRunId),
|
|
53
|
-
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" }),
|
|
54
142
|
kind: "tool_call",
|
|
55
|
-
actor_id: actorId,
|
|
56
|
-
session_id_hint: sessionId ?? null,
|
|
57
|
-
started_at: startedAt,
|
|
58
|
-
finished_at: finishedAt,
|
|
59
|
-
duration_ms: durationMs,
|
|
60
|
-
ok: status === "ok",
|
|
61
|
-
error: errorMessage ?? null,
|
|
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,
|
|
62
150
|
metadata: {
|
|
63
|
-
tool_name: toolName,
|
|
64
|
-
user_turn: t?.user_turn ?? null,
|
|
151
|
+
tool_name: candidate.toolName,
|
|
65
152
|
user_intent: t?.user_intent ?? null,
|
|
66
153
|
agent_thinking: t?.agent_thinking ?? null,
|
|
67
154
|
user_frustration: t?.user_frustration ?? null,
|
|
@@ -71,6 +158,7 @@ const buildToolCallEvent = ({ toolName, telemetry, input, output, status, durati
|
|
|
71
158
|
context: t?.agent_thinking ?? null,
|
|
72
159
|
frustration_level: t?.user_frustration ?? null,
|
|
73
160
|
input_preview: inputPreview.value,
|
|
161
|
+
...(input.capabilityRequest ? { capability_request: true } : {}),
|
|
74
162
|
},
|
|
75
163
|
script_source: source.value,
|
|
76
164
|
script_source_truncated: source.truncated,
|
|
@@ -81,7 +169,32 @@ const buildToolCallEvent = ({ toolName, telemetry, input, output, status, durati
|
|
|
81
169
|
search_calls: [],
|
|
82
170
|
};
|
|
83
171
|
};
|
|
172
|
+
const buildToolCallEvent = (input) => {
|
|
173
|
+
return assembleToolCallEvent(prepareToolCallCandidate(input), input);
|
|
174
|
+
};
|
|
84
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;
|
|
85
198
|
const buildSessionInitEvent = ({ actorId, sessionId, startedAt, extra, clientInfo, workflowRunId, }) => {
|
|
86
199
|
return {
|
|
87
200
|
...workflowStamp(workflowRunId),
|
|
@@ -120,8 +233,10 @@ const buildSessionInitEvent = ({ actorId, sessionId, startedAt, extra, clientInf
|
|
|
120
233
|
};
|
|
121
234
|
};
|
|
122
235
|
exports.buildSessionInitEvent = buildSessionInitEvent;
|
|
123
|
-
const buildBatch = ({ event, extra, actorId, startedAt, sessionInitKeys, clientInfo, workflowRunId, }) => {
|
|
236
|
+
const buildBatch = ({ event, identityEvent, extra, actorId, startedAt, sessionInitKeys, clientInfo, workflowRunId, }) => {
|
|
124
237
|
const events = [];
|
|
238
|
+
if (identityEvent)
|
|
239
|
+
events.push(identityEvent);
|
|
125
240
|
if (extra?.sessionId) {
|
|
126
241
|
const key = `${actorId}:${extra.sessionId}`;
|
|
127
242
|
if (!sessionInitKeys.has(key)) {
|
|
@@ -140,14 +255,18 @@ const buildBatch = ({ event, extra, actorId, startedAt, sessionInitKeys, clientI
|
|
|
140
255
|
return { schema_version: utils_js_1.SCHEMA_VERSION, events };
|
|
141
256
|
};
|
|
142
257
|
exports.buildBatch = buildBatch;
|
|
143
|
-
const buildSessionInitBatch = ({ actorId, sessionId, startedAt, extra, sessionInitKeys, clientInfo, workflowRunId, }) => {
|
|
258
|
+
const buildSessionInitBatch = ({ actorId, sessionId, startedAt, extra, sessionInitKeys, clientInfo, workflowRunId, identityEvent, }) => {
|
|
144
259
|
const key = `${actorId}:${sessionId}`;
|
|
145
|
-
if (sessionInitKeys.has(key))
|
|
146
|
-
return
|
|
260
|
+
if (sessionInitKeys.has(key)) {
|
|
261
|
+
return identityEvent
|
|
262
|
+
? { schema_version: utils_js_1.SCHEMA_VERSION, events: [identityEvent] }
|
|
263
|
+
: null;
|
|
264
|
+
}
|
|
147
265
|
sessionInitKeys.add(key);
|
|
148
266
|
return {
|
|
149
267
|
schema_version: utils_js_1.SCHEMA_VERSION,
|
|
150
268
|
events: [
|
|
269
|
+
...(identityEvent ? [identityEvent] : []),
|
|
151
270
|
(0, exports.buildSessionInitEvent)({
|
|
152
271
|
actorId,
|
|
153
272
|
sessionId,
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
export type { ActorIdResolver, ActorIdResolverInput, AnalyticsEventKind, AnalyticsIngestBatch, AnalyticsIngestEvent, AnalyticsRecorder, ExtractedToolArguments, HeaderBag, InstrumentToolCallEvent, JsonObjectSchema, McpAnalyticsConfig, McpClientInfo, McpServerInfo, RecordSessionInitEvent, RecordToolCallEvent, RegisteredToolHandler, RequestExtra, TelemetryArgs, TelemetryEmitter, ToolCallHandler, ToolDefinition, ToolHandlerContext, ToolRegistration, WithMcpAnalyticsResult, } from "./types.js";
|
|
2
|
-
export { appendTelemetryHint, createTelemetryInputSchema, createTelemetryJsonSchema, decorateInputSchemaWithTelemetry, extractTelemetryArguments, normalizeTelemetryArgs, } from "./schema.js";
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
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
|
+
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, 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";
|
|
5
8
|
export { createAnalyticsRecorder } from "./recorder.js";
|
|
6
9
|
export { buildStatelessSessionId, parseStatelessSessionClientInfo, resolveStatelessHttpSession, type StatelessHttpSession, } from "./stateless-http.js";
|
|
7
10
|
export { createMcpAnalyticsServer, withMcpAnalytics } from "./server.js";
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,23 +1,47 @@
|
|
|
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.normalizeTelemetryArgs = exports.extractTelemetryArguments = exports.decorateInputSchemaWithTelemetry = exports.createTelemetryJsonSchema = exports.createTelemetryInputSchema = 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
|
+
Object.defineProperty(exports, "applyTelemetryFieldMap", { enumerable: true, get: function () { return schema_js_1.applyTelemetryFieldMap; } });
|
|
6
7
|
Object.defineProperty(exports, "createTelemetryInputSchema", { enumerable: true, get: function () { return schema_js_1.createTelemetryInputSchema; } });
|
|
7
8
|
Object.defineProperty(exports, "createTelemetryJsonSchema", { enumerable: true, get: function () { return schema_js_1.createTelemetryJsonSchema; } });
|
|
8
9
|
Object.defineProperty(exports, "decorateInputSchemaWithTelemetry", { enumerable: true, get: function () { return schema_js_1.decorateInputSchemaWithTelemetry; } });
|
|
9
10
|
Object.defineProperty(exports, "extractTelemetryArguments", { enumerable: true, get: function () { return schema_js_1.extractTelemetryArguments; } });
|
|
11
|
+
Object.defineProperty(exports, "isCaptureEnabled", { enumerable: true, get: function () { return schema_js_1.isCaptureEnabled; } });
|
|
10
12
|
Object.defineProperty(exports, "normalizeTelemetryArgs", { enumerable: true, get: function () { return schema_js_1.normalizeTelemetryArgs; } });
|
|
13
|
+
Object.defineProperty(exports, "planToolTelemetry", { enumerable: true, get: function () { return schema_js_1.planToolTelemetry; } });
|
|
14
|
+
Object.defineProperty(exports, "schemaDeclaresTelemetry", { enumerable: true, get: function () { return schema_js_1.schemaDeclaresTelemetry; } });
|
|
15
|
+
var sanitize_js_1 = require("./sanitize.js");
|
|
16
|
+
Object.defineProperty(exports, "BASE64_REMOVED_PLACEHOLDER", { enumerable: true, get: function () { return sanitize_js_1.BASE64_REMOVED_PLACEHOLDER; } });
|
|
17
|
+
Object.defineProperty(exports, "BINARY_REMOVED_PLACEHOLDER", { enumerable: true, get: function () { return sanitize_js_1.BINARY_REMOVED_PLACEHOLDER; } });
|
|
18
|
+
Object.defineProperty(exports, "prepareForPreview", { enumerable: true, get: function () { return sanitize_js_1.prepareForPreview; } });
|
|
19
|
+
Object.defineProperty(exports, "REDACTION_FAILED_PLACEHOLDER", { enumerable: true, get: function () { return sanitize_js_1.REDACTION_FAILED_PLACEHOLDER; } });
|
|
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; } });
|
|
11
32
|
var events_js_1 = require("./events.js");
|
|
12
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; } });
|
|
13
35
|
Object.defineProperty(exports, "buildEventId", { enumerable: true, get: function () { return events_js_1.buildEventId; } });
|
|
14
36
|
Object.defineProperty(exports, "buildSessionInitEvent", { enumerable: true, get: function () { return events_js_1.buildSessionInitEvent; } });
|
|
15
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; } });
|
|
16
39
|
Object.defineProperty(exports, "normalizeSessionId", { enumerable: true, get: function () { return events_js_1.normalizeSessionId; } });
|
|
17
40
|
var emit_js_1 = require("./emit.js");
|
|
18
41
|
Object.defineProperty(exports, "defaultMcpAnalyticsConfig", { enumerable: true, get: function () { return emit_js_1.defaultMcpAnalyticsConfig; } });
|
|
19
42
|
Object.defineProperty(exports, "emitTelemetryEvent", { enumerable: true, get: function () { return emit_js_1.emitTelemetryEvent; } });
|
|
20
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; } });
|
|
21
45
|
var recorder_js_1 = require("./recorder.js");
|
|
22
46
|
Object.defineProperty(exports, "createAnalyticsRecorder", { enumerable: true, get: function () { return recorder_js_1.createAnalyticsRecorder; } });
|
|
23
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;
|
|
@@ -94,9 +96,11 @@ const wrapOneTool = (toolKey, tool, recorder, config, resolveExtra) => {
|
|
|
94
96
|
}
|
|
95
97
|
const originalExecute = tool.execute;
|
|
96
98
|
const toolName = tool.id ?? toolKey;
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
99
|
+
// Mastra tools without an inputSchema keep no schema (Mastra derives its own
|
|
100
|
+
// default); planToolTelemetry is only consulted for decoration when the tool
|
|
101
|
+
// has one, but its mode still drives extraction either way.
|
|
102
|
+
const plan = (0, schema_js_1.planToolTelemetry)(toolName, tool.inputSchema, config);
|
|
103
|
+
const decoratedInputSchema = tool.inputSchema === undefined ? undefined : plan.inputSchema;
|
|
100
104
|
const wrappedExecute = (inputData, mastraContext) => {
|
|
101
105
|
// Always extract standard Mastra MCP context (`context.mcp.extra` or the
|
|
102
106
|
// `requestContext.get("mcp.extra")` fallback). A user-supplied
|
|
@@ -116,11 +120,13 @@ const wrapOneTool = (toolKey, tool, recorder, config, resolveExtra) => {
|
|
|
116
120
|
// `normalizeRequestId`). The JSON-RPC id stays available via `extra`.
|
|
117
121
|
authInfo: extra?.authInfo,
|
|
118
122
|
headers: extra?.requestInfo?.headers,
|
|
123
|
+
telemetryMode: plan.mode,
|
|
119
124
|
}, (strippedArgs) => originalExecute(strippedArgs, mastraContext));
|
|
120
125
|
};
|
|
126
|
+
const description = plan.applyDescription(tool.description);
|
|
121
127
|
return {
|
|
122
128
|
...tool,
|
|
123
|
-
description
|
|
129
|
+
...(description !== undefined ? { description } : {}),
|
|
124
130
|
...(decoratedInputSchema !== undefined
|
|
125
131
|
? { inputSchema: decoratedInputSchema }
|
|
126
132
|
: {}),
|
|
@@ -137,9 +143,49 @@ const wrapOneTool = (toolKey, tool, recorder, config, resolveExtra) => {
|
|
|
137
143
|
// passes the constraint without a cast.
|
|
138
144
|
const wrapMastraToolsWithRecorder = (tools, recorder, config = {}, options = {}) => {
|
|
139
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
|
+
}
|
|
140
155
|
for (const [key, tool] of Object.entries(tools)) {
|
|
141
156
|
out[key] = wrapOneTool(key, tool, recorder, config, options.resolveExtra);
|
|
142
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
|
+
}
|
|
143
189
|
return out;
|
|
144
190
|
};
|
|
145
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;
|