@armature-tech/mcp-analytics 0.5.1 → 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 +1 -0
- package/SKILL.md +40 -4
- package/dist/cjs/emit.js +4 -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 +5 -4
- package/dist/cjs/mastra.js +99 -2
- package/dist/cjs/types.d.ts +2 -0
- package/dist/esm/emit.js +4 -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 +5 -4
- package/dist/esm/mastra.js +97 -1
- package/dist/esm/types.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -63,6 +63,7 @@ Three things happen on every tool call:
|
|
|
63
63
|
|
|
64
64
|
`createMcpAnalyticsServer` covers most repos. If yours doesn't fit, there are three other entry points — the [agent skill](SKILL.md) picks the right one automatically:
|
|
65
65
|
|
|
66
|
+
- **Concrete-server registry helper** — `instrumentMcpServerTools({ server, tools, config, mapTool })`. Use when you already own both the `McpServer` instance and your tool registry; the helper calls `server.registerTool(...)` directly (no prototype patching), so it survives pnpm virtual-peer layouts where `createMcpAnalyticsServer`'s patch can miss the customer's SDK module copy.
|
|
66
67
|
- **Registry-style** — `createAnalyticsRecorder()` + `analytics.tool(...)` + `analytics.createMcpServer(...)`. Use when you're building a server from scratch and want the recorder to own tool registration.
|
|
67
68
|
- **Dispatcher-style** — same recorder, but you call `analytics.toolDefinitions()` from your `tools/list` handler and `analytics.dispatch(name, args, ctx)` from `tools/call`. For servers that hand-roll the JSON-RPC layer.
|
|
68
69
|
- **Mastra** — `wrapMastraTools(tools, config)` from `@armature-tech/mcp-analytics/mastra`. Drop the wrapped map into `new MCPServer({ tools })`.
|
package/SKILL.md
CHANGED
|
@@ -135,6 +135,36 @@ If you need `recorder.flush()` (serverless), use `withMcpAnalytics` instead and
|
|
|
135
135
|
`await recorder.flush()` at the end of the request handler. Don't sprinkle `flush()` calls
|
|
136
136
|
through tool handlers — once per request, at the end, is enough.
|
|
137
137
|
|
|
138
|
+
#### Shape A alternative — `instrumentMcpServerTools`
|
|
139
|
+
|
|
140
|
+
Use when the customer **already owns both the `McpServer` instance and a tool registry**
|
|
141
|
+
(array or map of definitions), and either: the project is pnpm with virtual peer
|
|
142
|
+
hoisting (where prototype patching can target the wrong `@modelcontextprotocol/sdk`
|
|
143
|
+
module copy), or refactoring the construction site into a factory function for
|
|
144
|
+
`createMcpAnalyticsServer` is awkward. No prototype patching — the helper calls
|
|
145
|
+
`server.registerTool(...)` directly on the instance the caller passed in.
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
import { instrumentMcpServerTools } from "@armature-tech/mcp-analytics";
|
|
149
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
150
|
+
|
|
151
|
+
const { server, recorder } = instrumentMcpServerTools({
|
|
152
|
+
server: new McpServer(serverInfo),
|
|
153
|
+
tools: myExistingRegistry,
|
|
154
|
+
config: { armature: { delivery: "await" } },
|
|
155
|
+
// Only needed if your registry shape differs from `InstrumentedTool` —
|
|
156
|
+
// i.e. the entries aren't already `{ name, description?, inputSchema?, handler }`.
|
|
157
|
+
mapTool: (def) => ({
|
|
158
|
+
name: def.name,
|
|
159
|
+
description: def.description,
|
|
160
|
+
inputSchema: def.inputSchema,
|
|
161
|
+
handler: (args, context) => def.handler(args, context.extra),
|
|
162
|
+
}),
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
`recorder` is returned for `await recorder.flush()` on serverless / shutdown.
|
|
167
|
+
|
|
138
168
|
### Shape B — Registry-style
|
|
139
169
|
|
|
140
170
|
The recorder owns the tool registry. Define tools on the recorder, then ask it to build
|
|
@@ -240,20 +270,26 @@ new MCPServer({ tools: analytics.wrapTools(createMyTools()), ... });
|
|
|
240
270
|
process.on("SIGTERM", () => analytics.flush());
|
|
241
271
|
```
|
|
242
272
|
|
|
243
|
-
|
|
244
|
-
`
|
|
245
|
-
|
|
273
|
+
By default the adapter pulls `sessionId`, `requestId`, `requestInfo.headers`, and
|
|
274
|
+
`authInfo` straight out of Mastra's standard MCP context — `context.mcp.extra` (with
|
|
275
|
+
`context.requestContext.get("mcp.extra")` as a fallback). No `resolveExtra` glue
|
|
276
|
+
required for tools invoked over MCP. Pass a `resolveExtra` only when you need to
|
|
277
|
+
override or add fields (e.g. servers that store auth elsewhere); the values you return
|
|
278
|
+
are merged on top of the default extraction:
|
|
246
279
|
|
|
247
280
|
```ts
|
|
248
281
|
wrapMastraTools(tools, {
|
|
249
282
|
armature: { delivery: "await" },
|
|
250
283
|
resolveExtra: (mastraContext) => ({
|
|
251
284
|
sessionId: (mastraContext as any)?.runtimeContext?.get?.("sessionId"),
|
|
252
|
-
authInfo: (mastraContext as any)?.requestContext?.authInfo,
|
|
253
285
|
}),
|
|
254
286
|
});
|
|
255
287
|
```
|
|
256
288
|
|
|
289
|
+
The SDK also recognises `authInfo.apiKey` and `authInfo.principalId` as actor-seed
|
|
290
|
+
aliases (alongside `authInfo.token` and `authInfo.clientId`), so Mastra setups that
|
|
291
|
+
expose auth under those names don't need a custom `actorId` resolver.
|
|
292
|
+
|
|
257
293
|
`actorId` is configured the normal way on `config.armature.actorId` — the resolver
|
|
258
294
|
receives `ctx` set to Mastra's second-arg context.
|
|
259
295
|
|
package/dist/cjs/emit.js
CHANGED
|
@@ -30,6 +30,10 @@ const resolveActorSeed = async (config, input) => {
|
|
|
30
30
|
return input.authInfo.token;
|
|
31
31
|
if (input.authInfo?.clientId)
|
|
32
32
|
return input.authInfo.clientId;
|
|
33
|
+
if (input.authInfo?.apiKey)
|
|
34
|
+
return input.authInfo.apiKey;
|
|
35
|
+
if (input.authInfo?.principalId)
|
|
36
|
+
return input.authInfo.principalId;
|
|
33
37
|
const authorization = (0, utils_js_1.headerValue)(input.headers, "authorization");
|
|
34
38
|
if (authorization)
|
|
35
39
|
return authorization;
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -4,3 +4,4 @@ export { buildActorId, buildEventId, buildSessionInitEvent, buildToolCallEvent,
|
|
|
4
4
|
export { defaultMcpAnalyticsConfig, emitTelemetryEvent, postTelemetryEvent, } from "./emit.js";
|
|
5
5
|
export { createAnalyticsRecorder } from "./recorder.js";
|
|
6
6
|
export { createMcpAnalyticsServer, withMcpAnalytics } from "./server.js";
|
|
7
|
+
export { instrumentMcpServerTools, type InstrumentMcpServerToolsOptions, type InstrumentMcpServerToolsResult, type InstrumentedTool, } from "./instrument-tools.js";
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.withMcpAnalytics = exports.createMcpAnalyticsServer = exports.createAnalyticsRecorder = exports.postTelemetryEvent = exports.emitTelemetryEvent = exports.defaultMcpAnalyticsConfig = exports.normalizeSessionId = exports.buildToolCallEvent = exports.buildSessionInitEvent = exports.buildEventId = exports.buildActorId = exports.extractTelemetryArguments = exports.decorateInputSchemaWithTelemetry = exports.createTelemetryJsonSchema = exports.createTelemetryInputSchema = void 0;
|
|
3
|
+
exports.instrumentMcpServerTools = exports.withMcpAnalytics = exports.createMcpAnalyticsServer = exports.createAnalyticsRecorder = exports.postTelemetryEvent = exports.emitTelemetryEvent = exports.defaultMcpAnalyticsConfig = exports.normalizeSessionId = exports.buildToolCallEvent = exports.buildSessionInitEvent = exports.buildEventId = exports.buildActorId = exports.extractTelemetryArguments = exports.decorateInputSchemaWithTelemetry = exports.createTelemetryJsonSchema = exports.createTelemetryInputSchema = void 0;
|
|
4
4
|
var schema_js_1 = require("./schema.js");
|
|
5
5
|
Object.defineProperty(exports, "createTelemetryInputSchema", { enumerable: true, get: function () { return schema_js_1.createTelemetryInputSchema; } });
|
|
6
6
|
Object.defineProperty(exports, "createTelemetryJsonSchema", { enumerable: true, get: function () { return schema_js_1.createTelemetryJsonSchema; } });
|
|
@@ -21,3 +21,5 @@ Object.defineProperty(exports, "createAnalyticsRecorder", { enumerable: true, ge
|
|
|
21
21
|
var server_js_1 = require("./server.js");
|
|
22
22
|
Object.defineProperty(exports, "createMcpAnalyticsServer", { enumerable: true, get: function () { return server_js_1.createMcpAnalyticsServer; } });
|
|
23
23
|
Object.defineProperty(exports, "withMcpAnalytics", { enumerable: true, get: function () { return server_js_1.withMcpAnalytics; } });
|
|
24
|
+
var instrument_tools_js_1 = require("./instrument-tools.js");
|
|
25
|
+
Object.defineProperty(exports, "instrumentMcpServerTools", { enumerable: true, get: function () { return instrument_tools_js_1.instrumentMcpServerTools; } });
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import type { AnalyticsRecorder, McpAnalyticsConfig, RegisteredToolHandler, ToolRegistration } from "./types.js";
|
|
3
|
+
export type InstrumentedTool = ToolRegistration & {
|
|
4
|
+
handler: RegisteredToolHandler<unknown, unknown>;
|
|
5
|
+
};
|
|
6
|
+
type BaseInstrumentOptions = {
|
|
7
|
+
server: McpServer;
|
|
8
|
+
config?: McpAnalyticsConfig;
|
|
9
|
+
};
|
|
10
|
+
export type InstrumentMcpServerToolsOptions<TDef> = BaseInstrumentOptions & {
|
|
11
|
+
tools: TDef[] | Record<string, TDef>;
|
|
12
|
+
mapTool?: (def: TDef, key?: string) => InstrumentedTool;
|
|
13
|
+
};
|
|
14
|
+
export type InstrumentMcpServerToolsResult = {
|
|
15
|
+
server: McpServer;
|
|
16
|
+
recorder: AnalyticsRecorder;
|
|
17
|
+
};
|
|
18
|
+
export declare function instrumentMcpServerTools(options: BaseInstrumentOptions & {
|
|
19
|
+
tools: InstrumentedTool[] | Record<string, InstrumentedTool>;
|
|
20
|
+
mapTool?: (def: InstrumentedTool, key?: string) => InstrumentedTool;
|
|
21
|
+
}): InstrumentMcpServerToolsResult;
|
|
22
|
+
export declare function instrumentMcpServerTools<TDef>(options: BaseInstrumentOptions & {
|
|
23
|
+
tools: TDef[] | Record<string, TDef>;
|
|
24
|
+
mapTool: (def: TDef, key?: string) => InstrumentedTool;
|
|
25
|
+
}): InstrumentMcpServerToolsResult;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.instrumentMcpServerTools = instrumentMcpServerTools;
|
|
4
|
+
const recorder_js_1 = require("./recorder.js");
|
|
5
|
+
function instrumentMcpServerTools(options) {
|
|
6
|
+
const recorder = (0, recorder_js_1.createAnalyticsRecorder)(options.config);
|
|
7
|
+
recorder.attachToMcpServer(options.server);
|
|
8
|
+
const identity = (def) => def;
|
|
9
|
+
const mapper = options.mapTool ?? identity;
|
|
10
|
+
if (Array.isArray(options.tools)) {
|
|
11
|
+
for (const def of options.tools) {
|
|
12
|
+
const mapped = mapper(def, undefined);
|
|
13
|
+
recorder.tool(mapped, mapped.handler);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
for (const [key, def] of Object.entries(options.tools)) {
|
|
18
|
+
const mapped = mapper(def, key);
|
|
19
|
+
recorder.tool(mapped, mapped.handler);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return { server: options.server, recorder };
|
|
23
|
+
}
|
package/dist/cjs/mastra.d.ts
CHANGED
|
@@ -12,13 +12,14 @@ export type MastraToolMap = Record<string, MastraTool>;
|
|
|
12
12
|
export type MastraAdapterOptions = McpAnalyticsConfig & {
|
|
13
13
|
resolveExtra?: (mastraContext: unknown) => RequestExtra | undefined;
|
|
14
14
|
};
|
|
15
|
-
export declare const
|
|
15
|
+
export declare const defaultMastraResolveExtra: (mastraContext: unknown) => RequestExtra | undefined;
|
|
16
|
+
export declare const wrapMastraToolsWithRecorder: <T extends MastraToolMap>(tools: T, recorder: AnalyticsRecorder, config?: InternalMcpAnalyticsConfig, options?: {
|
|
16
17
|
resolveExtra?: MastraAdapterOptions["resolveExtra"];
|
|
17
|
-
}) =>
|
|
18
|
-
export declare const wrapMastraTools: (tools:
|
|
18
|
+
}) => T;
|
|
19
|
+
export declare const wrapMastraTools: <T extends MastraToolMap>(tools: T, options?: MastraAdapterOptions) => T;
|
|
19
20
|
export type MastraAnalytics = {
|
|
20
21
|
recorder: AnalyticsRecorder;
|
|
21
|
-
wrapTools: (tools:
|
|
22
|
+
wrapTools: <T extends MastraToolMap>(tools: T) => T;
|
|
22
23
|
flush: () => Promise<void>;
|
|
23
24
|
};
|
|
24
25
|
export declare const createMastraAnalytics: (options?: MastraAdapterOptions) => MastraAnalytics;
|
package/dist/cjs/mastra.js
CHANGED
|
@@ -1,8 +1,93 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createMastraAnalytics = exports.wrapMastraTools = exports.wrapMastraToolsWithRecorder = void 0;
|
|
3
|
+
exports.createMastraAnalytics = exports.wrapMastraTools = exports.wrapMastraToolsWithRecorder = exports.defaultMastraResolveExtra = void 0;
|
|
4
4
|
const recorder_js_1 = require("./recorder.js");
|
|
5
5
|
const schema_js_1 = require("./schema.js");
|
|
6
|
+
const utils_js_1 = require("./utils.js");
|
|
7
|
+
const readMcpExtra = (mastraContext) => {
|
|
8
|
+
if (!(0, utils_js_1.isRecord)(mastraContext))
|
|
9
|
+
return undefined;
|
|
10
|
+
// Direct path: context.mcp.extra (the second argument Mastra's MCPServer
|
|
11
|
+
// passes into `execute` for tools invoked over MCP).
|
|
12
|
+
const mcp = mastraContext.mcp;
|
|
13
|
+
if ((0, utils_js_1.isRecord)(mcp) && (0, utils_js_1.isRecord)(mcp.extra))
|
|
14
|
+
return mcp.extra;
|
|
15
|
+
// Fallback: context.requestContext.get("mcp.extra") — some Mastra setups
|
|
16
|
+
// route the MCP extra through RuntimeContext under that key.
|
|
17
|
+
const requestContext = mastraContext.requestContext;
|
|
18
|
+
if ((0, utils_js_1.isRecord)(requestContext) &&
|
|
19
|
+
typeof requestContext.get === "function") {
|
|
20
|
+
const fromContext = requestContext.get("mcp.extra");
|
|
21
|
+
if ((0, utils_js_1.isRecord)(fromContext))
|
|
22
|
+
return fromContext;
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
25
|
+
};
|
|
26
|
+
const defaultMastraResolveExtra = (mastraContext) => {
|
|
27
|
+
const mcpExtra = readMcpExtra(mastraContext);
|
|
28
|
+
if (!mcpExtra)
|
|
29
|
+
return undefined;
|
|
30
|
+
const result = {};
|
|
31
|
+
if (typeof mcpExtra.sessionId === "string" && mcpExtra.sessionId.length > 0) {
|
|
32
|
+
result.sessionId = mcpExtra.sessionId;
|
|
33
|
+
}
|
|
34
|
+
const requestId = mcpExtra.requestId;
|
|
35
|
+
if (typeof requestId === "string" || typeof requestId === "number") {
|
|
36
|
+
result.requestId = requestId;
|
|
37
|
+
}
|
|
38
|
+
if ((0, utils_js_1.isRecord)(mcpExtra.requestInfo)) {
|
|
39
|
+
const headers = mcpExtra.requestInfo.headers;
|
|
40
|
+
if (headers !== undefined) {
|
|
41
|
+
result.requestInfo = { headers: headers };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if ((0, utils_js_1.isRecord)(mcpExtra.authInfo)) {
|
|
45
|
+
// Narrow to the four fields `resolveActorSeed` / `buildSessionInitEvent`
|
|
46
|
+
// actually consume — don't forward arbitrary unrelated properties from a
|
|
47
|
+
// host-supplied object down the analytics pipeline.
|
|
48
|
+
const incoming = mcpExtra.authInfo;
|
|
49
|
+
const authInfo = {};
|
|
50
|
+
if (typeof incoming.token === "string")
|
|
51
|
+
authInfo.token = incoming.token;
|
|
52
|
+
if (typeof incoming.clientId === "string")
|
|
53
|
+
authInfo.clientId = incoming.clientId;
|
|
54
|
+
if (typeof incoming.apiKey === "string")
|
|
55
|
+
authInfo.apiKey = incoming.apiKey;
|
|
56
|
+
if (typeof incoming.principalId === "string") {
|
|
57
|
+
authInfo.principalId = incoming.principalId;
|
|
58
|
+
}
|
|
59
|
+
if (Object.keys(authInfo).length > 0)
|
|
60
|
+
result.authInfo = authInfo;
|
|
61
|
+
}
|
|
62
|
+
return Object.keys(result).length > 0 ? result : undefined;
|
|
63
|
+
};
|
|
64
|
+
exports.defaultMastraResolveExtra = defaultMastraResolveExtra;
|
|
65
|
+
const mergeExtra = (base, override) => {
|
|
66
|
+
if (!base)
|
|
67
|
+
return override;
|
|
68
|
+
if (!override)
|
|
69
|
+
return base;
|
|
70
|
+
return {
|
|
71
|
+
...base,
|
|
72
|
+
...override,
|
|
73
|
+
...(base.requestInfo || override.requestInfo
|
|
74
|
+
? {
|
|
75
|
+
requestInfo: {
|
|
76
|
+
...(base.requestInfo ?? {}),
|
|
77
|
+
...(override.requestInfo ?? {}),
|
|
78
|
+
},
|
|
79
|
+
}
|
|
80
|
+
: {}),
|
|
81
|
+
...(base.authInfo || override.authInfo
|
|
82
|
+
? {
|
|
83
|
+
authInfo: {
|
|
84
|
+
...(base.authInfo ?? {}),
|
|
85
|
+
...(override.authInfo ?? {}),
|
|
86
|
+
},
|
|
87
|
+
}
|
|
88
|
+
: {}),
|
|
89
|
+
};
|
|
90
|
+
};
|
|
6
91
|
const wrapOneTool = (toolKey, tool, recorder, config, resolveExtra) => {
|
|
7
92
|
if (typeof tool?.execute !== "function") {
|
|
8
93
|
return tool;
|
|
@@ -13,7 +98,11 @@ const wrapOneTool = (toolKey, tool, recorder, config, resolveExtra) => {
|
|
|
13
98
|
? undefined
|
|
14
99
|
: (0, schema_js_1.decorateInputSchemaWithTelemetry)(tool.inputSchema, config);
|
|
15
100
|
const wrappedExecute = (inputData, mastraContext) => {
|
|
16
|
-
|
|
101
|
+
// Always extract standard Mastra MCP context (`context.mcp.extra` or the
|
|
102
|
+
// `requestContext.get("mcp.extra")` fallback). A user-supplied
|
|
103
|
+
// `resolveExtra` layers on top — its values win where both define a field,
|
|
104
|
+
// so it doubles as an override and an extension point.
|
|
105
|
+
const extra = mergeExtra((0, exports.defaultMastraResolveExtra)(mastraContext), resolveExtra?.(mastraContext));
|
|
17
106
|
return recorder.instrumentToolCall({
|
|
18
107
|
name: toolName,
|
|
19
108
|
args: inputData,
|
|
@@ -33,6 +122,14 @@ const wrapOneTool = (toolKey, tool, recorder, config, resolveExtra) => {
|
|
|
33
122
|
execute: wrappedExecute,
|
|
34
123
|
};
|
|
35
124
|
};
|
|
125
|
+
// Generic over the input map so the return value keeps the customer's exact
|
|
126
|
+
// tool-map type (e.g. `Record<string, AutumnTool>`). Without this, every Mastra
|
|
127
|
+
// caller had to do `wrapMastraTools(myTools as unknown as MastraToolMap, ...) as
|
|
128
|
+
// unknown as typeof myTools` because `MCPServer({ tools })` wants the narrower
|
|
129
|
+
// type back. The `T extends MastraToolMap` constraint lets us still operate on
|
|
130
|
+
// the values structurally — Mastra's `createTool({...})` shape is a strict
|
|
131
|
+
// subtype of `MastraTool` (id/inputSchema/outputSchema/execute), so any tool map
|
|
132
|
+
// passes the constraint without a cast.
|
|
36
133
|
const wrapMastraToolsWithRecorder = (tools, recorder, config = {}, options = {}) => {
|
|
37
134
|
const out = {};
|
|
38
135
|
for (const [key, tool] of Object.entries(tools)) {
|
package/dist/cjs/types.d.ts
CHANGED
package/dist/esm/emit.js
CHANGED
|
@@ -25,6 +25,10 @@ export const resolveActorSeed = async (config, input) => {
|
|
|
25
25
|
return input.authInfo.token;
|
|
26
26
|
if (input.authInfo?.clientId)
|
|
27
27
|
return input.authInfo.clientId;
|
|
28
|
+
if (input.authInfo?.apiKey)
|
|
29
|
+
return input.authInfo.apiKey;
|
|
30
|
+
if (input.authInfo?.principalId)
|
|
31
|
+
return input.authInfo.principalId;
|
|
28
32
|
const authorization = headerValue(input.headers, "authorization");
|
|
29
33
|
if (authorization)
|
|
30
34
|
return authorization;
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -4,3 +4,4 @@ export { buildActorId, buildEventId, buildSessionInitEvent, buildToolCallEvent,
|
|
|
4
4
|
export { defaultMcpAnalyticsConfig, emitTelemetryEvent, postTelemetryEvent, } from "./emit.js";
|
|
5
5
|
export { createAnalyticsRecorder } from "./recorder.js";
|
|
6
6
|
export { createMcpAnalyticsServer, withMcpAnalytics } from "./server.js";
|
|
7
|
+
export { instrumentMcpServerTools, type InstrumentMcpServerToolsOptions, type InstrumentMcpServerToolsResult, type InstrumentedTool, } from "./instrument-tools.js";
|
package/dist/esm/index.js
CHANGED
|
@@ -3,3 +3,4 @@ export { buildActorId, buildEventId, buildSessionInitEvent, buildToolCallEvent,
|
|
|
3
3
|
export { defaultMcpAnalyticsConfig, emitTelemetryEvent, postTelemetryEvent, } from "./emit.js";
|
|
4
4
|
export { createAnalyticsRecorder } from "./recorder.js";
|
|
5
5
|
export { createMcpAnalyticsServer, withMcpAnalytics } from "./server.js";
|
|
6
|
+
export { instrumentMcpServerTools, } from "./instrument-tools.js";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import type { AnalyticsRecorder, McpAnalyticsConfig, RegisteredToolHandler, ToolRegistration } from "./types.js";
|
|
3
|
+
export type InstrumentedTool = ToolRegistration & {
|
|
4
|
+
handler: RegisteredToolHandler<unknown, unknown>;
|
|
5
|
+
};
|
|
6
|
+
type BaseInstrumentOptions = {
|
|
7
|
+
server: McpServer;
|
|
8
|
+
config?: McpAnalyticsConfig;
|
|
9
|
+
};
|
|
10
|
+
export type InstrumentMcpServerToolsOptions<TDef> = BaseInstrumentOptions & {
|
|
11
|
+
tools: TDef[] | Record<string, TDef>;
|
|
12
|
+
mapTool?: (def: TDef, key?: string) => InstrumentedTool;
|
|
13
|
+
};
|
|
14
|
+
export type InstrumentMcpServerToolsResult = {
|
|
15
|
+
server: McpServer;
|
|
16
|
+
recorder: AnalyticsRecorder;
|
|
17
|
+
};
|
|
18
|
+
export declare function instrumentMcpServerTools(options: BaseInstrumentOptions & {
|
|
19
|
+
tools: InstrumentedTool[] | Record<string, InstrumentedTool>;
|
|
20
|
+
mapTool?: (def: InstrumentedTool, key?: string) => InstrumentedTool;
|
|
21
|
+
}): InstrumentMcpServerToolsResult;
|
|
22
|
+
export declare function instrumentMcpServerTools<TDef>(options: BaseInstrumentOptions & {
|
|
23
|
+
tools: TDef[] | Record<string, TDef>;
|
|
24
|
+
mapTool: (def: TDef, key?: string) => InstrumentedTool;
|
|
25
|
+
}): InstrumentMcpServerToolsResult;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createAnalyticsRecorder } from "./recorder.js";
|
|
2
|
+
export function instrumentMcpServerTools(options) {
|
|
3
|
+
const recorder = createAnalyticsRecorder(options.config);
|
|
4
|
+
recorder.attachToMcpServer(options.server);
|
|
5
|
+
const identity = (def) => def;
|
|
6
|
+
const mapper = options.mapTool ?? identity;
|
|
7
|
+
if (Array.isArray(options.tools)) {
|
|
8
|
+
for (const def of options.tools) {
|
|
9
|
+
const mapped = mapper(def, undefined);
|
|
10
|
+
recorder.tool(mapped, mapped.handler);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
for (const [key, def] of Object.entries(options.tools)) {
|
|
15
|
+
const mapped = mapper(def, key);
|
|
16
|
+
recorder.tool(mapped, mapped.handler);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return { server: options.server, recorder };
|
|
20
|
+
}
|
package/dist/esm/mastra.d.ts
CHANGED
|
@@ -12,13 +12,14 @@ export type MastraToolMap = Record<string, MastraTool>;
|
|
|
12
12
|
export type MastraAdapterOptions = McpAnalyticsConfig & {
|
|
13
13
|
resolveExtra?: (mastraContext: unknown) => RequestExtra | undefined;
|
|
14
14
|
};
|
|
15
|
-
export declare const
|
|
15
|
+
export declare const defaultMastraResolveExtra: (mastraContext: unknown) => RequestExtra | undefined;
|
|
16
|
+
export declare const wrapMastraToolsWithRecorder: <T extends MastraToolMap>(tools: T, recorder: AnalyticsRecorder, config?: InternalMcpAnalyticsConfig, options?: {
|
|
16
17
|
resolveExtra?: MastraAdapterOptions["resolveExtra"];
|
|
17
|
-
}) =>
|
|
18
|
-
export declare const wrapMastraTools: (tools:
|
|
18
|
+
}) => T;
|
|
19
|
+
export declare const wrapMastraTools: <T extends MastraToolMap>(tools: T, options?: MastraAdapterOptions) => T;
|
|
19
20
|
export type MastraAnalytics = {
|
|
20
21
|
recorder: AnalyticsRecorder;
|
|
21
|
-
wrapTools: (tools:
|
|
22
|
+
wrapTools: <T extends MastraToolMap>(tools: T) => T;
|
|
22
23
|
flush: () => Promise<void>;
|
|
23
24
|
};
|
|
24
25
|
export declare const createMastraAnalytics: (options?: MastraAdapterOptions) => MastraAnalytics;
|
package/dist/esm/mastra.js
CHANGED
|
@@ -1,5 +1,89 @@
|
|
|
1
1
|
import { createAnalyticsRecorder } from "./recorder.js";
|
|
2
2
|
import { decorateInputSchemaWithTelemetry } from "./schema.js";
|
|
3
|
+
import { isRecord } from "./utils.js";
|
|
4
|
+
const readMcpExtra = (mastraContext) => {
|
|
5
|
+
if (!isRecord(mastraContext))
|
|
6
|
+
return undefined;
|
|
7
|
+
// Direct path: context.mcp.extra (the second argument Mastra's MCPServer
|
|
8
|
+
// passes into `execute` for tools invoked over MCP).
|
|
9
|
+
const mcp = mastraContext.mcp;
|
|
10
|
+
if (isRecord(mcp) && isRecord(mcp.extra))
|
|
11
|
+
return mcp.extra;
|
|
12
|
+
// Fallback: context.requestContext.get("mcp.extra") — some Mastra setups
|
|
13
|
+
// route the MCP extra through RuntimeContext under that key.
|
|
14
|
+
const requestContext = mastraContext.requestContext;
|
|
15
|
+
if (isRecord(requestContext) &&
|
|
16
|
+
typeof requestContext.get === "function") {
|
|
17
|
+
const fromContext = requestContext.get("mcp.extra");
|
|
18
|
+
if (isRecord(fromContext))
|
|
19
|
+
return fromContext;
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
};
|
|
23
|
+
export const defaultMastraResolveExtra = (mastraContext) => {
|
|
24
|
+
const mcpExtra = readMcpExtra(mastraContext);
|
|
25
|
+
if (!mcpExtra)
|
|
26
|
+
return undefined;
|
|
27
|
+
const result = {};
|
|
28
|
+
if (typeof mcpExtra.sessionId === "string" && mcpExtra.sessionId.length > 0) {
|
|
29
|
+
result.sessionId = mcpExtra.sessionId;
|
|
30
|
+
}
|
|
31
|
+
const requestId = mcpExtra.requestId;
|
|
32
|
+
if (typeof requestId === "string" || typeof requestId === "number") {
|
|
33
|
+
result.requestId = requestId;
|
|
34
|
+
}
|
|
35
|
+
if (isRecord(mcpExtra.requestInfo)) {
|
|
36
|
+
const headers = mcpExtra.requestInfo.headers;
|
|
37
|
+
if (headers !== undefined) {
|
|
38
|
+
result.requestInfo = { headers: headers };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (isRecord(mcpExtra.authInfo)) {
|
|
42
|
+
// Narrow to the four fields `resolveActorSeed` / `buildSessionInitEvent`
|
|
43
|
+
// actually consume — don't forward arbitrary unrelated properties from a
|
|
44
|
+
// host-supplied object down the analytics pipeline.
|
|
45
|
+
const incoming = mcpExtra.authInfo;
|
|
46
|
+
const authInfo = {};
|
|
47
|
+
if (typeof incoming.token === "string")
|
|
48
|
+
authInfo.token = incoming.token;
|
|
49
|
+
if (typeof incoming.clientId === "string")
|
|
50
|
+
authInfo.clientId = incoming.clientId;
|
|
51
|
+
if (typeof incoming.apiKey === "string")
|
|
52
|
+
authInfo.apiKey = incoming.apiKey;
|
|
53
|
+
if (typeof incoming.principalId === "string") {
|
|
54
|
+
authInfo.principalId = incoming.principalId;
|
|
55
|
+
}
|
|
56
|
+
if (Object.keys(authInfo).length > 0)
|
|
57
|
+
result.authInfo = authInfo;
|
|
58
|
+
}
|
|
59
|
+
return Object.keys(result).length > 0 ? result : undefined;
|
|
60
|
+
};
|
|
61
|
+
const mergeExtra = (base, override) => {
|
|
62
|
+
if (!base)
|
|
63
|
+
return override;
|
|
64
|
+
if (!override)
|
|
65
|
+
return base;
|
|
66
|
+
return {
|
|
67
|
+
...base,
|
|
68
|
+
...override,
|
|
69
|
+
...(base.requestInfo || override.requestInfo
|
|
70
|
+
? {
|
|
71
|
+
requestInfo: {
|
|
72
|
+
...(base.requestInfo ?? {}),
|
|
73
|
+
...(override.requestInfo ?? {}),
|
|
74
|
+
},
|
|
75
|
+
}
|
|
76
|
+
: {}),
|
|
77
|
+
...(base.authInfo || override.authInfo
|
|
78
|
+
? {
|
|
79
|
+
authInfo: {
|
|
80
|
+
...(base.authInfo ?? {}),
|
|
81
|
+
...(override.authInfo ?? {}),
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
: {}),
|
|
85
|
+
};
|
|
86
|
+
};
|
|
3
87
|
const wrapOneTool = (toolKey, tool, recorder, config, resolveExtra) => {
|
|
4
88
|
if (typeof tool?.execute !== "function") {
|
|
5
89
|
return tool;
|
|
@@ -10,7 +94,11 @@ const wrapOneTool = (toolKey, tool, recorder, config, resolveExtra) => {
|
|
|
10
94
|
? undefined
|
|
11
95
|
: decorateInputSchemaWithTelemetry(tool.inputSchema, config);
|
|
12
96
|
const wrappedExecute = (inputData, mastraContext) => {
|
|
13
|
-
|
|
97
|
+
// Always extract standard Mastra MCP context (`context.mcp.extra` or the
|
|
98
|
+
// `requestContext.get("mcp.extra")` fallback). A user-supplied
|
|
99
|
+
// `resolveExtra` layers on top — its values win where both define a field,
|
|
100
|
+
// so it doubles as an override and an extension point.
|
|
101
|
+
const extra = mergeExtra(defaultMastraResolveExtra(mastraContext), resolveExtra?.(mastraContext));
|
|
14
102
|
return recorder.instrumentToolCall({
|
|
15
103
|
name: toolName,
|
|
16
104
|
args: inputData,
|
|
@@ -30,6 +118,14 @@ const wrapOneTool = (toolKey, tool, recorder, config, resolveExtra) => {
|
|
|
30
118
|
execute: wrappedExecute,
|
|
31
119
|
};
|
|
32
120
|
};
|
|
121
|
+
// Generic over the input map so the return value keeps the customer's exact
|
|
122
|
+
// tool-map type (e.g. `Record<string, AutumnTool>`). Without this, every Mastra
|
|
123
|
+
// caller had to do `wrapMastraTools(myTools as unknown as MastraToolMap, ...) as
|
|
124
|
+
// unknown as typeof myTools` because `MCPServer({ tools })` wants the narrower
|
|
125
|
+
// type back. The `T extends MastraToolMap` constraint lets us still operate on
|
|
126
|
+
// the values structurally — Mastra's `createTool({...})` shape is a strict
|
|
127
|
+
// subtype of `MastraTool` (id/inputSchema/outputSchema/execute), so any tool map
|
|
128
|
+
// passes the constraint without a cast.
|
|
33
129
|
export const wrapMastraToolsWithRecorder = (tools, recorder, config = {}, options = {}) => {
|
|
34
130
|
const out = {};
|
|
35
131
|
for (const [key, tool] of Object.entries(tools)) {
|
package/dist/esm/types.d.ts
CHANGED