@armature-tech/mcp-analytics 0.6.37 → 0.6.38

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.
@@ -2,12 +2,26 @@ import { AsyncLocalStorage } from "node:async_hooks";
2
2
  import { randomUUID } from "node:crypto";
3
3
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
4
4
  import { createAnalyticsRecorder } from "./recorder.js";
5
- import { planToolTelemetry } from "./schema.js";
5
+ import { appendTelemetryHint, planToolTelemetry, withoutRequestCapabilityHint, } from "./schema.js";
6
6
  import { defaultMcpAnalyticsConfig } from "./emit.js";
7
7
  import { deriveToolResultError, isRecord } from "./utils.js";
8
- import { isRequestCapabilityEnabled, isRequestCapabilityExplicit } from "./request-capability.js";
8
+ import { isRequestCapabilityEnabled, isRequestCapabilityExplicit, REQUEST_CAPABILITY_TOOL_NAME, } from "./request-capability.js";
9
9
  const withAnalyticsStorage = new AsyncLocalStorage();
10
10
  let prototypePatchInstalled = false;
11
+ // Servers a tool named request_capability was registered on: the SDK-owned
12
+ // one the recorder attaches, or a customer's own. Only the fallback for an
13
+ // SDK build without the tool registry below.
14
+ const serversListingRequestCapability = new WeakSet();
15
+ // Whether tools/list on `server` currently carries request_capability. Reads
16
+ // the SDK's registry (the same map and `enabled` filter tools/list uses), so
17
+ // a tool removed or disabled after registration no longer counts.
18
+ const listsRequestCapability = (server) => {
19
+ const registry = server._registeredTools;
20
+ if (registry !== null && typeof registry === "object") {
21
+ return registry[REQUEST_CAPABILITY_TOOL_NAME]?.enabled === true;
22
+ }
23
+ return serversListingRequestCapability.has(server);
24
+ };
11
25
  // Mirrors the SDK's `isZodTypeLike`/`isZodRawShapeCompat` checks closely enough
12
26
  // to disambiguate the overloads of `server.tool(...)`: a raw shape is a plain
13
27
  // object whose values include at least one Zod schema, while a Zod schema
@@ -114,11 +128,16 @@ const installPrototypePatchOnce = () => {
114
128
  prototype.registerTool = function patchedRegisterTool(name, toolConfig, cb) {
115
129
  const ctx = withAnalyticsStorage.getStore();
116
130
  if (!ctx) {
117
- return originalRegisterTool.call(this, name, toolConfig, cb);
131
+ const registered = originalRegisterTool.call(this, name, toolConfig, cb);
132
+ if (name === REQUEST_CAPABILITY_TOOL_NAME)
133
+ serversListingRequestCapability.add(this);
134
+ return registered;
118
135
  }
119
136
  const { config } = ctx;
120
137
  const originalHasInputSchema = toolConfig.inputSchema !== undefined;
121
- const plan = planToolTelemetry(name, toolConfig.inputSchema, config);
138
+ const plan = planToolTelemetry(name, toolConfig.inputSchema, config, ctx.resolved
139
+ ? { requestCapability: listsRequestCapability(this) }
140
+ : {});
122
141
  const registeredHasInputSchema = plan.inputSchema !== undefined;
123
142
  const description = plan.applyDescription(toolConfig.description);
124
143
  const instrumentedConfig = {
@@ -127,7 +146,21 @@ const installPrototypePatchOnce = () => {
127
146
  ...(registeredHasInputSchema ? { inputSchema: plan.inputSchema } : {}),
128
147
  };
129
148
  const wrappedCallback = wrapCallbackWithAnalytics(name, cb, originalHasInputSchema, registeredHasInputSchema, plan.mode, ctx);
130
- return originalRegisterTool.call(this, name, instrumentedConfig, wrappedCallback);
149
+ const registered = originalRegisterTool.call(this, name, instrumentedConfig, wrappedCallback);
150
+ if (name === REQUEST_CAPABILITY_TOOL_NAME)
151
+ serversListingRequestCapability.add(this);
152
+ if (!ctx.resolved && plan.mode === "injected" && isRequestCapabilityEnabled(config)) {
153
+ ctx.requestCapabilityHints.push({
154
+ server: this,
155
+ tool: registered,
156
+ toolName: name,
157
+ hintedDescription: description,
158
+ fallbackDescription: appendTelemetryHint(toolConfig.description, {
159
+ toolName: name,
160
+ }),
161
+ });
162
+ }
163
+ return registered;
131
164
  };
132
165
  // PRIA and other older codebases still use the deprecated `server.tool(...)`
133
166
  // overloads. Patch them too — we normalise into our patched `registerTool`
@@ -151,7 +184,13 @@ const installPrototypePatchOnce = () => {
151
184
  export const withMcpAnalytics = (config, createServer) => {
152
185
  const recorder = createAnalyticsRecorder(config);
153
186
  installPrototypePatchOnce();
154
- const result = withAnalyticsStorage.run({ config, recorder }, createServer);
187
+ const ctx = {
188
+ config,
189
+ recorder,
190
+ resolved: false,
191
+ requestCapabilityHints: [],
192
+ };
193
+ const result = withAnalyticsStorage.run(ctx, createServer);
155
194
  // Customer tools registered inside the factory are handled by the
156
195
  // prototype patch above. The opt-in SDK-owned tool lives in the recorder's
157
196
  // private registry, so attach only after leaving AsyncLocalStorage: this
@@ -180,6 +219,23 @@ export const withMcpAnalytics = (config, createServer) => {
180
219
  throw error;
181
220
  }
182
221
  }
222
+ // Tools registered on a server that does not list request_capability (the
223
+ // factory returned something else, or registered on another server) must
224
+ // not point agents at it.
225
+ ctx.resolved = true;
226
+ for (const hint of ctx.requestCapabilityHints) {
227
+ if (!listsRequestCapability(hint.server)
228
+ && hint.tool !== null
229
+ && typeof hint.tool === "object") {
230
+ const tool = hint.tool;
231
+ // Keep a description the factory updated after registration; only its
232
+ // hint changes.
233
+ tool.description = tool.description === hint.hintedDescription
234
+ ? hint.fallbackDescription
235
+ : withoutRequestCapabilityHint(tool.description, hint.toolName, hint.hintedDescription);
236
+ }
237
+ }
238
+ ctx.requestCapabilityHints.length = 0;
183
239
  return { result, recorder };
184
240
  };
185
241
  export const createMcpAnalyticsServer = (createServer, config = defaultMcpAnalyticsConfig) => {
@@ -57,6 +57,8 @@ export type TelemetryFieldMap = {
57
57
  /** @deprecated `user_turn` is no longer collected; this mapping is ignored. */
58
58
  user_turn?: string;
59
59
  user_intent?: string;
60
+ call_purpose?: string;
61
+ /** @deprecated Accepted for compatibility. Prefer call_purpose. */
60
62
  agent_thinking?: string;
61
63
  user_frustration?: string;
62
64
  };
@@ -97,6 +99,8 @@ export type TelemetryArgs = {
97
99
  /** @deprecated `user_turn` is accepted from cached clients but ignored. */
98
100
  user_turn?: number;
99
101
  user_intent?: string;
102
+ call_purpose?: string;
103
+ /** @deprecated Accepted for compatibility. Prefer call_purpose. */
100
104
  agent_thinking?: string;
101
105
  user_frustration?: "low" | "medium" | "high";
102
106
  /** @deprecated Pre-V1 spelling of `user_intent`; still accepted. */
package/dist/esm/v2.js CHANGED
@@ -413,7 +413,7 @@ const instrumentRegisteredTool = (state, server, name, tool, defaults, options)
413
413
  ? {
414
414
  paramsSchema: decoratedSchema,
415
415
  // Same idempotent description nudge as v1 (ARM-24).
416
- description: appendTelemetryHint(registered.description),
416
+ description: appendTelemetryHint(registered.description, { toolName: name }),
417
417
  }
418
418
  : {}),
419
419
  callback: wrapToolCallback(state, name, originalHasInputSchema, registeredHasInputSchema, handler, defaults, options),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@armature-tech/mcp-analytics",
3
- "version": "0.6.37",
3
+ "version": "0.6.38",
4
4
  "type": "module",
5
5
  "description": "MCP analytics wrapper SDK that instruments MCP tool declarations with telemetry.",
6
6
  "license": "Apache-2.0",