@armature-tech/mcp-analytics 0.6.22 → 0.6.24

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/SKILL.md CHANGED
@@ -313,6 +313,51 @@ wrapMastraTools(tools, {
313
313
  });
314
314
  ```
315
315
 
316
+ **Stateless / serverless HTTP (Vercel, Lambda):** do NOT serve with
317
+ `startHTTP({ options: { serverless: true } })`. Mastra's serverless mode
318
+ hard-codes `sessionIdGenerator: undefined` on the transient transport (your
319
+ generator is ignored), so no `Mcp-Session-Id` is ever issued at `initialize`,
320
+ clients have nothing to echo, and every tool call ships with no session hint —
321
+ ingest falls back to per-actor-per-day bucketing and concurrent conversations
322
+ merge into one dashboard session with client "unknown".
323
+
324
+ Use the hybrid instead: resolve the session from the request body first, then
325
+ let `initialize` (the only request where the resolved `sessionIdGenerator` is
326
+ defined) take Mastra's stateful branch and issue the identity-bearing id. Every
327
+ other request carries `sessionIdGenerator: undefined`, which Mastra treats as
328
+ stateless — fresh transient transport, no in-memory session lookup, so warm and
329
+ cold instances behave identically:
330
+
331
+ ```ts
332
+ import { resolveStatelessHttpSession } from "@armature-tech/mcp-analytics";
333
+
334
+ const body = req.method === "POST" ? await readJsonBody(req) : undefined;
335
+ // Mastra's own readJsonBody reuses a pre-parsed body — set it so the stream
336
+ // isn't consumed twice.
337
+ (req as IncomingMessage & { body?: unknown }).body = body;
338
+ const session = resolveStatelessHttpSession({ body, headers: req.headers });
339
+ await mcpServer.startHTTP({
340
+ url: new URL(req.url ?? "/", `http://${req.headers.host ?? "localhost"}`),
341
+ httpPath: "/mcp",
342
+ req,
343
+ res,
344
+ options: {
345
+ sessionIdGenerator: session.sessionIdGenerator, // defined only at initialize
346
+ enableJsonResponse: true,
347
+ },
348
+ });
349
+ await analytics.flush(); // with createMastraAnalytics; or rely on delivery: "await"
350
+ ```
351
+
352
+ The client echoes the issued `Mcp-Session-Id` (`mcp_<name>_v_<version>_<uuid>`)
353
+ on every subsequent request; the adapter forwards `requestInfo.headers` from
354
+ `context.mcp.extra`, and the recorder recovers the session from that header —
355
+ stable sessions and client attribution with no session store. Verify the wiring
356
+ with one curl: POST an `initialize` and check the response carries an
357
+ `mcp-session-id` header shaped `mcp_<client>_v_<version>_<uuid>`. If it's
358
+ missing, the request took the serverless path — make sure the `options` object
359
+ passes the resolved `sessionIdGenerator` field itself, not a wrapper function.
360
+
316
361
  The SDK also recognises `authInfo.apiKey` and `authInfo.principalId` as actor-seed
317
362
  aliases (alongside `authInfo.token` and `authInfo.clientId`), so Mastra setups that
318
363
  expose auth under those names don't need a custom `actorId` resolver.
@@ -57,6 +57,16 @@ exports.buildActorIdentityEvent = buildActorIdentityEvent;
57
57
  const buildToolCallSource = (toolName, input) => {
58
58
  return `MCP tool call: ${toolName}\n\nInput:\n${(0, utils_js_1.stringifyPreview)(input)}`;
59
59
  };
60
+ // Telemetry text values get the same treatment as tool inputs/outputs:
61
+ // base64/binary sanitization (always) plus built-in secret redaction (when
62
+ // enabled). Previously these fields only saw redactSecretsInString, so a
63
+ // whole-value or embedded ≥512-char base64 blob reached the ingest body
64
+ // verbatim (#1393). The legacy `redact` hook still runs once over the whole
65
+ // telemetry object in prepareTelemetry, so it is not passed here.
66
+ const sanitizeTelemetryText = (value, redactSecrets) => {
67
+ const prepared = (0, sanitize_js_1.prepareForPreview)(value, undefined, { redactSecrets });
68
+ return typeof prepared === "string" ? prepared : (0, utils_js_1.stringifyPreview)(prepared);
69
+ };
60
70
  const prepareTelemetry = (telemetry, redact, redactSecrets) => {
61
71
  const normalized = (0, schema_js_1.normalizeTelemetryArgs)(telemetry);
62
72
  if (!normalized)
@@ -64,18 +74,10 @@ const prepareTelemetry = (telemetry, redact, redactSecrets) => {
64
74
  const protectedTelemetry = {
65
75
  ...normalized,
66
76
  ...(typeof normalized.user_intent === "string"
67
- ? {
68
- user_intent: redactSecrets
69
- ? (0, redact_secrets_js_1.redactSecretsInString)(normalized.user_intent)
70
- : normalized.user_intent,
71
- }
77
+ ? { user_intent: sanitizeTelemetryText(normalized.user_intent, redactSecrets) }
72
78
  : {}),
73
79
  ...(typeof normalized.agent_thinking === "string"
74
- ? {
75
- agent_thinking: redactSecrets
76
- ? (0, redact_secrets_js_1.redactSecretsInString)(normalized.agent_thinking)
77
- : normalized.agent_thinking,
78
- }
80
+ ? { agent_thinking: sanitizeTelemetryText(normalized.agent_thinking, redactSecrets) }
79
81
  : {}),
80
82
  };
81
83
  if (!redact)
@@ -51,6 +51,16 @@ export const buildActorIdentityEvent = ({ actorId, identifier, startedAt, }) =>
51
51
  const buildToolCallSource = (toolName, input) => {
52
52
  return `MCP tool call: ${toolName}\n\nInput:\n${stringifyPreview(input)}`;
53
53
  };
54
+ // Telemetry text values get the same treatment as tool inputs/outputs:
55
+ // base64/binary sanitization (always) plus built-in secret redaction (when
56
+ // enabled). Previously these fields only saw redactSecretsInString, so a
57
+ // whole-value or embedded ≥512-char base64 blob reached the ingest body
58
+ // verbatim (#1393). The legacy `redact` hook still runs once over the whole
59
+ // telemetry object in prepareTelemetry, so it is not passed here.
60
+ const sanitizeTelemetryText = (value, redactSecrets) => {
61
+ const prepared = prepareForPreview(value, undefined, { redactSecrets });
62
+ return typeof prepared === "string" ? prepared : stringifyPreview(prepared);
63
+ };
54
64
  const prepareTelemetry = (telemetry, redact, redactSecrets) => {
55
65
  const normalized = normalizeTelemetryArgs(telemetry);
56
66
  if (!normalized)
@@ -58,18 +68,10 @@ const prepareTelemetry = (telemetry, redact, redactSecrets) => {
58
68
  const protectedTelemetry = {
59
69
  ...normalized,
60
70
  ...(typeof normalized.user_intent === "string"
61
- ? {
62
- user_intent: redactSecrets
63
- ? redactSecretsInString(normalized.user_intent)
64
- : normalized.user_intent,
65
- }
71
+ ? { user_intent: sanitizeTelemetryText(normalized.user_intent, redactSecrets) }
66
72
  : {}),
67
73
  ...(typeof normalized.agent_thinking === "string"
68
- ? {
69
- agent_thinking: redactSecrets
70
- ? redactSecretsInString(normalized.agent_thinking)
71
- : normalized.agent_thinking,
72
- }
74
+ ? { agent_thinking: sanitizeTelemetryText(normalized.agent_thinking, redactSecrets) }
73
75
  : {}),
74
76
  };
75
77
  if (!redact)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@armature-tech/mcp-analytics",
3
- "version": "0.6.22",
3
+ "version": "0.6.24",
4
4
  "type": "module",
5
5
  "description": "MCP analytics wrapper SDK that instruments MCP tool declarations with telemetry.",
6
6
  "license": "Apache-2.0",