@armature-tech/mcp-analytics 0.6.6 → 0.6.7

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.
@@ -128,7 +128,7 @@ const wrapOneTool = (toolKey, tool, recorder, config, resolveExtra) => {
128
128
  };
129
129
  };
130
130
  // Generic over the input map so the return value keeps the customer's exact
131
- // tool-map type (e.g. `Record<string, AutumnTool>`). Without this, every Mastra
131
+ // tool-map type (e.g. `Record<string, HostTool>`). Without this, every Mastra
132
132
  // caller had to do `wrapMastraTools(myTools as unknown as MastraToolMap, ...) as
133
133
  // unknown as typeof myTools` because `MCPServer({ tools })` wants the narrower
134
134
  // type back. The `T extends MastraToolMap` constraint lets us still operate on
@@ -169,14 +169,19 @@ const createAnalyticsRecorder = (config = emit_js_1.defaultMcpAnalyticsConfig) =
169
169
  const startedAt = new Date(startedAtMs).toISOString();
170
170
  try {
171
171
  const result = await handler(args);
172
+ // A handler that returns an MCP error result (`isError: true`) instead of
173
+ // throwing is still a failed call; record it as such while returning the
174
+ // original result to the caller untouched.
175
+ const resultError = (0, utils_js_1.deriveToolResultError)(result);
172
176
  await recordToolCall({
173
177
  ...event,
174
178
  args,
175
179
  telemetry,
176
180
  startedAt,
177
181
  durationMs: Date.now() - startedAtMs,
178
- status: "ok",
179
- result,
182
+ ...(resultError === undefined
183
+ ? { status: "ok", result }
184
+ : { status: "error", result, error: resultError }),
180
185
  });
181
186
  return result;
182
187
  }
@@ -67,6 +67,10 @@ const wrapCallbackWithAnalytics = (name, cb, originalHasInputSchema, ctx) => {
67
67
  const output = originalHasInputSchema
68
68
  ? await cb(args, maybeExtra)
69
69
  : await cb(maybeExtra ?? argsOrExtra);
70
+ // A returned MCP error result (`isError: true`) is a failed call even
71
+ // though the callback resolved; record it as an error while returning the
72
+ // original output to the caller untouched.
73
+ const resultError = (0, utils_js_1.deriveToolResultError)(output);
70
74
  await recorder.recordToolCall({
71
75
  name,
72
76
  args,
@@ -75,8 +79,9 @@ const wrapCallbackWithAnalytics = (name, cb, originalHasInputSchema, ctx) => {
75
79
  requestId,
76
80
  startedAt,
77
81
  durationMs: Date.now() - startedAtMs,
78
- status: "ok",
79
- result: output,
82
+ ...(resultError === undefined
83
+ ? { status: "ok", result: output }
84
+ : { status: "error", result: output, error: resultError }),
80
85
  });
81
86
  return output;
82
87
  }
@@ -13,6 +13,7 @@ export type BoundedKeySet = {
13
13
  add: (key: string) => void;
14
14
  };
15
15
  export declare const createBoundedKeySet: (maxEntries: number) => BoundedKeySet;
16
+ export declare const deriveToolResultError: (result: unknown) => string | undefined;
16
17
  export declare const stringifyPreview: (value: unknown) => string;
17
18
  export declare const truncateUtf8: (value: string, maxBytes: number) => {
18
19
  value: string;
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.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;
3
+ exports.headerValue = exports.workflowRunIdFromHeaders = exports.WORKFLOW_RUN_ID_HEADER = exports.truncateUtf8 = exports.stringifyPreview = exports.deriveToolResultError = 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;
@@ -54,6 +54,31 @@ const createBoundedKeySet = (maxEntries) => {
54
54
  };
55
55
  };
56
56
  exports.createBoundedKeySet = createBoundedKeySet;
57
+ // Per MCP convention, a server signals a recoverable/upstream failure by
58
+ // returning a normal CallToolResult with `isError: true` (so the agent can see
59
+ // and retry it) rather than throwing. The handler therefore resolves rather
60
+ // than rejects, so without this check those failures would be recorded as
61
+ // successes. Returns a human-readable error message when `result` is such an
62
+ // error result, or undefined for a successful (or non-result-shaped) value.
63
+ // Defensive about shape: a non-object, missing `content`, or absent text all
64
+ // fall back gracefully and never throw.
65
+ const deriveToolResultError = (result) => {
66
+ if (!(0, exports.isRecord)(result) || result.isError !== true)
67
+ return undefined;
68
+ const content = result.content;
69
+ if (Array.isArray(content)) {
70
+ for (const item of content) {
71
+ if ((0, exports.isRecord)(item) &&
72
+ item.type === "text" &&
73
+ typeof item.text === "string" &&
74
+ item.text.trim().length > 0) {
75
+ return item.text;
76
+ }
77
+ }
78
+ }
79
+ return "tool returned isError";
80
+ };
81
+ exports.deriveToolResultError = deriveToolResultError;
57
82
  const stringifyPreview = (value) => {
58
83
  if (value === undefined)
59
84
  return "undefined";
@@ -124,7 +124,7 @@ const wrapOneTool = (toolKey, tool, recorder, config, resolveExtra) => {
124
124
  };
125
125
  };
126
126
  // Generic over the input map so the return value keeps the customer's exact
127
- // tool-map type (e.g. `Record<string, AutumnTool>`). Without this, every Mastra
127
+ // tool-map type (e.g. `Record<string, HostTool>`). Without this, every Mastra
128
128
  // caller had to do `wrapMastraTools(myTools as unknown as MastraToolMap, ...) as
129
129
  // unknown as typeof myTools` because `MCPServer({ tools })` wants the narrower
130
130
  // type back. The `T extends MastraToolMap` constraint lets us still operate on
@@ -2,7 +2,7 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
2
  import { buildActorId, buildBatch, buildSessionInitBatch, buildToolCallEvent, normalizeRequestId, normalizeSessionId, normalizeStartedAt, } from "./events.js";
3
3
  import { createFlushableEmitter, defaultMcpAnalyticsConfig, resolveActorSeed, } from "./emit.js";
4
4
  import { appendTelemetryHint, decorateInputSchemaWithTelemetry, extractTelemetryArguments, INTENT_DESCRIPTION, TELEMETRY_PROPERTY_DESCRIPTION, } from "./schema.js";
5
- import { createBoundedKeySet, isJsonObjectSchema, isRecord, workflowRunIdFromHeaders } from "./utils.js";
5
+ import { createBoundedKeySet, deriveToolResultError, isJsonObjectSchema, isRecord, workflowRunIdFromHeaders } from "./utils.js";
6
6
  import { getClientInfoForSessionId, installClientInfoCapture, } from "./client-info-cache.js";
7
7
  import { parseStatelessSessionClientInfo } from "./stateless-http.js";
8
8
  const nudgeTelemetryDescriptions = (schema) => {
@@ -166,14 +166,19 @@ export const createAnalyticsRecorder = (config = defaultMcpAnalyticsConfig) => {
166
166
  const startedAt = new Date(startedAtMs).toISOString();
167
167
  try {
168
168
  const result = await handler(args);
169
+ // A handler that returns an MCP error result (`isError: true`) instead of
170
+ // throwing is still a failed call; record it as such while returning the
171
+ // original result to the caller untouched.
172
+ const resultError = deriveToolResultError(result);
169
173
  await recordToolCall({
170
174
  ...event,
171
175
  args,
172
176
  telemetry,
173
177
  startedAt,
174
178
  durationMs: Date.now() - startedAtMs,
175
- status: "ok",
176
- result,
179
+ ...(resultError === undefined
180
+ ? { status: "ok", result }
181
+ : { status: "error", result, error: resultError }),
177
182
  });
178
183
  return result;
179
184
  }
@@ -4,7 +4,7 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
4
4
  import { createAnalyticsRecorder } from "./recorder.js";
5
5
  import { appendTelemetryHint, decorateInputSchemaWithTelemetry, } from "./schema.js";
6
6
  import { defaultMcpAnalyticsConfig } from "./emit.js";
7
- import { isRecord } from "./utils.js";
7
+ import { deriveToolResultError, isRecord } from "./utils.js";
8
8
  const withAnalyticsStorage = new AsyncLocalStorage();
9
9
  let prototypePatchInstalled = false;
10
10
  // Mirrors the SDK's `isZodTypeLike`/`isZodRawShapeCompat` checks closely enough
@@ -64,6 +64,10 @@ const wrapCallbackWithAnalytics = (name, cb, originalHasInputSchema, ctx) => {
64
64
  const output = originalHasInputSchema
65
65
  ? await cb(args, maybeExtra)
66
66
  : await cb(maybeExtra ?? argsOrExtra);
67
+ // A returned MCP error result (`isError: true`) is a failed call even
68
+ // though the callback resolved; record it as an error while returning the
69
+ // original output to the caller untouched.
70
+ const resultError = deriveToolResultError(output);
67
71
  await recorder.recordToolCall({
68
72
  name,
69
73
  args,
@@ -72,8 +76,9 @@ const wrapCallbackWithAnalytics = (name, cb, originalHasInputSchema, ctx) => {
72
76
  requestId,
73
77
  startedAt,
74
78
  durationMs: Date.now() - startedAtMs,
75
- status: "ok",
76
- result: output,
79
+ ...(resultError === undefined
80
+ ? { status: "ok", result: output }
81
+ : { status: "error", result: output, error: resultError }),
77
82
  });
78
83
  return output;
79
84
  }
@@ -13,6 +13,7 @@ export type BoundedKeySet = {
13
13
  add: (key: string) => void;
14
14
  };
15
15
  export declare const createBoundedKeySet: (maxEntries: number) => BoundedKeySet;
16
+ export declare const deriveToolResultError: (result: unknown) => string | undefined;
16
17
  export declare const stringifyPreview: (value: unknown) => string;
17
18
  export declare const truncateUtf8: (value: string, maxBytes: number) => {
18
19
  value: string;
package/dist/esm/utils.js CHANGED
@@ -45,6 +45,30 @@ export const createBoundedKeySet = (maxEntries) => {
45
45
  },
46
46
  };
47
47
  };
48
+ // Per MCP convention, a server signals a recoverable/upstream failure by
49
+ // returning a normal CallToolResult with `isError: true` (so the agent can see
50
+ // and retry it) rather than throwing. The handler therefore resolves rather
51
+ // than rejects, so without this check those failures would be recorded as
52
+ // successes. Returns a human-readable error message when `result` is such an
53
+ // error result, or undefined for a successful (or non-result-shaped) value.
54
+ // Defensive about shape: a non-object, missing `content`, or absent text all
55
+ // fall back gracefully and never throw.
56
+ export const deriveToolResultError = (result) => {
57
+ if (!isRecord(result) || result.isError !== true)
58
+ return undefined;
59
+ const content = result.content;
60
+ if (Array.isArray(content)) {
61
+ for (const item of content) {
62
+ if (isRecord(item) &&
63
+ item.type === "text" &&
64
+ typeof item.text === "string" &&
65
+ item.text.trim().length > 0) {
66
+ return item.text;
67
+ }
68
+ }
69
+ }
70
+ return "tool returned isError";
71
+ };
48
72
  export const stringifyPreview = (value) => {
49
73
  if (value === undefined)
50
74
  return "undefined";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@armature-tech/mcp-analytics",
3
- "version": "0.6.6",
3
+ "version": "0.6.7",
4
4
  "type": "module",
5
5
  "description": "MCP analytics wrapper SDK that instruments MCP tool declarations with telemetry.",
6
6
  "license": "Apache-2.0",
@@ -56,8 +56,8 @@
56
56
  ],
57
57
  "scripts": {
58
58
  "dev:armature": "tsx experimental/mock-env/mock-armature-server.ts",
59
- "dev:instrumented-server": "tsx experimental/mock-env/instrumented-mock-autumn-mcp-server.ts",
60
- "dev:server": "tsx experimental/mock-env/mock-autumn-mcp-server.ts",
59
+ "dev:instrumented-server": "tsx experimental/mock-env/instrumented-mock-example-mcp-server.ts",
60
+ "dev:server": "tsx experimental/mock-env/mock-example-mcp-server.ts",
61
61
  "demo": "tsx experimental/mock-env/demo-client.ts",
62
62
  "demo:instrumented": "tsx experimental/mock-env/instrumented-demo-client.ts",
63
63
  "test": "tsx --test tests/*.test.ts",
@@ -82,7 +82,7 @@
82
82
  "@modelcontextprotocol/sdk": "^1.20.0"
83
83
  },
84
84
  "devDependencies": {
85
- "@modelcontextprotocol/sdk": "^1.20.0",
85
+ "@modelcontextprotocol/sdk": "1.20.0",
86
86
  "@types/node": "^22.10.2",
87
87
  "tsx": "^4.19.2",
88
88
  "typescript": "^5.7.2"