@armature-tech/mcp-analytics 0.6.37 → 0.6.39
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/README.md +28 -9
- package/SKILL.md +1 -1
- package/dist/cjs/doctor.js +8 -4
- package/dist/cjs/fastmcp.js +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/mastra.js +5 -3
- package/dist/cjs/redact-secrets.js +5 -0
- package/dist/cjs/request-capability.d.ts +2 -2
- package/dist/cjs/request-capability.js +2 -2
- package/dist/cjs/schema.d.ts +20 -13
- package/dist/cjs/schema.js +157 -41
- package/dist/cjs/server.js +60 -4
- package/dist/cjs/types.d.ts +4 -0
- package/dist/cjs/v2.js +1 -1
- package/dist/esm/doctor.js +8 -4
- package/dist/esm/fastmcp.js +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/mastra.js +5 -3
- package/dist/esm/redact-secrets.js +5 -0
- package/dist/esm/request-capability.d.ts +2 -2
- package/dist/esm/request-capability.js +2 -2
- package/dist/esm/schema.d.ts +20 -13
- package/dist/esm/schema.js +155 -40
- package/dist/esm/server.js +62 -6
- package/dist/esm/types.d.ts +4 -0
- package/dist/esm/v2.js +1 -1
- package/package.json +1 -1
package/dist/esm/schema.js
CHANGED
|
@@ -1,38 +1,146 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import * as zv4 from "zod/v4";
|
|
3
|
+
import { isRequestCapabilityEnabled } from "./request-capability.js";
|
|
3
4
|
import { isJsonObjectSchema, isRawShape, isRecord } from "./utils.js";
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
//
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
5
|
+
// Public task context is identical across all four SDKs. Legacy field names
|
|
6
|
+
// remain accepted on input and in storage, but are never advertised.
|
|
7
|
+
export const TELEMETRY_PROPERTY_DESCRIPTION = "Optional task context for usage analytics, based on the visible user request and the action performed by this tool.";
|
|
8
|
+
const TELEMETRY_DESCRIPTION_HINT = "\n\nInclude telemetry.call_purpose with a short description of this action. Include telemetry.user_intent and telemetry.user_frustration only on the first tool call after each new user message.";
|
|
9
|
+
const TELEMETRY_SENTENCE = TELEMETRY_DESCRIPTION_HINT.trimStart();
|
|
10
|
+
const REQUEST_CAPABILITY_SENTENCE = "Call request_capability before you tell the user something can't be done here or has to be done elsewhere.";
|
|
11
|
+
const TELEMETRY_DESCRIPTION_HINT_WITH_REQUEST_CAPABILITY = `${TELEMETRY_DESCRIPTION_HINT} ${REQUEST_CAPABILITY_SENTENCE}`;
|
|
12
|
+
const TELEMETRY_SENTENCE_HINT = TELEMETRY_DESCRIPTION_HINT;
|
|
13
|
+
// Only exact SDK suffixes are replaced. A mention within customer prose stays.
|
|
14
|
+
const RECOGNIZED_HINT_MARKERS = [
|
|
15
|
+
`${TELEMETRY_SENTENCE} ${REQUEST_CAPABILITY_SENTENCE}`,
|
|
16
|
+
`${TELEMETRY_SENTENCE} If no tool can do what the user asks, call request_capability.`,
|
|
17
|
+
TELEMETRY_SENTENCE,
|
|
18
|
+
"Pass telemetry.agent_thinking on every call, telemetry.user_intent on the first call after each user message. If no tool can do what the user asks, call request_capability.",
|
|
19
|
+
"Pass telemetry.agent_thinking on every call, telemetry.user_intent on the first call after each user message.",
|
|
20
|
+
"On every call, pass telemetry.agent_thinking with your reasoning for this specific call. Pass telemetry.user_intent only on the first tool call after a new user message.",
|
|
21
|
+
"Pass telemetry.user_intent with a one-line restatement of the user's most recent request, and telemetry.agent_thinking with your reasoning for making this specific call.",
|
|
22
|
+
"Pass telemetry.user_intent with a one-line restatement of the user's most recent request.",
|
|
23
|
+
"Pass telemetry.intent with a one-line user intent for analytics.",
|
|
24
|
+
];
|
|
25
|
+
// Some providers reject the whole request when any tool description exceeds
|
|
26
|
+
// 1024 characters (Azure OpenAI, some OpenAI-compatible gateways), and Claude
|
|
27
|
+
// Code cuts descriptions at 2048, which would drop the hint first. Measured in
|
|
28
|
+
// UTF-8 bytes, which never undercounts characters and matches the Python, Go,
|
|
29
|
+
// and PHP SDKs exactly.
|
|
30
|
+
export const MAX_TOOL_DESCRIPTION_LENGTH = 1024;
|
|
31
|
+
const utf8 = new TextEncoder();
|
|
32
|
+
const fits = (text) => utf8.encode(text).length <= MAX_TOOL_DESCRIPTION_LENGTH;
|
|
33
|
+
const warnedLongDescriptions = new Set();
|
|
34
|
+
const warnDescriptionTooLong = (toolName, message) => {
|
|
35
|
+
if (warnedLongDescriptions.has(toolName))
|
|
36
|
+
return;
|
|
37
|
+
warnedLongDescriptions.add(toolName);
|
|
38
|
+
// eslint-disable-next-line no-console
|
|
39
|
+
console.warn(`[mcp-analytics] Tool "${toolName}" description is too long ${message}`);
|
|
40
|
+
};
|
|
41
|
+
// Appends the current telemetry hint and upgrades exact SDK hint suffixes.
|
|
42
|
+
// Every integration shape must run tool descriptions through this
|
|
43
|
+
// so calling agents know to pass telemetry (ARM-24). With `requestCapability`,
|
|
44
|
+
// the hint also points agents to request_capability.
|
|
45
|
+
//
|
|
46
|
+
// The result never exceeds MAX_TOOL_DESCRIPTION_LENGTH because of us: when the
|
|
47
|
+
// full hint does not fit, only the telemetry sentence is appended (it matters
|
|
48
|
+
// per tool; request_capability is a list-wide instruction other tools carry);
|
|
49
|
+
// when that does not fit either, the description is left unchanged. Sentences
|
|
50
|
+
// are never cut, and the telemetry field is still advertised either way.
|
|
51
|
+
export const appendTelemetryHint = (description, options = {}) => {
|
|
52
|
+
const requestCapability = options.requestCapability === true;
|
|
22
53
|
if (description === undefined) {
|
|
23
|
-
return
|
|
54
|
+
return (requestCapability
|
|
55
|
+
? TELEMETRY_DESCRIPTION_HINT_WITH_REQUEST_CAPABILITY
|
|
56
|
+
: TELEMETRY_DESCRIPTION_HINT).trimStart();
|
|
57
|
+
}
|
|
58
|
+
// Upgrade cached SDK suffixes before checking the size budget. Remove a
|
|
59
|
+
// full hint before its shorter forms, including stacked older wrappers.
|
|
60
|
+
let base = description;
|
|
61
|
+
for (;;) {
|
|
62
|
+
const trimmed = base.trimEnd();
|
|
63
|
+
const marker = RECOGNIZED_HINT_MARKERS.find((candidate) => trimmed === candidate || trimmed.endsWith(`\n\n${candidate}`));
|
|
64
|
+
if (marker === undefined)
|
|
65
|
+
break;
|
|
66
|
+
base = trimmed === marker ? "" : trimmed.slice(0, -marker.length - 2);
|
|
24
67
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
68
|
+
description = base;
|
|
69
|
+
if (description.length === 0) {
|
|
70
|
+
return (requestCapability
|
|
71
|
+
? TELEMETRY_DESCRIPTION_HINT_WITH_REQUEST_CAPABILITY
|
|
72
|
+
: TELEMETRY_DESCRIPTION_HINT).trimStart();
|
|
73
|
+
}
|
|
74
|
+
// A customer who already wrote the request_capability sentence gets only the
|
|
75
|
+
// telemetry one, so the instruction is not repeated.
|
|
76
|
+
const hint = !requestCapability
|
|
77
|
+
? TELEMETRY_DESCRIPTION_HINT
|
|
78
|
+
: description.includes(REQUEST_CAPABILITY_SENTENCE)
|
|
79
|
+
? TELEMETRY_SENTENCE_HINT
|
|
80
|
+
: TELEMETRY_DESCRIPTION_HINT_WITH_REQUEST_CAPABILITY;
|
|
81
|
+
if (fits(`${description}${hint}`)) {
|
|
82
|
+
return `${description}${hint}`;
|
|
83
|
+
}
|
|
84
|
+
if (fits(`${description}${TELEMETRY_SENTENCE_HINT}`)) {
|
|
85
|
+
if (options.toolName !== undefined) {
|
|
86
|
+
warnDescriptionTooLong(options.toolName, `for the full Armature telemetry hint within ${MAX_TOOL_DESCRIPTION_LENGTH} characters; appended only the telemetry sentence.`);
|
|
87
|
+
}
|
|
88
|
+
return `${description}${TELEMETRY_SENTENCE_HINT}`;
|
|
89
|
+
}
|
|
90
|
+
if (options.toolName !== undefined) {
|
|
91
|
+
warnDescriptionTooLong(options.toolName, `to append the Armature telemetry hint without exceeding ${MAX_TOOL_DESCRIPTION_LENGTH} characters; leaving it unchanged. Telemetry is still collected.`);
|
|
92
|
+
}
|
|
93
|
+
return description;
|
|
94
|
+
};
|
|
95
|
+
// The factory can append customer text after the SDK's registered hint. Update
|
|
96
|
+
// that exact block in place when its server does not expose request_capability.
|
|
97
|
+
export const withoutRequestCapabilityHint = (description, toolName, registeredDescription) => {
|
|
98
|
+
if (description !== undefined) {
|
|
99
|
+
// The last paragraph is the SDK suffix, even when an earlier customer
|
|
100
|
+
// paragraph quotes the same instruction. If the factory kept the registered
|
|
101
|
+
// description, anchor to its suffix so later customer quotes also survive.
|
|
102
|
+
const registeredIndex = registeredDescription === undefined
|
|
103
|
+
? -1
|
|
104
|
+
: description.indexOf(registeredDescription);
|
|
105
|
+
const registeredMarker = registeredDescription === undefined
|
|
106
|
+
? undefined
|
|
107
|
+
: RECOGNIZED_HINT_MARKERS.find((marker) => registeredDescription === marker || registeredDescription.endsWith(`\n\n${marker}`));
|
|
108
|
+
let match;
|
|
109
|
+
if (registeredIndex >= 0 && registeredDescription !== undefined) {
|
|
110
|
+
if (registeredMarker !== undefined) {
|
|
111
|
+
match = {
|
|
112
|
+
marker: registeredMarker,
|
|
113
|
+
index: registeredIndex + registeredDescription.length - registeredMarker.length,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
for (const marker of RECOGNIZED_HINT_MARKERS) {
|
|
119
|
+
const paragraph = description.lastIndexOf(`\n\n${marker}`);
|
|
120
|
+
const index = paragraph >= 0 ? paragraph + 2 : description.startsWith(marker) ? 0 : -1;
|
|
121
|
+
// Longer markers precede their shorter prefixes in the list.
|
|
122
|
+
if (index >= 0 && (match === undefined || index > match.index))
|
|
123
|
+
match = { marker, index };
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (match !== undefined) {
|
|
127
|
+
const { marker, index } = match;
|
|
128
|
+
const updated = description.slice(0, index) + TELEMETRY_SENTENCE + description.slice(index + marker.length);
|
|
129
|
+
if (fits(updated))
|
|
130
|
+
return updated;
|
|
131
|
+
if (toolName !== undefined) {
|
|
132
|
+
warnDescriptionTooLong(toolName, `to keep the Armature telemetry hint within ${MAX_TOOL_DESCRIPTION_LENGTH} characters; removed it. Telemetry is still collected.`);
|
|
133
|
+
}
|
|
134
|
+
return index === 0
|
|
135
|
+
? description.slice(marker.length).trimStart()
|
|
136
|
+
: description.slice(0, index - 2) + description.slice(index + marker.length);
|
|
137
|
+
}
|
|
30
138
|
}
|
|
31
|
-
return
|
|
139
|
+
return appendTelemetryHint(description, toolName === undefined ? {} : { toolName });
|
|
32
140
|
};
|
|
33
|
-
export const USER_INTENT_DESCRIPTION = "
|
|
34
|
-
export const
|
|
35
|
-
export const USER_FRUSTRATION_DESCRIPTION = "Frustration
|
|
141
|
+
export const USER_INTENT_DESCRIPTION = "Generalized one-sentence summary of the task stated in the user's latest message. Describe actions and generic roles only. Replace all tool argument values with generic terms, including names, contacts, IDs, credentials, document titles, team names and filters. For example, 'List employees in the selected team.' Include only on the first tool call after each new user message; omit on later calls in the same turn. Use English.";
|
|
142
|
+
export const CALL_PURPOSE_DESCRIPTION = "Short public description of the action this tool performs toward the user's stated goal. Base it only on the visible request, the tool's function and its inputs. Use English. Omit names, contact details, identifiers, credentials and argument values. Generalize document titles, team names and filter values (for example, 'the selected team').";
|
|
143
|
+
export const USER_FRUSTRATION_DESCRIPTION = "Frustration expressed in the user's latest message: low when none is expressed, medium for explicit dissatisfaction, high for strong or repeated dissatisfaction. Use only the user's words. Include on the first tool call after each new user message; omit on later calls in the same turn.";
|
|
36
144
|
// Each telemetry object schema carries the object-level description via
|
|
37
145
|
// `.describe(...)` so it survives zod→JSON-schema conversion in every
|
|
38
146
|
// integration shape — including caller-owned McpServer registration, where no
|
|
@@ -46,7 +154,7 @@ export const USER_FRUSTRATION_DESCRIPTION = "Frustration evident in the user's m
|
|
|
46
154
|
const looseTelemetryInputSchema = z
|
|
47
155
|
.object({
|
|
48
156
|
user_intent: z.string().describe(USER_INTENT_DESCRIPTION).optional(),
|
|
49
|
-
|
|
157
|
+
call_purpose: z.string().describe(CALL_PURPOSE_DESCRIPTION).optional(),
|
|
50
158
|
user_frustration: z
|
|
51
159
|
.string()
|
|
52
160
|
.describe(USER_FRUSTRATION_DESCRIPTION)
|
|
@@ -57,9 +165,9 @@ const looseTelemetryInputSchema = z
|
|
|
57
165
|
const looseTelemetryInputSchemaV4 = zv4
|
|
58
166
|
.looseObject({
|
|
59
167
|
user_intent: zv4.string().describe(USER_INTENT_DESCRIPTION).optional(),
|
|
60
|
-
|
|
168
|
+
call_purpose: zv4
|
|
61
169
|
.string()
|
|
62
|
-
.describe(
|
|
170
|
+
.describe(CALL_PURPOSE_DESCRIPTION)
|
|
63
171
|
.optional(),
|
|
64
172
|
user_frustration: zv4
|
|
65
173
|
.string()
|
|
@@ -131,7 +239,10 @@ const warnTelemetryCollision = (toolName) => {
|
|
|
131
239
|
// prototype patch, Mastra adapter, custom dispatchers) must register and
|
|
132
240
|
// extract with the same plan, so the advertised schema always matches runtime
|
|
133
241
|
// behavior.
|
|
134
|
-
|
|
242
|
+
// `requestCapability` overrides the config-derived answer to "does this
|
|
243
|
+
// server list request_capability?" for adapters whose recorder, not the
|
|
244
|
+
// wrap-time config, is the source of truth (Mastra).
|
|
245
|
+
export const planToolTelemetry = (toolName, inputSchema, config = {}, options = {}) => {
|
|
135
246
|
if (schemaDeclaresTelemetry(inputSchema)) {
|
|
136
247
|
warnTelemetryCollision(toolName);
|
|
137
248
|
return {
|
|
@@ -150,7 +261,10 @@ export const planToolTelemetry = (toolName, inputSchema, config = {}) => {
|
|
|
150
261
|
return {
|
|
151
262
|
mode: "injected",
|
|
152
263
|
inputSchema: decorateInputSchemaWithTelemetry(inputSchema, config),
|
|
153
|
-
applyDescription: appendTelemetryHint,
|
|
264
|
+
applyDescription: (description) => appendTelemetryHint(description, {
|
|
265
|
+
requestCapability: options.requestCapability ?? isRequestCapabilityEnabled(config),
|
|
266
|
+
toolName,
|
|
267
|
+
}),
|
|
154
268
|
};
|
|
155
269
|
};
|
|
156
270
|
// The telemetry object and every field stay optional. In particular,
|
|
@@ -170,9 +284,9 @@ export const createTelemetryJsonSchema = (_config = {}) => {
|
|
|
170
284
|
type: "string",
|
|
171
285
|
description: USER_INTENT_DESCRIPTION,
|
|
172
286
|
},
|
|
173
|
-
|
|
287
|
+
call_purpose: {
|
|
174
288
|
type: "string",
|
|
175
|
-
description:
|
|
289
|
+
description: CALL_PURPOSE_DESCRIPTION,
|
|
176
290
|
},
|
|
177
291
|
user_frustration: {
|
|
178
292
|
type: "string",
|
|
@@ -234,7 +348,8 @@ const firstString = (...values) => {
|
|
|
234
348
|
}
|
|
235
349
|
return undefined;
|
|
236
350
|
};
|
|
237
|
-
//
|
|
351
|
+
// Maps public call_purpose onto the existing agent_thinking storage field.
|
|
352
|
+
// Canonicalizes telemetry onto the storage field names. Legacy spellings
|
|
238
353
|
// (`intent`/`context`/`frustration_level`) still arrive from clients that
|
|
239
354
|
// cached a pre-V1 tool schema and from callers passing telemetry directly to
|
|
240
355
|
// recordToolCall; they lose to an explicit current value when both are present.
|
|
@@ -248,7 +363,7 @@ export const normalizeTelemetryArgs = (telemetry) => {
|
|
|
248
363
|
const userIntent = firstString(telemetry.user_intent, telemetry.intent);
|
|
249
364
|
if (userIntent !== undefined)
|
|
250
365
|
normalized.user_intent = userIntent;
|
|
251
|
-
const agentThinking = firstString(telemetry.agent_thinking, telemetry.context);
|
|
366
|
+
const agentThinking = firstString(telemetry.call_purpose, telemetry.agent_thinking, telemetry.context);
|
|
252
367
|
if (agentThinking !== undefined)
|
|
253
368
|
normalized.agent_thinking = agentThinking;
|
|
254
369
|
const userFrustration = asFrustration(telemetry.user_frustration)
|
|
@@ -277,10 +392,10 @@ export const applyTelemetryFieldMap = (telemetry, args, fieldMap) => {
|
|
|
277
392
|
if (value !== undefined)
|
|
278
393
|
merged.user_intent = value;
|
|
279
394
|
}
|
|
280
|
-
if (merged.agent_thinking === undefined && merged.context === undefined) {
|
|
281
|
-
const value = argString(fieldMap.agent_thinking);
|
|
395
|
+
if (merged.call_purpose === undefined && merged.agent_thinking === undefined && merged.context === undefined) {
|
|
396
|
+
const value = argString(fieldMap.call_purpose) ?? argString(fieldMap.agent_thinking);
|
|
282
397
|
if (value !== undefined)
|
|
283
|
-
merged.
|
|
398
|
+
merged.call_purpose = value;
|
|
284
399
|
}
|
|
285
400
|
if (merged.user_frustration === undefined
|
|
286
401
|
&& merged.frustration_level === undefined
|
package/dist/esm/server.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
|
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) => {
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -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),
|