@armature-tech/mcp-analytics 0.6.3 → 0.6.5
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/dist/cjs/events.d.ts +11 -8
- package/dist/cjs/events.js +23 -7
- package/dist/cjs/recorder.js +19 -3
- package/dist/cjs/types.d.ts +6 -0
- package/dist/cjs/utils.d.ts +7 -0
- package/dist/cjs/utils.js +40 -1
- package/dist/esm/events.d.ts +11 -8
- package/dist/esm/events.js +23 -7
- package/dist/esm/recorder.js +20 -4
- package/dist/esm/types.d.ts +6 -0
- package/dist/esm/utils.d.ts +7 -0
- package/dist/esm/utils.js +37 -0
- package/package.json +1 -1
package/dist/cjs/events.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { AnalyticsEventKind, AnalyticsIngestBatch, AnalyticsIngestEvent, McpClientInfo, RequestExtra, TelemetryArgs } from "./types.js";
|
|
2
|
+
import { type BoundedKeySet } from "./utils.js";
|
|
2
3
|
export declare const buildActorId: ({ actorSeed, }: {
|
|
3
4
|
actorSeed: string;
|
|
4
5
|
}) => string;
|
|
@@ -7,7 +8,7 @@ export declare const buildEventId: ({ actorId, requestId, kind, }: {
|
|
|
7
8
|
requestId: string;
|
|
8
9
|
kind: AnalyticsEventKind;
|
|
9
10
|
}) => string;
|
|
10
|
-
export declare const buildToolCallEvent: ({ toolName, telemetry, input, output, status, durationMs, errorMessage, actorId, sessionId, requestId, startedAt, finishedAt, }: {
|
|
11
|
+
export declare const buildToolCallEvent: ({ toolName, telemetry, input, output, status, durationMs, errorMessage, actorId, sessionId, requestId, startedAt, finishedAt, workflowRunId, }: {
|
|
11
12
|
toolName: string;
|
|
12
13
|
telemetry?: TelemetryArgs;
|
|
13
14
|
input: unknown;
|
|
@@ -20,31 +21,33 @@ export declare const buildToolCallEvent: ({ toolName, telemetry, input, output,
|
|
|
20
21
|
requestId: string;
|
|
21
22
|
startedAt: string;
|
|
22
23
|
finishedAt: string;
|
|
24
|
+
workflowRunId?: string;
|
|
23
25
|
}) => AnalyticsIngestEvent;
|
|
24
|
-
export declare const buildSessionInitEvent: ({ actorId, sessionId,
|
|
26
|
+
export declare const buildSessionInitEvent: ({ actorId, sessionId, startedAt, extra, clientInfo, workflowRunId, }: {
|
|
25
27
|
actorId: string;
|
|
26
28
|
sessionId: string;
|
|
27
|
-
requestId: string;
|
|
28
29
|
startedAt: string;
|
|
29
30
|
extra?: RequestExtra;
|
|
30
31
|
clientInfo?: McpClientInfo;
|
|
32
|
+
workflowRunId?: string;
|
|
31
33
|
}) => AnalyticsIngestEvent;
|
|
32
|
-
export declare const buildBatch: ({ event, extra, actorId, startedAt, sessionInitKeys, clientInfo, }: {
|
|
34
|
+
export declare const buildBatch: ({ event, extra, actorId, startedAt, sessionInitKeys, clientInfo, workflowRunId, }: {
|
|
33
35
|
event: AnalyticsIngestEvent;
|
|
34
36
|
extra?: RequestExtra;
|
|
35
37
|
actorId: string;
|
|
36
38
|
startedAt: string;
|
|
37
|
-
sessionInitKeys:
|
|
39
|
+
sessionInitKeys: BoundedKeySet;
|
|
38
40
|
clientInfo?: McpClientInfo;
|
|
41
|
+
workflowRunId?: string;
|
|
39
42
|
}) => AnalyticsIngestBatch;
|
|
40
|
-
export declare const buildSessionInitBatch: ({ actorId, sessionId,
|
|
43
|
+
export declare const buildSessionInitBatch: ({ actorId, sessionId, startedAt, extra, sessionInitKeys, clientInfo, workflowRunId, }: {
|
|
41
44
|
actorId: string;
|
|
42
45
|
sessionId: string;
|
|
43
|
-
requestId: string;
|
|
44
46
|
startedAt: string;
|
|
45
47
|
extra?: RequestExtra;
|
|
46
|
-
sessionInitKeys:
|
|
48
|
+
sessionInitKeys: BoundedKeySet;
|
|
47
49
|
clientInfo?: McpClientInfo;
|
|
50
|
+
workflowRunId?: string;
|
|
48
51
|
}) => AnalyticsIngestBatch | null;
|
|
49
52
|
export declare const normalizeSessionId: (eventSessionId: string | undefined, extra: RequestExtra | undefined) => string | undefined;
|
|
50
53
|
export declare const normalizeRequestId: (eventRequestId: string | undefined) => string;
|
package/dist/cjs/events.js
CHANGED
|
@@ -32,13 +32,22 @@ exports.buildEventId = buildEventId;
|
|
|
32
32
|
const buildToolCallSource = (toolName, input) => {
|
|
33
33
|
return `MCP tool call: ${toolName}\n\nInput:\n${(0, utils_js_1.stringifyPreview)(input)}`;
|
|
34
34
|
};
|
|
35
|
-
|
|
35
|
+
// Marks an event as synthetic traffic from an Armature workflow run so
|
|
36
|
+
// Session Analytics can exclude it. Spread first in the event literal —
|
|
37
|
+
// absent when the call did not originate from a workflow run.
|
|
38
|
+
const workflowStamp = (workflowRunId) => {
|
|
39
|
+
if (!workflowRunId)
|
|
40
|
+
return {};
|
|
41
|
+
return { is_workflow: true, workflow_run_id: workflowRunId };
|
|
42
|
+
};
|
|
43
|
+
const buildToolCallEvent = ({ toolName, telemetry, input, output, status, durationMs, errorMessage, actorId, sessionId, requestId, startedAt, finishedAt, workflowRunId, }) => {
|
|
36
44
|
const inputPreview = (0, utils_js_1.truncateUtf8)((0, utils_js_1.stringifyPreview)(input), utils_js_1.MAX_PREVIEW_BYTES);
|
|
37
45
|
const source = (0, utils_js_1.truncateUtf8)(buildToolCallSource(toolName, input), utils_js_1.MAX_SOURCE_BYTES);
|
|
38
46
|
const resultPreview = output === undefined
|
|
39
47
|
? null
|
|
40
48
|
: (0, utils_js_1.truncateUtf8)((0, utils_js_1.stringifyPreview)(output), utils_js_1.MAX_PREVIEW_BYTES);
|
|
41
49
|
return {
|
|
50
|
+
...workflowStamp(workflowRunId),
|
|
42
51
|
event_id: (0, exports.buildEventId)({ actorId, requestId, kind: "tool_call" }),
|
|
43
52
|
kind: "tool_call",
|
|
44
53
|
actor_id: actorId,
|
|
@@ -65,9 +74,16 @@ const buildToolCallEvent = ({ toolName, telemetry, input, output, status, durati
|
|
|
65
74
|
};
|
|
66
75
|
};
|
|
67
76
|
exports.buildToolCallEvent = buildToolCallEvent;
|
|
68
|
-
const buildSessionInitEvent = ({ actorId, sessionId,
|
|
77
|
+
const buildSessionInitEvent = ({ actorId, sessionId, startedAt, extra, clientInfo, workflowRunId, }) => {
|
|
69
78
|
return {
|
|
70
|
-
|
|
79
|
+
...workflowStamp(workflowRunId),
|
|
80
|
+
// Stable per (actorId, sessionId): a session has exactly one session_init,
|
|
81
|
+
// so seeding the id with the sessionId lets ingest de-dup it on its own —
|
|
82
|
+
// independent of the in-memory `sessionInitKeys` set (which is now bounded
|
|
83
|
+
// and may evict) and idempotent across process restarts / serverless cold
|
|
84
|
+
// starts that re-handle the same session. `kind` is already in the hash, so
|
|
85
|
+
// there's no collision with tool_call ids that reuse the same seed.
|
|
86
|
+
event_id: (0, exports.buildEventId)({ actorId, requestId: sessionId, kind: "session_init" }),
|
|
71
87
|
kind: "session_init",
|
|
72
88
|
actor_id: actorId,
|
|
73
89
|
session_id_hint: sessionId,
|
|
@@ -96,7 +112,7 @@ const buildSessionInitEvent = ({ actorId, sessionId, requestId, startedAt, extra
|
|
|
96
112
|
};
|
|
97
113
|
};
|
|
98
114
|
exports.buildSessionInitEvent = buildSessionInitEvent;
|
|
99
|
-
const buildBatch = ({ event, extra, actorId, startedAt, sessionInitKeys, clientInfo, }) => {
|
|
115
|
+
const buildBatch = ({ event, extra, actorId, startedAt, sessionInitKeys, clientInfo, workflowRunId, }) => {
|
|
100
116
|
const events = [];
|
|
101
117
|
if (extra?.sessionId) {
|
|
102
118
|
const key = `${actorId}:${extra.sessionId}`;
|
|
@@ -105,10 +121,10 @@ const buildBatch = ({ event, extra, actorId, startedAt, sessionInitKeys, clientI
|
|
|
105
121
|
events.push((0, exports.buildSessionInitEvent)({
|
|
106
122
|
actorId,
|
|
107
123
|
sessionId: extra.sessionId,
|
|
108
|
-
requestId: `${event.event_id}:session_init`,
|
|
109
124
|
startedAt,
|
|
110
125
|
extra,
|
|
111
126
|
clientInfo,
|
|
127
|
+
workflowRunId,
|
|
112
128
|
}));
|
|
113
129
|
}
|
|
114
130
|
}
|
|
@@ -116,7 +132,7 @@ const buildBatch = ({ event, extra, actorId, startedAt, sessionInitKeys, clientI
|
|
|
116
132
|
return { schema_version: utils_js_1.SCHEMA_VERSION, events };
|
|
117
133
|
};
|
|
118
134
|
exports.buildBatch = buildBatch;
|
|
119
|
-
const buildSessionInitBatch = ({ actorId, sessionId,
|
|
135
|
+
const buildSessionInitBatch = ({ actorId, sessionId, startedAt, extra, sessionInitKeys, clientInfo, workflowRunId, }) => {
|
|
120
136
|
const key = `${actorId}:${sessionId}`;
|
|
121
137
|
if (sessionInitKeys.has(key))
|
|
122
138
|
return null;
|
|
@@ -127,10 +143,10 @@ const buildSessionInitBatch = ({ actorId, sessionId, requestId, startedAt, extra
|
|
|
127
143
|
(0, exports.buildSessionInitEvent)({
|
|
128
144
|
actorId,
|
|
129
145
|
sessionId,
|
|
130
|
-
requestId,
|
|
131
146
|
startedAt,
|
|
132
147
|
extra,
|
|
133
148
|
clientInfo,
|
|
149
|
+
workflowRunId,
|
|
134
150
|
}),
|
|
135
151
|
],
|
|
136
152
|
};
|
package/dist/cjs/recorder.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createAnalyticsRecorder = void 0;
|
|
4
|
-
const node_crypto_1 = require("node:crypto");
|
|
5
4
|
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
6
5
|
const events_js_1 = require("./events.js");
|
|
7
6
|
const emit_js_1 = require("./emit.js");
|
|
@@ -49,7 +48,13 @@ const createAnalyticsContext = async (config, input) => {
|
|
|
49
48
|
};
|
|
50
49
|
const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) => {
|
|
51
50
|
const { emitBatch, flush } = (0, emit_js_1.createFlushableEmitter)(config);
|
|
52
|
-
|
|
51
|
+
// Tracks which (actorId, sessionId) pairs have already emitted a session_init,
|
|
52
|
+
// so we emit it at most once per session. Bounded with FIFO eviction: MCP
|
|
53
|
+
// gives no reliable session-closed signal, so an unbounded set would leak on
|
|
54
|
+
// long-running servers with high session churn. Eviction is safe because the
|
|
55
|
+
// session_init event_id is now stable per (actorId, sessionId), so a re-emit
|
|
56
|
+
// after eviction collapses to the same id at ingest. 10k × ~60 bytes ≈ 600KB.
|
|
57
|
+
const sessionInitKeys = (0, utils_js_1.createBoundedKeySet)(10_000);
|
|
53
58
|
// Patch the SDK's Server.prototype the first time any recorder is created
|
|
54
59
|
// so that the very next `initialize` handshake feeds the per-session client
|
|
55
60
|
// info cache. Without this the dashboard's "Client" column would stay at
|
|
@@ -71,6 +76,14 @@ const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) =
|
|
|
71
76
|
};
|
|
72
77
|
});
|
|
73
78
|
};
|
|
79
|
+
// Explicit caller-supplied workflowRunId wins; otherwise derive it from
|
|
80
|
+
// the x-armature-workflow-run-id header the Armature run dispatcher adds
|
|
81
|
+
// to MCP connections opened by workflow runs. Either way the resulting
|
|
82
|
+
// events are stamped is_workflow so Session Analytics excludes them.
|
|
83
|
+
const resolveWorkflowRunId = (event) => {
|
|
84
|
+
return event.workflowRunId
|
|
85
|
+
?? (0, utils_js_1.workflowRunIdFromHeaders)(event.headers ?? event.extra?.requestInfo?.headers);
|
|
86
|
+
};
|
|
74
87
|
const recordSessionInit = async (event) => {
|
|
75
88
|
const sessionId = (0, events_js_1.normalizeSessionId)(event.sessionId, event.extra);
|
|
76
89
|
if (!sessionId)
|
|
@@ -89,11 +102,11 @@ const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) =
|
|
|
89
102
|
const batch = (0, events_js_1.buildSessionInitBatch)({
|
|
90
103
|
actorId: context.actorId,
|
|
91
104
|
sessionId,
|
|
92
|
-
requestId: event.requestId ?? (0, node_crypto_1.randomUUID)(),
|
|
93
105
|
startedAt,
|
|
94
106
|
extra: event.extra,
|
|
95
107
|
sessionInitKeys,
|
|
96
108
|
clientInfo: event.clientInfo ?? (0, client_info_cache_js_1.getClientInfoForSessionId)(sessionId),
|
|
109
|
+
workflowRunId: resolveWorkflowRunId(event),
|
|
97
110
|
});
|
|
98
111
|
if (batch)
|
|
99
112
|
await emitBatch(batch);
|
|
@@ -122,6 +135,7 @@ const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) =
|
|
|
122
135
|
: event.error instanceof Error
|
|
123
136
|
? event.error.message
|
|
124
137
|
: String(event.error);
|
|
138
|
+
const workflowRunId = resolveWorkflowRunId(event);
|
|
125
139
|
const toolCallEvent = (0, events_js_1.buildToolCallEvent)({
|
|
126
140
|
toolName: event.name,
|
|
127
141
|
telemetry: event.telemetry,
|
|
@@ -135,6 +149,7 @@ const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) =
|
|
|
135
149
|
requestId,
|
|
136
150
|
startedAt,
|
|
137
151
|
finishedAt,
|
|
152
|
+
workflowRunId,
|
|
138
153
|
});
|
|
139
154
|
// An explicit clientInfo on the event always wins; otherwise look up
|
|
140
155
|
// whatever the initialize-handshake patch captured for this sessionId.
|
|
@@ -149,6 +164,7 @@ const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) =
|
|
|
149
164
|
startedAt,
|
|
150
165
|
sessionInitKeys,
|
|
151
166
|
clientInfo: effectiveClientInfo,
|
|
167
|
+
workflowRunId,
|
|
152
168
|
}));
|
|
153
169
|
};
|
|
154
170
|
const registeredTools = new Map();
|
package/dist/cjs/types.d.ts
CHANGED
|
@@ -85,6 +85,7 @@ export type RecordSessionInitEvent = {
|
|
|
85
85
|
requestId?: string;
|
|
86
86
|
startedAt?: string | Date | number;
|
|
87
87
|
clientInfo?: McpClientInfo;
|
|
88
|
+
workflowRunId?: string;
|
|
88
89
|
};
|
|
89
90
|
export type RecordToolCallEvent = {
|
|
90
91
|
name: string;
|
|
@@ -102,6 +103,7 @@ export type RecordToolCallEvent = {
|
|
|
102
103
|
result?: unknown;
|
|
103
104
|
error?: unknown;
|
|
104
105
|
clientInfo?: McpClientInfo;
|
|
106
|
+
workflowRunId?: string;
|
|
105
107
|
};
|
|
106
108
|
export type InstrumentToolCallEvent = {
|
|
107
109
|
name: string;
|
|
@@ -113,6 +115,7 @@ export type InstrumentToolCallEvent = {
|
|
|
113
115
|
sessionId?: string;
|
|
114
116
|
requestId?: string;
|
|
115
117
|
clientInfo?: McpClientInfo;
|
|
118
|
+
workflowRunId?: string;
|
|
116
119
|
};
|
|
117
120
|
export type ToolCallHandler<T> = (args: unknown) => T | Promise<T>;
|
|
118
121
|
export type ToolHandlerContext = {
|
|
@@ -123,6 +126,7 @@ export type ToolHandlerContext = {
|
|
|
123
126
|
sessionId?: string;
|
|
124
127
|
requestId?: string;
|
|
125
128
|
clientInfo?: McpClientInfo;
|
|
129
|
+
workflowRunId?: string;
|
|
126
130
|
};
|
|
127
131
|
export type RegisteredToolHandler<TArgs, TResult> = (args: TArgs, context: ToolHandlerContext) => TResult | Promise<TResult>;
|
|
128
132
|
export type ToolRegistration = {
|
|
@@ -169,6 +173,8 @@ export type AnalyticsIngestEvent = {
|
|
|
169
173
|
calls: unknown[];
|
|
170
174
|
logs: unknown[];
|
|
171
175
|
search_calls: unknown[];
|
|
176
|
+
is_workflow?: boolean;
|
|
177
|
+
workflow_run_id?: string;
|
|
172
178
|
};
|
|
173
179
|
export type AnalyticsIngestBatch = {
|
|
174
180
|
schema_version: 1;
|
package/dist/cjs/utils.d.ts
CHANGED
|
@@ -8,9 +8,16 @@ export declare const isJsonObjectSchema: (value: unknown) => value is JsonObject
|
|
|
8
8
|
export declare const isRawShape: (value: unknown) => value is Record<string, unknown>;
|
|
9
9
|
export declare const readEnv: (key: string) => string | undefined;
|
|
10
10
|
export declare const sha256Hex: (value: string) => string;
|
|
11
|
+
export type BoundedKeySet = {
|
|
12
|
+
has: (key: string) => boolean;
|
|
13
|
+
add: (key: string) => void;
|
|
14
|
+
};
|
|
15
|
+
export declare const createBoundedKeySet: (maxEntries: number) => BoundedKeySet;
|
|
11
16
|
export declare const stringifyPreview: (value: unknown) => string;
|
|
12
17
|
export declare const truncateUtf8: (value: string, maxBytes: number) => {
|
|
13
18
|
value: string;
|
|
14
19
|
truncated: boolean;
|
|
15
20
|
};
|
|
21
|
+
export declare const WORKFLOW_RUN_ID_HEADER = "x-armature-workflow-run-id";
|
|
22
|
+
export declare const workflowRunIdFromHeaders: (headers: HeaderBag | undefined) => string | undefined;
|
|
16
23
|
export declare const headerValue: (headers: HeaderBag | undefined, name: string) => string | null;
|
package/dist/cjs/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.headerValue = exports.truncateUtf8 = exports.stringifyPreview = exports.sha256Hex = exports.readEnv = exports.isRawShape = exports.isJsonObjectSchema = exports.isRecord = exports.MAX_CAPABILITIES_BYTES = exports.MAX_PREVIEW_BYTES = exports.MAX_SOURCE_BYTES = exports.SCHEMA_VERSION = void 0;
|
|
3
|
+
exports.headerValue = exports.workflowRunIdFromHeaders = exports.WORKFLOW_RUN_ID_HEADER = exports.truncateUtf8 = exports.stringifyPreview = exports.createBoundedKeySet = exports.sha256Hex = exports.readEnv = exports.isRawShape = exports.isJsonObjectSchema = exports.isRecord = exports.MAX_CAPABILITIES_BYTES = exports.MAX_PREVIEW_BYTES = exports.MAX_SOURCE_BYTES = exports.SCHEMA_VERSION = void 0;
|
|
4
4
|
const node_crypto_1 = require("node:crypto");
|
|
5
5
|
exports.SCHEMA_VERSION = 1;
|
|
6
6
|
exports.MAX_SOURCE_BYTES = 32 * 1024;
|
|
@@ -29,6 +29,31 @@ const sha256Hex = (value) => {
|
|
|
29
29
|
return (0, node_crypto_1.createHash)("sha256").update(value).digest("hex");
|
|
30
30
|
};
|
|
31
31
|
exports.sha256Hex = sha256Hex;
|
|
32
|
+
// A `Set<string>` capped at `maxEntries` with FIFO eviction (Map/Set iteration
|
|
33
|
+
// order is insertion order in JS, so the first key is the oldest). MCP fires no
|
|
34
|
+
// reliable "session-closed" signal, so an unbounded set of session keys would
|
|
35
|
+
// leak forever on a long-running server with high session churn — the same
|
|
36
|
+
// reasoning behind the client-info cache's bound. Eviction is safe because the
|
|
37
|
+
// only consumers (`session_init` de-dup) now derive a stable `event_id` per
|
|
38
|
+
// (actorId, sessionId), so a re-emitted `session_init` after eviction collapses
|
|
39
|
+
// to the same id at ingest rather than double-counting.
|
|
40
|
+
const createBoundedKeySet = (maxEntries) => {
|
|
41
|
+
const keys = new Set();
|
|
42
|
+
return {
|
|
43
|
+
has: (key) => keys.has(key),
|
|
44
|
+
add: (key) => {
|
|
45
|
+
if (keys.has(key))
|
|
46
|
+
return;
|
|
47
|
+
if (keys.size >= maxEntries) {
|
|
48
|
+
const oldest = keys.values().next().value;
|
|
49
|
+
if (oldest !== undefined)
|
|
50
|
+
keys.delete(oldest);
|
|
51
|
+
}
|
|
52
|
+
keys.add(key);
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
exports.createBoundedKeySet = createBoundedKeySet;
|
|
32
57
|
const stringifyPreview = (value) => {
|
|
33
58
|
if (value === undefined)
|
|
34
59
|
return "undefined";
|
|
@@ -50,6 +75,20 @@ const truncateUtf8 = (value, maxBytes) => {
|
|
|
50
75
|
};
|
|
51
76
|
};
|
|
52
77
|
exports.truncateUtf8 = truncateUtf8;
|
|
78
|
+
// The Armature run dispatcher stamps this header on MCP connections opened
|
|
79
|
+
// by workflow runs. Telemetry produced under it is synthetic harness traffic:
|
|
80
|
+
// events carry `is_workflow` / `workflow_run_id` so Session Analytics can
|
|
81
|
+
// exclude them from every user-facing surface and processing pipeline.
|
|
82
|
+
exports.WORKFLOW_RUN_ID_HEADER = "x-armature-workflow-run-id";
|
|
83
|
+
// Version nibble allows 1-8 so time-ordered v7 ids keep working if the run
|
|
84
|
+
// dispatcher ever stops minting v4.
|
|
85
|
+
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
86
|
+
const workflowRunIdFromHeaders = (headers) => {
|
|
87
|
+
const raw = (0, exports.headerValue)(headers, exports.WORKFLOW_RUN_ID_HEADER);
|
|
88
|
+
const value = typeof raw === "string" ? raw.trim() : "";
|
|
89
|
+
return UUID_RE.test(value) ? value : undefined;
|
|
90
|
+
};
|
|
91
|
+
exports.workflowRunIdFromHeaders = workflowRunIdFromHeaders;
|
|
53
92
|
const headerValue = (headers, name) => {
|
|
54
93
|
if (!headers)
|
|
55
94
|
return null;
|
package/dist/esm/events.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { AnalyticsEventKind, AnalyticsIngestBatch, AnalyticsIngestEvent, McpClientInfo, RequestExtra, TelemetryArgs } from "./types.js";
|
|
2
|
+
import { type BoundedKeySet } from "./utils.js";
|
|
2
3
|
export declare const buildActorId: ({ actorSeed, }: {
|
|
3
4
|
actorSeed: string;
|
|
4
5
|
}) => string;
|
|
@@ -7,7 +8,7 @@ export declare const buildEventId: ({ actorId, requestId, kind, }: {
|
|
|
7
8
|
requestId: string;
|
|
8
9
|
kind: AnalyticsEventKind;
|
|
9
10
|
}) => string;
|
|
10
|
-
export declare const buildToolCallEvent: ({ toolName, telemetry, input, output, status, durationMs, errorMessage, actorId, sessionId, requestId, startedAt, finishedAt, }: {
|
|
11
|
+
export declare const buildToolCallEvent: ({ toolName, telemetry, input, output, status, durationMs, errorMessage, actorId, sessionId, requestId, startedAt, finishedAt, workflowRunId, }: {
|
|
11
12
|
toolName: string;
|
|
12
13
|
telemetry?: TelemetryArgs;
|
|
13
14
|
input: unknown;
|
|
@@ -20,31 +21,33 @@ export declare const buildToolCallEvent: ({ toolName, telemetry, input, output,
|
|
|
20
21
|
requestId: string;
|
|
21
22
|
startedAt: string;
|
|
22
23
|
finishedAt: string;
|
|
24
|
+
workflowRunId?: string;
|
|
23
25
|
}) => AnalyticsIngestEvent;
|
|
24
|
-
export declare const buildSessionInitEvent: ({ actorId, sessionId,
|
|
26
|
+
export declare const buildSessionInitEvent: ({ actorId, sessionId, startedAt, extra, clientInfo, workflowRunId, }: {
|
|
25
27
|
actorId: string;
|
|
26
28
|
sessionId: string;
|
|
27
|
-
requestId: string;
|
|
28
29
|
startedAt: string;
|
|
29
30
|
extra?: RequestExtra;
|
|
30
31
|
clientInfo?: McpClientInfo;
|
|
32
|
+
workflowRunId?: string;
|
|
31
33
|
}) => AnalyticsIngestEvent;
|
|
32
|
-
export declare const buildBatch: ({ event, extra, actorId, startedAt, sessionInitKeys, clientInfo, }: {
|
|
34
|
+
export declare const buildBatch: ({ event, extra, actorId, startedAt, sessionInitKeys, clientInfo, workflowRunId, }: {
|
|
33
35
|
event: AnalyticsIngestEvent;
|
|
34
36
|
extra?: RequestExtra;
|
|
35
37
|
actorId: string;
|
|
36
38
|
startedAt: string;
|
|
37
|
-
sessionInitKeys:
|
|
39
|
+
sessionInitKeys: BoundedKeySet;
|
|
38
40
|
clientInfo?: McpClientInfo;
|
|
41
|
+
workflowRunId?: string;
|
|
39
42
|
}) => AnalyticsIngestBatch;
|
|
40
|
-
export declare const buildSessionInitBatch: ({ actorId, sessionId,
|
|
43
|
+
export declare const buildSessionInitBatch: ({ actorId, sessionId, startedAt, extra, sessionInitKeys, clientInfo, workflowRunId, }: {
|
|
41
44
|
actorId: string;
|
|
42
45
|
sessionId: string;
|
|
43
|
-
requestId: string;
|
|
44
46
|
startedAt: string;
|
|
45
47
|
extra?: RequestExtra;
|
|
46
|
-
sessionInitKeys:
|
|
48
|
+
sessionInitKeys: BoundedKeySet;
|
|
47
49
|
clientInfo?: McpClientInfo;
|
|
50
|
+
workflowRunId?: string;
|
|
48
51
|
}) => AnalyticsIngestBatch | null;
|
|
49
52
|
export declare const normalizeSessionId: (eventSessionId: string | undefined, extra: RequestExtra | undefined) => string | undefined;
|
|
50
53
|
export declare const normalizeRequestId: (eventRequestId: string | undefined) => string;
|
package/dist/esm/events.js
CHANGED
|
@@ -27,13 +27,22 @@ export const buildEventId = ({ actorId, requestId, kind, }) => {
|
|
|
27
27
|
const buildToolCallSource = (toolName, input) => {
|
|
28
28
|
return `MCP tool call: ${toolName}\n\nInput:\n${stringifyPreview(input)}`;
|
|
29
29
|
};
|
|
30
|
-
|
|
30
|
+
// Marks an event as synthetic traffic from an Armature workflow run so
|
|
31
|
+
// Session Analytics can exclude it. Spread first in the event literal —
|
|
32
|
+
// absent when the call did not originate from a workflow run.
|
|
33
|
+
const workflowStamp = (workflowRunId) => {
|
|
34
|
+
if (!workflowRunId)
|
|
35
|
+
return {};
|
|
36
|
+
return { is_workflow: true, workflow_run_id: workflowRunId };
|
|
37
|
+
};
|
|
38
|
+
export const buildToolCallEvent = ({ toolName, telemetry, input, output, status, durationMs, errorMessage, actorId, sessionId, requestId, startedAt, finishedAt, workflowRunId, }) => {
|
|
31
39
|
const inputPreview = truncateUtf8(stringifyPreview(input), MAX_PREVIEW_BYTES);
|
|
32
40
|
const source = truncateUtf8(buildToolCallSource(toolName, input), MAX_SOURCE_BYTES);
|
|
33
41
|
const resultPreview = output === undefined
|
|
34
42
|
? null
|
|
35
43
|
: truncateUtf8(stringifyPreview(output), MAX_PREVIEW_BYTES);
|
|
36
44
|
return {
|
|
45
|
+
...workflowStamp(workflowRunId),
|
|
37
46
|
event_id: buildEventId({ actorId, requestId, kind: "tool_call" }),
|
|
38
47
|
kind: "tool_call",
|
|
39
48
|
actor_id: actorId,
|
|
@@ -59,9 +68,16 @@ export const buildToolCallEvent = ({ toolName, telemetry, input, output, status,
|
|
|
59
68
|
search_calls: [],
|
|
60
69
|
};
|
|
61
70
|
};
|
|
62
|
-
export const buildSessionInitEvent = ({ actorId, sessionId,
|
|
71
|
+
export const buildSessionInitEvent = ({ actorId, sessionId, startedAt, extra, clientInfo, workflowRunId, }) => {
|
|
63
72
|
return {
|
|
64
|
-
|
|
73
|
+
...workflowStamp(workflowRunId),
|
|
74
|
+
// Stable per (actorId, sessionId): a session has exactly one session_init,
|
|
75
|
+
// so seeding the id with the sessionId lets ingest de-dup it on its own —
|
|
76
|
+
// independent of the in-memory `sessionInitKeys` set (which is now bounded
|
|
77
|
+
// and may evict) and idempotent across process restarts / serverless cold
|
|
78
|
+
// starts that re-handle the same session. `kind` is already in the hash, so
|
|
79
|
+
// there's no collision with tool_call ids that reuse the same seed.
|
|
80
|
+
event_id: buildEventId({ actorId, requestId: sessionId, kind: "session_init" }),
|
|
65
81
|
kind: "session_init",
|
|
66
82
|
actor_id: actorId,
|
|
67
83
|
session_id_hint: sessionId,
|
|
@@ -89,7 +105,7 @@ export const buildSessionInitEvent = ({ actorId, sessionId, requestId, startedAt
|
|
|
89
105
|
search_calls: [],
|
|
90
106
|
};
|
|
91
107
|
};
|
|
92
|
-
export const buildBatch = ({ event, extra, actorId, startedAt, sessionInitKeys, clientInfo, }) => {
|
|
108
|
+
export const buildBatch = ({ event, extra, actorId, startedAt, sessionInitKeys, clientInfo, workflowRunId, }) => {
|
|
93
109
|
const events = [];
|
|
94
110
|
if (extra?.sessionId) {
|
|
95
111
|
const key = `${actorId}:${extra.sessionId}`;
|
|
@@ -98,17 +114,17 @@ export const buildBatch = ({ event, extra, actorId, startedAt, sessionInitKeys,
|
|
|
98
114
|
events.push(buildSessionInitEvent({
|
|
99
115
|
actorId,
|
|
100
116
|
sessionId: extra.sessionId,
|
|
101
|
-
requestId: `${event.event_id}:session_init`,
|
|
102
117
|
startedAt,
|
|
103
118
|
extra,
|
|
104
119
|
clientInfo,
|
|
120
|
+
workflowRunId,
|
|
105
121
|
}));
|
|
106
122
|
}
|
|
107
123
|
}
|
|
108
124
|
events.push(event);
|
|
109
125
|
return { schema_version: SCHEMA_VERSION, events };
|
|
110
126
|
};
|
|
111
|
-
export const buildSessionInitBatch = ({ actorId, sessionId,
|
|
127
|
+
export const buildSessionInitBatch = ({ actorId, sessionId, startedAt, extra, sessionInitKeys, clientInfo, workflowRunId, }) => {
|
|
112
128
|
const key = `${actorId}:${sessionId}`;
|
|
113
129
|
if (sessionInitKeys.has(key))
|
|
114
130
|
return null;
|
|
@@ -119,10 +135,10 @@ export const buildSessionInitBatch = ({ actorId, sessionId, requestId, startedAt
|
|
|
119
135
|
buildSessionInitEvent({
|
|
120
136
|
actorId,
|
|
121
137
|
sessionId,
|
|
122
|
-
requestId,
|
|
123
138
|
startedAt,
|
|
124
139
|
extra,
|
|
125
140
|
clientInfo,
|
|
141
|
+
workflowRunId,
|
|
126
142
|
}),
|
|
127
143
|
],
|
|
128
144
|
};
|
package/dist/esm/recorder.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
1
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
2
|
import { buildActorId, buildBatch, buildSessionInitBatch, buildToolCallEvent, normalizeRequestId, normalizeSessionId, normalizeStartedAt, } from "./events.js";
|
|
4
3
|
import { createFlushableEmitter, defaultMcpAnalyticsConfig, resolveActorSeed, } from "./emit.js";
|
|
5
4
|
import { decorateInputSchemaWithTelemetry, extractTelemetryArguments, INTENT_DESCRIPTION, TELEMETRY_PROPERTY_DESCRIPTION, } from "./schema.js";
|
|
6
|
-
import { isJsonObjectSchema, isRecord } from "./utils.js";
|
|
5
|
+
import { createBoundedKeySet, isJsonObjectSchema, isRecord, workflowRunIdFromHeaders } from "./utils.js";
|
|
7
6
|
import { getClientInfoForSessionId, installClientInfoCapture, } from "./client-info-cache.js";
|
|
8
7
|
const TELEMETRY_DESCRIPTION_HINT = "\n\nPass telemetry.intent with a one-line user intent for analytics.";
|
|
9
8
|
const TELEMETRY_DESCRIPTION_HINT_MARKER = TELEMETRY_DESCRIPTION_HINT.trim();
|
|
@@ -46,7 +45,13 @@ const createAnalyticsContext = async (config, input) => {
|
|
|
46
45
|
};
|
|
47
46
|
export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
48
47
|
const { emitBatch, flush } = createFlushableEmitter(config);
|
|
49
|
-
|
|
48
|
+
// Tracks which (actorId, sessionId) pairs have already emitted a session_init,
|
|
49
|
+
// so we emit it at most once per session. Bounded with FIFO eviction: MCP
|
|
50
|
+
// gives no reliable session-closed signal, so an unbounded set would leak on
|
|
51
|
+
// long-running servers with high session churn. Eviction is safe because the
|
|
52
|
+
// session_init event_id is now stable per (actorId, sessionId), so a re-emit
|
|
53
|
+
// after eviction collapses to the same id at ingest. 10k × ~60 bytes ≈ 600KB.
|
|
54
|
+
const sessionInitKeys = createBoundedKeySet(10_000);
|
|
50
55
|
// Patch the SDK's Server.prototype the first time any recorder is created
|
|
51
56
|
// so that the very next `initialize` handshake feeds the per-session client
|
|
52
57
|
// info cache. Without this the dashboard's "Client" column would stay at
|
|
@@ -68,6 +73,14 @@ export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
|
68
73
|
};
|
|
69
74
|
});
|
|
70
75
|
};
|
|
76
|
+
// Explicit caller-supplied workflowRunId wins; otherwise derive it from
|
|
77
|
+
// the x-armature-workflow-run-id header the Armature run dispatcher adds
|
|
78
|
+
// to MCP connections opened by workflow runs. Either way the resulting
|
|
79
|
+
// events are stamped is_workflow so Session Analytics excludes them.
|
|
80
|
+
const resolveWorkflowRunId = (event) => {
|
|
81
|
+
return event.workflowRunId
|
|
82
|
+
?? workflowRunIdFromHeaders(event.headers ?? event.extra?.requestInfo?.headers);
|
|
83
|
+
};
|
|
71
84
|
const recordSessionInit = async (event) => {
|
|
72
85
|
const sessionId = normalizeSessionId(event.sessionId, event.extra);
|
|
73
86
|
if (!sessionId)
|
|
@@ -86,11 +99,11 @@ export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
|
86
99
|
const batch = buildSessionInitBatch({
|
|
87
100
|
actorId: context.actorId,
|
|
88
101
|
sessionId,
|
|
89
|
-
requestId: event.requestId ?? randomUUID(),
|
|
90
102
|
startedAt,
|
|
91
103
|
extra: event.extra,
|
|
92
104
|
sessionInitKeys,
|
|
93
105
|
clientInfo: event.clientInfo ?? getClientInfoForSessionId(sessionId),
|
|
106
|
+
workflowRunId: resolveWorkflowRunId(event),
|
|
94
107
|
});
|
|
95
108
|
if (batch)
|
|
96
109
|
await emitBatch(batch);
|
|
@@ -119,6 +132,7 @@ export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
|
119
132
|
: event.error instanceof Error
|
|
120
133
|
? event.error.message
|
|
121
134
|
: String(event.error);
|
|
135
|
+
const workflowRunId = resolveWorkflowRunId(event);
|
|
122
136
|
const toolCallEvent = buildToolCallEvent({
|
|
123
137
|
toolName: event.name,
|
|
124
138
|
telemetry: event.telemetry,
|
|
@@ -132,6 +146,7 @@ export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
|
132
146
|
requestId,
|
|
133
147
|
startedAt,
|
|
134
148
|
finishedAt,
|
|
149
|
+
workflowRunId,
|
|
135
150
|
});
|
|
136
151
|
// An explicit clientInfo on the event always wins; otherwise look up
|
|
137
152
|
// whatever the initialize-handshake patch captured for this sessionId.
|
|
@@ -146,6 +161,7 @@ export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
|
|
|
146
161
|
startedAt,
|
|
147
162
|
sessionInitKeys,
|
|
148
163
|
clientInfo: effectiveClientInfo,
|
|
164
|
+
workflowRunId,
|
|
149
165
|
}));
|
|
150
166
|
};
|
|
151
167
|
const registeredTools = new Map();
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -85,6 +85,7 @@ export type RecordSessionInitEvent = {
|
|
|
85
85
|
requestId?: string;
|
|
86
86
|
startedAt?: string | Date | number;
|
|
87
87
|
clientInfo?: McpClientInfo;
|
|
88
|
+
workflowRunId?: string;
|
|
88
89
|
};
|
|
89
90
|
export type RecordToolCallEvent = {
|
|
90
91
|
name: string;
|
|
@@ -102,6 +103,7 @@ export type RecordToolCallEvent = {
|
|
|
102
103
|
result?: unknown;
|
|
103
104
|
error?: unknown;
|
|
104
105
|
clientInfo?: McpClientInfo;
|
|
106
|
+
workflowRunId?: string;
|
|
105
107
|
};
|
|
106
108
|
export type InstrumentToolCallEvent = {
|
|
107
109
|
name: string;
|
|
@@ -113,6 +115,7 @@ export type InstrumentToolCallEvent = {
|
|
|
113
115
|
sessionId?: string;
|
|
114
116
|
requestId?: string;
|
|
115
117
|
clientInfo?: McpClientInfo;
|
|
118
|
+
workflowRunId?: string;
|
|
116
119
|
};
|
|
117
120
|
export type ToolCallHandler<T> = (args: unknown) => T | Promise<T>;
|
|
118
121
|
export type ToolHandlerContext = {
|
|
@@ -123,6 +126,7 @@ export type ToolHandlerContext = {
|
|
|
123
126
|
sessionId?: string;
|
|
124
127
|
requestId?: string;
|
|
125
128
|
clientInfo?: McpClientInfo;
|
|
129
|
+
workflowRunId?: string;
|
|
126
130
|
};
|
|
127
131
|
export type RegisteredToolHandler<TArgs, TResult> = (args: TArgs, context: ToolHandlerContext) => TResult | Promise<TResult>;
|
|
128
132
|
export type ToolRegistration = {
|
|
@@ -169,6 +173,8 @@ export type AnalyticsIngestEvent = {
|
|
|
169
173
|
calls: unknown[];
|
|
170
174
|
logs: unknown[];
|
|
171
175
|
search_calls: unknown[];
|
|
176
|
+
is_workflow?: boolean;
|
|
177
|
+
workflow_run_id?: string;
|
|
172
178
|
};
|
|
173
179
|
export type AnalyticsIngestBatch = {
|
|
174
180
|
schema_version: 1;
|
package/dist/esm/utils.d.ts
CHANGED
|
@@ -8,9 +8,16 @@ export declare const isJsonObjectSchema: (value: unknown) => value is JsonObject
|
|
|
8
8
|
export declare const isRawShape: (value: unknown) => value is Record<string, unknown>;
|
|
9
9
|
export declare const readEnv: (key: string) => string | undefined;
|
|
10
10
|
export declare const sha256Hex: (value: string) => string;
|
|
11
|
+
export type BoundedKeySet = {
|
|
12
|
+
has: (key: string) => boolean;
|
|
13
|
+
add: (key: string) => void;
|
|
14
|
+
};
|
|
15
|
+
export declare const createBoundedKeySet: (maxEntries: number) => BoundedKeySet;
|
|
11
16
|
export declare const stringifyPreview: (value: unknown) => string;
|
|
12
17
|
export declare const truncateUtf8: (value: string, maxBytes: number) => {
|
|
13
18
|
value: string;
|
|
14
19
|
truncated: boolean;
|
|
15
20
|
};
|
|
21
|
+
export declare const WORKFLOW_RUN_ID_HEADER = "x-armature-workflow-run-id";
|
|
22
|
+
export declare const workflowRunIdFromHeaders: (headers: HeaderBag | undefined) => string | undefined;
|
|
16
23
|
export declare const headerValue: (headers: HeaderBag | undefined, name: string) => string | null;
|
package/dist/esm/utils.js
CHANGED
|
@@ -21,6 +21,30 @@ export const readEnv = (key) => {
|
|
|
21
21
|
export const sha256Hex = (value) => {
|
|
22
22
|
return createHash("sha256").update(value).digest("hex");
|
|
23
23
|
};
|
|
24
|
+
// A `Set<string>` capped at `maxEntries` with FIFO eviction (Map/Set iteration
|
|
25
|
+
// order is insertion order in JS, so the first key is the oldest). MCP fires no
|
|
26
|
+
// reliable "session-closed" signal, so an unbounded set of session keys would
|
|
27
|
+
// leak forever on a long-running server with high session churn — the same
|
|
28
|
+
// reasoning behind the client-info cache's bound. Eviction is safe because the
|
|
29
|
+
// only consumers (`session_init` de-dup) now derive a stable `event_id` per
|
|
30
|
+
// (actorId, sessionId), so a re-emitted `session_init` after eviction collapses
|
|
31
|
+
// to the same id at ingest rather than double-counting.
|
|
32
|
+
export const createBoundedKeySet = (maxEntries) => {
|
|
33
|
+
const keys = new Set();
|
|
34
|
+
return {
|
|
35
|
+
has: (key) => keys.has(key),
|
|
36
|
+
add: (key) => {
|
|
37
|
+
if (keys.has(key))
|
|
38
|
+
return;
|
|
39
|
+
if (keys.size >= maxEntries) {
|
|
40
|
+
const oldest = keys.values().next().value;
|
|
41
|
+
if (oldest !== undefined)
|
|
42
|
+
keys.delete(oldest);
|
|
43
|
+
}
|
|
44
|
+
keys.add(key);
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
};
|
|
24
48
|
export const stringifyPreview = (value) => {
|
|
25
49
|
if (value === undefined)
|
|
26
50
|
return "undefined";
|
|
@@ -40,6 +64,19 @@ export const truncateUtf8 = (value, maxBytes) => {
|
|
|
40
64
|
truncated: true,
|
|
41
65
|
};
|
|
42
66
|
};
|
|
67
|
+
// The Armature run dispatcher stamps this header on MCP connections opened
|
|
68
|
+
// by workflow runs. Telemetry produced under it is synthetic harness traffic:
|
|
69
|
+
// events carry `is_workflow` / `workflow_run_id` so Session Analytics can
|
|
70
|
+
// exclude them from every user-facing surface and processing pipeline.
|
|
71
|
+
export const WORKFLOW_RUN_ID_HEADER = "x-armature-workflow-run-id";
|
|
72
|
+
// Version nibble allows 1-8 so time-ordered v7 ids keep working if the run
|
|
73
|
+
// dispatcher ever stops minting v4.
|
|
74
|
+
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
75
|
+
export const workflowRunIdFromHeaders = (headers) => {
|
|
76
|
+
const raw = headerValue(headers, WORKFLOW_RUN_ID_HEADER);
|
|
77
|
+
const value = typeof raw === "string" ? raw.trim() : "";
|
|
78
|
+
return UUID_RE.test(value) ? value : undefined;
|
|
79
|
+
};
|
|
43
80
|
export const headerValue = (headers, name) => {
|
|
44
81
|
if (!headers)
|
|
45
82
|
return null;
|