@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/esm/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/esm/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export { appendTelemetryHint, applyTelemetryFieldMap, createTelemetryInputSchema, createTelemetryJsonSchema, decorateInputSchemaWithTelemetry, extractTelemetryArguments, isCaptureEnabled, normalizeTelemetryArgs, planToolTelemetry, schemaDeclaresTelemetry, } from "./schema.js";
|
|
2
|
-
export { BASE64_REMOVED_PLACEHOLDER, BINARY_REMOVED_PLACEHOLDER, prepareForPreview, REDACTION_FAILED_PLACEHOLDER, sanitizeValue, } from "./sanitize.js";
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
2
|
+
export { BASE64_REMOVED_PLACEHOLDER, BINARY_REMOVED_PLACEHOLDER, prepareForPreview, REDACTION_FAILED_PLACEHOLDER, sanitizeValue, SANITIZATION_BUDGET, } from "./sanitize.js";
|
|
3
|
+
export { normalizeSensitiveFieldName, redactSecretsInString, redactSecretsInValue, SECRET_PATTERN_RULES, SENSITIVE_FIELD_NAMES, } from "./redact-secrets.js";
|
|
4
|
+
export { createPrivacyQueue, PRIVACY_QUEUE_BATCH_SIZE, PRIVACY_QUEUE_CAPACITY, } from "./queue.js";
|
|
5
|
+
export { buildActorId, buildActorIdentityEvent, buildEventId, buildSessionInitEvent, buildToolCallEvent, finalizeToolCallEvent, normalizeSessionId, } from "./events.js";
|
|
6
|
+
export { defaultMcpAnalyticsConfig, emitTelemetryEvent, postTelemetryEvent, reportEmitError, } from "./emit.js";
|
|
5
7
|
export { createAnalyticsRecorder } from "./recorder.js";
|
|
6
8
|
export { buildStatelessSessionId, parseStatelessSessionClientInfo, resolveStatelessHttpSession, } from "./stateless-http.js";
|
|
7
9
|
export { createMcpAnalyticsServer, withMcpAnalytics } from "./server.js";
|
package/dist/esm/mastra.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { createAnalyticsRecorder } from "./recorder.js";
|
|
2
2
|
import { planToolTelemetry } from "./schema.js";
|
|
3
3
|
import { isRecord } from "./utils.js";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { isRequestCapabilityEnabled, REQUEST_CAPABILITY_ARGUMENT_DESCRIPTION, REQUEST_CAPABILITY_DESCRIPTION, REQUEST_CAPABILITY_TOOL_NAME, } from "./request-capability.js";
|
|
4
6
|
const readMcpExtra = (mastraContext) => {
|
|
5
7
|
if (!isRecord(mastraContext))
|
|
6
8
|
return undefined;
|
|
@@ -137,9 +139,49 @@ const wrapOneTool = (toolKey, tool, recorder, config, resolveExtra) => {
|
|
|
137
139
|
// passes the constraint without a cast.
|
|
138
140
|
export const wrapMastraToolsWithRecorder = (tools, recorder, config = {}, options = {}) => {
|
|
139
141
|
const out = {};
|
|
142
|
+
const requestCapabilityEnabled = isRequestCapabilityEnabled(config);
|
|
143
|
+
if (requestCapabilityEnabled !== recorder.hasTool(REQUEST_CAPABILITY_TOOL_NAME)) {
|
|
144
|
+
throw new Error("request_capability configuration does not match the supplied analytics recorder.");
|
|
145
|
+
}
|
|
146
|
+
if (requestCapabilityEnabled
|
|
147
|
+
&& Object.entries(tools).some(([key, tool]) => key === REQUEST_CAPABILITY_TOOL_NAME
|
|
148
|
+
|| tool.id === REQUEST_CAPABILITY_TOOL_NAME)) {
|
|
149
|
+
throw new Error(`Tool name "${REQUEST_CAPABILITY_TOOL_NAME}" is reserved while armature.requestCapability is enabled.`);
|
|
150
|
+
}
|
|
140
151
|
for (const [key, tool] of Object.entries(tools)) {
|
|
141
152
|
out[key] = wrapOneTool(key, tool, recorder, config, options.resolveExtra);
|
|
142
153
|
}
|
|
154
|
+
if (requestCapabilityEnabled) {
|
|
155
|
+
out[REQUEST_CAPABILITY_TOOL_NAME] = {
|
|
156
|
+
id: REQUEST_CAPABILITY_TOOL_NAME,
|
|
157
|
+
description: REQUEST_CAPABILITY_DESCRIPTION,
|
|
158
|
+
inputSchema: z.object({
|
|
159
|
+
capability: z
|
|
160
|
+
.string()
|
|
161
|
+
.min(1)
|
|
162
|
+
.max(1000)
|
|
163
|
+
.describe(REQUEST_CAPABILITY_ARGUMENT_DESCRIPTION),
|
|
164
|
+
}),
|
|
165
|
+
execute: async (inputData, mastraContext) => {
|
|
166
|
+
const extra = mergeExtra(defaultMastraResolveExtra(mastraContext), options.resolveExtra?.(mastraContext));
|
|
167
|
+
const result = await recorder.dispatch(REQUEST_CAPABILITY_TOOL_NAME, inputData, {
|
|
168
|
+
ctx: mastraContext,
|
|
169
|
+
extra,
|
|
170
|
+
sessionId: extra?.sessionId,
|
|
171
|
+
authInfo: extra?.authInfo,
|
|
172
|
+
headers: extra?.requestInfo?.headers,
|
|
173
|
+
});
|
|
174
|
+
const text = result.content.find((content) => content.type === "text")?.text;
|
|
175
|
+
if (result.isError) {
|
|
176
|
+
throw new Error(text ?? "capability request failed");
|
|
177
|
+
}
|
|
178
|
+
// Mastra's MCPServer turns raw tool output into CallToolResult content.
|
|
179
|
+
// Returning the dispatcher's CallToolResult here would JSON-stringify
|
|
180
|
+
// and double-wrap it as text.
|
|
181
|
+
return text ?? "Capability request acknowledged.";
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
}
|
|
143
185
|
return out;
|
|
144
186
|
};
|
|
145
187
|
export const wrapMastraTools = (tools, options = {}) => {
|
|
@@ -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,112 @@
|
|
|
1
|
+
import { postTelemetryEvent, reportEmitError, } from "./emit.js";
|
|
2
|
+
import { readEnv, SCHEMA_VERSION } from "./utils.js";
|
|
3
|
+
export const PRIVACY_QUEUE_CAPACITY = 1_000;
|
|
4
|
+
export const PRIVACY_QUEUE_BATCH_SIZE = 20;
|
|
5
|
+
let warnedServerlessWithoutSchedule = false;
|
|
6
|
+
const warn = (message, error) => {
|
|
7
|
+
// eslint-disable-next-line no-console
|
|
8
|
+
console.warn(`[mcp-analytics] ${message}`, ...(error === undefined ? [] : [error]));
|
|
9
|
+
};
|
|
10
|
+
export const createPrivacyQueue = (config) => {
|
|
11
|
+
const pending = [];
|
|
12
|
+
let running = null;
|
|
13
|
+
let dropped = 0;
|
|
14
|
+
let warnedDropped = false;
|
|
15
|
+
const isEnabled = () => config.armature?.enabled !== false;
|
|
16
|
+
if (isEnabled()
|
|
17
|
+
&& config.armature?.delivery !== "await"
|
|
18
|
+
&& !config.armature?.schedule
|
|
19
|
+
&& !warnedServerlessWithoutSchedule
|
|
20
|
+
&& (readEnv("AWS_LAMBDA_FUNCTION_NAME") || readEnv("VERCEL"))) {
|
|
21
|
+
warnedServerlessWithoutSchedule = true;
|
|
22
|
+
warn("background delivery may be frozen by this serverless runtime; use delivery: \"await\" or armature.schedule");
|
|
23
|
+
}
|
|
24
|
+
const emit = async (batch) => {
|
|
25
|
+
const emitter = config.armature?.emit
|
|
26
|
+
?? (async (value) => {
|
|
27
|
+
await postTelemetryEvent(value, config);
|
|
28
|
+
});
|
|
29
|
+
try {
|
|
30
|
+
await emitter(batch);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
reportEmitError(error, batch, config);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const drain = async () => {
|
|
37
|
+
while (pending.length > 0) {
|
|
38
|
+
const items = pending.splice(0, PRIVACY_QUEUE_BATCH_SIZE);
|
|
39
|
+
const events = [];
|
|
40
|
+
for (const finalize of items) {
|
|
41
|
+
try {
|
|
42
|
+
const finalized = await finalize();
|
|
43
|
+
if (finalized)
|
|
44
|
+
events.push(...finalized);
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
warn("privacy queue candidate failed and was dropped:", error);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (events.length > 0) {
|
|
51
|
+
await emit({ schema_version: SCHEMA_VERSION, events });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const start = (deferred) => {
|
|
56
|
+
if (running)
|
|
57
|
+
return running;
|
|
58
|
+
const gate = deferred
|
|
59
|
+
? new Promise((resolve) => {
|
|
60
|
+
if (typeof setImmediate === "function")
|
|
61
|
+
setImmediate(resolve);
|
|
62
|
+
else
|
|
63
|
+
queueMicrotask(resolve);
|
|
64
|
+
})
|
|
65
|
+
: Promise.resolve();
|
|
66
|
+
const task = gate
|
|
67
|
+
.then(drain)
|
|
68
|
+
.finally(() => {
|
|
69
|
+
if (running === task)
|
|
70
|
+
running = null;
|
|
71
|
+
if (pending.length > 0 && isEnabled())
|
|
72
|
+
scheduleBackground();
|
|
73
|
+
});
|
|
74
|
+
running = task;
|
|
75
|
+
return task;
|
|
76
|
+
};
|
|
77
|
+
const scheduleBackground = () => {
|
|
78
|
+
const work = start(true);
|
|
79
|
+
try {
|
|
80
|
+
config.armature?.schedule?.(work);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
warn("schedule hook threw; background work remains active:", error);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
const flush = async () => {
|
|
87
|
+
if (!isEnabled())
|
|
88
|
+
return;
|
|
89
|
+
while (running || pending.length > 0) {
|
|
90
|
+
await (running ?? start(false));
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const enqueue = (finalize) => {
|
|
94
|
+
if (!isEnabled())
|
|
95
|
+
return Promise.resolve();
|
|
96
|
+
if (pending.length >= PRIVACY_QUEUE_CAPACITY) {
|
|
97
|
+
pending.shift();
|
|
98
|
+
dropped += 1;
|
|
99
|
+
if (!warnedDropped) {
|
|
100
|
+
warnedDropped = true;
|
|
101
|
+
warn(`privacy queue overflow; dropped ${dropped} oldest candidate(s)`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
pending.push(finalize);
|
|
105
|
+
if (config.armature?.delivery === "await")
|
|
106
|
+
return flush();
|
|
107
|
+
if (!running)
|
|
108
|
+
scheduleBackground();
|
|
109
|
+
return Promise.resolve();
|
|
110
|
+
};
|
|
111
|
+
return { enqueue, flush };
|
|
112
|
+
};
|
package/dist/esm/recorder.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
-
import { buildActorId, buildBatch, buildSessionInitBatch,
|
|
3
|
-
import {
|
|
2
|
+
import { buildActorId, buildActorIdentityEvent, buildBatch, buildSessionInitBatch, finalizeToolCallEvent, normalizeRequestId, normalizeSessionId, normalizeStartedAt, } from "./events.js";
|
|
3
|
+
import { defaultMcpAnalyticsConfig, resolveActorSeed, resolveActorIdentifier, } from "./emit.js";
|
|
4
4
|
import { applyTelemetryFieldMap, extractTelemetryArguments, isCaptureEnabled, planToolTelemetry, TELEMETRY_PROPERTY_DESCRIPTION, USER_INTENT_DESCRIPTION, } from "./schema.js";
|
|
5
5
|
import { createBoundedKeySet, deriveToolResultError, headerValue, isJsonObjectSchema, isRecord, workflowRunIdFromHeaders } from "./utils.js";
|
|
6
6
|
import { processScopedSessionId } from "./stdio-session.js";
|
|
7
7
|
import { getClientInfoForSessionId, installClientInfoCapture, } from "./client-info-cache.js";
|
|
8
8
|
import { parseStatelessSessionClientInfo } from "./stateless-http.js";
|
|
9
|
+
import { handleRequestCapability, isRequestCapabilityEnabled, REQUEST_CAPABILITY_DESCRIPTION, REQUEST_CAPABILITY_INPUT_SCHEMA, REQUEST_CAPABILITY_TOOL_NAME, REQUEST_CAPABILITY_ZOD_SHAPE, } from "./request-capability.js";
|
|
10
|
+
import { createPrivacyQueue } from "./queue.js";
|
|
9
11
|
const nudgeTelemetryDescriptions = (schema) => {
|
|
10
12
|
if (!isJsonObjectSchema(schema))
|
|
11
13
|
return schema;
|
|
@@ -31,11 +33,13 @@ const nudgeTelemetryDescriptions = (schema) => {
|
|
|
31
33
|
};
|
|
32
34
|
};
|
|
33
35
|
const createAnalyticsContext = async (config, input) => {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
+
const actorIdentifier = await resolveActorIdentifier(config, input);
|
|
37
|
+
const actorSeed = actorIdentifier ?? await resolveActorSeed(config, input);
|
|
38
|
+
return { actorId: buildActorId({ actorSeed }), actorIdentifier };
|
|
36
39
|
};
|
|
37
40
|
export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
38
|
-
const
|
|
41
|
+
const privacyQueue = createPrivacyQueue(config);
|
|
42
|
+
const flush = privacyQueue.flush;
|
|
39
43
|
// Tracks which (actorId, sessionId) pairs have already emitted a session_init,
|
|
40
44
|
// so we emit it at most once per session. Bounded with FIFO eviction: MCP
|
|
41
45
|
// gives no reliable session-closed signal, so an unbounded set would leak on
|
|
@@ -43,6 +47,26 @@ export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
|
43
47
|
// session_init event_id is now stable per (actorId, sessionId), so a re-emit
|
|
44
48
|
// after eviction collapses to the same id at ingest. 10k × ~60 bytes ≈ 600KB.
|
|
45
49
|
const sessionInitKeys = createBoundedKeySet(10_000);
|
|
50
|
+
// Per-process change detection. Event ids are content-addressed, so an
|
|
51
|
+
// identical re-emit after restart is harmless and converges at ingest.
|
|
52
|
+
const actorIdentifiers = new Map();
|
|
53
|
+
const identityEventFor = (context, startedAt) => {
|
|
54
|
+
if (context.actorIdentifier === undefined)
|
|
55
|
+
return undefined;
|
|
56
|
+
if (actorIdentifiers.get(context.actorId) === context.actorIdentifier)
|
|
57
|
+
return undefined;
|
|
58
|
+
actorIdentifiers.set(context.actorId, context.actorIdentifier);
|
|
59
|
+
if (actorIdentifiers.size > 10_000) {
|
|
60
|
+
const oldest = actorIdentifiers.keys().next().value;
|
|
61
|
+
if (oldest !== undefined)
|
|
62
|
+
actorIdentifiers.delete(oldest);
|
|
63
|
+
}
|
|
64
|
+
return buildActorIdentityEvent({
|
|
65
|
+
actorId: context.actorId,
|
|
66
|
+
identifier: context.actorIdentifier,
|
|
67
|
+
startedAt,
|
|
68
|
+
});
|
|
69
|
+
};
|
|
46
70
|
// Patch the SDK's Server.prototype the first time any recorder is created
|
|
47
71
|
// so that the very next `initialize` handshake feeds the per-session client
|
|
48
72
|
// info cache. Without this the dashboard's "Client" column would stay at
|
|
@@ -102,30 +126,33 @@ export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
|
102
126
|
const sessionId = resolveSessionId(event);
|
|
103
127
|
if (!sessionId)
|
|
104
128
|
return;
|
|
105
|
-
const context = await analyticsContextFor({
|
|
106
|
-
ctx: event.ctx,
|
|
107
|
-
extra: event.extra,
|
|
108
|
-
headers: event.headers ?? event.extra?.requestInfo?.headers,
|
|
109
|
-
authInfo: event.authInfo ?? event.extra?.authInfo,
|
|
110
|
-
});
|
|
111
129
|
const finishedAtMs = Date.now();
|
|
112
130
|
const startedAt = normalizeStartedAt({
|
|
113
131
|
startedAt: event.startedAt,
|
|
114
132
|
finishedAtMs,
|
|
115
133
|
});
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
134
|
+
const workflowRunId = resolveWorkflowRunId(event);
|
|
135
|
+
return privacyQueue.enqueue(async () => {
|
|
136
|
+
const context = await analyticsContextFor({
|
|
137
|
+
ctx: event.ctx,
|
|
138
|
+
extra: event.extra,
|
|
139
|
+
headers: event.headers ?? event.extra?.requestInfo?.headers,
|
|
140
|
+
authInfo: event.authInfo ?? event.extra?.authInfo,
|
|
141
|
+
});
|
|
142
|
+
const batch = buildSessionInitBatch({
|
|
143
|
+
actorId: context.actorId,
|
|
144
|
+
sessionId,
|
|
145
|
+
startedAt,
|
|
146
|
+
extra: event.extra,
|
|
147
|
+
sessionInitKeys,
|
|
148
|
+
clientInfo: event.clientInfo
|
|
149
|
+
?? getClientInfoForSessionId(sessionId)
|
|
150
|
+
?? parseStatelessSessionClientInfo(sessionId),
|
|
151
|
+
workflowRunId,
|
|
152
|
+
identityEvent: identityEventFor(context, startedAt),
|
|
153
|
+
});
|
|
154
|
+
return batch?.events ?? null;
|
|
126
155
|
});
|
|
127
|
-
if (batch)
|
|
128
|
-
await emitBatch(batch);
|
|
129
156
|
};
|
|
130
157
|
const recordToolCall = async (event) => {
|
|
131
158
|
// Single choke point for capture-off and field ownership
|
|
@@ -139,14 +166,6 @@ export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
|
139
166
|
const telemetry = isCaptureEnabled(config)
|
|
140
167
|
? applyTelemetryFieldMap(ownedTool ? undefined : event.telemetry, event.args, config.armature?.telemetryFieldMap)
|
|
141
168
|
: undefined;
|
|
142
|
-
const context = await analyticsContextFor({
|
|
143
|
-
ctx: event.ctx,
|
|
144
|
-
extra: event.extra,
|
|
145
|
-
headers: event.headers ?? event.extra?.requestInfo?.headers,
|
|
146
|
-
authInfo: event.authInfo ?? event.extra?.authInfo,
|
|
147
|
-
toolName: event.name,
|
|
148
|
-
telemetry,
|
|
149
|
-
});
|
|
150
169
|
const finishedAtMs = Date.now();
|
|
151
170
|
const finishedAt = new Date(finishedAtMs).toISOString();
|
|
152
171
|
const durationMs = event.durationMs ?? 0;
|
|
@@ -163,40 +182,68 @@ export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
|
163
182
|
? event.error.message
|
|
164
183
|
: String(event.error);
|
|
165
184
|
const workflowRunId = resolveWorkflowRunId(event);
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
185
|
+
return privacyQueue.enqueue(async () => {
|
|
186
|
+
const context = await analyticsContextFor({
|
|
187
|
+
ctx: event.ctx,
|
|
188
|
+
extra: event.extra,
|
|
189
|
+
headers: event.headers ?? event.extra?.requestInfo?.headers,
|
|
190
|
+
authInfo: event.authInfo ?? event.extra?.authInfo,
|
|
191
|
+
toolName: event.name,
|
|
192
|
+
telemetry,
|
|
193
|
+
});
|
|
194
|
+
const toolCallEvent = await finalizeToolCallEvent({
|
|
195
|
+
toolName: event.name,
|
|
196
|
+
telemetry,
|
|
197
|
+
input: event.args,
|
|
198
|
+
output: event.result,
|
|
199
|
+
status: event.status,
|
|
200
|
+
durationMs,
|
|
201
|
+
errorMessage,
|
|
202
|
+
actorId: context.actorId,
|
|
203
|
+
sessionId,
|
|
204
|
+
requestId,
|
|
205
|
+
startedAt,
|
|
206
|
+
finishedAt,
|
|
207
|
+
workflowRunId,
|
|
208
|
+
capabilityRequest: event.capabilityRequest,
|
|
209
|
+
redact: config.armature?.redact,
|
|
210
|
+
redactSecrets: config.armature?.redactSecrets,
|
|
211
|
+
redactEvent: config.armature?.redactEvent,
|
|
212
|
+
});
|
|
213
|
+
const effectiveClientInfo = event.clientInfo
|
|
214
|
+
?? getClientInfoForSessionId(sessionId)
|
|
215
|
+
?? parseStatelessSessionClientInfo(sessionId);
|
|
216
|
+
const identityEvent = identityEventFor(context, startedAt);
|
|
217
|
+
const extra = {
|
|
191
218
|
...(event.extra ?? {}),
|
|
192
219
|
...(sessionId ? { sessionId } : {}),
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
220
|
+
};
|
|
221
|
+
if (toolCallEvent) {
|
|
222
|
+
return buildBatch({
|
|
223
|
+
event: toolCallEvent,
|
|
224
|
+
extra,
|
|
225
|
+
actorId: context.actorId,
|
|
226
|
+
startedAt,
|
|
227
|
+
sessionInitKeys,
|
|
228
|
+
clientInfo: effectiveClientInfo,
|
|
229
|
+
workflowRunId,
|
|
230
|
+
identityEvent,
|
|
231
|
+
}).events;
|
|
232
|
+
}
|
|
233
|
+
if (sessionId) {
|
|
234
|
+
return buildSessionInitBatch({
|
|
235
|
+
actorId: context.actorId,
|
|
236
|
+
sessionId,
|
|
237
|
+
startedAt,
|
|
238
|
+
extra,
|
|
239
|
+
sessionInitKeys,
|
|
240
|
+
clientInfo: effectiveClientInfo,
|
|
241
|
+
workflowRunId,
|
|
242
|
+
identityEvent,
|
|
243
|
+
})?.events ?? null;
|
|
244
|
+
}
|
|
245
|
+
return identityEvent ? [identityEvent] : null;
|
|
246
|
+
});
|
|
200
247
|
};
|
|
201
248
|
const registeredTools = new Map();
|
|
202
249
|
const instrumentToolCall = async (event, handler) => {
|
|
@@ -239,7 +286,13 @@ export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
|
239
286
|
if (!tool) {
|
|
240
287
|
throw new Error(`Unknown tool: ${name}`);
|
|
241
288
|
}
|
|
242
|
-
return instrumentToolCall({
|
|
289
|
+
return instrumentToolCall({
|
|
290
|
+
name,
|
|
291
|
+
args: rawArgs,
|
|
292
|
+
telemetryMode: tool.telemetryMode,
|
|
293
|
+
capabilityRequest: tool.internal,
|
|
294
|
+
...context,
|
|
295
|
+
}, (args) => tool.handler(args, context));
|
|
243
296
|
};
|
|
244
297
|
let attachedServer = null;
|
|
245
298
|
const buildHandlerContext = (extra) => ({
|
|
@@ -255,9 +308,15 @@ export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
|
255
308
|
headers: extra?.requestInfo?.headers,
|
|
256
309
|
ctx: extra,
|
|
257
310
|
});
|
|
258
|
-
const registerWithServer = (server, registration, handler) => {
|
|
311
|
+
const registerWithServer = (server, registration, handler, internal = false) => {
|
|
259
312
|
const originalHasInputSchema = registration.inputSchema !== undefined;
|
|
260
|
-
const plan =
|
|
313
|
+
const plan = internal
|
|
314
|
+
? {
|
|
315
|
+
mode: "scrub",
|
|
316
|
+
inputSchema: registration.inputSchema,
|
|
317
|
+
applyDescription: (description) => description,
|
|
318
|
+
}
|
|
319
|
+
: planToolTelemetry(registration.name, registration.inputSchema, config);
|
|
261
320
|
// The MCP SDK passes (args, extra) to the callback whenever the registered
|
|
262
321
|
// tool has an input schema, and just (extra) when it has none — so the
|
|
263
322
|
// positional juggling below keys on what we actually registered, which for
|
|
@@ -283,17 +342,23 @@ export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
|
283
342
|
extra,
|
|
284
343
|
sessionId: extra?.sessionId,
|
|
285
344
|
telemetryMode: plan.mode,
|
|
345
|
+
capabilityRequest: internal,
|
|
286
346
|
}, (strippedArgs) => handler(originalHasInputSchema ? strippedArgs : {}, buildHandlerContext(extra)));
|
|
287
347
|
}));
|
|
288
348
|
};
|
|
289
349
|
const tool = (registration, handler) => {
|
|
350
|
+
if (isRequestCapabilityEnabled(config)
|
|
351
|
+
&& registration.name === REQUEST_CAPABILITY_TOOL_NAME) {
|
|
352
|
+
throw new Error(`Tool name "${REQUEST_CAPABILITY_TOOL_NAME}" is reserved while armature.requestCapability is enabled.`);
|
|
353
|
+
}
|
|
290
354
|
registeredTools.set(registration.name, {
|
|
291
355
|
registration,
|
|
292
356
|
handler: handler,
|
|
293
357
|
telemetryMode: planToolTelemetry(registration.name, registration.inputSchema, config).mode,
|
|
358
|
+
internal: false,
|
|
294
359
|
});
|
|
295
360
|
if (attachedServer) {
|
|
296
|
-
registerWithServer(attachedServer, registration, handler);
|
|
361
|
+
registerWithServer(attachedServer, registration, handler, false);
|
|
297
362
|
}
|
|
298
363
|
return (rawArgs, context = {}) => dispatch(registration.name, rawArgs, context);
|
|
299
364
|
};
|
|
@@ -302,8 +367,8 @@ export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
|
302
367
|
throw new Error("This recorder is already attached to an McpServer.");
|
|
303
368
|
}
|
|
304
369
|
attachedServer = server;
|
|
305
|
-
for (const { registration, handler } of registeredTools.values()) {
|
|
306
|
-
registerWithServer(server, registration, handler);
|
|
370
|
+
for (const { registration, handler, internal } of registeredTools.values()) {
|
|
371
|
+
registerWithServer(server, registration, handler, internal);
|
|
307
372
|
}
|
|
308
373
|
return server;
|
|
309
374
|
};
|
|
@@ -311,7 +376,16 @@ export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
|
311
376
|
return attachToMcpServer(new McpServer(info));
|
|
312
377
|
};
|
|
313
378
|
const toolDefinitions = () => {
|
|
314
|
-
|
|
379
|
+
const definitions = [];
|
|
380
|
+
for (const { registration, internal } of registeredTools.values()) {
|
|
381
|
+
if (internal) {
|
|
382
|
+
definitions.push({
|
|
383
|
+
name: REQUEST_CAPABILITY_TOOL_NAME,
|
|
384
|
+
description: REQUEST_CAPABILITY_DESCRIPTION,
|
|
385
|
+
inputSchema: REQUEST_CAPABILITY_INPUT_SCHEMA,
|
|
386
|
+
});
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
315
389
|
const definition = { name: registration.name };
|
|
316
390
|
if (registration.title !== undefined)
|
|
317
391
|
definition.title = registration.title;
|
|
@@ -321,10 +395,23 @@ export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
|
321
395
|
if (registration.inputSchema !== undefined) {
|
|
322
396
|
definition.inputSchema = registration.inputSchema;
|
|
323
397
|
}
|
|
324
|
-
|
|
325
|
-
}
|
|
398
|
+
definitions.push(...decorateDefinitions([definition]));
|
|
399
|
+
}
|
|
400
|
+
return definitions;
|
|
326
401
|
};
|
|
327
402
|
const hasTool = (name) => registeredTools.has(name);
|
|
403
|
+
if (isRequestCapabilityEnabled(config)) {
|
|
404
|
+
registeredTools.set(REQUEST_CAPABILITY_TOOL_NAME, {
|
|
405
|
+
registration: {
|
|
406
|
+
name: REQUEST_CAPABILITY_TOOL_NAME,
|
|
407
|
+
description: REQUEST_CAPABILITY_DESCRIPTION,
|
|
408
|
+
inputSchema: REQUEST_CAPABILITY_ZOD_SHAPE,
|
|
409
|
+
},
|
|
410
|
+
handler: handleRequestCapability,
|
|
411
|
+
telemetryMode: "scrub",
|
|
412
|
+
internal: true,
|
|
413
|
+
});
|
|
414
|
+
}
|
|
328
415
|
return {
|
|
329
416
|
decorateDefinitions,
|
|
330
417
|
extractTelemetry: extractTelemetryArguments,
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type SecretPatternRule = {
|
|
2
|
+
id: string;
|
|
3
|
+
pattern: RegExp;
|
|
4
|
+
replacement: string;
|
|
5
|
+
};
|
|
6
|
+
export declare const SECRET_PATTERN_RULES: readonly SecretPatternRule[];
|
|
7
|
+
export declare const SENSITIVE_FIELD_NAMES: Set<string>;
|
|
8
|
+
export declare const normalizeSensitiveFieldName: (key: string) => string;
|
|
9
|
+
export declare const redactSecretsInString: (value: string) => string;
|
|
10
|
+
export declare const redactSecretsInValue: (value: unknown, seen?: WeakSet<object>) => unknown;
|