@alpic-ai/insights 0.0.0-dev.g1ab1e48 → 0.0.0-dev.g1ba9633
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/dist/index.d.mts +43 -10
- package/dist/index.mjs +149 -50
- package/package.json +7 -7
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
2
2
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
|
-
|
|
4
3
|
//#region src/intent-middleware.d.ts
|
|
5
4
|
interface PromptData {
|
|
6
5
|
toolName: string;
|
|
@@ -37,6 +36,40 @@ type McpMiddlewareFn = (request: {
|
|
|
37
36
|
*/
|
|
38
37
|
declare function intentMiddleware(options?: IntentMiddlewareOptions): McpMiddlewareFn;
|
|
39
38
|
//#endregion
|
|
39
|
+
//#region src/analytics-middleware.d.ts
|
|
40
|
+
interface CaptureOptions {
|
|
41
|
+
message?: string;
|
|
42
|
+
/** Duration of the operation the event describes, in milliseconds. */
|
|
43
|
+
duration?: number;
|
|
44
|
+
isError?: boolean;
|
|
45
|
+
error?: string;
|
|
46
|
+
/** Freeform event detail. Not indexed — filtering on a property key is a query-time scan. */
|
|
47
|
+
properties?: Record<string, unknown>;
|
|
48
|
+
}
|
|
49
|
+
interface Analytics {
|
|
50
|
+
/** Records a custom event on the current session's timeline, interleaved with tool calls. */
|
|
51
|
+
capture(name: string, options?: CaptureOptions): void;
|
|
52
|
+
/**
|
|
53
|
+
* Attaches traits to the current request's user (e.g. email, name, plan). The target user is
|
|
54
|
+
* resolved by Alpic from the request auth context — no id or sessionId is passed.
|
|
55
|
+
*/
|
|
56
|
+
identify(traits: Record<string, string>): void;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Shape of the handler `extra` once `analyticsMiddleware()` (or `track(server)`) is installed.
|
|
60
|
+
* Cast the handler's `extra` to this to access `analytics` with types.
|
|
61
|
+
*/
|
|
62
|
+
interface AnalyticsExtra {
|
|
63
|
+
analytics: Analytics;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Lets MCP server builders record custom analytics events and user traits from inside tool
|
|
67
|
+
* handlers, via `extra.analytics.capture(...)` / `extra.analytics.identify(...)`. Calls are
|
|
68
|
+
* buffered during the request and flushed into the response `_meta`, where the Alpic proxy
|
|
69
|
+
* ingests and strips them before the result reaches the client.
|
|
70
|
+
*/
|
|
71
|
+
declare function analyticsMiddleware(): McpMiddlewareFn;
|
|
72
|
+
//#endregion
|
|
40
73
|
//#region src/feedback-middleware.d.ts
|
|
41
74
|
interface FeedbackData {
|
|
42
75
|
content: string;
|
|
@@ -65,10 +98,6 @@ declare function feedbackMiddleware(options?: FeedbackMiddlewareOptions): McpMid
|
|
|
65
98
|
* or the low-level `Server` and patches the `tools/list` and `tools/call`
|
|
66
99
|
* request handlers to surface captured feedback via `options.handler` (or,
|
|
67
100
|
* when `ALPIC_FEEDBACK_META_KEY` is set, via the response `_meta`).
|
|
68
|
-
*
|
|
69
|
-
* Already-registered handlers are wrapped immediately; future registrations
|
|
70
|
-
* are wrapped via a `Map.set` proxy so order of calls relative to
|
|
71
|
-
* `registerTool` does not matter.
|
|
72
101
|
*/
|
|
73
102
|
declare const captureFeedback: (server: McpServer | Server, options?: FeedbackMiddlewareOptions) => void;
|
|
74
103
|
//#endregion
|
|
@@ -79,11 +108,15 @@ declare const captureFeedback: (server: McpServer | Server, options?: FeedbackMi
|
|
|
79
108
|
* low-level `Server` and patches the `tools/list` and `tools/call` request
|
|
80
109
|
* handlers to surface the captured intent via `options.handler` (or, when
|
|
81
110
|
* `ALPIC_INTENT_META_KEY` is set, via the response `_meta`).
|
|
82
|
-
*
|
|
83
|
-
* Already-registered handlers are wrapped immediately; future registrations
|
|
84
|
-
* (e.g. tools added after this call) are wrapped via a `Map.set` proxy so order
|
|
85
|
-
* of calls relative to `registerTool` does not matter.
|
|
86
111
|
*/
|
|
87
112
|
declare const captureIntents: (server: McpServer | Server, options?: IntentMiddlewareOptions) => void;
|
|
88
113
|
//#endregion
|
|
89
|
-
|
|
114
|
+
//#region src/track.d.ts
|
|
115
|
+
/**
|
|
116
|
+
* Enables custom analytics events on a vanilla `@modelcontextprotocol/sdk` server. Accepts the
|
|
117
|
+
* high-level `McpServer` or the low-level `Server` and patches the `tools/call` request handlers
|
|
118
|
+
* so tool handlers can call `extra.analytics.capture(...)` / `extra.analytics.identify(...)`.
|
|
119
|
+
*/
|
|
120
|
+
declare const track: (server: McpServer | Server) => void;
|
|
121
|
+
//#endregion
|
|
122
|
+
export { type Analytics, type AnalyticsExtra, type CaptureOptions, type FeedbackData, type FeedbackMiddlewareOptions, type IntentMiddlewareOptions, type McpMiddlewareFn, type PromptData, analyticsMiddleware, captureFeedback, captureIntents, feedbackMiddleware, intentMiddleware, track };
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,99 @@
|
|
|
1
1
|
import { CallToolRequestSchema, CallToolResultSchema, ListToolsResultSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
+
//#region src/analytics-middleware.ts
|
|
3
|
+
const ALPIC_EVENTS_META_KEY = "alpic/events";
|
|
4
|
+
const ALPIC_IDENTIFY_META_KEY = "alpic/identify";
|
|
5
|
+
const MAX_EVENTS_PER_REQUEST = 50;
|
|
6
|
+
function warnInDev(message) {
|
|
7
|
+
if (process.env.NODE_ENV !== "production") console.warn(`[insights] ${message}`);
|
|
8
|
+
}
|
|
9
|
+
function createAnalyticsBuffer() {
|
|
10
|
+
const events = [];
|
|
11
|
+
let traits;
|
|
12
|
+
let settled = false;
|
|
13
|
+
return {
|
|
14
|
+
analytics: {
|
|
15
|
+
capture(name, options) {
|
|
16
|
+
if (settled) {
|
|
17
|
+
warnInDev(`analytics.capture("${name}") was called after the request settled; the event was dropped. Capture events before the tool handler returns.`);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (events.length >= MAX_EVENTS_PER_REQUEST) {
|
|
21
|
+
warnInDev(`analytics.capture("${name}") exceeded the ${MAX_EVENTS_PER_REQUEST} events per request limit; the event was dropped.`);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
events.push({
|
|
25
|
+
...options,
|
|
26
|
+
name,
|
|
27
|
+
timestamp: Date.now()
|
|
28
|
+
});
|
|
29
|
+
},
|
|
30
|
+
identify(newTraits) {
|
|
31
|
+
if (settled) {
|
|
32
|
+
warnInDev("analytics.identify() was called after the request settled; the traits were dropped. Identify before the tool handler returns.");
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
traits = {
|
|
36
|
+
...traits,
|
|
37
|
+
...newTraits
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
settle: () => {
|
|
42
|
+
settled = true;
|
|
43
|
+
},
|
|
44
|
+
get events() {
|
|
45
|
+
return events;
|
|
46
|
+
},
|
|
47
|
+
get traits() {
|
|
48
|
+
return traits;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Lets MCP server builders record custom analytics events and user traits from inside tool
|
|
54
|
+
* handlers, via `extra.analytics.capture(...)` / `extra.analytics.identify(...)`. Calls are
|
|
55
|
+
* buffered during the request and flushed into the response `_meta`, where the Alpic proxy
|
|
56
|
+
* ingests and strips them before the result reaches the client.
|
|
57
|
+
*/
|
|
58
|
+
function analyticsMiddleware() {
|
|
59
|
+
return async (request, extra, next) => {
|
|
60
|
+
if (request.method !== "tools/call" || extra === null || typeof extra !== "object") return next();
|
|
61
|
+
const buffer = createAnalyticsBuffer();
|
|
62
|
+
extra.analytics = buffer.analytics;
|
|
63
|
+
let rawResult;
|
|
64
|
+
try {
|
|
65
|
+
rawResult = await next();
|
|
66
|
+
} finally {
|
|
67
|
+
buffer.settle();
|
|
68
|
+
}
|
|
69
|
+
if (buffer.events.length === 0 && buffer.traits === void 0) return rawResult;
|
|
70
|
+
if (!CallToolResultSchema.safeParse(rawResult).success) return rawResult;
|
|
71
|
+
const result = rawResult;
|
|
72
|
+
const meta = { ...result._meta };
|
|
73
|
+
if (buffer.events.length > 0) meta[ALPIC_EVENTS_META_KEY] = buffer.events;
|
|
74
|
+
if (buffer.traits !== void 0) meta[ALPIC_IDENTIFY_META_KEY] = buffer.traits;
|
|
75
|
+
return {
|
|
76
|
+
...result,
|
|
77
|
+
_meta: meta
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
2
82
|
//#region src/feedback-middleware.ts
|
|
3
83
|
const FEEDBACK_TOOL_NAME = "send_feedback";
|
|
4
84
|
const FEEDBACK_TOOL_DESCRIPTION = "Send feedback about this MCP server to its operators. Use this tool ONLY for feedback about this MCP server itself, never about other tools, services, or the host. You MAY call this tool when you detect a genuine issue with this server (e.g. a tool that failed unexpectedly, an unhelpful response, a missing capability). You MAY also call it when the user explicitly asks to send feedback. Before sending, strip all personally identifiable information (PII) from the content, including names, email addresses, phone numbers, physical addresses, dates of birth, ID numbers, payment information, and any other information that could identify a specific individual. Replace stripped values with generic placeholders (e.g. \"[name]\", \"[email]\").";
|
|
5
85
|
const FEEDBACK_RESPONSE_TEXT = "Feedback received. Thanks!";
|
|
86
|
+
const FEEDBACK_OUTPUT_SCHEMA = {
|
|
87
|
+
type: "object",
|
|
88
|
+
properties: { status: {
|
|
89
|
+
type: "string",
|
|
90
|
+
enum: ["received"],
|
|
91
|
+
description: "Confirmation that the feedback was received by the server operators."
|
|
92
|
+
} },
|
|
93
|
+
required: ["status"],
|
|
94
|
+
additionalProperties: false
|
|
95
|
+
};
|
|
96
|
+
const FEEDBACK_STRUCTURED_CONTENT = { status: "received" };
|
|
6
97
|
/**
|
|
7
98
|
* Lets MCP server builders collect qualitative feedback from end users about their
|
|
8
99
|
* tool/server. Injects a `send_feedback` tool at `tools/list` time and intercepts calls
|
|
@@ -36,10 +127,11 @@ function feedbackMiddleware(options) {
|
|
|
36
127
|
},
|
|
37
128
|
required: ["content", "source"]
|
|
38
129
|
},
|
|
130
|
+
outputSchema: FEEDBACK_OUTPUT_SCHEMA,
|
|
39
131
|
annotations: {
|
|
40
|
-
readOnlyHint:
|
|
132
|
+
readOnlyHint: true,
|
|
41
133
|
destructiveHint: false,
|
|
42
|
-
openWorldHint:
|
|
134
|
+
openWorldHint: false,
|
|
43
135
|
idempotentHint: true
|
|
44
136
|
}
|
|
45
137
|
});
|
|
@@ -73,39 +165,39 @@ function feedbackMiddleware(options) {
|
|
|
73
165
|
type: "text",
|
|
74
166
|
text: FEEDBACK_RESPONSE_TEXT
|
|
75
167
|
}],
|
|
168
|
+
structuredContent: FEEDBACK_STRUCTURED_CONTENT,
|
|
76
169
|
_meta: { [metaKeyName]: feedback }
|
|
77
170
|
};
|
|
78
|
-
return {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
171
|
+
return {
|
|
172
|
+
content: [{
|
|
173
|
+
type: "text",
|
|
174
|
+
text: FEEDBACK_RESPONSE_TEXT
|
|
175
|
+
}],
|
|
176
|
+
structuredContent: FEEDBACK_STRUCTURED_CONTENT
|
|
177
|
+
};
|
|
82
178
|
};
|
|
83
179
|
}
|
|
84
180
|
//#endregion
|
|
85
|
-
//#region src/capture-
|
|
86
|
-
const INSTALLED_MARKER$1 = "__alpicCaptureFeedbackInstalled";
|
|
181
|
+
//#region src/install-capture-middleware.ts
|
|
87
182
|
/**
|
|
88
|
-
*
|
|
89
|
-
* server
|
|
90
|
-
*
|
|
91
|
-
* request handlers to surface captured feedback via `options.handler` (or,
|
|
92
|
-
* when `ALPIC_FEEDBACK_META_KEY` is set, via the response `_meta`).
|
|
183
|
+
* Patches the `tools/list` and `tools/call` request handlers of a vanilla
|
|
184
|
+
* `@modelcontextprotocol/sdk` server (high-level `McpServer` or low-level
|
|
185
|
+
* `Server`) to run the given middleware.
|
|
93
186
|
*
|
|
94
187
|
* Already-registered handlers are wrapped immediately; future registrations
|
|
95
|
-
* are wrapped via a `Map.set` proxy so order
|
|
96
|
-
* `registerTool` does not matter.
|
|
188
|
+
* (e.g. tools added after this call) are wrapped via a `Map.set` proxy so order
|
|
189
|
+
* of calls relative to `registerTool` does not matter.
|
|
97
190
|
*/
|
|
98
|
-
const
|
|
191
|
+
const installCaptureMiddleware = (server, { middleware, installedMarker, disabledWarning }) => {
|
|
99
192
|
const handlers = ("server" in server ? server.server : server)?._requestHandlers;
|
|
100
193
|
if (!(handlers instanceof Map)) {
|
|
101
|
-
console.warn(
|
|
194
|
+
console.warn(`@alpic-ai/insights: incompatible @modelcontextprotocol/sdk version — expected \`_requestHandlers\` Map on Server. ${disabledWarning}`);
|
|
102
195
|
return;
|
|
103
196
|
}
|
|
104
197
|
const marked = handlers;
|
|
105
|
-
if (marked[
|
|
106
|
-
marked[
|
|
107
|
-
const
|
|
108
|
-
const targets = new Set(["tools/list", "tools/call"]);
|
|
198
|
+
if (marked[installedMarker]) return;
|
|
199
|
+
marked[installedMarker] = true;
|
|
200
|
+
const targets = /* @__PURE__ */ new Set(["tools/list", "tools/call"]);
|
|
109
201
|
const wrap = (method, handler) => {
|
|
110
202
|
if (!targets.has(method)) return handler;
|
|
111
203
|
return async (...args) => {
|
|
@@ -121,6 +213,22 @@ const captureFeedback = (server, options) => {
|
|
|
121
213
|
handlers.set = (method, handler) => originalSet(method, wrap(method, handler));
|
|
122
214
|
};
|
|
123
215
|
//#endregion
|
|
216
|
+
//#region src/capture-feedback.ts
|
|
217
|
+
/**
|
|
218
|
+
* Injects a `send_feedback` tool into a vanilla `@modelcontextprotocol/sdk`
|
|
219
|
+
* server and captures feedback submissions. Accepts the high-level `McpServer`
|
|
220
|
+
* or the low-level `Server` and patches the `tools/list` and `tools/call`
|
|
221
|
+
* request handlers to surface captured feedback via `options.handler` (or,
|
|
222
|
+
* when `ALPIC_FEEDBACK_META_KEY` is set, via the response `_meta`).
|
|
223
|
+
*/
|
|
224
|
+
const captureFeedback = (server, options) => {
|
|
225
|
+
installCaptureMiddleware(server, {
|
|
226
|
+
middleware: feedbackMiddleware(options),
|
|
227
|
+
installedMarker: "__alpicCaptureFeedbackInstalled",
|
|
228
|
+
disabledWarning: "Feedback capture disabled."
|
|
229
|
+
});
|
|
230
|
+
};
|
|
231
|
+
//#endregion
|
|
124
232
|
//#region src/intent-middleware.ts
|
|
125
233
|
const USER_INTENT_FIELD = "user_intent";
|
|
126
234
|
/**
|
|
@@ -228,42 +336,33 @@ Examples:
|
|
|
228
336
|
}
|
|
229
337
|
//#endregion
|
|
230
338
|
//#region src/capture-intents.ts
|
|
231
|
-
const INSTALLED_MARKER = "__alpicCaptureIntentsInstalled";
|
|
232
339
|
/**
|
|
233
340
|
* Captures the user's natural-language intent behind each tool call on a vanilla
|
|
234
341
|
* `@modelcontextprotocol/sdk` server. Accepts the high-level `McpServer` or the
|
|
235
342
|
* low-level `Server` and patches the `tools/list` and `tools/call` request
|
|
236
343
|
* handlers to surface the captured intent via `options.handler` (or, when
|
|
237
344
|
* `ALPIC_INTENT_META_KEY` is set, via the response `_meta`).
|
|
238
|
-
*
|
|
239
|
-
* Already-registered handlers are wrapped immediately; future registrations
|
|
240
|
-
* (e.g. tools added after this call) are wrapped via a `Map.set` proxy so order
|
|
241
|
-
* of calls relative to `registerTool` does not matter.
|
|
242
345
|
*/
|
|
243
346
|
const captureIntents = (server, options) => {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
};
|
|
264
|
-
for (const [method, handler] of [...handlers]) handlers.set(method, wrap(method, handler));
|
|
265
|
-
const originalSet = handlers.set.bind(handlers);
|
|
266
|
-
handlers.set = (method, handler) => originalSet(method, wrap(method, handler));
|
|
347
|
+
installCaptureMiddleware(server, {
|
|
348
|
+
middleware: intentMiddleware(options),
|
|
349
|
+
installedMarker: "__alpicCaptureIntentsInstalled",
|
|
350
|
+
disabledWarning: "Prompt capture disabled."
|
|
351
|
+
});
|
|
352
|
+
};
|
|
353
|
+
//#endregion
|
|
354
|
+
//#region src/track.ts
|
|
355
|
+
/**
|
|
356
|
+
* Enables custom analytics events on a vanilla `@modelcontextprotocol/sdk` server. Accepts the
|
|
357
|
+
* high-level `McpServer` or the low-level `Server` and patches the `tools/call` request handlers
|
|
358
|
+
* so tool handlers can call `extra.analytics.capture(...)` / `extra.analytics.identify(...)`.
|
|
359
|
+
*/
|
|
360
|
+
const track = (server) => {
|
|
361
|
+
installCaptureMiddleware(server, {
|
|
362
|
+
middleware: analyticsMiddleware(),
|
|
363
|
+
installedMarker: "__alpicTrackInstalled",
|
|
364
|
+
disabledWarning: "Analytics capture disabled."
|
|
365
|
+
});
|
|
267
366
|
};
|
|
268
367
|
//#endregion
|
|
269
|
-
export { captureFeedback, captureIntents, feedbackMiddleware, intentMiddleware };
|
|
368
|
+
export { analyticsMiddleware, captureFeedback, captureIntents, feedbackMiddleware, intentMiddleware, track };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alpic-ai/insights",
|
|
3
|
-
"version": "0.0.0-dev.
|
|
3
|
+
"version": "0.0.0-dev.g1ba9633",
|
|
4
4
|
"description": "User insights middlewares for Alpic-hosted MCP servers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.mjs",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"license": "ISC",
|
|
19
19
|
"peerDependencies": {
|
|
20
20
|
"@modelcontextprotocol/sdk": ">=1.29.0 <2",
|
|
21
|
-
"skybridge": "
|
|
21
|
+
"skybridge": "^0.36.3 || ^1.0.0"
|
|
22
22
|
},
|
|
23
23
|
"peerDependenciesMeta": {
|
|
24
24
|
"skybridge": {
|
|
@@ -28,12 +28,12 @@
|
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
30
30
|
"@total-typescript/tsconfig": "^1.0.4",
|
|
31
|
-
"@types/node": "^25.
|
|
31
|
+
"@types/node": "^25.9.5",
|
|
32
32
|
"shx": "^0.4.0",
|
|
33
|
-
"skybridge": "^
|
|
34
|
-
"tsdown": "^0.22.
|
|
33
|
+
"skybridge": "^1.2.7",
|
|
34
|
+
"tsdown": "^0.22.5",
|
|
35
35
|
"typescript": "^6.0.3",
|
|
36
|
-
"vitest": "^4.1.
|
|
36
|
+
"vitest": "^4.1.10",
|
|
37
37
|
"zod": "^4.4.3"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"test": "pnpm run test:unit && pnpm run test:type && pnpm run test:format",
|
|
43
43
|
"test:unit": "vitest run",
|
|
44
44
|
"test:format": "biome check --error-on-warnings .",
|
|
45
|
-
"test:type": "
|
|
45
|
+
"test:type": "tsgo --noEmit",
|
|
46
46
|
"publish:npm": "pnpm publish --tag \"${NPM_TAG}\" --access public --no-git-checks"
|
|
47
47
|
}
|
|
48
48
|
}
|