@armature-tech/mcp-analytics 0.6.34 → 0.6.35

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.
@@ -4,7 +4,6 @@ exports.defaultMcpAnalyticsConfig = exports.withMcpHandlerAnalytics = exports.fl
4
4
  const recorder_core_js_1 = require("./recorder-core.js");
5
5
  const emit_js_1 = require("./emit.js");
6
6
  const stateless_http_js_1 = require("./stateless-http.js");
7
- const utils_js_1 = require("./utils.js");
8
7
  const SESSION_ID_HEADER = "mcp-session-id";
9
8
  // Keyed on the config object so every request shares one recorder (privacy
10
9
  // queue + session_init dedupe) — hoist the config, same contract as the v2
@@ -29,35 +28,6 @@ const flushMcpHandlerAnalytics = (config) => {
29
28
  return recorderStateFor(config).core.flush();
30
29
  };
31
30
  exports.flushMcpHandlerAnalytics = flushMcpHandlerAnalytics;
32
- const trimOrUndefined = (value) => {
33
- if (typeof value !== "string")
34
- return undefined;
35
- const trimmed = value.trim();
36
- return trimmed.length > 0 ? trimmed : undefined;
37
- };
38
- // Full-fidelity clientInfo from the initialize body itself (name, version,
39
- // protocolVersion, capabilities) — richer than what the minted id can carry,
40
- // so the session_init recorded at initialize matches what a stateful v1
41
- // deployment would report.
42
- const clientInfoFromInitializeBody = (body) => {
43
- const messages = Array.isArray(body) ? body : [body];
44
- const initialize = messages.find((message) => (0, utils_js_1.isRecord)(message) && message.method === "initialize");
45
- const params = (0, utils_js_1.isRecord)(initialize?.params) ? initialize.params : undefined;
46
- if (!params)
47
- return undefined;
48
- const info = (0, utils_js_1.isRecord)(params.clientInfo) ? params.clientInfo : undefined;
49
- const name = trimOrUndefined(info?.name);
50
- if (name === undefined)
51
- return undefined;
52
- const version = trimOrUndefined(info?.version);
53
- const protocolVersion = trimOrUndefined(params.protocolVersion);
54
- return {
55
- name,
56
- ...(version !== undefined ? { version } : {}),
57
- ...(protocolVersion !== undefined ? { protocolVersion } : {}),
58
- capabilities: (0, utils_js_1.isRecord)(params.capabilities) ? params.capabilities : null,
59
- };
60
- };
61
31
  const withSessionIdHeader = (response, sessionId) => {
62
32
  if (response.headers.get(SESSION_ID_HEADER))
63
33
  return response;
@@ -113,7 +83,7 @@ const withMcpHandlerAnalytics = (handler, config) => {
113
83
  await state.core.recordSessionInit({
114
84
  sessionId: session.sessionId,
115
85
  headers: request.headers,
116
- clientInfo: clientInfoFromInitializeBody(body),
86
+ clientInfo: (0, stateless_http_js_1.clientInfoFromInitializeBody)(body),
117
87
  extra: {
118
88
  sessionId: session.sessionId,
119
89
  requestInfo: { headers: request.headers },
@@ -3,6 +3,7 @@ import type { HeaderBag, McpClientInfo } from "./types.js";
3
3
  export declare const buildStatelessSessionId: (clientInfo?: McpClientInfo, sessionSeed?: string) => string;
4
4
  /** Recover the client identity from an identity-bearing session id. */
5
5
  export declare const parseStatelessSessionClientInfo: (sessionId: string | undefined) => McpClientInfo | undefined;
6
+ export declare const clientInfoFromInitializeBody: (body: unknown) => McpClientInfo | undefined;
6
7
  export type StatelessHttpSession = {
7
8
  /** Stable session id: minted at initialize, parsed from the echoed header otherwise. */
8
9
  sessionId: string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveStatelessHttpSession = exports.parseStatelessSessionClientInfo = exports.buildStatelessSessionId = void 0;
3
+ exports.resolveStatelessHttpSession = exports.clientInfoFromInitializeBody = exports.parseStatelessSessionClientInfo = exports.buildStatelessSessionId = void 0;
4
4
  const node_crypto_1 = require("node:crypto");
5
5
  const utils_js_1 = require("./utils.js");
6
6
  // Stateless HTTP deployments (Vercel, Lambda, Cloud Run with no sticky
@@ -71,6 +71,36 @@ const clientInfoFromInitialize = (message) => {
71
71
  ...(typeof info.version === "string" ? { version: info.version } : {}),
72
72
  };
73
73
  };
74
+ const trimOrUndefined = (value) => {
75
+ if (typeof value !== "string")
76
+ return undefined;
77
+ const trimmed = value.trim();
78
+ return trimmed.length > 0 ? trimmed : undefined;
79
+ };
80
+ // Full-fidelity clientInfo from the initialize body itself (name, version,
81
+ // protocolVersion, capabilities) — richer than what the minted id can carry,
82
+ // so the session_init recorded at initialize matches what a stateful v1
83
+ // deployment would report. Shared by the stateless-handler wrappers
84
+ // (withMcpHandlerAnalytics and the v2 wrapMcpHandler legacy leg).
85
+ const clientInfoFromInitializeBody = (body) => {
86
+ const initialize = findInitializeMessage(body);
87
+ const params = initialize && (0, utils_js_1.isRecord)(initialize.params) ? initialize.params : undefined;
88
+ if (!params)
89
+ return undefined;
90
+ const info = (0, utils_js_1.isRecord)(params.clientInfo) ? params.clientInfo : undefined;
91
+ const name = trimOrUndefined(info?.name);
92
+ if (name === undefined)
93
+ return undefined;
94
+ const version = trimOrUndefined(info?.version);
95
+ const protocolVersion = trimOrUndefined(params.protocolVersion);
96
+ return {
97
+ name,
98
+ ...(version !== undefined ? { version } : {}),
99
+ ...(protocolVersion !== undefined ? { protocolVersion } : {}),
100
+ capabilities: (0, utils_js_1.isRecord)(params.capabilities) ? params.capabilities : null,
101
+ };
102
+ };
103
+ exports.clientInfoFromInitializeBody = clientInfoFromInitializeBody;
74
104
  const resolveStatelessHttpSession = (input) => {
75
105
  const initialize = findInitializeMessage(input.body);
76
106
  if (initialize) {
package/dist/cjs/v2.js CHANGED
@@ -582,10 +582,11 @@ const wrapMcpHandler = (handler, config) => {
582
582
  ? fetchOptions.authInfo
583
583
  : undefined;
584
584
  let session;
585
+ let body;
585
586
  if (request.method === "POST") {
586
587
  try {
587
588
  const raw = await request.clone().text();
588
- const body = raw ? JSON.parse(raw) : undefined;
589
+ body = raw ? JSON.parse(raw) : undefined;
589
590
  // `initialize` exists only on the 2025 era, so `isInitialize` doubles
590
591
  // as the legacy detector; modern-era requests fall through untouched.
591
592
  session = (0, stateless_http_js_1.resolveStatelessHttpSession)({ body, headers: request.headers });
@@ -596,14 +597,17 @@ const wrapMcpHandler = (handler, config) => {
596
597
  }
597
598
  const response = await originalFetch(request, ...rest);
598
599
  if (session?.isInitialize && response.ok) {
599
- // The identity-bearing id embeds the initialize body's clientInfo, and
600
- // the recorder recovers name/version from it (parseStatelessSessionClientInfo)
601
- // — so the session_init recorded here is attributed even though the
602
- // initialize landed on a throwaway per-request server instance.
600
+ // Full-fidelity clientInfo from the initialize body: the minted id only
601
+ // carries name/version, so without this the session_init would lose the
602
+ // negotiated protocolVersion (and capabilities) — recorded here even
603
+ // though the initialize landed on a throwaway per-request server
604
+ // instance.
605
+ const clientInfo = (0, stateless_http_js_1.clientInfoFromInitializeBody)(body);
603
606
  try {
604
607
  await state.core.recordSessionInit({
605
608
  sessionId: session.sessionId,
606
609
  headers: request.headers,
610
+ ...(clientInfo !== undefined ? { clientInfo } : {}),
607
611
  ...(authInfo !== undefined ? { authInfo } : {}),
608
612
  extra: {
609
613
  sessionId: session.sessionId,
@@ -1,7 +1,6 @@
1
1
  import { createAnalyticsRecorderCore, } from "./recorder-core.js";
2
2
  import { defaultMcpAnalyticsConfig } from "./emit.js";
3
- import { resolveStatelessHttpSession } from "./stateless-http.js";
4
- import { isRecord } from "./utils.js";
3
+ import { clientInfoFromInitializeBody, resolveStatelessHttpSession, } from "./stateless-http.js";
5
4
  const SESSION_ID_HEADER = "mcp-session-id";
6
5
  // Keyed on the config object so every request shares one recorder (privacy
7
6
  // queue + session_init dedupe) — hoist the config, same contract as the v2
@@ -25,35 +24,6 @@ const recorderStateFor = (config) => {
25
24
  export const flushMcpHandlerAnalytics = (config) => {
26
25
  return recorderStateFor(config).core.flush();
27
26
  };
28
- const trimOrUndefined = (value) => {
29
- if (typeof value !== "string")
30
- return undefined;
31
- const trimmed = value.trim();
32
- return trimmed.length > 0 ? trimmed : undefined;
33
- };
34
- // Full-fidelity clientInfo from the initialize body itself (name, version,
35
- // protocolVersion, capabilities) — richer than what the minted id can carry,
36
- // so the session_init recorded at initialize matches what a stateful v1
37
- // deployment would report.
38
- const clientInfoFromInitializeBody = (body) => {
39
- const messages = Array.isArray(body) ? body : [body];
40
- const initialize = messages.find((message) => isRecord(message) && message.method === "initialize");
41
- const params = isRecord(initialize?.params) ? initialize.params : undefined;
42
- if (!params)
43
- return undefined;
44
- const info = isRecord(params.clientInfo) ? params.clientInfo : undefined;
45
- const name = trimOrUndefined(info?.name);
46
- if (name === undefined)
47
- return undefined;
48
- const version = trimOrUndefined(info?.version);
49
- const protocolVersion = trimOrUndefined(params.protocolVersion);
50
- return {
51
- name,
52
- ...(version !== undefined ? { version } : {}),
53
- ...(protocolVersion !== undefined ? { protocolVersion } : {}),
54
- capabilities: isRecord(params.capabilities) ? params.capabilities : null,
55
- };
56
- };
57
27
  const withSessionIdHeader = (response, sessionId) => {
58
28
  if (response.headers.get(SESSION_ID_HEADER))
59
29
  return response;
@@ -3,6 +3,7 @@ import type { HeaderBag, McpClientInfo } from "./types.js";
3
3
  export declare const buildStatelessSessionId: (clientInfo?: McpClientInfo, sessionSeed?: string) => string;
4
4
  /** Recover the client identity from an identity-bearing session id. */
5
5
  export declare const parseStatelessSessionClientInfo: (sessionId: string | undefined) => McpClientInfo | undefined;
6
+ export declare const clientInfoFromInitializeBody: (body: unknown) => McpClientInfo | undefined;
6
7
  export type StatelessHttpSession = {
7
8
  /** Stable session id: minted at initialize, parsed from the echoed header otherwise. */
8
9
  sessionId: string;
@@ -66,6 +66,35 @@ const clientInfoFromInitialize = (message) => {
66
66
  ...(typeof info.version === "string" ? { version: info.version } : {}),
67
67
  };
68
68
  };
69
+ const trimOrUndefined = (value) => {
70
+ if (typeof value !== "string")
71
+ return undefined;
72
+ const trimmed = value.trim();
73
+ return trimmed.length > 0 ? trimmed : undefined;
74
+ };
75
+ // Full-fidelity clientInfo from the initialize body itself (name, version,
76
+ // protocolVersion, capabilities) — richer than what the minted id can carry,
77
+ // so the session_init recorded at initialize matches what a stateful v1
78
+ // deployment would report. Shared by the stateless-handler wrappers
79
+ // (withMcpHandlerAnalytics and the v2 wrapMcpHandler legacy leg).
80
+ export const clientInfoFromInitializeBody = (body) => {
81
+ const initialize = findInitializeMessage(body);
82
+ const params = initialize && isRecord(initialize.params) ? initialize.params : undefined;
83
+ if (!params)
84
+ return undefined;
85
+ const info = isRecord(params.clientInfo) ? params.clientInfo : undefined;
86
+ const name = trimOrUndefined(info?.name);
87
+ if (name === undefined)
88
+ return undefined;
89
+ const version = trimOrUndefined(info?.version);
90
+ const protocolVersion = trimOrUndefined(params.protocolVersion);
91
+ return {
92
+ name,
93
+ ...(version !== undefined ? { version } : {}),
94
+ ...(protocolVersion !== undefined ? { protocolVersion } : {}),
95
+ capabilities: isRecord(params.capabilities) ? params.capabilities : null,
96
+ };
97
+ };
69
98
  export const resolveStatelessHttpSession = (input) => {
70
99
  const initialize = findInitializeMessage(input.body);
71
100
  if (initialize) {
package/dist/esm/v2.js CHANGED
@@ -6,7 +6,7 @@ import { capCapabilities } from "./events.js";
6
6
  import { prepareForPreview } from "./sanitize.js";
7
7
  import { headerValue, isRecord, stringifyPreview, truncateUtf8 } from "./utils.js";
8
8
  import { processScopedSessionId } from "./stdio-session.js";
9
- import { resolveStatelessHttpSession } from "./stateless-http.js";
9
+ import { clientInfoFromInitializeBody, resolveStatelessHttpSession, } from "./stateless-http.js";
10
10
  // Adapter for the v2 MCP TypeScript SDK (@modelcontextprotocol/server 2.x,
11
11
  // protocol revision 2026-07-28). The v2 protocol is stateless: there is no
12
12
  // initialize handshake and no Mcp-Session-Id — client identity travels
@@ -575,10 +575,11 @@ export const wrapMcpHandler = (handler, config) => {
575
575
  ? fetchOptions.authInfo
576
576
  : undefined;
577
577
  let session;
578
+ let body;
578
579
  if (request.method === "POST") {
579
580
  try {
580
581
  const raw = await request.clone().text();
581
- const body = raw ? JSON.parse(raw) : undefined;
582
+ body = raw ? JSON.parse(raw) : undefined;
582
583
  // `initialize` exists only on the 2025 era, so `isInitialize` doubles
583
584
  // as the legacy detector; modern-era requests fall through untouched.
584
585
  session = resolveStatelessHttpSession({ body, headers: request.headers });
@@ -589,14 +590,17 @@ export const wrapMcpHandler = (handler, config) => {
589
590
  }
590
591
  const response = await originalFetch(request, ...rest);
591
592
  if (session?.isInitialize && response.ok) {
592
- // The identity-bearing id embeds the initialize body's clientInfo, and
593
- // the recorder recovers name/version from it (parseStatelessSessionClientInfo)
594
- // — so the session_init recorded here is attributed even though the
595
- // initialize landed on a throwaway per-request server instance.
593
+ // Full-fidelity clientInfo from the initialize body: the minted id only
594
+ // carries name/version, so without this the session_init would lose the
595
+ // negotiated protocolVersion (and capabilities) — recorded here even
596
+ // though the initialize landed on a throwaway per-request server
597
+ // instance.
598
+ const clientInfo = clientInfoFromInitializeBody(body);
596
599
  try {
597
600
  await state.core.recordSessionInit({
598
601
  sessionId: session.sessionId,
599
602
  headers: request.headers,
603
+ ...(clientInfo !== undefined ? { clientInfo } : {}),
600
604
  ...(authInfo !== undefined ? { authInfo } : {}),
601
605
  extra: {
602
606
  sessionId: session.sessionId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@armature-tech/mcp-analytics",
3
- "version": "0.6.34",
3
+ "version": "0.6.35",
4
4
  "type": "module",
5
5
  "description": "MCP analytics wrapper SDK that instruments MCP tool declarations with telemetry.",
6
6
  "license": "Apache-2.0",