@armature-tech/mcp-analytics 0.4.4 → 0.6.0
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 +5 -5
- package/SKILL.md +40 -4
- package/dist/cjs/client-info-cache.d.ts +8 -0
- package/dist/cjs/client-info-cache.js +107 -0
- package/dist/cjs/emit.d.ts +0 -3
- package/dist/cjs/emit.js +4 -3
- package/dist/cjs/events.js +1 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/instrument-tools.d.ts +26 -0
- package/dist/cjs/instrument-tools.js +23 -0
- package/dist/cjs/mastra.d.ts +6 -5
- package/dist/cjs/mastra.js +99 -2
- package/dist/cjs/recorder.js +12 -2
- package/dist/cjs/schema.d.ts +10 -10
- package/dist/cjs/schema.js +5 -2
- package/dist/cjs/server.js +108 -39
- package/dist/cjs/types.d.ts +7 -3
- package/dist/esm/client-info-cache.d.ts +8 -0
- package/dist/esm/client-info-cache.js +97 -0
- package/dist/esm/emit.d.ts +0 -3
- package/dist/esm/emit.js +4 -3
- package/dist/esm/events.js +1 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/instrument-tools.d.ts +26 -0
- package/dist/esm/instrument-tools.js +20 -0
- package/dist/esm/mastra.d.ts +6 -5
- package/dist/esm/mastra.js +97 -1
- package/dist/esm/recorder.js +12 -2
- package/dist/esm/schema.d.ts +10 -10
- package/dist/esm/schema.js +5 -2
- package/dist/esm/server.js +108 -39
- package/dist/esm/types.d.ts +7 -3
- package/package.json +5 -2
package/dist/esm/schema.js
CHANGED
|
@@ -50,15 +50,18 @@ const isZodV3ObjectSchema = (value) => {
|
|
|
50
50
|
"shape" in value &&
|
|
51
51
|
typeof value.extend === "function");
|
|
52
52
|
};
|
|
53
|
+
// Loose mode wraps the telemetry object with `.optional()` so callers that omit
|
|
54
|
+
// the `telemetry` key entirely still parse. Strict mode keeps the object itself
|
|
55
|
+
// required (and `intent` required inside).
|
|
53
56
|
export const createTelemetryInputSchema = (config = {}) => {
|
|
54
57
|
return config.telemetry?.intent === "required"
|
|
55
58
|
? strictTelemetryInputSchema
|
|
56
|
-
: looseTelemetryInputSchema;
|
|
59
|
+
: looseTelemetryInputSchema.optional();
|
|
57
60
|
};
|
|
58
61
|
const createTelemetryInputSchemaV4 = (config) => {
|
|
59
62
|
return config.telemetry?.intent === "required"
|
|
60
63
|
? strictTelemetryInputSchemaV4
|
|
61
|
-
: looseTelemetryInputSchemaV4;
|
|
64
|
+
: looseTelemetryInputSchemaV4.optional();
|
|
62
65
|
};
|
|
63
66
|
export const createTelemetryJsonSchema = (config = {}) => {
|
|
64
67
|
const strict = config.telemetry?.intent === "required";
|
package/dist/esm/server.js
CHANGED
|
@@ -4,65 +4,134 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
|
4
4
|
import { createAnalyticsRecorder } from "./recorder.js";
|
|
5
5
|
import { decorateInputSchemaWithTelemetry } from "./schema.js";
|
|
6
6
|
import { defaultMcpAnalyticsConfig } from "./emit.js";
|
|
7
|
+
import { isRecord } from "./utils.js";
|
|
7
8
|
const withAnalyticsStorage = new AsyncLocalStorage();
|
|
8
9
|
let prototypePatchInstalled = false;
|
|
10
|
+
// Mirrors the SDK's `isZodTypeLike`/`isZodRawShapeCompat` checks closely enough
|
|
11
|
+
// to disambiguate the overloads of `server.tool(...)`: a raw shape is a plain
|
|
12
|
+
// object whose values include at least one Zod schema, while a Zod schema
|
|
13
|
+
// instance carries `_def` (v3) or `_zod` (v4).
|
|
14
|
+
const isZodSchemaLike = (value) => {
|
|
15
|
+
if (!isRecord(value))
|
|
16
|
+
return false;
|
|
17
|
+
return "_def" in value || "_zod" in value;
|
|
18
|
+
};
|
|
19
|
+
const isZodRawShapeLike = (value) => {
|
|
20
|
+
if (!isRecord(value))
|
|
21
|
+
return false;
|
|
22
|
+
if (isZodSchemaLike(value))
|
|
23
|
+
return false;
|
|
24
|
+
if (Object.keys(value).length === 0)
|
|
25
|
+
return true;
|
|
26
|
+
return Object.values(value).some(isZodSchemaLike);
|
|
27
|
+
};
|
|
28
|
+
// Mirrors @modelcontextprotocol/sdk/server/mcp.js `tool(name, ...rest)` argument
|
|
29
|
+
// parsing so we can normalise every overload through our patched `registerTool`.
|
|
30
|
+
const parseDeprecatedToolArgs = (rest) => {
|
|
31
|
+
const args = [...rest];
|
|
32
|
+
let description;
|
|
33
|
+
let inputSchema;
|
|
34
|
+
let annotations;
|
|
35
|
+
if (typeof args[0] === "string") {
|
|
36
|
+
description = args.shift();
|
|
37
|
+
}
|
|
38
|
+
if (args.length > 1) {
|
|
39
|
+
const firstArg = args[0];
|
|
40
|
+
if (isZodRawShapeLike(firstArg)) {
|
|
41
|
+
inputSchema = args.shift();
|
|
42
|
+
if (args.length > 1 &&
|
|
43
|
+
isRecord(args[0]) &&
|
|
44
|
+
!isZodRawShapeLike(args[0])) {
|
|
45
|
+
annotations = args.shift();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else if (isRecord(firstArg)) {
|
|
49
|
+
annotations = args.shift();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const callback = args[0];
|
|
53
|
+
return { description, inputSchema, annotations, callback };
|
|
54
|
+
};
|
|
55
|
+
const wrapCallbackWithAnalytics = (name, cb, originalHasInputSchema, ctx) => {
|
|
56
|
+
const { recorder } = ctx;
|
|
57
|
+
return async (argsOrExtra, maybeExtra) => {
|
|
58
|
+
const startedAtMs = Date.now();
|
|
59
|
+
const startedAt = new Date(startedAtMs).toISOString();
|
|
60
|
+
const requestId = randomUUID();
|
|
61
|
+
const extra = (originalHasInputSchema ? maybeExtra : argsOrExtra);
|
|
62
|
+
const { args, telemetry } = recorder.extractTelemetry(argsOrExtra);
|
|
63
|
+
try {
|
|
64
|
+
const output = originalHasInputSchema
|
|
65
|
+
? await cb(args, maybeExtra)
|
|
66
|
+
: await cb(maybeExtra ?? argsOrExtra);
|
|
67
|
+
await recorder.recordToolCall({
|
|
68
|
+
name,
|
|
69
|
+
args,
|
|
70
|
+
telemetry,
|
|
71
|
+
extra,
|
|
72
|
+
requestId,
|
|
73
|
+
startedAt,
|
|
74
|
+
durationMs: Date.now() - startedAtMs,
|
|
75
|
+
status: "ok",
|
|
76
|
+
result: output,
|
|
77
|
+
});
|
|
78
|
+
return output;
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
await recorder.recordToolCall({
|
|
82
|
+
name,
|
|
83
|
+
args,
|
|
84
|
+
telemetry,
|
|
85
|
+
extra,
|
|
86
|
+
requestId,
|
|
87
|
+
startedAt,
|
|
88
|
+
durationMs: Date.now() - startedAtMs,
|
|
89
|
+
status: "error",
|
|
90
|
+
error,
|
|
91
|
+
});
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
};
|
|
9
96
|
const installPrototypePatchOnce = () => {
|
|
10
97
|
if (prototypePatchInstalled)
|
|
11
98
|
return;
|
|
12
99
|
prototypePatchInstalled = true;
|
|
13
100
|
const prototype = McpServer.prototype;
|
|
14
101
|
const originalRegisterTool = prototype.registerTool;
|
|
102
|
+
const originalTool = prototype.tool;
|
|
15
103
|
prototype.registerTool = function patchedRegisterTool(name, toolConfig, cb) {
|
|
16
104
|
const ctx = withAnalyticsStorage.getStore();
|
|
17
105
|
if (!ctx) {
|
|
18
106
|
return originalRegisterTool.call(this, name, toolConfig, cb);
|
|
19
107
|
}
|
|
20
|
-
const { config
|
|
108
|
+
const { config } = ctx;
|
|
21
109
|
const originalHasInputSchema = toolConfig.inputSchema !== undefined;
|
|
22
110
|
const instrumentedConfig = {
|
|
23
111
|
...toolConfig,
|
|
24
112
|
inputSchema: decorateInputSchemaWithTelemetry(toolConfig.inputSchema, config),
|
|
25
113
|
};
|
|
26
|
-
const wrappedCallback =
|
|
27
|
-
const startedAtMs = Date.now();
|
|
28
|
-
const startedAt = new Date(startedAtMs).toISOString();
|
|
29
|
-
const requestId = randomUUID();
|
|
30
|
-
const extra = (originalHasInputSchema ? maybeExtra : argsOrExtra);
|
|
31
|
-
const { args, telemetry } = recorder.extractTelemetry(argsOrExtra);
|
|
32
|
-
try {
|
|
33
|
-
const output = originalHasInputSchema
|
|
34
|
-
? await cb(args, maybeExtra)
|
|
35
|
-
: await cb(maybeExtra ?? argsOrExtra);
|
|
36
|
-
await recorder.recordToolCall({
|
|
37
|
-
name,
|
|
38
|
-
args,
|
|
39
|
-
telemetry,
|
|
40
|
-
extra,
|
|
41
|
-
requestId,
|
|
42
|
-
startedAt,
|
|
43
|
-
durationMs: Date.now() - startedAtMs,
|
|
44
|
-
status: "ok",
|
|
45
|
-
result: output,
|
|
46
|
-
});
|
|
47
|
-
return output;
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
await recorder.recordToolCall({
|
|
51
|
-
name,
|
|
52
|
-
args,
|
|
53
|
-
telemetry,
|
|
54
|
-
extra,
|
|
55
|
-
requestId,
|
|
56
|
-
startedAt,
|
|
57
|
-
durationMs: Date.now() - startedAtMs,
|
|
58
|
-
status: "error",
|
|
59
|
-
error,
|
|
60
|
-
});
|
|
61
|
-
throw error;
|
|
62
|
-
}
|
|
63
|
-
};
|
|
114
|
+
const wrappedCallback = wrapCallbackWithAnalytics(name, cb, originalHasInputSchema, ctx);
|
|
64
115
|
return originalRegisterTool.call(this, name, instrumentedConfig, wrappedCallback);
|
|
65
116
|
};
|
|
117
|
+
// PRIA and other older codebases still use the deprecated `server.tool(...)`
|
|
118
|
+
// overloads. Patch them too — we normalise into our patched `registerTool`
|
|
119
|
+
// so decoration and wrapping have a single code path.
|
|
120
|
+
prototype.tool = function patchedTool(name, ...rest) {
|
|
121
|
+
const ctx = withAnalyticsStorage.getStore();
|
|
122
|
+
if (!ctx) {
|
|
123
|
+
return originalTool.call(this, name, ...rest);
|
|
124
|
+
}
|
|
125
|
+
const { description, inputSchema, annotations, callback } = parseDeprecatedToolArgs(rest);
|
|
126
|
+
const config = {};
|
|
127
|
+
if (description !== undefined)
|
|
128
|
+
config.description = description;
|
|
129
|
+
if (inputSchema !== undefined)
|
|
130
|
+
config.inputSchema = inputSchema;
|
|
131
|
+
if (annotations !== undefined)
|
|
132
|
+
config.annotations = annotations;
|
|
133
|
+
return this.registerTool(name, config, callback);
|
|
134
|
+
};
|
|
66
135
|
};
|
|
67
136
|
export const withMcpAnalytics = (config, createServer) => {
|
|
68
137
|
const recorder = createAnalyticsRecorder(config);
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -23,6 +23,8 @@ export type RequestExtra = {
|
|
|
23
23
|
authInfo?: {
|
|
24
24
|
token?: string;
|
|
25
25
|
clientId?: string;
|
|
26
|
+
apiKey?: string;
|
|
27
|
+
principalId?: string;
|
|
26
28
|
};
|
|
27
29
|
requestInfo?: {
|
|
28
30
|
headers?: HeaderBag;
|
|
@@ -38,9 +40,6 @@ export type ActorIdResolverInput = {
|
|
|
38
40
|
};
|
|
39
41
|
export type ActorIdResolver = (input: ActorIdResolverInput) => string | Promise<string>;
|
|
40
42
|
export type McpAnalyticsConfig = {
|
|
41
|
-
telemetry?: {
|
|
42
|
-
intent?: "required" | "optional";
|
|
43
|
-
};
|
|
44
43
|
armature?: {
|
|
45
44
|
endpointUrl?: string;
|
|
46
45
|
apiKey?: string;
|
|
@@ -52,6 +51,11 @@ export type McpAnalyticsConfig = {
|
|
|
52
51
|
timeoutMs?: number;
|
|
53
52
|
};
|
|
54
53
|
};
|
|
54
|
+
export type InternalMcpAnalyticsConfig = McpAnalyticsConfig & {
|
|
55
|
+
telemetry?: {
|
|
56
|
+
intent?: "required" | "optional";
|
|
57
|
+
};
|
|
58
|
+
};
|
|
55
59
|
export type TelemetryArgs = {
|
|
56
60
|
intent?: string;
|
|
57
61
|
context?: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@armature-tech/mcp-analytics",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "MCP analytics wrapper SDK that instruments MCP tool declarations with telemetry.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -76,10 +76,13 @@
|
|
|
76
76
|
"registry": "https://registry.npmjs.org"
|
|
77
77
|
},
|
|
78
78
|
"dependencies": {
|
|
79
|
-
"@modelcontextprotocol/sdk": "^1.20.0",
|
|
80
79
|
"zod": "^3.25.76"
|
|
81
80
|
},
|
|
81
|
+
"peerDependencies": {
|
|
82
|
+
"@modelcontextprotocol/sdk": "^1.20.0"
|
|
83
|
+
},
|
|
82
84
|
"devDependencies": {
|
|
85
|
+
"@modelcontextprotocol/sdk": "^1.20.0",
|
|
83
86
|
"@types/node": "^22.10.2",
|
|
84
87
|
"tsx": "^4.19.2",
|
|
85
88
|
"typescript": "^5.7.2"
|