@armature-tech/mcp-analytics 0.6.33 → 0.6.34

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.
@@ -0,0 +1,112 @@
1
+ import type { McpAnalyticsConfig } from "./types.js";
2
+ /** Header a caller can set to pin the analytics session id (rung 2 of the ladder). */
3
+ export declare const SESSION_SEED_HEADER = "x-armature-session-seed";
4
+ /** Structural stand-in for a v2 `McpServer`: anything with `registerTool`. */
5
+ export type V2McpServerLike = {
6
+ registerTool: (name: string, config: never, cb: never) => unknown;
7
+ };
8
+ /**
9
+ * v2-adapter behavior options, passed alongside the analytics config to
10
+ * `withMcpAnalytics` / `instrumentedFactory`. Kept separate from
11
+ * `McpAnalyticsConfig` because the recorder (delivery queue, session_init
12
+ * dedupe) is keyed on the config object; these options only shape how tools
13
+ * are wrapped and what metadata each event carries.
14
+ */
15
+ export type V2AdapterOptions = {
16
+ /**
17
+ * Advertise the Armature `telemetry` input property on every instrumented
18
+ * tool's schema (v1 parity, opt-in). The adapter rebuilds each tool's
19
+ * ADVERTISED schema from the SDK's own JSON conversion
20
+ * (`toolInputSchemaJson`) plus the telemetry property (byte-identical
21
+ * descriptions to the v1 integration) via `fromJsonSchema`, and swaps it in
22
+ * with `RegisteredTool.update({ paramsSchema })` — so `tools/list`
23
+ * advertises telemetry AND the SDK's pre-dispatch Ajv validation accepts it
24
+ * before the analytics wrapper strips it. A top-level
25
+ * `additionalProperties: false` in the customer schema is preserved:
26
+ * `telemetry` becomes a declared property (so it passes), while every other
27
+ * undeclared key is still rejected exactly as before.
28
+ *
29
+ * Tradeoff: validation then runs against the JSON Schema projection of the
30
+ * original schema. Zod runtime effects that don't survive JSON Schema
31
+ * conversion (`.transform()`, `.refine()`, applied `.default()` values) no
32
+ * longer run inside the SDK's validation step — which is why this is
33
+ * opt-in. Tools whose schema cannot be projected to a JSON object schema
34
+ * are left undecorated (telemetry is still stripped and exported when a
35
+ * client sends it). Tools that declare their own `telemetry` property stay
36
+ * customer-owned and untouched.
37
+ */
38
+ declareTelemetry?: boolean;
39
+ /**
40
+ * Per-event metadata hook: called once per tool call with the handler
41
+ * context, and its returned keys are merged into the tool_call event's
42
+ * metadata alongside the adapter's own keys (`protocol_era`, client
43
+ * identity, `request_meta`, …). Hook keys win over adapter-derived keys;
44
+ * contract-defined keys (`tool_name`, `user_intent`, …) always win over
45
+ * both (see events.ts metadataExtra). A throwing hook is ignored — it never
46
+ * breaks recording or the tool call.
47
+ */
48
+ metadata?: (ctx: unknown) => Record<string, unknown> | undefined;
49
+ };
50
+ /**
51
+ * Parse `gen_ai.conversation.id` out of a W3C `baggage` value: comma-separated
52
+ * `key=value` list entries, each optionally carrying `;`-separated properties,
53
+ * values URL-decoded. Rung 1 of the session-id ladder.
54
+ */
55
+ export declare const conversationIdFromBaggage: (baggage: unknown) => string | undefined;
56
+ /**
57
+ * Drain the analytics pipeline tied to a config object (or the default
58
+ * config). Call before process exit / serverless freeze when not using
59
+ * `delivery: "await"` or a `schedule` hook.
60
+ */
61
+ export declare const flushMcpAnalytics: (config?: McpAnalyticsConfig) => Promise<void>;
62
+ /**
63
+ * Instrument a v2 `McpServer` instance: every tool registered on it (before or
64
+ * after this call) records a `tool_call` analytics event with the same wire
65
+ * shape as the v1 SDK integration, plus a `session_init` on first sight of
66
+ * each session key. Returns the same (mutated) server instance.
67
+ *
68
+ * Under `createMcpHandler` the factory runs per request — prefer
69
+ * `instrumentedFactory`, or hoist the config object so every request shares
70
+ * one recorder (delivery queue + session_init dedupe are keyed on it).
71
+ *
72
+ * `options` (see V2AdapterOptions): `declareTelemetry` advertises the
73
+ * Armature telemetry property on each tool's schema (v1 parity, opt-in);
74
+ * `metadata` adds per-event metadata keys.
75
+ */
76
+ export declare const withMcpAnalytics: <S extends V2McpServerLike>(server: S, config?: McpAnalyticsConfig, options?: V2AdapterOptions) => S;
77
+ /**
78
+ * Wrap a `createMcpHandler` / `serveStdio` factory so every server it produces
79
+ * is instrumented with `withMcpAnalytics`. One recorder (privacy queue +
80
+ * session_init dedupe) is shared across all servers the wrapped factory
81
+ * creates, and the factory's `McpRequestContext` (`requestInfo`, `authInfo`)
82
+ * backfills header-based capture when the handler ctx carries no HTTP request
83
+ * (the low-level `Server` sets `ctx.http.authInfo` without `req`).
84
+ */
85
+ export declare const instrumentedFactory: <TArgs extends unknown[], TResult>(factory: (...args: TArgs) => TResult, config?: McpAnalyticsConfig, options?: V2AdapterOptions) => ((...args: TArgs) => TResult | Promise<Awaited<TResult>>);
86
+ /** Structural stand-in for the object `createMcpHandler` returns. */
87
+ export type V2McpHandlerLike = {
88
+ fetch: (request: Request, options?: never) => Promise<Response>;
89
+ };
90
+ /**
91
+ * Wrap a `createMcpHandler` handler's `fetch`.
92
+ *
93
+ * v2's `createMcpHandler` serves 2025-era clients through its stateless legacy
94
+ * leg, which mints NO `Mcp-Session-Id` (and offers no option to) — so a
95
+ * legacy client through a v2 handler silently loses session attribution.
96
+ * This wrapper repairs that: on a legacy `initialize` request it mints an
97
+ * identity-bearing session id (`mcp_<name>_v_<version>_<uuid>`, honoring
98
+ * `X-Armature-Session-Seed` as the uuid seed — the same scheme as
99
+ * `resolveStatelessHttpSession` in the v1 integration), attaches it as the
100
+ * response's `Mcp-Session-Id`, records the session_init, and echoes the id on
101
+ * later legacy responses. Conforming legacy clients echo the header on every
102
+ * subsequent request, where rung 3 of the session-id ladder picks it up.
103
+ *
104
+ * Composable and optional: `withMcpAnalytics` / `instrumentedFactory` work
105
+ * standalone (modern-era attribution needs no handler wrapper); pass the SAME
106
+ * config object to both so they share one recorder. Modern-era requests pass
107
+ * through this wrapper untouched (they carry no `initialize`).
108
+ */
109
+ export declare const wrapMcpHandler: <H extends V2McpHandlerLike>(handler: H, config?: McpAnalyticsConfig) => H;
110
+ export type { AnalyticsRecorderCore } from "./recorder-core.js";
111
+ export type { AnalyticsEventKind, AnalyticsIngestBatch, AnalyticsIngestEvent, McpAnalyticsConfig, McpClientInfo, TelemetryArgs, TelemetryEmitter, } from "./types.js";
112
+ export { defaultMcpAnalyticsConfig } from "./emit.js";