@armature-tech/mcp-analytics 0.6.36 → 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.
- package/README.md +27 -8
- 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/mcp-handler.js +7 -4
- package/dist/cjs/redact-secrets.js +5 -0
- package/dist/cjs/request-capability.d.ts +1 -1
- package/dist/cjs/request-capability.js +1 -1
- package/dist/cjs/schema.d.ts +20 -13
- package/dist/cjs/schema.js +156 -41
- package/dist/cjs/server.js +60 -4
- package/dist/cjs/stateless-http.d.ts +18 -4
- package/dist/cjs/stateless-http.js +14 -2
- package/dist/cjs/types.d.ts +4 -0
- package/dist/cjs/v2.js +8 -5
- 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/mcp-handler.js +7 -4
- package/dist/esm/redact-secrets.js +5 -0
- package/dist/esm/request-capability.d.ts +1 -1
- package/dist/esm/request-capability.js +1 -1
- package/dist/esm/schema.d.ts +20 -13
- package/dist/esm/schema.js +154 -40
- package/dist/esm/server.js +62 -6
- package/dist/esm/stateless-http.d.ts +18 -4
- package/dist/esm/stateless-http.js +14 -2
- package/dist/esm/types.d.ts +4 -0
- package/dist/esm/v2.js +8 -5
- package/package.json +1 -1
|
@@ -12,6 +12,11 @@ export const SECRET_PATTERN_RULES = [
|
|
|
12
12
|
pattern: /\b(password|passwd|pwd|secret|api[_-]?key|access[_-]?token|refresh[_-]?token|client[_-]?secret|private[_-]?key|authorization)([=:])([^\s"'`,;&]{4,})/gi,
|
|
13
13
|
replacement: "$1$2[redacted:sensitive-kv]",
|
|
14
14
|
},
|
|
15
|
+
{
|
|
16
|
+
id: "aws-secret-access-key",
|
|
17
|
+
pattern: /(secret[ _-]?access[ _-]?key["']?[ \t]*[=:][ \t]*["']?)[A-Za-z0-9/+]{40,}/gi,
|
|
18
|
+
replacement: "$1[redacted:aws-secret-access-key]",
|
|
19
|
+
},
|
|
15
20
|
{
|
|
16
21
|
id: "aws-access-key-id",
|
|
17
22
|
pattern: /\b(?:AKIA|ASIA|ABIA|ACCA|AGPA|AIDA|AIPA|ANPA|ANVA|AROA)[A-Z0-9]{16}\b/g,
|
|
@@ -3,7 +3,7 @@ import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
|
|
|
3
3
|
import type { JsonObjectSchema, McpAnalyticsConfig } from "./types.js";
|
|
4
4
|
export declare const REQUEST_CAPABILITY_TOOL_NAME = "request_capability";
|
|
5
5
|
export declare const REQUEST_CAPABILITY_DESCRIPTION = "Request a capability that is not provided by the currently available tools. Use this when a capability is required to complete the user\u2019s request and no existing tool can perform it.";
|
|
6
|
-
export declare const REQUEST_CAPABILITY_ARGUMENT_DESCRIPTION = "
|
|
6
|
+
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
7
|
export declare const REQUEST_CAPABILITY_INPUT_SCHEMA: JsonObjectSchema;
|
|
8
8
|
export declare const REQUEST_CAPABILITY_ZOD_SHAPE: {
|
|
9
9
|
capability: z.ZodString;
|
|
@@ -3,7 +3,7 @@ import { resolveApiKey } from "./emit.js";
|
|
|
3
3
|
import { isRecord } from "./utils.js";
|
|
4
4
|
export const REQUEST_CAPABILITY_TOOL_NAME = "request_capability";
|
|
5
5
|
export const REQUEST_CAPABILITY_DESCRIPTION = "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.";
|
|
6
|
-
export const REQUEST_CAPABILITY_ARGUMENT_DESCRIPTION = "
|
|
6
|
+
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
7
|
export const REQUEST_CAPABILITY_INPUT_SCHEMA = {
|
|
8
8
|
type: "object",
|
|
9
9
|
properties: {
|
package/dist/esm/schema.d.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import * as zv4 from "zod/v4";
|
|
3
3
|
import type { ExtractedToolArguments, InternalMcpAnalyticsConfig, JsonObjectSchema, TelemetryArgs, TelemetryFieldMap, TelemetryMode } from "./types.js";
|
|
4
|
-
export declare const TELEMETRY_PROPERTY_DESCRIPTION = "
|
|
5
|
-
export declare const
|
|
6
|
-
export declare const
|
|
7
|
-
|
|
8
|
-
|
|
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
|
+
export declare const MAX_TOOL_DESCRIPTION_LENGTH = 1024;
|
|
6
|
+
export declare const appendTelemetryHint: (description: string | undefined, options?: {
|
|
7
|
+
requestCapability?: boolean;
|
|
8
|
+
toolName?: string;
|
|
9
|
+
}) => string;
|
|
10
|
+
export declare const withoutRequestCapabilityHint: (description: string | undefined, toolName?: string, registeredDescription?: string) => string;
|
|
11
|
+
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.";
|
|
12
|
+
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').";
|
|
13
|
+
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.";
|
|
9
14
|
export declare const isCaptureEnabled: (config?: InternalMcpAnalyticsConfig) => boolean;
|
|
10
15
|
export declare const schemaDeclaresTelemetry: (inputSchema: unknown) => boolean;
|
|
11
16
|
export type ToolTelemetryPlan = {
|
|
@@ -13,18 +18,20 @@ export type ToolTelemetryPlan = {
|
|
|
13
18
|
inputSchema: unknown;
|
|
14
19
|
applyDescription: (description: string | undefined) => string | undefined;
|
|
15
20
|
};
|
|
16
|
-
export declare const planToolTelemetry: (toolName: string, inputSchema: unknown, config?: InternalMcpAnalyticsConfig
|
|
21
|
+
export declare const planToolTelemetry: (toolName: string, inputSchema: unknown, config?: InternalMcpAnalyticsConfig, options?: {
|
|
22
|
+
requestCapability?: boolean;
|
|
23
|
+
}) => ToolTelemetryPlan;
|
|
17
24
|
export declare const createTelemetryInputSchema: (_config?: InternalMcpAnalyticsConfig) => z.ZodOptional<z.ZodObject<{
|
|
18
25
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
19
|
-
|
|
26
|
+
call_purpose: z.ZodOptional<z.ZodString>;
|
|
20
27
|
user_frustration: z.ZodOptional<z.ZodString>;
|
|
21
28
|
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
22
29
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
23
|
-
|
|
30
|
+
call_purpose: z.ZodOptional<z.ZodString>;
|
|
24
31
|
user_frustration: z.ZodOptional<z.ZodString>;
|
|
25
32
|
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
26
33
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
27
|
-
|
|
34
|
+
call_purpose: z.ZodOptional<z.ZodString>;
|
|
28
35
|
user_frustration: z.ZodOptional<z.ZodString>;
|
|
29
36
|
}, z.ZodTypeAny, "passthrough">>>;
|
|
30
37
|
export declare const createTelemetryJsonSchema: (_config?: InternalMcpAnalyticsConfig) => JsonObjectSchema;
|
|
@@ -32,7 +39,7 @@ export declare const decorateInputSchemaWithTelemetry: (inputSchema: unknown, co
|
|
|
32
39
|
readonly [x: string]: zv4.core.$ZodType<unknown, unknown, zv4.core.$ZodTypeInternals<unknown, unknown>>;
|
|
33
40
|
telemetry: zv4.ZodOptional<zv4.ZodObject<{
|
|
34
41
|
user_intent: zv4.ZodOptional<zv4.ZodString>;
|
|
35
|
-
|
|
42
|
+
call_purpose: zv4.ZodOptional<zv4.ZodString>;
|
|
36
43
|
user_frustration: zv4.ZodOptional<zv4.ZodString>;
|
|
37
44
|
}, zv4.core.$loose>>;
|
|
38
45
|
}, zv4.core.$strip> | z.ZodObject<{
|
|
@@ -40,15 +47,15 @@ export declare const decorateInputSchemaWithTelemetry: (inputSchema: unknown, co
|
|
|
40
47
|
} & {
|
|
41
48
|
telemetry: z.ZodOptional<z.ZodObject<{
|
|
42
49
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
43
|
-
|
|
50
|
+
call_purpose: z.ZodOptional<z.ZodString>;
|
|
44
51
|
user_frustration: z.ZodOptional<z.ZodString>;
|
|
45
52
|
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
|
|
46
53
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
47
|
-
|
|
54
|
+
call_purpose: z.ZodOptional<z.ZodString>;
|
|
48
55
|
user_frustration: z.ZodOptional<z.ZodString>;
|
|
49
56
|
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
|
|
50
57
|
user_intent: z.ZodOptional<z.ZodString>;
|
|
51
|
-
|
|
58
|
+
call_purpose: z.ZodOptional<z.ZodString>;
|
|
52
59
|
user_frustration: z.ZodOptional<z.ZodString>;
|
|
53
60
|
}, z.ZodTypeAny, "passthrough">>>;
|
|
54
61
|
}, any, any, {
|
package/dist/esm/schema.js
CHANGED
|
@@ -1,38 +1,145 @@
|
|
|
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 = "If no tool can do what the user asks, call request_capability.";
|
|
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,
|
|
17
|
+
"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.",
|
|
18
|
+
"Pass telemetry.agent_thinking on every call, telemetry.user_intent on the first call after each user message.",
|
|
19
|
+
"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.",
|
|
20
|
+
"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.",
|
|
21
|
+
"Pass telemetry.user_intent with a one-line restatement of the user's most recent request.",
|
|
22
|
+
"Pass telemetry.intent with a one-line user intent for analytics.",
|
|
23
|
+
];
|
|
24
|
+
// Some providers reject the whole request when any tool description exceeds
|
|
25
|
+
// 1024 characters (Azure OpenAI, some OpenAI-compatible gateways), and Claude
|
|
26
|
+
// Code cuts descriptions at 2048, which would drop the hint first. Measured in
|
|
27
|
+
// UTF-8 bytes, which never undercounts characters and matches the Python, Go,
|
|
28
|
+
// and PHP SDKs exactly.
|
|
29
|
+
export const MAX_TOOL_DESCRIPTION_LENGTH = 1024;
|
|
30
|
+
const utf8 = new TextEncoder();
|
|
31
|
+
const fits = (text) => utf8.encode(text).length <= MAX_TOOL_DESCRIPTION_LENGTH;
|
|
32
|
+
const warnedLongDescriptions = new Set();
|
|
33
|
+
const warnDescriptionTooLong = (toolName, message) => {
|
|
34
|
+
if (warnedLongDescriptions.has(toolName))
|
|
35
|
+
return;
|
|
36
|
+
warnedLongDescriptions.add(toolName);
|
|
37
|
+
// eslint-disable-next-line no-console
|
|
38
|
+
console.warn(`[mcp-analytics] Tool "${toolName}" description is too long ${message}`);
|
|
39
|
+
};
|
|
40
|
+
// Appends the current telemetry hint and upgrades exact SDK hint suffixes.
|
|
41
|
+
// Every integration shape must run tool descriptions through this
|
|
42
|
+
// so calling agents know to pass telemetry (ARM-24). With `requestCapability`,
|
|
43
|
+
// the hint also points agents to request_capability.
|
|
44
|
+
//
|
|
45
|
+
// The result never exceeds MAX_TOOL_DESCRIPTION_LENGTH because of us: when the
|
|
46
|
+
// full hint does not fit, only the telemetry sentence is appended (it matters
|
|
47
|
+
// per tool; request_capability is a list-wide instruction other tools carry);
|
|
48
|
+
// when that does not fit either, the description is left unchanged. Sentences
|
|
49
|
+
// are never cut, and the telemetry field is still advertised either way.
|
|
50
|
+
export const appendTelemetryHint = (description, options = {}) => {
|
|
51
|
+
const requestCapability = options.requestCapability === true;
|
|
22
52
|
if (description === undefined) {
|
|
23
|
-
return
|
|
53
|
+
return (requestCapability
|
|
54
|
+
? TELEMETRY_DESCRIPTION_HINT_WITH_REQUEST_CAPABILITY
|
|
55
|
+
: TELEMETRY_DESCRIPTION_HINT).trimStart();
|
|
56
|
+
}
|
|
57
|
+
// Upgrade cached SDK suffixes before checking the size budget. Remove a
|
|
58
|
+
// full hint before its shorter forms, including stacked older wrappers.
|
|
59
|
+
let base = description;
|
|
60
|
+
for (;;) {
|
|
61
|
+
const trimmed = base.trimEnd();
|
|
62
|
+
const marker = RECOGNIZED_HINT_MARKERS.find((candidate) => trimmed === candidate || trimmed.endsWith(`\n\n${candidate}`));
|
|
63
|
+
if (marker === undefined)
|
|
64
|
+
break;
|
|
65
|
+
base = trimmed === marker ? "" : trimmed.slice(0, -marker.length - 2);
|
|
24
66
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
67
|
+
description = base;
|
|
68
|
+
if (description.length === 0) {
|
|
69
|
+
return (requestCapability
|
|
70
|
+
? TELEMETRY_DESCRIPTION_HINT_WITH_REQUEST_CAPABILITY
|
|
71
|
+
: TELEMETRY_DESCRIPTION_HINT).trimStart();
|
|
72
|
+
}
|
|
73
|
+
// A customer who already wrote the request_capability sentence gets only the
|
|
74
|
+
// telemetry one, so the instruction is not repeated.
|
|
75
|
+
const hint = !requestCapability
|
|
76
|
+
? TELEMETRY_DESCRIPTION_HINT
|
|
77
|
+
: description.includes(REQUEST_CAPABILITY_SENTENCE)
|
|
78
|
+
? TELEMETRY_SENTENCE_HINT
|
|
79
|
+
: TELEMETRY_DESCRIPTION_HINT_WITH_REQUEST_CAPABILITY;
|
|
80
|
+
if (fits(`${description}${hint}`)) {
|
|
81
|
+
return `${description}${hint}`;
|
|
82
|
+
}
|
|
83
|
+
if (fits(`${description}${TELEMETRY_SENTENCE_HINT}`)) {
|
|
84
|
+
if (options.toolName !== undefined) {
|
|
85
|
+
warnDescriptionTooLong(options.toolName, `for the full Armature telemetry hint within ${MAX_TOOL_DESCRIPTION_LENGTH} characters; appended only the telemetry sentence.`);
|
|
86
|
+
}
|
|
87
|
+
return `${description}${TELEMETRY_SENTENCE_HINT}`;
|
|
88
|
+
}
|
|
89
|
+
if (options.toolName !== undefined) {
|
|
90
|
+
warnDescriptionTooLong(options.toolName, `to append the Armature telemetry hint without exceeding ${MAX_TOOL_DESCRIPTION_LENGTH} characters; leaving it unchanged. Telemetry is still collected.`);
|
|
91
|
+
}
|
|
92
|
+
return description;
|
|
93
|
+
};
|
|
94
|
+
// The factory can append customer text after the SDK's registered hint. Update
|
|
95
|
+
// that exact block in place when its server does not expose request_capability.
|
|
96
|
+
export const withoutRequestCapabilityHint = (description, toolName, registeredDescription) => {
|
|
97
|
+
if (description !== undefined) {
|
|
98
|
+
// The last paragraph is the SDK suffix, even when an earlier customer
|
|
99
|
+
// paragraph quotes the same instruction. If the factory kept the registered
|
|
100
|
+
// description, anchor to its suffix so later customer quotes also survive.
|
|
101
|
+
const registeredIndex = registeredDescription === undefined
|
|
102
|
+
? -1
|
|
103
|
+
: description.indexOf(registeredDescription);
|
|
104
|
+
const registeredMarker = registeredDescription === undefined
|
|
105
|
+
? undefined
|
|
106
|
+
: RECOGNIZED_HINT_MARKERS.find((marker) => registeredDescription === marker || registeredDescription.endsWith(`\n\n${marker}`));
|
|
107
|
+
let match;
|
|
108
|
+
if (registeredIndex >= 0 && registeredDescription !== undefined) {
|
|
109
|
+
if (registeredMarker !== undefined) {
|
|
110
|
+
match = {
|
|
111
|
+
marker: registeredMarker,
|
|
112
|
+
index: registeredIndex + registeredDescription.length - registeredMarker.length,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
for (const marker of RECOGNIZED_HINT_MARKERS) {
|
|
118
|
+
const paragraph = description.lastIndexOf(`\n\n${marker}`);
|
|
119
|
+
const index = paragraph >= 0 ? paragraph + 2 : description.startsWith(marker) ? 0 : -1;
|
|
120
|
+
// Longer markers precede their shorter prefixes in the list.
|
|
121
|
+
if (index >= 0 && (match === undefined || index > match.index))
|
|
122
|
+
match = { marker, index };
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (match !== undefined) {
|
|
126
|
+
const { marker, index } = match;
|
|
127
|
+
const updated = description.slice(0, index) + TELEMETRY_SENTENCE + description.slice(index + marker.length);
|
|
128
|
+
if (fits(updated))
|
|
129
|
+
return updated;
|
|
130
|
+
if (toolName !== undefined) {
|
|
131
|
+
warnDescriptionTooLong(toolName, `to keep the Armature telemetry hint within ${MAX_TOOL_DESCRIPTION_LENGTH} characters; removed it. Telemetry is still collected.`);
|
|
132
|
+
}
|
|
133
|
+
return index === 0
|
|
134
|
+
? description.slice(marker.length).trimStart()
|
|
135
|
+
: description.slice(0, index - 2) + description.slice(index + marker.length);
|
|
136
|
+
}
|
|
30
137
|
}
|
|
31
|
-
return
|
|
138
|
+
return appendTelemetryHint(description, toolName === undefined ? {} : { toolName });
|
|
32
139
|
};
|
|
33
|
-
export const USER_INTENT_DESCRIPTION = "
|
|
34
|
-
export const
|
|
35
|
-
export const USER_FRUSTRATION_DESCRIPTION = "Frustration
|
|
140
|
+
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.";
|
|
141
|
+
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').";
|
|
142
|
+
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
143
|
// Each telemetry object schema carries the object-level description via
|
|
37
144
|
// `.describe(...)` so it survives zod→JSON-schema conversion in every
|
|
38
145
|
// integration shape — including caller-owned McpServer registration, where no
|
|
@@ -46,7 +153,7 @@ export const USER_FRUSTRATION_DESCRIPTION = "Frustration evident in the user's m
|
|
|
46
153
|
const looseTelemetryInputSchema = z
|
|
47
154
|
.object({
|
|
48
155
|
user_intent: z.string().describe(USER_INTENT_DESCRIPTION).optional(),
|
|
49
|
-
|
|
156
|
+
call_purpose: z.string().describe(CALL_PURPOSE_DESCRIPTION).optional(),
|
|
50
157
|
user_frustration: z
|
|
51
158
|
.string()
|
|
52
159
|
.describe(USER_FRUSTRATION_DESCRIPTION)
|
|
@@ -57,9 +164,9 @@ const looseTelemetryInputSchema = z
|
|
|
57
164
|
const looseTelemetryInputSchemaV4 = zv4
|
|
58
165
|
.looseObject({
|
|
59
166
|
user_intent: zv4.string().describe(USER_INTENT_DESCRIPTION).optional(),
|
|
60
|
-
|
|
167
|
+
call_purpose: zv4
|
|
61
168
|
.string()
|
|
62
|
-
.describe(
|
|
169
|
+
.describe(CALL_PURPOSE_DESCRIPTION)
|
|
63
170
|
.optional(),
|
|
64
171
|
user_frustration: zv4
|
|
65
172
|
.string()
|
|
@@ -131,7 +238,10 @@ const warnTelemetryCollision = (toolName) => {
|
|
|
131
238
|
// prototype patch, Mastra adapter, custom dispatchers) must register and
|
|
132
239
|
// extract with the same plan, so the advertised schema always matches runtime
|
|
133
240
|
// behavior.
|
|
134
|
-
|
|
241
|
+
// `requestCapability` overrides the config-derived answer to "does this
|
|
242
|
+
// server list request_capability?" for adapters whose recorder, not the
|
|
243
|
+
// wrap-time config, is the source of truth (Mastra).
|
|
244
|
+
export const planToolTelemetry = (toolName, inputSchema, config = {}, options = {}) => {
|
|
135
245
|
if (schemaDeclaresTelemetry(inputSchema)) {
|
|
136
246
|
warnTelemetryCollision(toolName);
|
|
137
247
|
return {
|
|
@@ -150,7 +260,10 @@ export const planToolTelemetry = (toolName, inputSchema, config = {}) => {
|
|
|
150
260
|
return {
|
|
151
261
|
mode: "injected",
|
|
152
262
|
inputSchema: decorateInputSchemaWithTelemetry(inputSchema, config),
|
|
153
|
-
applyDescription: appendTelemetryHint,
|
|
263
|
+
applyDescription: (description) => appendTelemetryHint(description, {
|
|
264
|
+
requestCapability: options.requestCapability ?? isRequestCapabilityEnabled(config),
|
|
265
|
+
toolName,
|
|
266
|
+
}),
|
|
154
267
|
};
|
|
155
268
|
};
|
|
156
269
|
// The telemetry object and every field stay optional. In particular,
|
|
@@ -170,9 +283,9 @@ export const createTelemetryJsonSchema = (_config = {}) => {
|
|
|
170
283
|
type: "string",
|
|
171
284
|
description: USER_INTENT_DESCRIPTION,
|
|
172
285
|
},
|
|
173
|
-
|
|
286
|
+
call_purpose: {
|
|
174
287
|
type: "string",
|
|
175
|
-
description:
|
|
288
|
+
description: CALL_PURPOSE_DESCRIPTION,
|
|
176
289
|
},
|
|
177
290
|
user_frustration: {
|
|
178
291
|
type: "string",
|
|
@@ -234,7 +347,8 @@ const firstString = (...values) => {
|
|
|
234
347
|
}
|
|
235
348
|
return undefined;
|
|
236
349
|
};
|
|
237
|
-
//
|
|
350
|
+
// Maps public call_purpose onto the existing agent_thinking storage field.
|
|
351
|
+
// Canonicalizes telemetry onto the storage field names. Legacy spellings
|
|
238
352
|
// (`intent`/`context`/`frustration_level`) still arrive from clients that
|
|
239
353
|
// cached a pre-V1 tool schema and from callers passing telemetry directly to
|
|
240
354
|
// recordToolCall; they lose to an explicit current value when both are present.
|
|
@@ -248,7 +362,7 @@ export const normalizeTelemetryArgs = (telemetry) => {
|
|
|
248
362
|
const userIntent = firstString(telemetry.user_intent, telemetry.intent);
|
|
249
363
|
if (userIntent !== undefined)
|
|
250
364
|
normalized.user_intent = userIntent;
|
|
251
|
-
const agentThinking = firstString(telemetry.agent_thinking, telemetry.context);
|
|
365
|
+
const agentThinking = firstString(telemetry.call_purpose, telemetry.agent_thinking, telemetry.context);
|
|
252
366
|
if (agentThinking !== undefined)
|
|
253
367
|
normalized.agent_thinking = agentThinking;
|
|
254
368
|
const userFrustration = asFrustration(telemetry.user_frustration)
|
|
@@ -277,10 +391,10 @@ export const applyTelemetryFieldMap = (telemetry, args, fieldMap) => {
|
|
|
277
391
|
if (value !== undefined)
|
|
278
392
|
merged.user_intent = value;
|
|
279
393
|
}
|
|
280
|
-
if (merged.agent_thinking === undefined && merged.context === undefined) {
|
|
281
|
-
const value = argString(fieldMap.agent_thinking);
|
|
394
|
+
if (merged.call_purpose === undefined && merged.agent_thinking === undefined && merged.context === undefined) {
|
|
395
|
+
const value = argString(fieldMap.call_purpose) ?? argString(fieldMap.agent_thinking);
|
|
282
396
|
if (value !== undefined)
|
|
283
|
-
merged.
|
|
397
|
+
merged.call_purpose = value;
|
|
284
398
|
}
|
|
285
399
|
if (merged.user_frustration === undefined
|
|
286
400
|
&& 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) => {
|
|
@@ -5,8 +5,12 @@ export declare const buildStatelessSessionId: (clientInfo?: McpClientInfo, sessi
|
|
|
5
5
|
export declare const parseStatelessSessionClientInfo: (sessionId: string | undefined) => McpClientInfo | undefined;
|
|
6
6
|
export declare const clientInfoFromInitializeBody: (body: unknown) => McpClientInfo | undefined;
|
|
7
7
|
export type StatelessHttpSession = {
|
|
8
|
-
/**
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Stable session id: minted at initialize, parsed from the echoed header
|
|
10
|
+
* otherwise. Undefined when the request carries neither — see
|
|
11
|
+
* `resolveStatelessHttpSession`.
|
|
12
|
+
*/
|
|
13
|
+
sessionId?: string;
|
|
10
14
|
/** Client identity recovered from the echoed session id (tool-call requests only). */
|
|
11
15
|
clientInfo?: McpClientInfo;
|
|
12
16
|
/**
|
|
@@ -17,10 +21,20 @@ export type StatelessHttpSession = {
|
|
|
17
21
|
sessionIdGenerator?: () => string;
|
|
18
22
|
/** True when the request (or any message in a batch) is `initialize`. */
|
|
19
23
|
isInitialize: boolean;
|
|
20
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* Spread into `dispatch(name, args, { ctx, ...dispatchContext })`.
|
|
26
|
+
*
|
|
27
|
+
* When there is no session id the request headers ride along instead. The
|
|
28
|
+
* recorder reads "no session id AND no headers" as "no HTTP request at
|
|
29
|
+
* all" and falls back to the process-scoped stdio id, which would merge
|
|
30
|
+
* every concurrent conversation in a long-lived server into one session.
|
|
31
|
+
* Passing the headers keeps the request identifiable as HTTP-with-no-id,
|
|
32
|
+
* so the event ships a null hint and ingest groups it.
|
|
33
|
+
*/
|
|
21
34
|
dispatchContext: {
|
|
22
|
-
sessionId
|
|
35
|
+
sessionId?: string;
|
|
23
36
|
clientInfo?: McpClientInfo;
|
|
37
|
+
headers?: HeaderBag;
|
|
24
38
|
};
|
|
25
39
|
};
|
|
26
40
|
export declare const resolveStatelessHttpSession: (input: {
|
|
@@ -107,8 +107,20 @@ export const resolveStatelessHttpSession = (input) => {
|
|
|
107
107
|
dispatchContext: { sessionId },
|
|
108
108
|
};
|
|
109
109
|
}
|
|
110
|
-
const
|
|
111
|
-
|
|
110
|
+
const sessionId = headerValue(input.headers, "mcp-session-id")?.trim();
|
|
111
|
+
if (!sessionId) {
|
|
112
|
+
// No initialize to mint from and no echo to recover: either modern-era
|
|
113
|
+
// traffic (identified through `_meta` / the session seed) or a client
|
|
114
|
+
// that drops the header. Minting here would stamp a distinct id on every
|
|
115
|
+
// POST, and ingest trusts an explicit hint, so each tool call would land
|
|
116
|
+
// in its own single-event session. Reporting no id ships
|
|
117
|
+
// `session_id_hint: null` and lets ingest group by actor + client with
|
|
118
|
+
// its inactivity window instead. Matches the Go SDK's empty session id.
|
|
119
|
+
return {
|
|
120
|
+
isInitialize: false,
|
|
121
|
+
dispatchContext: input.headers ? { headers: input.headers } : {},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
112
124
|
const clientInfo = parseStatelessSessionClientInfo(sessionId);
|
|
113
125
|
return {
|
|
114
126
|
sessionId,
|
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),
|
|
@@ -589,7 +589,10 @@ export const wrapMcpHandler = (handler, config) => {
|
|
|
589
589
|
}
|
|
590
590
|
}
|
|
591
591
|
const response = await originalFetch(request, ...rest);
|
|
592
|
-
|
|
592
|
+
// `sessionId` is always minted on initialize; the check narrows the type
|
|
593
|
+
// for the non-initialize case, where it is now optional.
|
|
594
|
+
if (session?.isInitialize && session.sessionId && response.ok) {
|
|
595
|
+
const mintedSessionId = session.sessionId;
|
|
593
596
|
// Full-fidelity clientInfo from the initialize body: the minted id only
|
|
594
597
|
// carries name/version, so without this the session_init would lose the
|
|
595
598
|
// negotiated protocolVersion (and capabilities) — recorded here even
|
|
@@ -598,12 +601,12 @@ export const wrapMcpHandler = (handler, config) => {
|
|
|
598
601
|
const clientInfo = clientInfoFromInitializeBody(body);
|
|
599
602
|
try {
|
|
600
603
|
await state.core.recordSessionInit({
|
|
601
|
-
sessionId:
|
|
604
|
+
sessionId: mintedSessionId,
|
|
602
605
|
headers: request.headers,
|
|
603
606
|
...(clientInfo !== undefined ? { clientInfo } : {}),
|
|
604
607
|
...(authInfo !== undefined ? { authInfo } : {}),
|
|
605
608
|
extra: {
|
|
606
|
-
sessionId:
|
|
609
|
+
sessionId: mintedSessionId,
|
|
607
610
|
requestInfo: { headers: request.headers },
|
|
608
611
|
...(authInfo !== undefined ? { authInfo } : {}),
|
|
609
612
|
},
|
|
@@ -612,7 +615,7 @@ export const wrapMcpHandler = (handler, config) => {
|
|
|
612
615
|
catch {
|
|
613
616
|
// Recording is best-effort; never break the MCP response over it.
|
|
614
617
|
}
|
|
615
|
-
return withSessionIdHeader(response,
|
|
618
|
+
return withSessionIdHeader(response, mintedSessionId);
|
|
616
619
|
}
|
|
617
620
|
// Echo an id the legacy client is already carrying, so it keeps carrying it.
|
|
618
621
|
const echoed = request.headers.get(SESSION_ID_HEADER)?.trim();
|