@armature-tech/mcp-analytics 0.6.18 → 0.6.20
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 +65 -0
- package/dist/cjs/doctor.js +342 -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 +65 -0
- package/dist/esm/doctor.js +331 -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/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;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { isRecord } from "./utils.js";
|
|
2
|
+
// Contract order is significant. Keep these expressions RE2-compatible so
|
|
3
|
+
// the TypeScript, Python, and Go implementations continue to agree.
|
|
4
|
+
export const SECRET_PATTERN_RULES = [
|
|
5
|
+
{
|
|
6
|
+
id: "pem",
|
|
7
|
+
pattern: /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z0-9 ]*PRIVATE KEY-----/g,
|
|
8
|
+
replacement: "[redacted:pem]",
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
id: "sensitive-kv",
|
|
12
|
+
pattern: /\b(password|passwd|pwd|secret|api[_-]?key|access[_-]?token|refresh[_-]?token|client[_-]?secret|private[_-]?key|authorization)([=:])([^\s"'`,;&]{4,})/gi,
|
|
13
|
+
replacement: "$1$2[redacted:sensitive-kv]",
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
id: "aws-access-key-id",
|
|
17
|
+
pattern: /\b(?:AKIA|ASIA|ABIA|ACCA|AGPA|AIDA|AIPA|ANPA|ANVA|AROA)[A-Z0-9]{16}\b/g,
|
|
18
|
+
replacement: "[redacted:aws-access-key-id]",
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
id: "github-token",
|
|
22
|
+
pattern: /\b(?:gh[pousr]_[A-Za-z0-9]{36}|github_pat_[A-Za-z0-9_]{22,255})\b/g,
|
|
23
|
+
replacement: "[redacted:github-token]",
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
id: "google-api-key",
|
|
27
|
+
pattern: /\bAIza[0-9A-Za-z_-]{35}\b/g,
|
|
28
|
+
replacement: "[redacted:google-api-key]",
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
id: "slack-token",
|
|
32
|
+
pattern: /\bxox[abprs]-[A-Za-z0-9-]{10,}\b/g,
|
|
33
|
+
replacement: "[redacted:slack-token]",
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
id: "stripe-key",
|
|
37
|
+
pattern: /\b[rs]k_(?:live|test)_[A-Za-z0-9]{16,}\b/g,
|
|
38
|
+
replacement: "[redacted:stripe-key]",
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
id: "anthropic-api-key",
|
|
42
|
+
pattern: /\bsk-ant-[A-Za-z0-9_-]{16,}\b/g,
|
|
43
|
+
replacement: "[redacted:anthropic-api-key]",
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
id: "openai-api-key",
|
|
47
|
+
pattern: /\bsk-[A-Za-z0-9_-]{20,}\b/g,
|
|
48
|
+
replacement: "[redacted:openai-api-key]",
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
id: "jwt",
|
|
52
|
+
pattern: /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{6,}\.[A-Za-z0-9_-]{10,}\b/g,
|
|
53
|
+
replacement: "[redacted:jwt]",
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
id: "connection-string",
|
|
57
|
+
pattern: /\b([a-zA-Z][a-zA-Z0-9+.-]*:\/\/[^\s:/@]+):([^\s@]+)@/g,
|
|
58
|
+
replacement: "$1:[redacted:connection-string]@",
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
id: "bearer",
|
|
62
|
+
pattern: /\b[Bb]earer +[A-Za-z0-9._~+/=-]{16,}/g,
|
|
63
|
+
replacement: "Bearer [redacted:bearer]",
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
id: "basic",
|
|
67
|
+
pattern: /\b[Bb]asic +[A-Za-z0-9+/=]{16,}/g,
|
|
68
|
+
replacement: "Basic [redacted:basic]",
|
|
69
|
+
},
|
|
70
|
+
];
|
|
71
|
+
export const SENSITIVE_FIELD_NAMES = new Set([
|
|
72
|
+
"password", "passwd", "pwd", "secret", "apikey", "accesskey",
|
|
73
|
+
"secretkey", "secretaccesskey", "token", "accesstoken", "refreshtoken",
|
|
74
|
+
"idtoken", "sessiontoken", "authorization", "auth", "clientsecret",
|
|
75
|
+
"privatekey", "credential", "credentials", "connectionstring",
|
|
76
|
+
"databaseurl", "dsn",
|
|
77
|
+
]);
|
|
78
|
+
export const normalizeSensitiveFieldName = (key) => {
|
|
79
|
+
return key.toLowerCase().replace(/[_-]/g, "");
|
|
80
|
+
};
|
|
81
|
+
export const redactSecretsInString = (value) => {
|
|
82
|
+
let redacted = value;
|
|
83
|
+
for (const rule of SECRET_PATTERN_RULES) {
|
|
84
|
+
redacted = redacted.replace(rule.pattern, rule.replacement);
|
|
85
|
+
}
|
|
86
|
+
return redacted;
|
|
87
|
+
};
|
|
88
|
+
export const redactSecretsInValue = (value, seen) => {
|
|
89
|
+
if (typeof value === "string")
|
|
90
|
+
return redactSecretsInString(value);
|
|
91
|
+
if (typeof value !== "object" || value === null)
|
|
92
|
+
return value;
|
|
93
|
+
const tracked = seen ?? new WeakSet();
|
|
94
|
+
if (tracked.has(value))
|
|
95
|
+
return "[circular]";
|
|
96
|
+
tracked.add(value);
|
|
97
|
+
try {
|
|
98
|
+
if (Array.isArray(value)) {
|
|
99
|
+
return value.map((item) => redactSecretsInValue(item, tracked));
|
|
100
|
+
}
|
|
101
|
+
if (!isRecord(value))
|
|
102
|
+
return value;
|
|
103
|
+
const out = {};
|
|
104
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
105
|
+
out[key] = typeof entry === "string"
|
|
106
|
+
&& SENSITIVE_FIELD_NAMES.has(normalizeSensitiveFieldName(key))
|
|
107
|
+
? "[redacted:sensitive-field]"
|
|
108
|
+
: redactSecretsInValue(entry, tracked);
|
|
109
|
+
}
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
tracked.delete(value);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
|
|
3
|
+
import type { JsonObjectSchema, McpAnalyticsConfig } from "./types.js";
|
|
4
|
+
export declare const REQUEST_CAPABILITY_TOOL_NAME = "request_capability";
|
|
5
|
+
export declare const REQUEST_CAPABILITY_DESCRIPTION = "Request a capability that is not provided by the currently available tools. Use this when a capability is required to complete the user\u2019s request and no existing tool can perform it.";
|
|
6
|
+
export declare const REQUEST_CAPABILITY_ARGUMENT_DESCRIPTION = "The capability required to complete the user's request. Omit argument values, PII, and secrets. Use English.";
|
|
7
|
+
export declare const REQUEST_CAPABILITY_INPUT_SCHEMA: JsonObjectSchema;
|
|
8
|
+
export declare const REQUEST_CAPABILITY_ZOD_SHAPE: {
|
|
9
|
+
capability: z.ZodString;
|
|
10
|
+
};
|
|
11
|
+
export declare const isRequestCapabilityEnabled: (config: McpAnalyticsConfig) => boolean;
|
|
12
|
+
export declare const handleRequestCapability: (args: unknown) => CallToolResult;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { resolveApiKey } from "./emit.js";
|
|
3
|
+
import { isRecord } from "./utils.js";
|
|
4
|
+
export const REQUEST_CAPABILITY_TOOL_NAME = "request_capability";
|
|
5
|
+
export const REQUEST_CAPABILITY_DESCRIPTION = "Request a capability that is not provided by the currently available tools. Use this when a capability is required to complete the user’s request and no existing tool can perform it.";
|
|
6
|
+
export const REQUEST_CAPABILITY_ARGUMENT_DESCRIPTION = "The capability required to complete the user's request. Omit argument values, PII, and secrets. Use English.";
|
|
7
|
+
export const REQUEST_CAPABILITY_INPUT_SCHEMA = {
|
|
8
|
+
type: "object",
|
|
9
|
+
properties: {
|
|
10
|
+
capability: {
|
|
11
|
+
type: "string",
|
|
12
|
+
description: REQUEST_CAPABILITY_ARGUMENT_DESCRIPTION,
|
|
13
|
+
minLength: 1,
|
|
14
|
+
maxLength: 1000,
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
required: ["capability"],
|
|
18
|
+
additionalProperties: false,
|
|
19
|
+
};
|
|
20
|
+
// McpServer.registerTool accepts a Zod raw shape, while custom dispatchers
|
|
21
|
+
// consume the JSON Schema above from recorder.toolDefinitions().
|
|
22
|
+
export const REQUEST_CAPABILITY_ZOD_SHAPE = {
|
|
23
|
+
capability: z
|
|
24
|
+
.string()
|
|
25
|
+
.min(1)
|
|
26
|
+
.max(1000)
|
|
27
|
+
.describe(REQUEST_CAPABILITY_ARGUMENT_DESCRIPTION),
|
|
28
|
+
};
|
|
29
|
+
export const isRequestCapabilityEnabled = (config) => config.armature?.requestCapability === true
|
|
30
|
+
&& config.armature?.enabled !== false
|
|
31
|
+
&& (typeof config.armature?.emit === "function" || Boolean(resolveApiKey(config)));
|
|
32
|
+
export const handleRequestCapability = (args) => {
|
|
33
|
+
if (!isRecord(args)
|
|
34
|
+
|| typeof args.capability !== "string"
|
|
35
|
+
|| args.capability.trim().length === 0
|
|
36
|
+
|| args.capability.length > 1000) {
|
|
37
|
+
return {
|
|
38
|
+
isError: true,
|
|
39
|
+
content: [{ type: "text", text: "capability must be a non-empty string" }],
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
content: [{ type: "text", text: "Capability request acknowledged." }],
|
|
44
|
+
};
|
|
45
|
+
};
|
package/dist/esm/sanitize.d.ts
CHANGED
|
@@ -2,5 +2,9 @@ import type { RedactFunction } from "./types.js";
|
|
|
2
2
|
export declare const BINARY_REMOVED_PLACEHOLDER = "[binary removed]";
|
|
3
3
|
export declare const BASE64_REMOVED_PLACEHOLDER = "[base64 removed]";
|
|
4
4
|
export declare const REDACTION_FAILED_PLACEHOLDER = "[redaction failed]";
|
|
5
|
+
export declare const SANITIZATION_BUDGET = 65536;
|
|
5
6
|
export declare const sanitizeValue: (value: unknown, seen?: WeakSet<object>) => unknown;
|
|
6
|
-
export
|
|
7
|
+
export type PrepareForPreviewOptions = {
|
|
8
|
+
redactSecrets?: boolean;
|
|
9
|
+
};
|
|
10
|
+
export declare const prepareForPreview: (value: unknown, redact?: RedactFunction, options?: PrepareForPreviewOptions) => unknown;
|
package/dist/esm/sanitize.js
CHANGED
|
@@ -1,18 +1,11 @@
|
|
|
1
|
+
import { redactSecretsInValue } from "./redact-secrets.js";
|
|
1
2
|
import { isRecord } from "./utils.js";
|
|
2
|
-
// Placeholder strings are part of the cross-SDK contract
|
|
3
|
-
// (packages/TELEMETRY-CONTRACT.md) — golden tests in all three SDKs assert
|
|
4
|
-
// them byte-for-byte.
|
|
5
3
|
export const BINARY_REMOVED_PLACEHOLDER = "[binary removed]";
|
|
6
4
|
export const BASE64_REMOVED_PLACEHOLDER = "[base64 removed]";
|
|
7
5
|
export const REDACTION_FAILED_PLACEHOLDER = "[redaction failed]";
|
|
8
|
-
|
|
9
|
-
// strings need the higher bar (length + strict charset) so prose, ids, and
|
|
10
|
-
// hashes below half a KB pass through untouched. Both thresholds are contract
|
|
11
|
-
// values — keep in sync with the Python and Go SDKs.
|
|
6
|
+
export const SANITIZATION_BUDGET = 65_536;
|
|
12
7
|
const DATA_URI_MIN_CHARS = 64;
|
|
13
8
|
const BASE64_MIN_CHARS = 512;
|
|
14
|
-
// Strict charset on purpose: no whitespace, so long prose (letters + spaces)
|
|
15
|
-
// never matches. Covers standard base64 and base64url, with optional padding.
|
|
16
9
|
const BASE64_RE = /^[A-Za-z0-9+/_-]+={0,2}$/;
|
|
17
10
|
const isBase64Payload = (value) => {
|
|
18
11
|
if (value.length >= DATA_URI_MIN_CHARS && value.startsWith("data:") && value.includes(";base64,")) {
|
|
@@ -23,59 +16,80 @@ const isBase64Payload = (value) => {
|
|
|
23
16
|
const sanitizeString = (value) => {
|
|
24
17
|
return isBase64Payload(value) ? BASE64_REMOVED_PLACEHOLDER : value;
|
|
25
18
|
};
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (typeof value === "string")
|
|
36
|
-
|
|
19
|
+
const charge = (budget, units) => {
|
|
20
|
+
if (budget.remaining < units) {
|
|
21
|
+
budget.remaining = 0;
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
budget.remaining -= units;
|
|
25
|
+
return true;
|
|
26
|
+
};
|
|
27
|
+
const sanitizeValueBounded = (value, seen, budget) => {
|
|
28
|
+
if (typeof value === "string") {
|
|
29
|
+
const sanitized = sanitizeString(value);
|
|
30
|
+
if (sanitized.length <= budget.remaining) {
|
|
31
|
+
budget.remaining -= sanitized.length;
|
|
32
|
+
return sanitized;
|
|
33
|
+
}
|
|
34
|
+
const sliced = sanitized.slice(0, budget.remaining);
|
|
35
|
+
budget.remaining = 0;
|
|
36
|
+
return sliced;
|
|
37
|
+
}
|
|
37
38
|
if (typeof value !== "object" || value === null)
|
|
38
39
|
return value;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
tracked.add(value);
|
|
40
|
+
if (seen.has(value))
|
|
41
|
+
return sanitizeValueBounded("[circular]", seen, budget);
|
|
42
|
+
seen.add(value);
|
|
43
43
|
try {
|
|
44
44
|
if (Array.isArray(value)) {
|
|
45
|
-
|
|
45
|
+
const out = [];
|
|
46
|
+
for (const item of value) {
|
|
47
|
+
if (!charge(budget, 2))
|
|
48
|
+
break;
|
|
49
|
+
out.push(sanitizeValueBounded(item, seen, budget));
|
|
50
|
+
if (budget.remaining === 0)
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
return out;
|
|
46
54
|
}
|
|
47
55
|
if (!isRecord(value))
|
|
48
56
|
return value;
|
|
49
57
|
const out = {};
|
|
50
58
|
for (const [key, entry] of Object.entries(value)) {
|
|
59
|
+
if (!charge(budget, key.length + 2))
|
|
60
|
+
break;
|
|
51
61
|
if (key === "data"
|
|
52
62
|
&& typeof entry === "string"
|
|
53
63
|
&& (value.type === "image" || value.type === "audio")) {
|
|
54
|
-
out[key] = BINARY_REMOVED_PLACEHOLDER;
|
|
64
|
+
out[key] = sanitizeValueBounded(BINARY_REMOVED_PLACEHOLDER, seen, budget);
|
|
55
65
|
}
|
|
56
66
|
else if (key === "blob" && typeof entry === "string") {
|
|
57
|
-
out[key] = BINARY_REMOVED_PLACEHOLDER;
|
|
67
|
+
out[key] = sanitizeValueBounded(BINARY_REMOVED_PLACEHOLDER, seen, budget);
|
|
58
68
|
}
|
|
59
69
|
else {
|
|
60
|
-
out[key] =
|
|
70
|
+
out[key] = sanitizeValueBounded(entry, seen, budget);
|
|
61
71
|
}
|
|
72
|
+
if (budget.remaining === 0)
|
|
73
|
+
break;
|
|
62
74
|
}
|
|
63
75
|
return out;
|
|
64
76
|
}
|
|
65
77
|
finally {
|
|
66
|
-
|
|
78
|
+
seen.delete(value);
|
|
67
79
|
}
|
|
68
80
|
};
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
export const prepareForPreview = (value, redact) => {
|
|
81
|
+
export const sanitizeValue = (value, seen) => {
|
|
82
|
+
return sanitizeValueBounded(value, seen ?? new WeakSet(), { remaining: SANITIZATION_BUDGET });
|
|
83
|
+
};
|
|
84
|
+
export const prepareForPreview = (value, redact, options = {}) => {
|
|
74
85
|
const sanitized = sanitizeValue(value);
|
|
86
|
+
const protectedValue = options.redactSecrets === false
|
|
87
|
+
? sanitized
|
|
88
|
+
: redactSecretsInValue(sanitized);
|
|
75
89
|
if (!redact)
|
|
76
|
-
return
|
|
90
|
+
return protectedValue;
|
|
77
91
|
try {
|
|
78
|
-
return redact(
|
|
92
|
+
return redact(protectedValue);
|
|
79
93
|
}
|
|
80
94
|
catch {
|
|
81
95
|
return REDACTION_FAILED_PLACEHOLDER;
|