@armature-tech/mcp-analytics 0.6.36 → 0.6.37

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.
@@ -78,14 +78,17 @@ const withMcpHandlerAnalytics = (handler, config) => {
78
78
  }
79
79
  }
80
80
  const response = await handler(request, ...rest);
81
- if (session?.isInitialize && response.ok) {
81
+ // `sessionId` is always minted on initialize; the check narrows the type
82
+ // for the non-initialize case, where it is now optional.
83
+ if (session?.isInitialize && session.sessionId && response.ok) {
84
+ const mintedSessionId = session.sessionId;
82
85
  try {
83
86
  await state.core.recordSessionInit({
84
- sessionId: session.sessionId,
87
+ sessionId: mintedSessionId,
85
88
  headers: request.headers,
86
89
  clientInfo: (0, stateless_http_js_1.clientInfoFromInitializeBody)(body),
87
90
  extra: {
88
- sessionId: session.sessionId,
91
+ sessionId: mintedSessionId,
89
92
  requestInfo: { headers: request.headers },
90
93
  },
91
94
  });
@@ -93,7 +96,7 @@ const withMcpHandlerAnalytics = (handler, config) => {
93
96
  catch {
94
97
  // Recording is best-effort; never break the MCP response over it.
95
98
  }
96
- return withSessionIdHeader(response, session.sessionId);
99
+ return withSessionIdHeader(response, mintedSessionId);
97
100
  }
98
101
  // Echo an id the client is already carrying, so it keeps carrying it.
99
102
  const echoed = request.headers.get(SESSION_ID_HEADER)?.trim();
@@ -5,8 +5,12 @@ export declare const buildStatelessSessionId: (clientInfo?: McpClientInfo, sessi
5
5
  export declare const parseStatelessSessionClientInfo: (sessionId: string | undefined) => McpClientInfo | undefined;
6
6
  export declare const clientInfoFromInitializeBody: (body: unknown) => McpClientInfo | undefined;
7
7
  export type StatelessHttpSession = {
8
- /** Stable session id: minted at initialize, parsed from the echoed header otherwise. */
9
- sessionId: string;
8
+ /**
9
+ * Stable session id: minted at initialize, parsed from the echoed header
10
+ * otherwise. Undefined when the request carries neither — see
11
+ * `resolveStatelessHttpSession`.
12
+ */
13
+ sessionId?: string;
10
14
  /** Client identity recovered from the echoed session id (tool-call requests only). */
11
15
  clientInfo?: McpClientInfo;
12
16
  /**
@@ -17,10 +21,20 @@ export type StatelessHttpSession = {
17
21
  sessionIdGenerator?: () => string;
18
22
  /** True when the request (or any message in a batch) is `initialize`. */
19
23
  isInitialize: boolean;
20
- /** Spread into `dispatch(name, args, { ctx, ...dispatchContext })`. */
24
+ /**
25
+ * Spread into `dispatch(name, args, { ctx, ...dispatchContext })`.
26
+ *
27
+ * When there is no session id the request headers ride along instead. The
28
+ * recorder reads "no session id AND no headers" as "no HTTP request at
29
+ * all" and falls back to the process-scoped stdio id, which would merge
30
+ * every concurrent conversation in a long-lived server into one session.
31
+ * Passing the headers keeps the request identifiable as HTTP-with-no-id,
32
+ * so the event ships a null hint and ingest groups it.
33
+ */
21
34
  dispatchContext: {
22
- sessionId: string;
35
+ sessionId?: string;
23
36
  clientInfo?: McpClientInfo;
37
+ headers?: HeaderBag;
24
38
  };
25
39
  };
26
40
  export declare const resolveStatelessHttpSession: (input: {
@@ -113,8 +113,20 @@ const resolveStatelessHttpSession = (input) => {
113
113
  dispatchContext: { sessionId },
114
114
  };
115
115
  }
116
- const echoed = (0, utils_js_1.headerValue)(input.headers, "mcp-session-id");
117
- const sessionId = echoed?.trim() || (0, node_crypto_1.randomUUID)();
116
+ const sessionId = (0, utils_js_1.headerValue)(input.headers, "mcp-session-id")?.trim();
117
+ if (!sessionId) {
118
+ // No initialize to mint from and no echo to recover: either modern-era
119
+ // traffic (identified through `_meta` / the session seed) or a client
120
+ // that drops the header. Minting here would stamp a distinct id on every
121
+ // POST, and ingest trusts an explicit hint, so each tool call would land
122
+ // in its own single-event session. Reporting no id ships
123
+ // `session_id_hint: null` and lets ingest group by actor + client with
124
+ // its inactivity window instead. Matches the Go SDK's empty session id.
125
+ return {
126
+ isInitialize: false,
127
+ dispatchContext: input.headers ? { headers: input.headers } : {},
128
+ };
129
+ }
118
130
  const clientInfo = (0, exports.parseStatelessSessionClientInfo)(sessionId);
119
131
  return {
120
132
  sessionId,
package/dist/cjs/v2.js CHANGED
@@ -596,7 +596,10 @@ const wrapMcpHandler = (handler, config) => {
596
596
  }
597
597
  }
598
598
  const response = await originalFetch(request, ...rest);
599
- if (session?.isInitialize && response.ok) {
599
+ // `sessionId` is always minted on initialize; the check narrows the type
600
+ // for the non-initialize case, where it is now optional.
601
+ if (session?.isInitialize && session.sessionId && response.ok) {
602
+ const mintedSessionId = session.sessionId;
600
603
  // Full-fidelity clientInfo from the initialize body: the minted id only
601
604
  // carries name/version, so without this the session_init would lose the
602
605
  // negotiated protocolVersion (and capabilities) — recorded here even
@@ -605,12 +608,12 @@ const wrapMcpHandler = (handler, config) => {
605
608
  const clientInfo = (0, stateless_http_js_1.clientInfoFromInitializeBody)(body);
606
609
  try {
607
610
  await state.core.recordSessionInit({
608
- sessionId: session.sessionId,
611
+ sessionId: mintedSessionId,
609
612
  headers: request.headers,
610
613
  ...(clientInfo !== undefined ? { clientInfo } : {}),
611
614
  ...(authInfo !== undefined ? { authInfo } : {}),
612
615
  extra: {
613
- sessionId: session.sessionId,
616
+ sessionId: mintedSessionId,
614
617
  requestInfo: { headers: request.headers },
615
618
  ...(authInfo !== undefined ? { authInfo } : {}),
616
619
  },
@@ -619,7 +622,7 @@ const wrapMcpHandler = (handler, config) => {
619
622
  catch {
620
623
  // Recording is best-effort; never break the MCP response over it.
621
624
  }
622
- return withSessionIdHeader(response, session.sessionId);
625
+ return withSessionIdHeader(response, mintedSessionId);
623
626
  }
624
627
  // Echo an id the legacy client is already carrying, so it keeps carrying it.
625
628
  const echoed = request.headers.get(SESSION_ID_HEADER)?.trim();
@@ -74,14 +74,17 @@ export const withMcpHandlerAnalytics = (handler, config) => {
74
74
  }
75
75
  }
76
76
  const response = await handler(request, ...rest);
77
- if (session?.isInitialize && response.ok) {
77
+ // `sessionId` is always minted on initialize; the check narrows the type
78
+ // for the non-initialize case, where it is now optional.
79
+ if (session?.isInitialize && session.sessionId && response.ok) {
80
+ const mintedSessionId = session.sessionId;
78
81
  try {
79
82
  await state.core.recordSessionInit({
80
- sessionId: session.sessionId,
83
+ sessionId: mintedSessionId,
81
84
  headers: request.headers,
82
85
  clientInfo: clientInfoFromInitializeBody(body),
83
86
  extra: {
84
- sessionId: session.sessionId,
87
+ sessionId: mintedSessionId,
85
88
  requestInfo: { headers: request.headers },
86
89
  },
87
90
  });
@@ -89,7 +92,7 @@ export const withMcpHandlerAnalytics = (handler, config) => {
89
92
  catch {
90
93
  // Recording is best-effort; never break the MCP response over it.
91
94
  }
92
- return withSessionIdHeader(response, session.sessionId);
95
+ return withSessionIdHeader(response, mintedSessionId);
93
96
  }
94
97
  // Echo an id the client is already carrying, so it keeps carrying it.
95
98
  const echoed = request.headers.get(SESSION_ID_HEADER)?.trim();
@@ -5,8 +5,12 @@ export declare const buildStatelessSessionId: (clientInfo?: McpClientInfo, sessi
5
5
  export declare const parseStatelessSessionClientInfo: (sessionId: string | undefined) => McpClientInfo | undefined;
6
6
  export declare const clientInfoFromInitializeBody: (body: unknown) => McpClientInfo | undefined;
7
7
  export type StatelessHttpSession = {
8
- /** Stable session id: minted at initialize, parsed from the echoed header otherwise. */
9
- sessionId: string;
8
+ /**
9
+ * Stable session id: minted at initialize, parsed from the echoed header
10
+ * otherwise. Undefined when the request carries neither — see
11
+ * `resolveStatelessHttpSession`.
12
+ */
13
+ sessionId?: string;
10
14
  /** Client identity recovered from the echoed session id (tool-call requests only). */
11
15
  clientInfo?: McpClientInfo;
12
16
  /**
@@ -17,10 +21,20 @@ export type StatelessHttpSession = {
17
21
  sessionIdGenerator?: () => string;
18
22
  /** True when the request (or any message in a batch) is `initialize`. */
19
23
  isInitialize: boolean;
20
- /** Spread into `dispatch(name, args, { ctx, ...dispatchContext })`. */
24
+ /**
25
+ * Spread into `dispatch(name, args, { ctx, ...dispatchContext })`.
26
+ *
27
+ * When there is no session id the request headers ride along instead. The
28
+ * recorder reads "no session id AND no headers" as "no HTTP request at
29
+ * all" and falls back to the process-scoped stdio id, which would merge
30
+ * every concurrent conversation in a long-lived server into one session.
31
+ * Passing the headers keeps the request identifiable as HTTP-with-no-id,
32
+ * so the event ships a null hint and ingest groups it.
33
+ */
21
34
  dispatchContext: {
22
- sessionId: string;
35
+ sessionId?: string;
23
36
  clientInfo?: McpClientInfo;
37
+ headers?: HeaderBag;
24
38
  };
25
39
  };
26
40
  export declare const resolveStatelessHttpSession: (input: {
@@ -107,8 +107,20 @@ export const resolveStatelessHttpSession = (input) => {
107
107
  dispatchContext: { sessionId },
108
108
  };
109
109
  }
110
- const echoed = headerValue(input.headers, "mcp-session-id");
111
- const sessionId = echoed?.trim() || randomUUID();
110
+ const sessionId = headerValue(input.headers, "mcp-session-id")?.trim();
111
+ if (!sessionId) {
112
+ // No initialize to mint from and no echo to recover: either modern-era
113
+ // traffic (identified through `_meta` / the session seed) or a client
114
+ // that drops the header. Minting here would stamp a distinct id on every
115
+ // POST, and ingest trusts an explicit hint, so each tool call would land
116
+ // in its own single-event session. Reporting no id ships
117
+ // `session_id_hint: null` and lets ingest group by actor + client with
118
+ // its inactivity window instead. Matches the Go SDK's empty session id.
119
+ return {
120
+ isInitialize: false,
121
+ dispatchContext: input.headers ? { headers: input.headers } : {},
122
+ };
123
+ }
112
124
  const clientInfo = parseStatelessSessionClientInfo(sessionId);
113
125
  return {
114
126
  sessionId,
package/dist/esm/v2.js CHANGED
@@ -589,7 +589,10 @@ export const wrapMcpHandler = (handler, config) => {
589
589
  }
590
590
  }
591
591
  const response = await originalFetch(request, ...rest);
592
- if (session?.isInitialize && response.ok) {
592
+ // `sessionId` is always minted on initialize; the check narrows the type
593
+ // for the non-initialize case, where it is now optional.
594
+ if (session?.isInitialize && session.sessionId && response.ok) {
595
+ const mintedSessionId = session.sessionId;
593
596
  // Full-fidelity clientInfo from the initialize body: the minted id only
594
597
  // carries name/version, so without this the session_init would lose the
595
598
  // negotiated protocolVersion (and capabilities) — recorded here even
@@ -598,12 +601,12 @@ export const wrapMcpHandler = (handler, config) => {
598
601
  const clientInfo = clientInfoFromInitializeBody(body);
599
602
  try {
600
603
  await state.core.recordSessionInit({
601
- sessionId: session.sessionId,
604
+ sessionId: mintedSessionId,
602
605
  headers: request.headers,
603
606
  ...(clientInfo !== undefined ? { clientInfo } : {}),
604
607
  ...(authInfo !== undefined ? { authInfo } : {}),
605
608
  extra: {
606
- sessionId: session.sessionId,
609
+ sessionId: mintedSessionId,
607
610
  requestInfo: { headers: request.headers },
608
611
  ...(authInfo !== undefined ? { authInfo } : {}),
609
612
  },
@@ -612,7 +615,7 @@ export const wrapMcpHandler = (handler, config) => {
612
615
  catch {
613
616
  // Recording is best-effort; never break the MCP response over it.
614
617
  }
615
- return withSessionIdHeader(response, session.sessionId);
618
+ return withSessionIdHeader(response, mintedSessionId);
616
619
  }
617
620
  // Echo an id the legacy client is already carrying, so it keeps carrying it.
618
621
  const echoed = request.headers.get(SESSION_ID_HEADER)?.trim();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@armature-tech/mcp-analytics",
3
- "version": "0.6.36",
3
+ "version": "0.6.37",
4
4
  "type": "module",
5
5
  "description": "MCP analytics wrapper SDK that instruments MCP tool declarations with telemetry.",
6
6
  "license": "Apache-2.0",