@armature-tech/mcp-analytics 0.6.41 → 0.6.43
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 +39 -42
- package/SKILL.md +6 -5
- package/dist/cjs/doctor.js +10 -6
- package/dist/cjs/events.js +3 -2
- package/dist/cjs/fastmcp.js +2 -5
- package/dist/cjs/mastra.js +6 -10
- package/dist/cjs/recorder.js +3 -5
- package/dist/cjs/request-capability.d.ts +5 -2
- package/dist/cjs/request-capability.js +27 -12
- package/dist/cjs/schema.d.ts +13 -12
- package/dist/cjs/schema.js +41 -172
- package/dist/cjs/server.js +8 -62
- package/dist/cjs/types.d.ts +7 -3
- package/dist/cjs/v2.js +2 -5
- package/dist/esm/doctor.js +11 -7
- package/dist/esm/events.js +3 -2
- package/dist/esm/fastmcp.js +3 -6
- package/dist/esm/mastra.js +7 -11
- package/dist/esm/recorder.js +3 -5
- package/dist/esm/request-capability.d.ts +5 -2
- package/dist/esm/request-capability.js +26 -11
- package/dist/esm/schema.d.ts +13 -12
- package/dist/esm/schema.js +39 -170
- package/dist/esm/server.js +9 -63
- package/dist/esm/types.d.ts +7 -3
- package/dist/esm/v2.js +3 -6
- package/package.json +1 -1
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
|
|
3
3
|
import type { JsonObjectSchema, McpAnalyticsConfig } from "./types.js";
|
|
4
|
-
export declare const
|
|
5
|
-
export declare const
|
|
4
|
+
export declare const SEND_FEEDBACK_TOOL_NAME = "send_feedback";
|
|
5
|
+
export declare const LEGACY_REQUEST_CAPABILITY_TOOL_NAME = "request_capability";
|
|
6
|
+
export declare const REQUEST_CAPABILITY_TOOL_NAME = "send_feedback";
|
|
7
|
+
export declare const REQUEST_CAPABILITY_DESCRIPTION = "Call this before you tell the user that these tools can't do what they asked. It records the request so the developers of this server can add it. It changes no data and contacts no one. Then answer the user as usual.";
|
|
8
|
+
export declare const PAST_REQUEST_CAPABILITY_DESCRIPTIONS: readonly string[];
|
|
6
9
|
export declare const REQUEST_CAPABILITY_ARGUMENT_DESCRIPTION = "One English sentence describing the missing capability needed for the user's task. Translate the summary into English even when the user writes in another language. Describe generic actions and roles. Omit names, contacts, IDs, credentials and all tool argument values.";
|
|
7
10
|
export declare const REQUEST_CAPABILITY_INPUT_SCHEMA: JsonObjectSchema;
|
|
8
11
|
export declare const REQUEST_CAPABILITY_ANNOTATIONS: {
|
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { resolveApiKey } from "./emit.js";
|
|
3
3
|
import { isRecord } from "./utils.js";
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
// The SDK-owned feedback tool. Named send_feedback, like PostHog's; releases
|
|
5
|
+
// before the connector-directory change named it request_capability, a name
|
|
6
|
+
// ingest and the doctor still recognize. The REQUEST_CAPABILITY_* identifiers
|
|
7
|
+
// keep their names so existing imports compile.
|
|
8
|
+
export const SEND_FEEDBACK_TOOL_NAME = "send_feedback";
|
|
9
|
+
export const LEGACY_REQUEST_CAPABILITY_TOOL_NAME = "request_capability";
|
|
10
|
+
export const REQUEST_CAPABILITY_TOOL_NAME = SEND_FEEDBACK_TOOL_NAME;
|
|
11
|
+
export const REQUEST_CAPABILITY_DESCRIPTION = "Call this before you tell the user that these tools can't do what they asked. It records the request so the developers of this server can add it. It changes no data and contacts no one. Then answer the user as usual.";
|
|
12
|
+
// Descriptions earlier releases gave the feedback tool, newest first. The
|
|
13
|
+
// doctor inspects servers on any release, so it recognizes all of them.
|
|
14
|
+
export const PAST_REQUEST_CAPABILITY_DESCRIPTIONS = [
|
|
15
|
+
"Records that the user asked for something these tools cannot do, so the developers of this server can add it. It changes no data and contacts no one. Call it whenever you cannot do what the user asked with these tools, including when you send them to an app, a website or a manual step instead. Then answer them as usual.",
|
|
16
|
+
"Request a capability that is not provided by the currently available tools. Use this when a capability is required to complete the user’s request and no existing tool can perform it.",
|
|
17
|
+
];
|
|
6
18
|
export const REQUEST_CAPABILITY_ARGUMENT_DESCRIPTION = "One English sentence describing the missing capability needed for the user's task. Translate the summary into English even when the user writes in another language. Describe generic actions and roles. Omit names, contacts, IDs, credentials and all tool argument values.";
|
|
7
19
|
export const REQUEST_CAPABILITY_INPUT_SCHEMA = {
|
|
8
20
|
type: "object",
|
|
@@ -22,7 +34,7 @@ export const REQUEST_CAPABILITY_INPUT_SCHEMA = {
|
|
|
22
34
|
// read-only), changes no user data and reaches no one outside the server.
|
|
23
35
|
// Matches the hosted Armature MCP (lib/mcp/index.js).
|
|
24
36
|
export const REQUEST_CAPABILITY_ANNOTATIONS = {
|
|
25
|
-
title: "
|
|
37
|
+
title: "Send feedback",
|
|
26
38
|
readOnlyHint: false,
|
|
27
39
|
destructiveHint: false,
|
|
28
40
|
idempotentHint: false,
|
|
@@ -37,16 +49,19 @@ export const REQUEST_CAPABILITY_ZOD_SHAPE = {
|
|
|
37
49
|
.max(1000)
|
|
38
50
|
.describe(REQUEST_CAPABILITY_ARGUMENT_DESCRIPTION),
|
|
39
51
|
};
|
|
40
|
-
|
|
52
|
+
// armature.sendFeedback, or the earlier armature.requestCapability; the new
|
|
53
|
+
// key wins when both are set. Undefined means the default.
|
|
54
|
+
const sendFeedbackSetting = (config) => config.armature?.sendFeedback ?? config.armature?.requestCapability;
|
|
55
|
+
// On by default when a delivery path is configured; `sendFeedback: false`
|
|
56
|
+
// disables it. No other tool description mentions it.
|
|
57
|
+
export const isRequestCapabilityEnabled = (config) => sendFeedbackSetting(config) !== false
|
|
41
58
|
&& config.armature?.enabled !== false
|
|
42
59
|
&& (typeof config.armature?.emit === "function" || Boolean(resolveApiKey(config)));
|
|
43
|
-
// True only when the caller explicitly opted in
|
|
44
|
-
//
|
|
45
|
-
//
|
|
46
|
-
//
|
|
47
|
-
|
|
48
|
-
// throwing and breaking an existing integration on upgrade.
|
|
49
|
-
export const isRequestCapabilityExplicit = (config) => config.armature?.requestCapability === true;
|
|
60
|
+
// True only when the caller explicitly opted in. The reserved-name and
|
|
61
|
+
// server-shape guards key off this stricter check, so a server that is on
|
|
62
|
+
// merely by default skips injection quietly on a collision or an
|
|
63
|
+
// incompatible factory result instead of breaking on upgrade.
|
|
64
|
+
export const isRequestCapabilityExplicit = (config) => sendFeedbackSetting(config) === true;
|
|
50
65
|
export const handleRequestCapability = (args) => {
|
|
51
66
|
if (!isRecord(args)
|
|
52
67
|
|| typeof args.capability !== "string"
|
package/dist/esm/schema.d.ts
CHANGED
|
@@ -2,16 +2,24 @@ import { z } from "zod";
|
|
|
2
2
|
import * as zv4 from "zod/v4";
|
|
3
3
|
import type { ExtractedToolArguments, InternalMcpAnalyticsConfig, JsonObjectSchema, TelemetryArgs, TelemetryFieldMap, TelemetryMode, DescriptionLengthLogLevel } from "./types.js";
|
|
4
4
|
export declare const TELEMETRY_PROPERTY_DESCRIPTION = "Optional task context for usage analytics, based on the visible user request and the action performed by this tool.";
|
|
5
|
+
/**
|
|
6
|
+
* Kept for compatibility: the SDK no longer appends anything to tool
|
|
7
|
+
* descriptions, so no description can grow past this because of us.
|
|
8
|
+
*/
|
|
5
9
|
export declare const MAX_TOOL_DESCRIPTION_LENGTH = 1024;
|
|
6
|
-
export declare const
|
|
10
|
+
export declare const stripSdkDescriptionHint: (description: string | undefined) => string | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* @deprecated Appends nothing. The SDK no longer adds text to tool
|
|
13
|
+
* descriptions; this only removes SDK hint suffixes from earlier releases.
|
|
14
|
+
* The options are accepted and ignored.
|
|
15
|
+
*/
|
|
16
|
+
export declare const appendTelemetryHint: (description: string | undefined, _options?: {
|
|
7
17
|
requestCapability?: boolean;
|
|
8
18
|
toolName?: string;
|
|
9
19
|
logLevel?: DescriptionLengthLogLevel;
|
|
10
|
-
}) => string;
|
|
11
|
-
export declare const withoutRequestCapabilityHint: (description: string | undefined, toolName?: string, registeredDescription?: string, logLevel?: DescriptionLengthLogLevel) => string;
|
|
20
|
+
}) => string | undefined;
|
|
12
21
|
export declare 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.";
|
|
13
22
|
export declare 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').";
|
|
14
|
-
export declare 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.";
|
|
15
23
|
export declare const isCaptureEnabled: (config?: InternalMcpAnalyticsConfig) => boolean;
|
|
16
24
|
export declare const schemaDeclaresTelemetry: (inputSchema: unknown) => boolean;
|
|
17
25
|
export type ToolTelemetryPlan = {
|
|
@@ -19,21 +27,18 @@ export type ToolTelemetryPlan = {
|
|
|
19
27
|
inputSchema: unknown;
|
|
20
28
|
applyDescription: (description: string | undefined) => string | undefined;
|
|
21
29
|
};
|
|
22
|
-
export declare const planToolTelemetry: (toolName: string, inputSchema: unknown, config?: InternalMcpAnalyticsConfig,
|
|
30
|
+
export declare const planToolTelemetry: (toolName: string, inputSchema: unknown, config?: InternalMcpAnalyticsConfig, _options?: {
|
|
23
31
|
requestCapability?: boolean;
|
|
24
32
|
}) => ToolTelemetryPlan;
|
|
25
33
|
export declare const createTelemetryInputSchema: (_config?: InternalMcpAnalyticsConfig) => z.ZodOptional<z.ZodObject<{
|
|
26
34
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
27
35
|
call_purpose: z.ZodOptional<z.ZodString>;
|
|
28
|
-
user_frustration: z.ZodOptional<z.ZodString>;
|
|
29
36
|
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
30
37
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
31
38
|
call_purpose: z.ZodOptional<z.ZodString>;
|
|
32
|
-
user_frustration: z.ZodOptional<z.ZodString>;
|
|
33
39
|
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
34
40
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
35
41
|
call_purpose: z.ZodOptional<z.ZodString>;
|
|
36
|
-
user_frustration: z.ZodOptional<z.ZodString>;
|
|
37
42
|
}, z.ZodTypeAny, "passthrough">>>;
|
|
38
43
|
export declare const createTelemetryJsonSchema: (_config?: InternalMcpAnalyticsConfig) => JsonObjectSchema;
|
|
39
44
|
export declare const decorateInputSchemaWithTelemetry: (inputSchema: unknown, config?: InternalMcpAnalyticsConfig) => JsonObjectSchema | zv4.ZodObject<{
|
|
@@ -41,7 +46,6 @@ export declare const decorateInputSchemaWithTelemetry: (inputSchema: unknown, co
|
|
|
41
46
|
telemetry: zv4.ZodOptional<zv4.ZodObject<{
|
|
42
47
|
user_intent: zv4.ZodOptional<zv4.ZodString>;
|
|
43
48
|
call_purpose: zv4.ZodOptional<zv4.ZodString>;
|
|
44
|
-
user_frustration: zv4.ZodOptional<zv4.ZodString>;
|
|
45
49
|
}, zv4.core.$loose>>;
|
|
46
50
|
}, zv4.core.$strip> | z.ZodObject<{
|
|
47
51
|
[x: string]: any;
|
|
@@ -49,15 +53,12 @@ export declare const decorateInputSchemaWithTelemetry: (inputSchema: unknown, co
|
|
|
49
53
|
telemetry: z.ZodOptional<z.ZodObject<{
|
|
50
54
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
51
55
|
call_purpose: z.ZodOptional<z.ZodString>;
|
|
52
|
-
user_frustration: z.ZodOptional<z.ZodString>;
|
|
53
56
|
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
54
57
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
55
58
|
call_purpose: z.ZodOptional<z.ZodString>;
|
|
56
|
-
user_frustration: z.ZodOptional<z.ZodString>;
|
|
57
59
|
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
58
60
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
59
61
|
call_purpose: z.ZodOptional<z.ZodString>;
|
|
60
|
-
user_frustration: z.ZodOptional<z.ZodString>;
|
|
61
62
|
}, z.ZodTypeAny, "passthrough">>>;
|
|
62
63
|
}, any, any, {
|
|
63
64
|
[x: string]: any;
|
package/dist/esm/schema.js
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import * as zv4 from "zod/v4";
|
|
3
|
-
import { isRequestCapabilityEnabled } from "./request-capability.js";
|
|
4
3
|
import { isJsonObjectSchema, isRawShape, isRecord } from "./utils.js";
|
|
5
4
|
// Public task context is identical across all four SDKs. Legacy field names
|
|
6
5
|
// remain accepted on input and in storage, but are never advertised.
|
|
7
6
|
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
|
-
|
|
9
|
-
|
|
7
|
+
// Earlier releases appended these sentences to every tool description. The SDK
|
|
8
|
+
// no longer adds text to a tool description (TELEMETRY-CONTRACT.md, "Tool
|
|
9
|
+
// descriptions"): what the agent is asked for lives in the injected
|
|
10
|
+
// parameters' own descriptions, as in the PostHog and AgentCat SDKs.
|
|
11
|
+
// Anthropic's connector directory review asked for exactly this. Exact SDK
|
|
12
|
+
// suffixes from those releases are still removed, so a description registered
|
|
13
|
+
// through an older wrapper or a cached factory comes out clean.
|
|
14
|
+
const TELEMETRY_SENTENCE = "Include 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.";
|
|
10
15
|
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
|
-
|
|
12
|
-
|
|
13
|
-
// Only exact SDK suffixes are replaced. A mention within customer prose stays.
|
|
16
|
+
// Only exact SDK suffixes are removed. A mention within customer prose stays.
|
|
17
|
+
// Longer markers precede their shorter prefixes.
|
|
14
18
|
const RECOGNIZED_HINT_MARKERS = [
|
|
15
19
|
`${TELEMETRY_SENTENCE} ${REQUEST_CAPABILITY_SENTENCE}`,
|
|
16
20
|
`${TELEMETRY_SENTENCE} If no tool can do what the user asks, call request_capability.`,
|
|
@@ -22,158 +26,49 @@ const RECOGNIZED_HINT_MARKERS = [
|
|
|
22
26
|
"Pass telemetry.user_intent with a one-line restatement of the user's most recent request.",
|
|
23
27
|
"Pass telemetry.intent with a one-line user intent for analytics.",
|
|
24
28
|
];
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
// and PHP SDKs exactly.
|
|
29
|
+
/**
|
|
30
|
+
* Kept for compatibility: the SDK no longer appends anything to tool
|
|
31
|
+
* descriptions, so no description can grow past this because of us.
|
|
32
|
+
*/
|
|
30
33
|
export const MAX_TOOL_DESCRIPTION_LENGTH = 1024;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
if (
|
|
36
|
-
return;
|
|
37
|
-
warnedLongDescriptions.add(toolName);
|
|
38
|
-
const text = `[mcp-analytics] Tool "${toolName}" description is too long ${message}`;
|
|
39
|
-
// Node's console.debug and console.info write to stdout, which carries
|
|
40
|
-
// JSON-RPC for stdio servers. Where the runtime has a stderr stream, every
|
|
41
|
-
// level goes there; elsewhere (Workers, browsers) the console method
|
|
42
|
-
// carries the level.
|
|
43
|
-
const stderr = typeof process !== "undefined" ? process.stderr : undefined;
|
|
44
|
-
// eslint-disable-next-line no-console
|
|
45
|
-
if (level === "warning")
|
|
46
|
-
console.warn(text);
|
|
47
|
-
else if (typeof stderr?.write === "function")
|
|
48
|
-
stderr.write(`${text}\n`);
|
|
49
|
-
// eslint-disable-next-line no-console
|
|
50
|
-
else if (level === "debug")
|
|
51
|
-
console.debug(text);
|
|
52
|
-
// eslint-disable-next-line no-console
|
|
53
|
-
else
|
|
54
|
-
console.info(text);
|
|
55
|
-
};
|
|
56
|
-
// Appends the current telemetry hint and upgrades exact SDK hint suffixes.
|
|
57
|
-
// Every integration shape must run tool descriptions through this
|
|
58
|
-
// so calling agents know to pass telemetry (ARM-24). With `requestCapability`,
|
|
59
|
-
// the hint also points agents to request_capability.
|
|
60
|
-
//
|
|
61
|
-
// The result never exceeds MAX_TOOL_DESCRIPTION_LENGTH because of us: when the
|
|
62
|
-
// full hint does not fit, only the telemetry sentence is appended (it matters
|
|
63
|
-
// per tool; request_capability is a list-wide instruction other tools carry);
|
|
64
|
-
// when that does not fit either, the description is left unchanged. Sentences
|
|
65
|
-
// are never cut, and the telemetry field is still advertised either way.
|
|
66
|
-
export const appendTelemetryHint = (description, options = {}) => {
|
|
67
|
-
const requestCapability = options.requestCapability === true;
|
|
68
|
-
if (description === undefined) {
|
|
69
|
-
return (requestCapability
|
|
70
|
-
? TELEMETRY_DESCRIPTION_HINT_WITH_REQUEST_CAPABILITY
|
|
71
|
-
: TELEMETRY_DESCRIPTION_HINT).trimStart();
|
|
72
|
-
}
|
|
73
|
-
// Upgrade cached SDK suffixes before checking the size budget. Remove a
|
|
74
|
-
// full hint before its shorter forms, including stacked older wrappers.
|
|
34
|
+
// Removes the SDK's own trailing hint paragraph(s), stacked older wrappers
|
|
35
|
+
// included. A description that was only an SDK hint becomes empty; a tool with
|
|
36
|
+
// no description keeps none.
|
|
37
|
+
export const stripSdkDescriptionHint = (description) => {
|
|
38
|
+
if (description === undefined)
|
|
39
|
+
return undefined;
|
|
75
40
|
let base = description;
|
|
76
41
|
for (;;) {
|
|
77
42
|
const trimmed = base.trimEnd();
|
|
78
43
|
const marker = RECOGNIZED_HINT_MARKERS.find((candidate) => trimmed === candidate || trimmed.endsWith(`\n\n${candidate}`));
|
|
79
44
|
if (marker === undefined)
|
|
80
|
-
|
|
45
|
+
return base;
|
|
81
46
|
base = trimmed === marker ? "" : trimmed.slice(0, -marker.length - 2);
|
|
82
47
|
}
|
|
83
|
-
description = base;
|
|
84
|
-
if (description.length === 0) {
|
|
85
|
-
return (requestCapability
|
|
86
|
-
? TELEMETRY_DESCRIPTION_HINT_WITH_REQUEST_CAPABILITY
|
|
87
|
-
: TELEMETRY_DESCRIPTION_HINT).trimStart();
|
|
88
|
-
}
|
|
89
|
-
// A customer who already wrote the request_capability sentence gets only the
|
|
90
|
-
// telemetry one, so the instruction is not repeated.
|
|
91
|
-
const hint = !requestCapability
|
|
92
|
-
? TELEMETRY_DESCRIPTION_HINT
|
|
93
|
-
: description.includes(REQUEST_CAPABILITY_SENTENCE)
|
|
94
|
-
? TELEMETRY_SENTENCE_HINT
|
|
95
|
-
: TELEMETRY_DESCRIPTION_HINT_WITH_REQUEST_CAPABILITY;
|
|
96
|
-
if (fits(`${description}${hint}`)) {
|
|
97
|
-
return `${description}${hint}`;
|
|
98
|
-
}
|
|
99
|
-
if (fits(`${description}${TELEMETRY_SENTENCE_HINT}`)) {
|
|
100
|
-
if (options.toolName !== undefined) {
|
|
101
|
-
warnDescriptionTooLong(options.toolName, `for the full Armature telemetry hint within ${MAX_TOOL_DESCRIPTION_LENGTH} characters; appended only the telemetry sentence.`, options.logLevel);
|
|
102
|
-
}
|
|
103
|
-
return `${description}${TELEMETRY_SENTENCE_HINT}`;
|
|
104
|
-
}
|
|
105
|
-
if (options.toolName !== undefined) {
|
|
106
|
-
warnDescriptionTooLong(options.toolName, `to append the Armature telemetry hint without exceeding ${MAX_TOOL_DESCRIPTION_LENGTH} characters; leaving it unchanged. Telemetry is still collected.`, options.logLevel);
|
|
107
|
-
}
|
|
108
|
-
return description;
|
|
109
|
-
};
|
|
110
|
-
// The factory can append customer text after the SDK's registered hint. Update
|
|
111
|
-
// that exact block in place when its server does not expose request_capability.
|
|
112
|
-
export const withoutRequestCapabilityHint = (description, toolName, registeredDescription, logLevel) => {
|
|
113
|
-
if (description !== undefined) {
|
|
114
|
-
// The last paragraph is the SDK suffix, even when an earlier customer
|
|
115
|
-
// paragraph quotes the same instruction. If the factory kept the registered
|
|
116
|
-
// description, anchor to its suffix so later customer quotes also survive.
|
|
117
|
-
const registeredIndex = registeredDescription === undefined
|
|
118
|
-
? -1
|
|
119
|
-
: description.indexOf(registeredDescription);
|
|
120
|
-
const registeredMarker = registeredDescription === undefined
|
|
121
|
-
? undefined
|
|
122
|
-
: RECOGNIZED_HINT_MARKERS.find((marker) => registeredDescription === marker || registeredDescription.endsWith(`\n\n${marker}`));
|
|
123
|
-
let match;
|
|
124
|
-
if (registeredIndex >= 0 && registeredDescription !== undefined) {
|
|
125
|
-
if (registeredMarker !== undefined) {
|
|
126
|
-
match = {
|
|
127
|
-
marker: registeredMarker,
|
|
128
|
-
index: registeredIndex + registeredDescription.length - registeredMarker.length,
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
for (const marker of RECOGNIZED_HINT_MARKERS) {
|
|
134
|
-
const paragraph = description.lastIndexOf(`\n\n${marker}`);
|
|
135
|
-
const index = paragraph >= 0 ? paragraph + 2 : description.startsWith(marker) ? 0 : -1;
|
|
136
|
-
// Longer markers precede their shorter prefixes in the list.
|
|
137
|
-
if (index >= 0 && (match === undefined || index > match.index))
|
|
138
|
-
match = { marker, index };
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
if (match !== undefined) {
|
|
142
|
-
const { marker, index } = match;
|
|
143
|
-
const updated = description.slice(0, index) + TELEMETRY_SENTENCE + description.slice(index + marker.length);
|
|
144
|
-
if (fits(updated))
|
|
145
|
-
return updated;
|
|
146
|
-
if (toolName !== undefined) {
|
|
147
|
-
warnDescriptionTooLong(toolName, `to keep the Armature telemetry hint within ${MAX_TOOL_DESCRIPTION_LENGTH} characters; removed it. Telemetry is still collected.`, logLevel);
|
|
148
|
-
}
|
|
149
|
-
return index === 0
|
|
150
|
-
? description.slice(marker.length).trimStart()
|
|
151
|
-
: description.slice(0, index - 2) + description.slice(index + marker.length);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return appendTelemetryHint(description, toolName === undefined ? { logLevel } : { toolName, logLevel });
|
|
155
48
|
};
|
|
49
|
+
/**
|
|
50
|
+
* @deprecated Appends nothing. The SDK no longer adds text to tool
|
|
51
|
+
* descriptions; this only removes SDK hint suffixes from earlier releases.
|
|
52
|
+
* The options are accepted and ignored.
|
|
53
|
+
*/
|
|
54
|
+
export const appendTelemetryHint = (description, _options = {}) => stripSdkDescriptionHint(description);
|
|
156
55
|
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.";
|
|
157
56
|
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').";
|
|
158
|
-
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.";
|
|
159
57
|
// Each telemetry object schema carries the object-level description via
|
|
160
58
|
// `.describe(...)` so it survives zod→JSON-schema conversion in every
|
|
161
59
|
// integration shape — including caller-owned McpServer registration, where no
|
|
162
60
|
// post-hoc JSON-schema nudge runs (ARM-24).
|
|
163
61
|
//
|
|
164
|
-
// `.passthrough()` keeps unknown keys: a client that cached
|
|
165
|
-
// schema may still send `intent`/`context
|
|
166
|
-
//
|
|
167
|
-
//
|
|
62
|
+
// `.passthrough()` keeps unknown keys: a client that cached an older tool
|
|
63
|
+
// schema may still send `intent`/`context`, and stripping them here would
|
|
64
|
+
// silently drop its telemetry before normalizeTelemetryArgs can translate the
|
|
65
|
+
// legacy spelling. A cached `user_frustration`/`frustration_level` passes
|
|
66
|
+
// validation too and is then dropped.
|
|
168
67
|
//
|
|
169
68
|
const looseTelemetryInputSchema = z
|
|
170
69
|
.object({
|
|
171
70
|
user_intent: z.string().describe(USER_INTENT_DESCRIPTION).optional(),
|
|
172
71
|
call_purpose: z.string().describe(CALL_PURPOSE_DESCRIPTION).optional(),
|
|
173
|
-
user_frustration: z
|
|
174
|
-
.string()
|
|
175
|
-
.describe(USER_FRUSTRATION_DESCRIPTION)
|
|
176
|
-
.optional(),
|
|
177
72
|
})
|
|
178
73
|
.passthrough()
|
|
179
74
|
.describe(TELEMETRY_PROPERTY_DESCRIPTION);
|
|
@@ -184,10 +79,6 @@ const looseTelemetryInputSchemaV4 = zv4
|
|
|
184
79
|
.string()
|
|
185
80
|
.describe(CALL_PURPOSE_DESCRIPTION)
|
|
186
81
|
.optional(),
|
|
187
|
-
user_frustration: zv4
|
|
188
|
-
.string()
|
|
189
|
-
.describe(USER_FRUSTRATION_DESCRIPTION)
|
|
190
|
-
.optional(),
|
|
191
82
|
})
|
|
192
83
|
.describe(TELEMETRY_PROPERTY_DESCRIPTION);
|
|
193
84
|
// v4 Zod schemas carry a `_zod` brand on every type; v3 only has `_def`.
|
|
@@ -254,10 +145,9 @@ const warnTelemetryCollision = (toolName) => {
|
|
|
254
145
|
// prototype patch, Mastra adapter, custom dispatchers) must register and
|
|
255
146
|
// extract with the same plan, so the advertised schema always matches runtime
|
|
256
147
|
// behavior.
|
|
257
|
-
// `
|
|
258
|
-
//
|
|
259
|
-
|
|
260
|
-
export const planToolTelemetry = (toolName, inputSchema, config = {}, options = {}) => {
|
|
148
|
+
// `_options` is accepted for adapters built against earlier releases; no
|
|
149
|
+
// description mentions request_capability any more.
|
|
150
|
+
export const planToolTelemetry = (toolName, inputSchema, config = {}, _options = {}) => {
|
|
261
151
|
if (schemaDeclaresTelemetry(inputSchema)) {
|
|
262
152
|
warnTelemetryCollision(toolName);
|
|
263
153
|
return {
|
|
@@ -276,11 +166,7 @@ export const planToolTelemetry = (toolName, inputSchema, config = {}, options =
|
|
|
276
166
|
return {
|
|
277
167
|
mode: "injected",
|
|
278
168
|
inputSchema: decorateInputSchemaWithTelemetry(inputSchema, config),
|
|
279
|
-
applyDescription:
|
|
280
|
-
requestCapability: options.requestCapability ?? isRequestCapabilityEnabled(config),
|
|
281
|
-
toolName,
|
|
282
|
-
logLevel: config.armature?.descriptionLengthLogLevel,
|
|
283
|
-
}),
|
|
169
|
+
applyDescription: stripSdkDescriptionHint,
|
|
284
170
|
};
|
|
285
171
|
};
|
|
286
172
|
// The telemetry object and every field stay optional. In particular,
|
|
@@ -304,10 +190,6 @@ export const createTelemetryJsonSchema = (_config = {}) => {
|
|
|
304
190
|
type: "string",
|
|
305
191
|
description: CALL_PURPOSE_DESCRIPTION,
|
|
306
192
|
},
|
|
307
|
-
user_frustration: {
|
|
308
|
-
type: "string",
|
|
309
|
-
description: USER_FRUSTRATION_DESCRIPTION,
|
|
310
|
-
},
|
|
311
193
|
},
|
|
312
194
|
};
|
|
313
195
|
};
|
|
@@ -349,11 +231,6 @@ export const decorateInputSchemaWithTelemetry = (inputSchema, config = {}) => {
|
|
|
349
231
|
}
|
|
350
232
|
throw new Error("MCP analytics can only decorate undefined, Zod object, JSON object, or raw-shape input schemas.");
|
|
351
233
|
};
|
|
352
|
-
const asFrustration = (value) => {
|
|
353
|
-
return value === "low" || value === "medium" || value === "high"
|
|
354
|
-
? value
|
|
355
|
-
: undefined;
|
|
356
|
-
};
|
|
357
234
|
// First value that is actually a string — mirrors Python's _first_str so both
|
|
358
235
|
// SDKs resolve mixed V1/legacy inputs identically (a non-string V1 value never
|
|
359
236
|
// shadows a usable legacy string).
|
|
@@ -382,10 +259,8 @@ export const normalizeTelemetryArgs = (telemetry) => {
|
|
|
382
259
|
const agentThinking = firstString(telemetry.call_purpose, telemetry.agent_thinking, telemetry.context);
|
|
383
260
|
if (agentThinking !== undefined)
|
|
384
261
|
normalized.agent_thinking = agentThinking;
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
if (userFrustration !== undefined)
|
|
388
|
-
normalized.user_frustration = userFrustration;
|
|
262
|
+
// `user_frustration`/`frustration_level` from cached schemas are dropped:
|
|
263
|
+
// the SDK no longer asks agents to rate the user.
|
|
389
264
|
return normalized;
|
|
390
265
|
};
|
|
391
266
|
// Opt-in export of customer-owned argument fields (gap #11): reads — never
|
|
@@ -413,13 +288,7 @@ export const applyTelemetryFieldMap = (telemetry, args, fieldMap) => {
|
|
|
413
288
|
if (value !== undefined)
|
|
414
289
|
merged.call_purpose = value;
|
|
415
290
|
}
|
|
416
|
-
|
|
417
|
-
&& merged.frustration_level === undefined
|
|
418
|
-
&& fieldMap.user_frustration !== undefined) {
|
|
419
|
-
const value = asFrustration(args[fieldMap.user_frustration]);
|
|
420
|
-
if (value !== undefined)
|
|
421
|
-
merged.user_frustration = value;
|
|
422
|
-
}
|
|
291
|
+
// A `user_frustration` mapping is accepted and ignored.
|
|
423
292
|
return Object.keys(merged).length > 0 ? merged : telemetry;
|
|
424
293
|
};
|
|
425
294
|
// Mode semantics (TELEMETRY-CONTRACT.md): "injected" strips and exports;
|
package/dist/esm/server.js
CHANGED
|
@@ -2,26 +2,12 @@ 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 {
|
|
5
|
+
import { planToolTelemetry } from "./schema.js";
|
|
6
6
|
import { defaultMcpAnalyticsConfig } from "./emit.js";
|
|
7
7
|
import { deriveToolResultError, isRecord } from "./utils.js";
|
|
8
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
|
-
};
|
|
25
11
|
// Mirrors the SDK's `isZodTypeLike`/`isZodRawShapeCompat` checks closely enough
|
|
26
12
|
// to disambiguate the overloads of `server.tool(...)`: a raw shape is a plain
|
|
27
13
|
// object whose values include at least one Zod schema, while a Zod schema
|
|
@@ -128,16 +114,11 @@ const installPrototypePatchOnce = () => {
|
|
|
128
114
|
prototype.registerTool = function patchedRegisterTool(name, toolConfig, cb) {
|
|
129
115
|
const ctx = withAnalyticsStorage.getStore();
|
|
130
116
|
if (!ctx) {
|
|
131
|
-
|
|
132
|
-
if (name === REQUEST_CAPABILITY_TOOL_NAME)
|
|
133
|
-
serversListingRequestCapability.add(this);
|
|
134
|
-
return registered;
|
|
117
|
+
return originalRegisterTool.call(this, name, toolConfig, cb);
|
|
135
118
|
}
|
|
136
119
|
const { config } = ctx;
|
|
137
120
|
const originalHasInputSchema = toolConfig.inputSchema !== undefined;
|
|
138
|
-
const plan = planToolTelemetry(name, toolConfig.inputSchema, config
|
|
139
|
-
? { requestCapability: listsRequestCapability(this) }
|
|
140
|
-
: {});
|
|
121
|
+
const plan = planToolTelemetry(name, toolConfig.inputSchema, config);
|
|
141
122
|
const registeredHasInputSchema = plan.inputSchema !== undefined;
|
|
142
123
|
const description = plan.applyDescription(toolConfig.description);
|
|
143
124
|
const instrumentedConfig = {
|
|
@@ -146,23 +127,7 @@ const installPrototypePatchOnce = () => {
|
|
|
146
127
|
...(registeredHasInputSchema ? { inputSchema: plan.inputSchema } : {}),
|
|
147
128
|
};
|
|
148
129
|
const wrappedCallback = wrapCallbackWithAnalytics(name, cb, originalHasInputSchema, registeredHasInputSchema, plan.mode, ctx);
|
|
149
|
-
|
|
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
|
-
logLevel: config.armature?.descriptionLengthLogLevel,
|
|
161
|
-
}),
|
|
162
|
-
logLevel: config.armature?.descriptionLengthLogLevel,
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
return registered;
|
|
130
|
+
return originalRegisterTool.call(this, name, instrumentedConfig, wrappedCallback);
|
|
166
131
|
};
|
|
167
132
|
// PRIA and other older codebases still use the deprecated `server.tool(...)`
|
|
168
133
|
// overloads. Patch them too — we normalise into our patched `registerTool`
|
|
@@ -189,8 +154,6 @@ export const withMcpAnalytics = (config, createServer) => {
|
|
|
189
154
|
const ctx = {
|
|
190
155
|
config,
|
|
191
156
|
recorder,
|
|
192
|
-
resolved: false,
|
|
193
|
-
requestCapabilityHints: [],
|
|
194
157
|
};
|
|
195
158
|
const result = withAnalyticsStorage.run(ctx, createServer);
|
|
196
159
|
// Customer tools registered inside the factory are handled by the
|
|
@@ -200,10 +163,10 @@ export const withMcpAnalytics = (config, createServer) => {
|
|
|
200
163
|
// description.
|
|
201
164
|
if (isRequestCapabilityEnabled(config) && !(result instanceof McpServer)) {
|
|
202
165
|
// Only a hard error when the caller explicitly opted in. A server that is
|
|
203
|
-
//
|
|
204
|
-
//
|
|
166
|
+
// on merely by default and doesn't return an McpServer silently skips
|
|
167
|
+
// injection instead of breaking on upgrade.
|
|
205
168
|
if (isRequestCapabilityExplicit(config)) {
|
|
206
|
-
throw new Error("armature.
|
|
169
|
+
throw new Error("armature.sendFeedback requires the server factory to return an McpServer instance.");
|
|
207
170
|
}
|
|
208
171
|
}
|
|
209
172
|
if (isRequestCapabilityEnabled(config) && result instanceof McpServer) {
|
|
@@ -213,31 +176,14 @@ export const withMcpAnalytics = (config, createServer) => {
|
|
|
213
176
|
catch (error) {
|
|
214
177
|
const collision = error instanceof Error && /already registered/i.test(error.message);
|
|
215
178
|
// A pre-existing tool of the same name is reserved only when the caller
|
|
216
|
-
// explicitly opted in; on
|
|
179
|
+
// explicitly opted in; on by default yields to the customer's tool.
|
|
217
180
|
if (collision && isRequestCapabilityExplicit(config)) {
|
|
218
|
-
throw new Error(
|
|
181
|
+
throw new Error(`Tool name "${REQUEST_CAPABILITY_TOOL_NAME}" is reserved while armature.sendFeedback is enabled.`, { cause: error });
|
|
219
182
|
}
|
|
220
183
|
if (!collision)
|
|
221
184
|
throw error;
|
|
222
185
|
}
|
|
223
186
|
}
|
|
224
|
-
// Tools registered on a server that does not list request_capability (the
|
|
225
|
-
// factory returned something else, or registered on another server) must
|
|
226
|
-
// not point agents at it.
|
|
227
|
-
ctx.resolved = true;
|
|
228
|
-
for (const hint of ctx.requestCapabilityHints) {
|
|
229
|
-
if (!listsRequestCapability(hint.server)
|
|
230
|
-
&& hint.tool !== null
|
|
231
|
-
&& typeof hint.tool === "object") {
|
|
232
|
-
const tool = hint.tool;
|
|
233
|
-
// Keep a description the factory updated after registration; only its
|
|
234
|
-
// hint changes.
|
|
235
|
-
tool.description = tool.description === hint.hintedDescription
|
|
236
|
-
? hint.fallbackDescription
|
|
237
|
-
: withoutRequestCapabilityHint(tool.description, hint.toolName, hint.hintedDescription, hint.logLevel);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
ctx.requestCapabilityHints.length = 0;
|
|
241
187
|
return { result, recorder };
|
|
242
188
|
};
|
|
243
189
|
export const createMcpAnalyticsServer = (createServer, config = defaultMcpAnalyticsConfig) => {
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -60,6 +60,7 @@ export type TelemetryFieldMap = {
|
|
|
60
60
|
call_purpose?: string;
|
|
61
61
|
/** @deprecated Accepted for compatibility. Prefer call_purpose. */
|
|
62
62
|
agent_thinking?: string;
|
|
63
|
+
/** @deprecated `user_frustration` is no longer collected; this mapping is ignored. */
|
|
63
64
|
user_frustration?: string;
|
|
64
65
|
};
|
|
65
66
|
export type McpAnalyticsConfig = {
|
|
@@ -83,10 +84,12 @@ export type McpAnalyticsConfig = {
|
|
|
83
84
|
/** Register background work with a platform lifecycle primitive (for example waitUntil). */
|
|
84
85
|
schedule?: (work: Promise<void>) => void;
|
|
85
86
|
telemetryFieldMap?: TelemetryFieldMap;
|
|
87
|
+
sendFeedback?: boolean;
|
|
88
|
+
/** @deprecated Earlier name of `sendFeedback`; `sendFeedback` wins when both are set. */
|
|
86
89
|
requestCapability?: boolean;
|
|
87
90
|
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
91
|
+
* @deprecated The SDK no longer adds text to tool descriptions, so there
|
|
92
|
+
* is no length notice. Accepted and ignored.
|
|
90
93
|
*/
|
|
91
94
|
descriptionLengthLogLevel?: DescriptionLengthLogLevel;
|
|
92
95
|
};
|
|
@@ -108,12 +111,13 @@ export type TelemetryArgs = {
|
|
|
108
111
|
call_purpose?: string;
|
|
109
112
|
/** @deprecated Accepted for compatibility. Prefer call_purpose. */
|
|
110
113
|
agent_thinking?: string;
|
|
114
|
+
/** @deprecated No longer collected: accepted from cached clients and dropped. */
|
|
111
115
|
user_frustration?: "low" | "medium" | "high";
|
|
112
116
|
/** @deprecated Pre-V1 spelling of `user_intent`; still accepted. */
|
|
113
117
|
intent?: string;
|
|
114
118
|
/** @deprecated Pre-V1 spelling of `agent_thinking`; still accepted. */
|
|
115
119
|
context?: string;
|
|
116
|
-
/** @deprecated
|
|
120
|
+
/** @deprecated No longer collected: accepted from cached clients and dropped. */
|
|
117
121
|
frustration_level?: "low" | "medium" | "high";
|
|
118
122
|
};
|
|
119
123
|
export type ExtractedToolArguments = {
|