@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/recorder.js
CHANGED
|
@@ -9,6 +9,8 @@ const utils_js_1 = require("./utils.js");
|
|
|
9
9
|
const stdio_session_js_1 = require("./stdio-session.js");
|
|
10
10
|
const client_info_cache_js_1 = require("./client-info-cache.js");
|
|
11
11
|
const stateless_http_js_1 = require("./stateless-http.js");
|
|
12
|
+
const request_capability_js_1 = require("./request-capability.js");
|
|
13
|
+
const queue_js_1 = require("./queue.js");
|
|
12
14
|
const nudgeTelemetryDescriptions = (schema) => {
|
|
13
15
|
if (!(0, utils_js_1.isJsonObjectSchema)(schema))
|
|
14
16
|
return schema;
|
|
@@ -34,11 +36,13 @@ const nudgeTelemetryDescriptions = (schema) => {
|
|
|
34
36
|
};
|
|
35
37
|
};
|
|
36
38
|
const createAnalyticsContext = async (config, input) => {
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
+
const actorIdentifier = await (0, emit_js_1.resolveActorIdentifier)(config, input);
|
|
40
|
+
const actorSeed = actorIdentifier ?? await (0, emit_js_1.resolveActorSeed)(config, input);
|
|
41
|
+
return { actorId: (0, events_js_1.buildActorId)({ actorSeed }), actorIdentifier };
|
|
39
42
|
};
|
|
40
43
|
const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) => {
|
|
41
|
-
const
|
|
44
|
+
const privacyQueue = (0, queue_js_1.createPrivacyQueue)(config);
|
|
45
|
+
const flush = privacyQueue.flush;
|
|
42
46
|
// Tracks which (actorId, sessionId) pairs have already emitted a session_init,
|
|
43
47
|
// so we emit it at most once per session. Bounded with FIFO eviction: MCP
|
|
44
48
|
// gives no reliable session-closed signal, so an unbounded set would leak on
|
|
@@ -46,6 +50,26 @@ const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) =
|
|
|
46
50
|
// session_init event_id is now stable per (actorId, sessionId), so a re-emit
|
|
47
51
|
// after eviction collapses to the same id at ingest. 10k × ~60 bytes ≈ 600KB.
|
|
48
52
|
const sessionInitKeys = (0, utils_js_1.createBoundedKeySet)(10_000);
|
|
53
|
+
// Per-process change detection. Event ids are content-addressed, so an
|
|
54
|
+
// identical re-emit after restart is harmless and converges at ingest.
|
|
55
|
+
const actorIdentifiers = new Map();
|
|
56
|
+
const identityEventFor = (context, startedAt) => {
|
|
57
|
+
if (context.actorIdentifier === undefined)
|
|
58
|
+
return undefined;
|
|
59
|
+
if (actorIdentifiers.get(context.actorId) === context.actorIdentifier)
|
|
60
|
+
return undefined;
|
|
61
|
+
actorIdentifiers.set(context.actorId, context.actorIdentifier);
|
|
62
|
+
if (actorIdentifiers.size > 10_000) {
|
|
63
|
+
const oldest = actorIdentifiers.keys().next().value;
|
|
64
|
+
if (oldest !== undefined)
|
|
65
|
+
actorIdentifiers.delete(oldest);
|
|
66
|
+
}
|
|
67
|
+
return (0, events_js_1.buildActorIdentityEvent)({
|
|
68
|
+
actorId: context.actorId,
|
|
69
|
+
identifier: context.actorIdentifier,
|
|
70
|
+
startedAt,
|
|
71
|
+
});
|
|
72
|
+
};
|
|
49
73
|
// Patch the SDK's Server.prototype the first time any recorder is created
|
|
50
74
|
// so that the very next `initialize` handshake feeds the per-session client
|
|
51
75
|
// info cache. Without this the dashboard's "Client" column would stay at
|
|
@@ -57,13 +81,18 @@ const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) =
|
|
|
57
81
|
};
|
|
58
82
|
const decorateDefinitions = (defs) => {
|
|
59
83
|
return defs.map((definition) => {
|
|
60
|
-
const
|
|
84
|
+
const plan = (0, schema_js_1.planToolTelemetry)(definition.name, definition.inputSchema ?? { type: "object", properties: {} }, config);
|
|
85
|
+
// Owned/scrub tools pass through undecorated — their advertised schema
|
|
86
|
+
// and description must keep matching what the handler actually receives.
|
|
87
|
+
if (plan.mode !== "injected") {
|
|
88
|
+
return { ...definition, inputSchema: plan.inputSchema };
|
|
89
|
+
}
|
|
61
90
|
return {
|
|
62
91
|
...definition,
|
|
63
|
-
description:
|
|
92
|
+
description: plan.applyDescription(typeof definition.description === "string"
|
|
64
93
|
? definition.description
|
|
65
94
|
: undefined),
|
|
66
|
-
inputSchema,
|
|
95
|
+
inputSchema: nudgeTelemetryDescriptions(plan.inputSchema),
|
|
67
96
|
};
|
|
68
97
|
});
|
|
69
98
|
};
|
|
@@ -100,40 +129,46 @@ const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) =
|
|
|
100
129
|
const sessionId = resolveSessionId(event);
|
|
101
130
|
if (!sessionId)
|
|
102
131
|
return;
|
|
103
|
-
const context = await analyticsContextFor({
|
|
104
|
-
ctx: event.ctx,
|
|
105
|
-
extra: event.extra,
|
|
106
|
-
headers: event.headers ?? event.extra?.requestInfo?.headers,
|
|
107
|
-
authInfo: event.authInfo ?? event.extra?.authInfo,
|
|
108
|
-
});
|
|
109
132
|
const finishedAtMs = Date.now();
|
|
110
133
|
const startedAt = (0, events_js_1.normalizeStartedAt)({
|
|
111
134
|
startedAt: event.startedAt,
|
|
112
135
|
finishedAtMs,
|
|
113
136
|
});
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
137
|
+
const workflowRunId = resolveWorkflowRunId(event);
|
|
138
|
+
return privacyQueue.enqueue(async () => {
|
|
139
|
+
const context = await analyticsContextFor({
|
|
140
|
+
ctx: event.ctx,
|
|
141
|
+
extra: event.extra,
|
|
142
|
+
headers: event.headers ?? event.extra?.requestInfo?.headers,
|
|
143
|
+
authInfo: event.authInfo ?? event.extra?.authInfo,
|
|
144
|
+
});
|
|
145
|
+
const batch = (0, events_js_1.buildSessionInitBatch)({
|
|
146
|
+
actorId: context.actorId,
|
|
147
|
+
sessionId,
|
|
148
|
+
startedAt,
|
|
149
|
+
extra: event.extra,
|
|
150
|
+
sessionInitKeys,
|
|
151
|
+
clientInfo: event.clientInfo
|
|
152
|
+
?? (0, client_info_cache_js_1.getClientInfoForSessionId)(sessionId)
|
|
153
|
+
?? (0, stateless_http_js_1.parseStatelessSessionClientInfo)(sessionId),
|
|
154
|
+
workflowRunId,
|
|
155
|
+
identityEvent: identityEventFor(context, startedAt),
|
|
156
|
+
});
|
|
157
|
+
return batch?.events ?? null;
|
|
124
158
|
});
|
|
125
|
-
if (batch)
|
|
126
|
-
await emitBatch(batch);
|
|
127
159
|
};
|
|
128
160
|
const recordToolCall = async (event) => {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
161
|
+
// Single choke point for capture-off and field ownership
|
|
162
|
+
// (TELEMETRY-CONTRACT.md): telemetry handed in by any path — extraction,
|
|
163
|
+
// direct recordToolCall callers, a cached-schema client — is dropped here
|
|
164
|
+
// before it can reach the actor resolver, the event builder, `emit`, or
|
|
165
|
+
// `onError`. A registered tool that owns its telemetry field never exports
|
|
166
|
+
// supplied telemetry either; the opt-in field map is the explicit way to
|
|
167
|
+
// export customer fields, and it only applies while capture is on.
|
|
168
|
+
const ownedTool = registeredTools.get(event.name)?.telemetryMode === "owned";
|
|
169
|
+
const telemetry = (0, schema_js_1.isCaptureEnabled)(config)
|
|
170
|
+
? (0, schema_js_1.applyTelemetryFieldMap)(ownedTool ? undefined : event.telemetry, event.args, config.armature?.telemetryFieldMap)
|
|
171
|
+
: undefined;
|
|
137
172
|
const finishedAtMs = Date.now();
|
|
138
173
|
const finishedAt = new Date(finishedAtMs).toISOString();
|
|
139
174
|
const durationMs = event.durationMs ?? 0;
|
|
@@ -150,43 +185,72 @@ const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) =
|
|
|
150
185
|
? event.error.message
|
|
151
186
|
: String(event.error);
|
|
152
187
|
const workflowRunId = resolveWorkflowRunId(event);
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
188
|
+
return privacyQueue.enqueue(async () => {
|
|
189
|
+
const context = await analyticsContextFor({
|
|
190
|
+
ctx: event.ctx,
|
|
191
|
+
extra: event.extra,
|
|
192
|
+
headers: event.headers ?? event.extra?.requestInfo?.headers,
|
|
193
|
+
authInfo: event.authInfo ?? event.extra?.authInfo,
|
|
194
|
+
toolName: event.name,
|
|
195
|
+
telemetry,
|
|
196
|
+
});
|
|
197
|
+
const toolCallEvent = await (0, events_js_1.finalizeToolCallEvent)({
|
|
198
|
+
toolName: event.name,
|
|
199
|
+
telemetry,
|
|
200
|
+
input: event.args,
|
|
201
|
+
output: event.result,
|
|
202
|
+
status: event.status,
|
|
203
|
+
durationMs,
|
|
204
|
+
errorMessage,
|
|
205
|
+
actorId: context.actorId,
|
|
206
|
+
sessionId,
|
|
207
|
+
requestId,
|
|
208
|
+
startedAt,
|
|
209
|
+
finishedAt,
|
|
210
|
+
workflowRunId,
|
|
211
|
+
capabilityRequest: event.capabilityRequest,
|
|
212
|
+
redact: config.armature?.redact,
|
|
213
|
+
redactSecrets: config.armature?.redactSecrets,
|
|
214
|
+
redactEvent: config.armature?.redactEvent,
|
|
215
|
+
});
|
|
216
|
+
const effectiveClientInfo = event.clientInfo
|
|
217
|
+
?? (0, client_info_cache_js_1.getClientInfoForSessionId)(sessionId)
|
|
218
|
+
?? (0, stateless_http_js_1.parseStatelessSessionClientInfo)(sessionId);
|
|
219
|
+
const identityEvent = identityEventFor(context, startedAt);
|
|
220
|
+
const extra = {
|
|
177
221
|
...(event.extra ?? {}),
|
|
178
222
|
...(sessionId ? { sessionId } : {}),
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
223
|
+
};
|
|
224
|
+
if (toolCallEvent) {
|
|
225
|
+
return (0, events_js_1.buildBatch)({
|
|
226
|
+
event: toolCallEvent,
|
|
227
|
+
extra,
|
|
228
|
+
actorId: context.actorId,
|
|
229
|
+
startedAt,
|
|
230
|
+
sessionInitKeys,
|
|
231
|
+
clientInfo: effectiveClientInfo,
|
|
232
|
+
workflowRunId,
|
|
233
|
+
identityEvent,
|
|
234
|
+
}).events;
|
|
235
|
+
}
|
|
236
|
+
if (sessionId) {
|
|
237
|
+
return (0, events_js_1.buildSessionInitBatch)({
|
|
238
|
+
actorId: context.actorId,
|
|
239
|
+
sessionId,
|
|
240
|
+
startedAt,
|
|
241
|
+
extra,
|
|
242
|
+
sessionInitKeys,
|
|
243
|
+
clientInfo: effectiveClientInfo,
|
|
244
|
+
workflowRunId,
|
|
245
|
+
identityEvent,
|
|
246
|
+
})?.events ?? null;
|
|
247
|
+
}
|
|
248
|
+
return identityEvent ? [identityEvent] : null;
|
|
249
|
+
});
|
|
186
250
|
};
|
|
187
251
|
const registeredTools = new Map();
|
|
188
252
|
const instrumentToolCall = async (event, handler) => {
|
|
189
|
-
const { args, telemetry } = (0, schema_js_1.extractTelemetryArguments)(event.args);
|
|
253
|
+
const { args, telemetry } = (0, schema_js_1.extractTelemetryArguments)(event.args, event.telemetryMode ?? "injected");
|
|
190
254
|
const startedAtMs = Date.now();
|
|
191
255
|
const startedAt = new Date(startedAtMs).toISOString();
|
|
192
256
|
try {
|
|
@@ -225,7 +289,13 @@ const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) =
|
|
|
225
289
|
if (!tool) {
|
|
226
290
|
throw new Error(`Unknown tool: ${name}`);
|
|
227
291
|
}
|
|
228
|
-
return instrumentToolCall({
|
|
292
|
+
return instrumentToolCall({
|
|
293
|
+
name,
|
|
294
|
+
args: rawArgs,
|
|
295
|
+
telemetryMode: tool.telemetryMode,
|
|
296
|
+
capabilityRequest: tool.internal,
|
|
297
|
+
...context,
|
|
298
|
+
}, (args) => tool.handler(args, context));
|
|
229
299
|
};
|
|
230
300
|
let attachedServer = null;
|
|
231
301
|
const buildHandlerContext = (extra) => ({
|
|
@@ -241,36 +311,57 @@ const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) =
|
|
|
241
311
|
headers: extra?.requestInfo?.headers,
|
|
242
312
|
ctx: extra,
|
|
243
313
|
});
|
|
244
|
-
const registerWithServer = (server, registration, handler) => {
|
|
314
|
+
const registerWithServer = (server, registration, handler, internal = false) => {
|
|
245
315
|
const originalHasInputSchema = registration.inputSchema !== undefined;
|
|
246
|
-
const
|
|
316
|
+
const plan = internal
|
|
317
|
+
? {
|
|
318
|
+
mode: "scrub",
|
|
319
|
+
inputSchema: registration.inputSchema,
|
|
320
|
+
applyDescription: (description) => description,
|
|
321
|
+
}
|
|
322
|
+
: (0, schema_js_1.planToolTelemetry)(registration.name, registration.inputSchema, config);
|
|
323
|
+
// The MCP SDK passes (args, extra) to the callback whenever the registered
|
|
324
|
+
// tool has an input schema, and just (extra) when it has none — so the
|
|
325
|
+
// positional juggling below keys on what we actually registered, which for
|
|
326
|
+
// owned/scrub tools is the caller's original (possibly absent) schema.
|
|
327
|
+
const registeredHasInputSchema = plan.inputSchema !== undefined;
|
|
328
|
+
// Same nudge `decorateDefinitions` applies in the registry path — the
|
|
329
|
+
// caller-owned McpServer path must also tell agents to pass
|
|
330
|
+
// telemetry.user_intent, or sessions arrive with no intent (ARM-24).
|
|
331
|
+
// Owned/scrub tools keep their original description untouched.
|
|
332
|
+
const description = plan.applyDescription(registration.description);
|
|
247
333
|
server.registerTool(registration.name, {
|
|
248
334
|
...(registration.title !== undefined ? { title: registration.title } : {}),
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
// telemetry.user_intent, or sessions arrive with no intent (ARM-24).
|
|
252
|
-
description: (0, schema_js_1.appendTelemetryHint)(registration.description),
|
|
253
|
-
inputSchema: decoratedSchema,
|
|
335
|
+
...(description !== undefined ? { description } : {}),
|
|
336
|
+
...(registeredHasInputSchema ? { inputSchema: plan.inputSchema } : {}),
|
|
254
337
|
}, (async (...callbackArgs) => {
|
|
255
338
|
const argsOrExtra = callbackArgs[0];
|
|
256
339
|
const maybeExtra = callbackArgs[1];
|
|
257
|
-
const rawArgs =
|
|
258
|
-
const extra = (
|
|
340
|
+
const rawArgs = registeredHasInputSchema ? argsOrExtra : {};
|
|
341
|
+
const extra = (registeredHasInputSchema ? maybeExtra : argsOrExtra);
|
|
259
342
|
return instrumentToolCall({
|
|
260
343
|
name: registration.name,
|
|
261
344
|
args: rawArgs,
|
|
262
345
|
extra,
|
|
263
346
|
sessionId: extra?.sessionId,
|
|
264
|
-
|
|
347
|
+
telemetryMode: plan.mode,
|
|
348
|
+
capabilityRequest: internal,
|
|
349
|
+
}, (strippedArgs) => handler(originalHasInputSchema ? strippedArgs : {}, buildHandlerContext(extra)));
|
|
265
350
|
}));
|
|
266
351
|
};
|
|
267
352
|
const tool = (registration, handler) => {
|
|
353
|
+
if ((0, request_capability_js_1.isRequestCapabilityEnabled)(config)
|
|
354
|
+
&& registration.name === request_capability_js_1.REQUEST_CAPABILITY_TOOL_NAME) {
|
|
355
|
+
throw new Error(`Tool name "${request_capability_js_1.REQUEST_CAPABILITY_TOOL_NAME}" is reserved while armature.requestCapability is enabled.`);
|
|
356
|
+
}
|
|
268
357
|
registeredTools.set(registration.name, {
|
|
269
358
|
registration,
|
|
270
359
|
handler: handler,
|
|
360
|
+
telemetryMode: (0, schema_js_1.planToolTelemetry)(registration.name, registration.inputSchema, config).mode,
|
|
361
|
+
internal: false,
|
|
271
362
|
});
|
|
272
363
|
if (attachedServer) {
|
|
273
|
-
registerWithServer(attachedServer, registration, handler);
|
|
364
|
+
registerWithServer(attachedServer, registration, handler, false);
|
|
274
365
|
}
|
|
275
366
|
return (rawArgs, context = {}) => dispatch(registration.name, rawArgs, context);
|
|
276
367
|
};
|
|
@@ -279,8 +370,8 @@ const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) =
|
|
|
279
370
|
throw new Error("This recorder is already attached to an McpServer.");
|
|
280
371
|
}
|
|
281
372
|
attachedServer = server;
|
|
282
|
-
for (const { registration, handler } of registeredTools.values()) {
|
|
283
|
-
registerWithServer(server, registration, handler);
|
|
373
|
+
for (const { registration, handler, internal } of registeredTools.values()) {
|
|
374
|
+
registerWithServer(server, registration, handler, internal);
|
|
284
375
|
}
|
|
285
376
|
return server;
|
|
286
377
|
};
|
|
@@ -288,7 +379,16 @@ const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) =
|
|
|
288
379
|
return attachToMcpServer(new mcp_js_1.McpServer(info));
|
|
289
380
|
};
|
|
290
381
|
const toolDefinitions = () => {
|
|
291
|
-
|
|
382
|
+
const definitions = [];
|
|
383
|
+
for (const { registration, internal } of registeredTools.values()) {
|
|
384
|
+
if (internal) {
|
|
385
|
+
definitions.push({
|
|
386
|
+
name: request_capability_js_1.REQUEST_CAPABILITY_TOOL_NAME,
|
|
387
|
+
description: request_capability_js_1.REQUEST_CAPABILITY_DESCRIPTION,
|
|
388
|
+
inputSchema: request_capability_js_1.REQUEST_CAPABILITY_INPUT_SCHEMA,
|
|
389
|
+
});
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
292
392
|
const definition = { name: registration.name };
|
|
293
393
|
if (registration.title !== undefined)
|
|
294
394
|
definition.title = registration.title;
|
|
@@ -298,10 +398,23 @@ const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) =
|
|
|
298
398
|
if (registration.inputSchema !== undefined) {
|
|
299
399
|
definition.inputSchema = registration.inputSchema;
|
|
300
400
|
}
|
|
301
|
-
|
|
302
|
-
}
|
|
401
|
+
definitions.push(...decorateDefinitions([definition]));
|
|
402
|
+
}
|
|
403
|
+
return definitions;
|
|
303
404
|
};
|
|
304
405
|
const hasTool = (name) => registeredTools.has(name);
|
|
406
|
+
if ((0, request_capability_js_1.isRequestCapabilityEnabled)(config)) {
|
|
407
|
+
registeredTools.set(request_capability_js_1.REQUEST_CAPABILITY_TOOL_NAME, {
|
|
408
|
+
registration: {
|
|
409
|
+
name: request_capability_js_1.REQUEST_CAPABILITY_TOOL_NAME,
|
|
410
|
+
description: request_capability_js_1.REQUEST_CAPABILITY_DESCRIPTION,
|
|
411
|
+
inputSchema: request_capability_js_1.REQUEST_CAPABILITY_ZOD_SHAPE,
|
|
412
|
+
},
|
|
413
|
+
handler: request_capability_js_1.handleRequestCapability,
|
|
414
|
+
telemetryMode: "scrub",
|
|
415
|
+
internal: true,
|
|
416
|
+
});
|
|
417
|
+
}
|
|
305
418
|
return {
|
|
306
419
|
decorateDefinitions,
|
|
307
420
|
extractTelemetry: schema_js_1.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,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.redactSecretsInValue = exports.redactSecretsInString = exports.normalizeSensitiveFieldName = exports.SENSITIVE_FIELD_NAMES = exports.SECRET_PATTERN_RULES = void 0;
|
|
4
|
+
const utils_js_1 = require("./utils.js");
|
|
5
|
+
// Contract order is significant. Keep these expressions RE2-compatible so
|
|
6
|
+
// the TypeScript, Python, and Go implementations continue to agree.
|
|
7
|
+
exports.SECRET_PATTERN_RULES = [
|
|
8
|
+
{
|
|
9
|
+
id: "pem",
|
|
10
|
+
pattern: /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z0-9 ]*PRIVATE KEY-----/g,
|
|
11
|
+
replacement: "[redacted:pem]",
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
id: "sensitive-kv",
|
|
15
|
+
pattern: /\b(password|passwd|pwd|secret|api[_-]?key|access[_-]?token|refresh[_-]?token|client[_-]?secret|private[_-]?key|authorization)([=:])([^\s"'`,;&]{4,})/gi,
|
|
16
|
+
replacement: "$1$2[redacted:sensitive-kv]",
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
id: "aws-access-key-id",
|
|
20
|
+
pattern: /\b(?:AKIA|ASIA|ABIA|ACCA|AGPA|AIDA|AIPA|ANPA|ANVA|AROA)[A-Z0-9]{16}\b/g,
|
|
21
|
+
replacement: "[redacted:aws-access-key-id]",
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
id: "github-token",
|
|
25
|
+
pattern: /\b(?:gh[pousr]_[A-Za-z0-9]{36}|github_pat_[A-Za-z0-9_]{22,255})\b/g,
|
|
26
|
+
replacement: "[redacted:github-token]",
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
id: "google-api-key",
|
|
30
|
+
pattern: /\bAIza[0-9A-Za-z_-]{35}\b/g,
|
|
31
|
+
replacement: "[redacted:google-api-key]",
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
id: "slack-token",
|
|
35
|
+
pattern: /\bxox[abprs]-[A-Za-z0-9-]{10,}\b/g,
|
|
36
|
+
replacement: "[redacted:slack-token]",
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
id: "stripe-key",
|
|
40
|
+
pattern: /\b[rs]k_(?:live|test)_[A-Za-z0-9]{16,}\b/g,
|
|
41
|
+
replacement: "[redacted:stripe-key]",
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
id: "anthropic-api-key",
|
|
45
|
+
pattern: /\bsk-ant-[A-Za-z0-9_-]{16,}\b/g,
|
|
46
|
+
replacement: "[redacted:anthropic-api-key]",
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
id: "openai-api-key",
|
|
50
|
+
pattern: /\bsk-[A-Za-z0-9_-]{20,}\b/g,
|
|
51
|
+
replacement: "[redacted:openai-api-key]",
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
id: "jwt",
|
|
55
|
+
pattern: /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{6,}\.[A-Za-z0-9_-]{10,}\b/g,
|
|
56
|
+
replacement: "[redacted:jwt]",
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
id: "connection-string",
|
|
60
|
+
pattern: /\b([a-zA-Z][a-zA-Z0-9+.-]*:\/\/[^\s:/@]+):([^\s@]+)@/g,
|
|
61
|
+
replacement: "$1:[redacted:connection-string]@",
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
id: "bearer",
|
|
65
|
+
pattern: /\b[Bb]earer +[A-Za-z0-9._~+/=-]{16,}/g,
|
|
66
|
+
replacement: "Bearer [redacted:bearer]",
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
id: "basic",
|
|
70
|
+
pattern: /\b[Bb]asic +[A-Za-z0-9+/=]{16,}/g,
|
|
71
|
+
replacement: "Basic [redacted:basic]",
|
|
72
|
+
},
|
|
73
|
+
];
|
|
74
|
+
exports.SENSITIVE_FIELD_NAMES = new Set([
|
|
75
|
+
"password", "passwd", "pwd", "secret", "apikey", "accesskey",
|
|
76
|
+
"secretkey", "secretaccesskey", "token", "accesstoken", "refreshtoken",
|
|
77
|
+
"idtoken", "sessiontoken", "authorization", "auth", "clientsecret",
|
|
78
|
+
"privatekey", "credential", "credentials", "connectionstring",
|
|
79
|
+
"databaseurl", "dsn",
|
|
80
|
+
]);
|
|
81
|
+
const normalizeSensitiveFieldName = (key) => {
|
|
82
|
+
return key.toLowerCase().replace(/[_-]/g, "");
|
|
83
|
+
};
|
|
84
|
+
exports.normalizeSensitiveFieldName = normalizeSensitiveFieldName;
|
|
85
|
+
const redactSecretsInString = (value) => {
|
|
86
|
+
let redacted = value;
|
|
87
|
+
for (const rule of exports.SECRET_PATTERN_RULES) {
|
|
88
|
+
redacted = redacted.replace(rule.pattern, rule.replacement);
|
|
89
|
+
}
|
|
90
|
+
return redacted;
|
|
91
|
+
};
|
|
92
|
+
exports.redactSecretsInString = redactSecretsInString;
|
|
93
|
+
const redactSecretsInValue = (value, seen) => {
|
|
94
|
+
if (typeof value === "string")
|
|
95
|
+
return (0, exports.redactSecretsInString)(value);
|
|
96
|
+
if (typeof value !== "object" || value === null)
|
|
97
|
+
return value;
|
|
98
|
+
const tracked = seen ?? new WeakSet();
|
|
99
|
+
if (tracked.has(value))
|
|
100
|
+
return "[circular]";
|
|
101
|
+
tracked.add(value);
|
|
102
|
+
try {
|
|
103
|
+
if (Array.isArray(value)) {
|
|
104
|
+
return value.map((item) => (0, exports.redactSecretsInValue)(item, tracked));
|
|
105
|
+
}
|
|
106
|
+
if (!(0, utils_js_1.isRecord)(value))
|
|
107
|
+
return value;
|
|
108
|
+
const out = {};
|
|
109
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
110
|
+
out[key] = typeof entry === "string"
|
|
111
|
+
&& exports.SENSITIVE_FIELD_NAMES.has((0, exports.normalizeSensitiveFieldName)(key))
|
|
112
|
+
? "[redacted:sensitive-field]"
|
|
113
|
+
: (0, exports.redactSecretsInValue)(entry, tracked);
|
|
114
|
+
}
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
finally {
|
|
118
|
+
tracked.delete(value);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
exports.redactSecretsInValue = redactSecretsInValue;
|
|
@@ -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,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.handleRequestCapability = exports.isRequestCapabilityEnabled = exports.REQUEST_CAPABILITY_ZOD_SHAPE = exports.REQUEST_CAPABILITY_INPUT_SCHEMA = exports.REQUEST_CAPABILITY_ARGUMENT_DESCRIPTION = exports.REQUEST_CAPABILITY_DESCRIPTION = exports.REQUEST_CAPABILITY_TOOL_NAME = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
const emit_js_1 = require("./emit.js");
|
|
6
|
+
const utils_js_1 = require("./utils.js");
|
|
7
|
+
exports.REQUEST_CAPABILITY_TOOL_NAME = "request_capability";
|
|
8
|
+
exports.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.";
|
|
9
|
+
exports.REQUEST_CAPABILITY_ARGUMENT_DESCRIPTION = "The capability required to complete the user's request. Omit argument values, PII, and secrets. Use English.";
|
|
10
|
+
exports.REQUEST_CAPABILITY_INPUT_SCHEMA = {
|
|
11
|
+
type: "object",
|
|
12
|
+
properties: {
|
|
13
|
+
capability: {
|
|
14
|
+
type: "string",
|
|
15
|
+
description: exports.REQUEST_CAPABILITY_ARGUMENT_DESCRIPTION,
|
|
16
|
+
minLength: 1,
|
|
17
|
+
maxLength: 1000,
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
required: ["capability"],
|
|
21
|
+
additionalProperties: false,
|
|
22
|
+
};
|
|
23
|
+
// McpServer.registerTool accepts a Zod raw shape, while custom dispatchers
|
|
24
|
+
// consume the JSON Schema above from recorder.toolDefinitions().
|
|
25
|
+
exports.REQUEST_CAPABILITY_ZOD_SHAPE = {
|
|
26
|
+
capability: zod_1.z
|
|
27
|
+
.string()
|
|
28
|
+
.min(1)
|
|
29
|
+
.max(1000)
|
|
30
|
+
.describe(exports.REQUEST_CAPABILITY_ARGUMENT_DESCRIPTION),
|
|
31
|
+
};
|
|
32
|
+
const isRequestCapabilityEnabled = (config) => config.armature?.requestCapability === true
|
|
33
|
+
&& config.armature?.enabled !== false
|
|
34
|
+
&& (typeof config.armature?.emit === "function" || Boolean((0, emit_js_1.resolveApiKey)(config)));
|
|
35
|
+
exports.isRequestCapabilityEnabled = isRequestCapabilityEnabled;
|
|
36
|
+
const handleRequestCapability = (args) => {
|
|
37
|
+
if (!(0, utils_js_1.isRecord)(args)
|
|
38
|
+
|| typeof args.capability !== "string"
|
|
39
|
+
|| args.capability.trim().length === 0
|
|
40
|
+
|| args.capability.length > 1000) {
|
|
41
|
+
return {
|
|
42
|
+
isError: true,
|
|
43
|
+
content: [{ type: "text", text: "capability must be a non-empty string" }],
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
content: [{ type: "text", text: "Capability request acknowledged." }],
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
exports.handleRequestCapability = handleRequestCapability;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { RedactFunction } from "./types.js";
|
|
2
|
+
export declare const BINARY_REMOVED_PLACEHOLDER = "[binary removed]";
|
|
3
|
+
export declare const BASE64_REMOVED_PLACEHOLDER = "[base64 removed]";
|
|
4
|
+
export declare const REDACTION_FAILED_PLACEHOLDER = "[redaction failed]";
|
|
5
|
+
export declare const SANITIZATION_BUDGET = 65536;
|
|
6
|
+
export declare const sanitizeValue: (value: unknown, seen?: WeakSet<object>) => unknown;
|
|
7
|
+
export type PrepareForPreviewOptions = {
|
|
8
|
+
redactSecrets?: boolean;
|
|
9
|
+
};
|
|
10
|
+
export declare const prepareForPreview: (value: unknown, redact?: RedactFunction, options?: PrepareForPreviewOptions) => unknown;
|