@alpic-ai/insights 0.0.0-dev.gbc49050 → 0.0.0-dev.gbc7d4f6
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 +20 -88
- package/dist/index.mjs +70 -117
- package/dist/react/index.d.mts +7 -23
- package/dist/react/index.mjs +8 -24
- package/package.json +23 -16
package/dist/index.d.mts
CHANGED
|
@@ -1,42 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
|
-
//#region src/intent-middleware.d.ts
|
|
1
|
+
import { ServerContext } from "@modelcontextprotocol/server";
|
|
4
2
|
interface PromptData {
|
|
5
3
|
toolName: string;
|
|
6
4
|
userPrompt: string;
|
|
7
5
|
}
|
|
8
6
|
interface IntentMiddlewareOptions {
|
|
9
7
|
handler?: (prompt: PromptData) => Promise<void> | void;
|
|
10
|
-
/**
|
|
11
|
-
* If provided, only these tool names will have the `user_intent` field injected and their
|
|
12
|
-
* prompts captured. All other tools are left untouched.
|
|
13
|
-
*/
|
|
8
|
+
/** When provided, only these tool names have the `user_intent` field injected and their prompts captured; all other tools are left untouched. */
|
|
14
9
|
tools?: string[];
|
|
15
|
-
/**
|
|
16
|
-
* Mapping of tool names to argument names whose values should be captured as the intent.
|
|
17
|
-
* Use this when the tool already has an argument (e.g. `query`, `question`) that conveys user
|
|
18
|
-
* intent. For tools in this mapping, the synthetic `user_intent` argument is not injected into the
|
|
19
|
-
* schema and the argument's value is read straight from the tool call arguments without being stripped.
|
|
20
|
-
*/
|
|
10
|
+
/** Maps tool names to an existing argument (e.g. `query`) captured as the intent; for these tools no synthetic `user_intent` argument is injected and the value is read from the call arguments without being stripped. */
|
|
21
11
|
argumentNameOverride?: Record<string, string>;
|
|
22
12
|
}
|
|
23
|
-
/**
|
|
24
|
-
* Structurally compatible with `skybridge/server`'s `McpMiddlewareFn` so
|
|
25
|
-
* skybridge users can still pass the result into `server.mcpMiddleware(...)`.
|
|
26
|
-
*/
|
|
13
|
+
/** Structurally compatible with `skybridge/server`'s `McpMiddlewareFn` so skybridge users can still pass the result into `server.mcpMiddleware(...)`. */
|
|
27
14
|
type McpMiddlewareFn = (request: {
|
|
28
15
|
method: string;
|
|
29
16
|
params: Record<string, unknown>;
|
|
30
17
|
}, extra: unknown, next: () => Promise<unknown>) => Promise<unknown> | unknown;
|
|
31
|
-
/**
|
|
32
|
-
|
|
33
|
-
* server builders can see *why* their tools are being invoked, not just that
|
|
34
|
-
* they were. The LLM fills in `user_intent` from the original user message
|
|
35
|
-
* (the server has no other way to access it).
|
|
36
|
-
*/
|
|
37
|
-
declare function intentMiddleware(options?: IntentMiddlewareOptions): McpMiddlewareFn;
|
|
38
|
-
//#endregion
|
|
39
|
-
//#region src/analytics-middleware.d.ts
|
|
18
|
+
/** Captures the user's natural-language intent behind each tool call: the LLM fills in `user_intent` from the original user message, which the server has no other way to access. */
|
|
19
|
+
export declare function intentMiddleware(options?: IntentMiddlewareOptions): McpMiddlewareFn;
|
|
40
20
|
interface CaptureOptions {
|
|
41
21
|
message?: string;
|
|
42
22
|
/** Duration of the operation the event describes, in milliseconds. */
|
|
@@ -49,17 +29,10 @@ interface CaptureOptions {
|
|
|
49
29
|
interface Analytics {
|
|
50
30
|
/** Records a custom event on the current session's timeline, interleaved with tool calls. */
|
|
51
31
|
capture(name: string, options?: CaptureOptions): void;
|
|
52
|
-
/**
|
|
53
|
-
* Replaces the current request user's trait snapshot (e.g. email, name, plan). The target user
|
|
54
|
-
* is resolved by Alpic from the request auth context — no id or sessionId is passed.
|
|
55
|
-
*/
|
|
32
|
+
/** Replaces the current request user's trait snapshot (e.g. email, name, plan); the target user is resolved by Alpic from the request auth context — no id or sessionId is passed. */
|
|
56
33
|
identify(traits: Record<string, string>): void;
|
|
57
34
|
}
|
|
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 {
|
|
35
|
+
interface AnalyticsExtra extends ServerContext {
|
|
63
36
|
analytics: Analytics;
|
|
64
37
|
}
|
|
65
38
|
interface AnalyticsEvent extends CaptureOptions {
|
|
@@ -74,63 +47,22 @@ interface AnalyticsMiddlewareOptions {
|
|
|
74
47
|
/** Receives each request's analytics locally, whether or not the server is hosted by Alpic. */
|
|
75
48
|
handler?: (batch: AnalyticsBatch) => Promise<void> | void;
|
|
76
49
|
}
|
|
77
|
-
/**
|
|
78
|
-
|
|
79
|
-
* handlers, via `extra.analytics.capture(...)` / `extra.analytics.identify(...)`. Calls are
|
|
80
|
-
* buffered during the request. On Alpic, private environment-provided `_meta` keys carry them to
|
|
81
|
-
* the proxy for ingestion. Locally, pass a handler to receive them without exposing analytics in
|
|
82
|
-
* the MCP response.
|
|
83
|
-
*/
|
|
84
|
-
declare function analyticsMiddleware(options?: AnalyticsMiddlewareOptions): McpMiddlewareFn;
|
|
85
|
-
//#endregion
|
|
86
|
-
//#region src/feedback-middleware.d.ts
|
|
50
|
+
/** Buffers `extra.analytics.capture(...)` / `identify(...)` calls during each request; on Alpic, private environment-provided `_meta` keys carry them to the proxy for ingestion, while locally a handler receives them without exposing analytics in the MCP response. */
|
|
51
|
+
export declare function analyticsMiddleware(options?: AnalyticsMiddlewareOptions): McpMiddlewareFn;
|
|
87
52
|
interface FeedbackData {
|
|
88
53
|
content: string;
|
|
89
54
|
source: "model" | "user";
|
|
90
55
|
}
|
|
91
56
|
interface FeedbackMiddlewareOptions {
|
|
92
|
-
/**
|
|
93
|
-
* Custom handler invoked with the user's feedback. When provided, the middleware still attaches the feedback to the response `_meta`.
|
|
94
|
-
* The handler runs **in addition to** Alpic's dashboard delivery, feedback are still captured when deployed on Alpic.
|
|
95
|
-
*/
|
|
57
|
+
/** Invoked with the user's feedback in addition to Alpic's dashboard delivery — the middleware still attaches the feedback to the response `_meta`. */
|
|
96
58
|
handler?: (feedback: FeedbackData) => Promise<void> | void;
|
|
97
59
|
}
|
|
98
|
-
/**
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
*/
|
|
105
|
-
declare
|
|
106
|
-
|
|
107
|
-
//#region src/capture-feedback.d.ts
|
|
108
|
-
/**
|
|
109
|
-
* Injects a `send_feedback` tool into a vanilla `@modelcontextprotocol/sdk`
|
|
110
|
-
* server and captures feedback submissions. Accepts the high-level `McpServer`
|
|
111
|
-
* or the low-level `Server` and patches the `tools/list` and `tools/call`
|
|
112
|
-
* request handlers to surface captured feedback via `options.handler` (or,
|
|
113
|
-
* when `ALPIC_FEEDBACK_META_KEY` is set, via the response `_meta`).
|
|
114
|
-
*/
|
|
115
|
-
declare const captureFeedback: (server: McpServer | Server, options?: FeedbackMiddlewareOptions) => void;
|
|
116
|
-
//#endregion
|
|
117
|
-
//#region src/capture-intents.d.ts
|
|
118
|
-
/**
|
|
119
|
-
* Captures the user's natural-language intent behind each tool call on a vanilla
|
|
120
|
-
* `@modelcontextprotocol/sdk` server. Accepts the high-level `McpServer` or the
|
|
121
|
-
* low-level `Server` and patches the `tools/list` and `tools/call` request
|
|
122
|
-
* handlers to surface the captured intent via `options.handler` (or, when
|
|
123
|
-
* `ALPIC_INTENT_META_KEY` is set, via the response `_meta`).
|
|
124
|
-
*/
|
|
125
|
-
declare const captureIntents: (server: McpServer | Server, options?: IntentMiddlewareOptions) => void;
|
|
126
|
-
//#endregion
|
|
127
|
-
//#region src/track.d.ts
|
|
128
|
-
/**
|
|
129
|
-
* Enables custom analytics events on a vanilla `@modelcontextprotocol/sdk` server. Accepts the
|
|
130
|
-
* high-level `McpServer` or the low-level `Server` and patches the `tools/call` request handlers
|
|
131
|
-
* so tool handlers can call `extra.analytics.capture(...)` / `extra.analytics.identify(...)`.
|
|
132
|
-
* Pass a handler in `options` to receive analytics when running outside Alpic.
|
|
133
|
-
*/
|
|
134
|
-
declare const track: (server: McpServer | Server, options?: AnalyticsMiddlewareOptions) => void;
|
|
135
|
-
//#endregion
|
|
136
|
-
export { type Analytics, type AnalyticsBatch, type AnalyticsEvent, type AnalyticsExtra, type AnalyticsMiddlewareOptions, type CaptureOptions, type FeedbackData, type FeedbackMiddlewareOptions, type IntentMiddlewareOptions, type McpMiddlewareFn, type PromptData, analyticsMiddleware, captureFeedback, captureIntents, feedbackMiddleware, intentMiddleware, track };
|
|
60
|
+
/** Injects a `send_feedback` tool at `tools/list` time and short-circuits calls to it at `tools/call` time (the tool has no server-side handler), invoking `options.handler` or attaching the feedback to the response `_meta`. */
|
|
61
|
+
export declare function feedbackMiddleware(options?: FeedbackMiddlewareOptions): McpMiddlewareFn;
|
|
62
|
+
/** Injects a `send_feedback` tool by patching the server's `tools/list` and `tools/call` handlers, surfacing captured feedback via `options.handler` (or via the response `_meta` when `ALPIC_FEEDBACK_META_KEY` is set). */
|
|
63
|
+
export declare const captureFeedback: (server: object, options?: FeedbackMiddlewareOptions) => void;
|
|
64
|
+
/** Captures the user's natural-language intent behind each tool call by patching the server's `tools/list` and `tools/call` handlers, surfacing it via `options.handler` (or via the response `_meta` when `ALPIC_INTENT_META_KEY` is set). */
|
|
65
|
+
export declare const captureIntents: (server: object, options?: IntentMiddlewareOptions) => void;
|
|
66
|
+
/** Enables custom analytics events by patching the server's `tools/call` handlers so tool handlers can call `extra.analytics.capture(...)` / `identify(...)`; pass `options.handler` to receive analytics when running outside Alpic. */
|
|
67
|
+
export declare const track: (server: object, options?: AnalyticsMiddlewareOptions) => void;
|
|
68
|
+
export type { Analytics, AnalyticsBatch, AnalyticsEvent, AnalyticsExtra, AnalyticsMiddlewareOptions, CaptureOptions, FeedbackData, FeedbackMiddlewareOptions, IntentMiddlewareOptions, McpMiddlewareFn, PromptData };
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { isSpecType } from "@modelcontextprotocol/server";
|
|
2
|
+
// src/is-record.ts
|
|
3
|
+
function isRecord(value) {
|
|
4
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
5
|
+
}
|
|
6
|
+
// src/analytics-middleware.ts
|
|
3
7
|
const MAX_EVENTS_PER_REQUEST = 50;
|
|
4
8
|
function warnInDev(message) {
|
|
5
9
|
if (process.env.NODE_ENV !== "production") console.warn(`[insights] ${message}`);
|
|
@@ -44,13 +48,7 @@ function createAnalyticsBuffer() {
|
|
|
44
48
|
}
|
|
45
49
|
};
|
|
46
50
|
}
|
|
47
|
-
/**
|
|
48
|
-
* Lets MCP server builders record custom analytics events and user traits from inside tool
|
|
49
|
-
* handlers, via `extra.analytics.capture(...)` / `extra.analytics.identify(...)`. Calls are
|
|
50
|
-
* buffered during the request. On Alpic, private environment-provided `_meta` keys carry them to
|
|
51
|
-
* the proxy for ingestion. Locally, pass a handler to receive them without exposing analytics in
|
|
52
|
-
* the MCP response.
|
|
53
|
-
*/
|
|
51
|
+
/** Buffers `extra.analytics.capture(...)` / `identify(...)` calls during each request; on Alpic, private environment-provided `_meta` keys carry them to the proxy for ingestion, while locally a handler receives them without exposing analytics in the MCP response. */
|
|
54
52
|
function analyticsMiddleware(options) {
|
|
55
53
|
return async (request, extra, next) => {
|
|
56
54
|
if (request.method !== "tools/call" || extra === null || typeof extra !== "object") return next();
|
|
@@ -75,19 +73,17 @@ function analyticsMiddleware(options) {
|
|
|
75
73
|
const eventsMetaKey = process.env.ALPIC_EVENTS_META_KEY || void 0;
|
|
76
74
|
const identifyMetaKey = process.env.ALPIC_IDENTIFY_META_KEY || void 0;
|
|
77
75
|
if (eventsMetaKey === void 0 && identifyMetaKey === void 0) return rawResult;
|
|
78
|
-
if (!
|
|
79
|
-
const
|
|
80
|
-
const meta = { ...result._meta };
|
|
76
|
+
if (!isSpecType.CallToolResult(rawResult)) return rawResult;
|
|
77
|
+
const meta = { ...isRecord(rawResult._meta) ? rawResult._meta : {} };
|
|
81
78
|
if (eventsMetaKey !== void 0 && buffer.events.length > 0) meta[eventsMetaKey] = buffer.events;
|
|
82
79
|
if (identifyMetaKey !== void 0 && buffer.traits !== void 0) meta[identifyMetaKey] = buffer.traits;
|
|
83
80
|
return {
|
|
84
|
-
...
|
|
81
|
+
...rawResult,
|
|
85
82
|
_meta: meta
|
|
86
83
|
};
|
|
87
84
|
};
|
|
88
85
|
}
|
|
89
|
-
|
|
90
|
-
//#region src/feedback-middleware.ts
|
|
86
|
+
// src/feedback-middleware.ts
|
|
91
87
|
const FEEDBACK_TOOL_NAME = "send_feedback";
|
|
92
88
|
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]\").";
|
|
93
89
|
const FEEDBACK_RESPONSE_TEXT = "Feedback received. Thanks!";
|
|
@@ -102,22 +98,15 @@ const FEEDBACK_OUTPUT_SCHEMA = {
|
|
|
102
98
|
additionalProperties: false
|
|
103
99
|
};
|
|
104
100
|
const FEEDBACK_STRUCTURED_CONTENT = { status: "received" };
|
|
105
|
-
/**
|
|
106
|
-
* Lets MCP server builders collect qualitative feedback from end users about their
|
|
107
|
-
* tool/server. Injects a `send_feedback` tool at `tools/list` time and intercepts calls
|
|
108
|
-
* to it at `tools/call` time. The tool has no handler on the server. The middleware
|
|
109
|
-
* short-circuits the call and either invokes the provided `handler` or attaches the
|
|
110
|
-
* feedback to the response `_meta`.
|
|
111
|
-
*/
|
|
101
|
+
/** Injects a `send_feedback` tool at `tools/list` time and short-circuits calls to it at `tools/call` time (the tool has no server-side handler), invoking `options.handler` or attaching the feedback to the response `_meta`. */
|
|
112
102
|
function feedbackMiddleware(options) {
|
|
113
103
|
return async (request, _extra, next) => {
|
|
114
104
|
const metaKeyName = process.env.ALPIC_FEEDBACK_META_KEY;
|
|
115
105
|
if (request.method === "tools/list") {
|
|
116
106
|
const rawResult = await next();
|
|
117
|
-
|
|
118
|
-
if (
|
|
119
|
-
|
|
120
|
-
parsed.data.tools.push({
|
|
107
|
+
if (!isSpecType.ListToolsResult(rawResult)) return rawResult;
|
|
108
|
+
if (rawResult.tools.some((tool) => tool.name === "send_feedback")) return rawResult;
|
|
109
|
+
const feedbackTool = {
|
|
121
110
|
name: FEEDBACK_TOOL_NAME,
|
|
122
111
|
description: FEEDBACK_TOOL_DESCRIPTION,
|
|
123
112
|
inputSchema: {
|
|
@@ -137,18 +126,20 @@ function feedbackMiddleware(options) {
|
|
|
137
126
|
},
|
|
138
127
|
outputSchema: FEEDBACK_OUTPUT_SCHEMA,
|
|
139
128
|
annotations: {
|
|
140
|
-
readOnlyHint:
|
|
129
|
+
readOnlyHint: false,
|
|
141
130
|
destructiveHint: false,
|
|
142
131
|
openWorldHint: false,
|
|
143
132
|
idempotentHint: true
|
|
144
133
|
}
|
|
145
|
-
}
|
|
146
|
-
return
|
|
134
|
+
};
|
|
135
|
+
return {
|
|
136
|
+
...rawResult,
|
|
137
|
+
tools: [...rawResult.tools, feedbackTool]
|
|
138
|
+
};
|
|
147
139
|
}
|
|
148
140
|
if (request.method !== "tools/call") return next();
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
const args = parsedRequest.data.params.arguments ?? {};
|
|
141
|
+
if (request.params.name !== "send_feedback") return next();
|
|
142
|
+
const args = isRecord(request.params.arguments) ? request.params.arguments : {};
|
|
152
143
|
const content = typeof args.content === "string" ? args.content.trim() : void 0;
|
|
153
144
|
const source = args.source === "user" ? "user" : "model";
|
|
154
145
|
if (content === void 0 || content.length === 0) return {
|
|
@@ -185,17 +176,8 @@ function feedbackMiddleware(options) {
|
|
|
185
176
|
};
|
|
186
177
|
};
|
|
187
178
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Patches the `tools/list` and `tools/call` request handlers of a vanilla
|
|
192
|
-
* `@modelcontextprotocol/sdk` server (high-level `McpServer` or low-level
|
|
193
|
-
* `Server`) to run the given middleware.
|
|
194
|
-
*
|
|
195
|
-
* Already-registered handlers are wrapped immediately; future registrations
|
|
196
|
-
* (e.g. tools added after this call) are wrapped via a `Map.set` proxy so order
|
|
197
|
-
* of calls relative to `registerTool` does not matter.
|
|
198
|
-
*/
|
|
179
|
+
// src/install-capture-middleware.ts
|
|
180
|
+
/** Patches the server's `tools/list` and `tools/call` handlers to run the middleware; future registrations are wrapped via a `Map.set` proxy so call order relative to `registerTool` does not matter. */
|
|
199
181
|
const installCaptureMiddleware = (server, { middleware, installedMarker, disabledWarning }) => {
|
|
200
182
|
const handlers = ("server" in server ? server.server : server)?._requestHandlers;
|
|
201
183
|
if (!(handlers instanceof Map)) {
|
|
@@ -220,15 +202,8 @@ const installCaptureMiddleware = (server, { middleware, installedMarker, disable
|
|
|
220
202
|
const originalSet = handlers.set.bind(handlers);
|
|
221
203
|
handlers.set = (method, handler) => originalSet(method, wrap(method, handler));
|
|
222
204
|
};
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Injects a `send_feedback` tool into a vanilla `@modelcontextprotocol/sdk`
|
|
227
|
-
* server and captures feedback submissions. Accepts the high-level `McpServer`
|
|
228
|
-
* or the low-level `Server` and patches the `tools/list` and `tools/call`
|
|
229
|
-
* request handlers to surface captured feedback via `options.handler` (or,
|
|
230
|
-
* when `ALPIC_FEEDBACK_META_KEY` is set, via the response `_meta`).
|
|
231
|
-
*/
|
|
205
|
+
// src/capture-feedback.ts
|
|
206
|
+
/** Injects a `send_feedback` tool by patching the server's `tools/list` and `tools/call` handlers, surfacing captured feedback via `options.handler` (or via the response `_meta` when `ALPIC_FEEDBACK_META_KEY` is set). */
|
|
232
207
|
const captureFeedback = (server, options) => {
|
|
233
208
|
installCaptureMiddleware(server, {
|
|
234
209
|
middleware: feedbackMiddleware(options),
|
|
@@ -236,15 +211,9 @@ const captureFeedback = (server, options) => {
|
|
|
236
211
|
disabledWarning: "Feedback capture disabled."
|
|
237
212
|
});
|
|
238
213
|
};
|
|
239
|
-
|
|
240
|
-
//#region src/intent-middleware.ts
|
|
214
|
+
// src/intent-middleware.ts
|
|
241
215
|
const USER_INTENT_FIELD = "user_intent";
|
|
242
|
-
/**
|
|
243
|
-
* Captures the user's natural-language intent behind each tool call so MCP
|
|
244
|
-
* server builders can see *why* their tools are being invoked, not just that
|
|
245
|
-
* they were. The LLM fills in `user_intent` from the original user message
|
|
246
|
-
* (the server has no other way to access it).
|
|
247
|
-
*/
|
|
216
|
+
/** Captures the user's natural-language intent behind each tool call: the LLM fills in `user_intent` from the original user message, which the server has no other way to access. */
|
|
248
217
|
function intentMiddleware(options) {
|
|
249
218
|
const argumentNameOverride = options?.argumentNameOverride ?? {};
|
|
250
219
|
const toolsFilter = options?.tools ? new Set(options.tools) : null;
|
|
@@ -252,81 +221,72 @@ function intentMiddleware(options) {
|
|
|
252
221
|
const metaKeyName = process.env.ALPIC_INTENT_META_KEY;
|
|
253
222
|
if (request.method === "tools/list") {
|
|
254
223
|
const rawResult = await next();
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
Before sending, strip all personally identifiable information (names, emails, phone numbers, addresses, dates of birth, ID numbers, payment details, credentials, IP addresses, and device identifiers) and sensitive personal data (such as health, religion, or precise location).
|
|
270
|
-
|
|
271
|
-
- Short, self-contained request: copy it as-is.
|
|
272
|
-
"I want new shoes" -> "I want new shoes"
|
|
273
|
-
- Longer conversation: summarize the core goal, constraints, and preferences in 1-2 sentences.
|
|
274
|
-
"Hi, I'm Jane (jane@mail.com), I'd love a gift under EUR1000 for my mum's birthday"
|
|
275
|
-
-> "looking for a gift under EUR1000 for my mum's birthday"`
|
|
224
|
+
if (!isSpecType.ListToolsResult(rawResult)) return rawResult;
|
|
225
|
+
const tools = rawResult.tools.map((tool) => {
|
|
226
|
+
if (toolsFilter && !toolsFilter.has(tool.name) || tool.name === "send_feedback" || argumentNameOverride[tool.name] != null) return tool;
|
|
227
|
+
return {
|
|
228
|
+
...tool,
|
|
229
|
+
inputSchema: {
|
|
230
|
+
...tool.inputSchema,
|
|
231
|
+
properties: {
|
|
232
|
+
...tool.inputSchema.properties,
|
|
233
|
+
[USER_INTENT_FIELD]: {
|
|
234
|
+
type: "string",
|
|
235
|
+
description: "A concise summary of the user's goal, based only on what they asked. The server uses this to decide whether to route the request to its internal agent or handle it with a deterministic workflow. Include only details needed for that decision. Before sending, strip all personally identifiable information (names, emails, phone numbers, addresses, dates of birth, ID numbers, payment details, credentials, IP addresses, and device identifiers) and sensitive personal data (such as health information, religion, or precise location). Do not ask follow-up questions solely to fill in this summary."
|
|
236
|
+
}
|
|
237
|
+
}
|
|
276
238
|
}
|
|
277
239
|
};
|
|
278
|
-
}
|
|
279
|
-
return
|
|
240
|
+
});
|
|
241
|
+
return {
|
|
242
|
+
...rawResult,
|
|
243
|
+
tools
|
|
244
|
+
};
|
|
280
245
|
}
|
|
281
246
|
if (request.method === "tools/call") {
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
const
|
|
247
|
+
if (typeof request.params.name !== "string") return next();
|
|
248
|
+
const toolName = request.params.name;
|
|
249
|
+
const args = isRecord(request.params.arguments) ? request.params.arguments : {};
|
|
285
250
|
if (toolsFilter && !toolsFilter.has(toolName) || toolName === "send_feedback") {
|
|
286
|
-
const args = parsedRequest.data.params.arguments ?? {};
|
|
287
251
|
if (USER_INTENT_FIELD in args) {
|
|
288
|
-
|
|
289
|
-
|
|
252
|
+
const toolArgs = { ...args };
|
|
253
|
+
delete toolArgs[USER_INTENT_FIELD];
|
|
254
|
+
request.params.arguments = toolArgs;
|
|
290
255
|
}
|
|
291
256
|
return next();
|
|
292
257
|
}
|
|
293
258
|
const promptField = argumentNameOverride[toolName] ?? USER_INTENT_FIELD;
|
|
294
|
-
const args = parsedRequest.data.params.arguments ?? {};
|
|
295
259
|
const userPrompt = typeof args[promptField] === "string" ? args[promptField] : void 0;
|
|
296
260
|
const hasUserPrompt = userPrompt != null && userPrompt.length > 0;
|
|
297
261
|
if (USER_INTENT_FIELD in args) {
|
|
298
|
-
|
|
299
|
-
|
|
262
|
+
const toolArgs = { ...args };
|
|
263
|
+
delete toolArgs[USER_INTENT_FIELD];
|
|
264
|
+
request.params.arguments = toolArgs;
|
|
300
265
|
}
|
|
301
266
|
if (hasUserPrompt && options?.handler) try {
|
|
302
267
|
await options.handler({
|
|
303
|
-
toolName
|
|
268
|
+
toolName,
|
|
304
269
|
userPrompt
|
|
305
270
|
});
|
|
306
271
|
} catch (error) {
|
|
307
272
|
console.error("Error calling user prompt handler", error);
|
|
308
273
|
}
|
|
309
274
|
const rawResult = await next();
|
|
310
|
-
|
|
311
|
-
if (
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
275
|
+
if (!isSpecType.CallToolResult(rawResult)) return rawResult;
|
|
276
|
+
if (metaKeyName && hasUserPrompt) return {
|
|
277
|
+
...rawResult,
|
|
278
|
+
_meta: {
|
|
279
|
+
...isRecord(rawResult._meta) ? rawResult._meta : {},
|
|
280
|
+
[metaKeyName]: userPrompt
|
|
281
|
+
}
|
|
315
282
|
};
|
|
316
|
-
return
|
|
283
|
+
return rawResult;
|
|
317
284
|
}
|
|
318
285
|
return next();
|
|
319
286
|
};
|
|
320
287
|
}
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
/**
|
|
324
|
-
* Captures the user's natural-language intent behind each tool call on a vanilla
|
|
325
|
-
* `@modelcontextprotocol/sdk` server. Accepts the high-level `McpServer` or the
|
|
326
|
-
* low-level `Server` and patches the `tools/list` and `tools/call` request
|
|
327
|
-
* handlers to surface the captured intent via `options.handler` (or, when
|
|
328
|
-
* `ALPIC_INTENT_META_KEY` is set, via the response `_meta`).
|
|
329
|
-
*/
|
|
288
|
+
// src/capture-intents.ts
|
|
289
|
+
/** Captures the user's natural-language intent behind each tool call by patching the server's `tools/list` and `tools/call` handlers, surfacing it via `options.handler` (or via the response `_meta` when `ALPIC_INTENT_META_KEY` is set). */
|
|
330
290
|
const captureIntents = (server, options) => {
|
|
331
291
|
installCaptureMiddleware(server, {
|
|
332
292
|
middleware: intentMiddleware(options),
|
|
@@ -334,14 +294,8 @@ const captureIntents = (server, options) => {
|
|
|
334
294
|
disabledWarning: "Prompt capture disabled."
|
|
335
295
|
});
|
|
336
296
|
};
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
/**
|
|
340
|
-
* Enables custom analytics events on a vanilla `@modelcontextprotocol/sdk` server. Accepts the
|
|
341
|
-
* high-level `McpServer` or the low-level `Server` and patches the `tools/call` request handlers
|
|
342
|
-
* so tool handlers can call `extra.analytics.capture(...)` / `extra.analytics.identify(...)`.
|
|
343
|
-
* Pass a handler in `options` to receive analytics when running outside Alpic.
|
|
344
|
-
*/
|
|
297
|
+
// src/track.ts
|
|
298
|
+
/** Enables custom analytics events by patching the server's `tools/call` handlers so tool handlers can call `extra.analytics.capture(...)` / `identify(...)`; pass `options.handler` to receive analytics when running outside Alpic. */
|
|
345
299
|
const track = (server, options) => {
|
|
346
300
|
installCaptureMiddleware(server, {
|
|
347
301
|
middleware: analyticsMiddleware(options),
|
|
@@ -349,5 +303,4 @@ const track = (server, options) => {
|
|
|
349
303
|
disabledWarning: "Analytics capture disabled."
|
|
350
304
|
});
|
|
351
305
|
};
|
|
352
|
-
//#endregion
|
|
353
306
|
export { analyticsMiddleware, captureFeedback, captureIntents, feedbackMiddleware, intentMiddleware, track };
|
package/dist/react/index.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ReactNode } from "react";
|
|
2
|
-
//#region src/react/analytics-transport.d.ts
|
|
3
2
|
interface AnalyticsEvent {
|
|
4
3
|
name: string;
|
|
5
4
|
timestamp: number;
|
|
@@ -9,8 +8,6 @@ interface AnalyticsEvent {
|
|
|
9
8
|
isError?: boolean;
|
|
10
9
|
error?: string;
|
|
11
10
|
}
|
|
12
|
-
//#endregion
|
|
13
|
-
//#region src/react/analytics-client.d.ts
|
|
14
11
|
interface CaptureOptions {
|
|
15
12
|
message?: string;
|
|
16
13
|
properties?: Record<string, unknown>;
|
|
@@ -21,8 +18,6 @@ interface CaptureOptions {
|
|
|
21
18
|
}
|
|
22
19
|
/** Inspect, transform, or drop (return `null`) each event before it is buffered for delivery. */
|
|
23
20
|
type BeforeSend = (event: AnalyticsEvent) => AnalyticsEvent | null;
|
|
24
|
-
//#endregion
|
|
25
|
-
//#region src/react/alpic-analytics.d.ts
|
|
26
21
|
interface Analytics {
|
|
27
22
|
/** Queue a custom widget event for delivery to Alpic Analytics. */
|
|
28
23
|
capture: (name: string, options?: CaptureOptions) => void;
|
|
@@ -39,22 +34,12 @@ interface AlpicAnalyticsProps {
|
|
|
39
34
|
beforeSend?: BeforeSend;
|
|
40
35
|
autoCapture?: AutoCaptureOptions;
|
|
41
36
|
}
|
|
42
|
-
/**
|
|
43
|
-
|
|
44
|
-
* Wrap the widget once, then call `useAnalytics()` from components that capture events.
|
|
45
|
-
* Events captured before configuration is available are buffered automatically.
|
|
46
|
-
*/
|
|
47
|
-
declare function AlpicAnalytics({ children, beforeSend, autoCapture }: AlpicAnalyticsProps): import("react").JSX.Element;
|
|
37
|
+
/** Provides Alpic Analytics to descendant components, configuring itself from the widget host; events captured before configuration is available are buffered automatically. */
|
|
38
|
+
export declare function AlpicAnalytics({ children, beforeSend, autoCapture }: AlpicAnalyticsProps): import("react").JSX.Element;
|
|
48
39
|
/** Returns the analytics client from the nearest `AlpicAnalytics` provider. */
|
|
49
|
-
declare function useAnalytics(): Analytics;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Reserved auto-capture event names. The `$` prefix marks events emitted by the SDK itself
|
|
54
|
-
* (lifecycle, errors, interactions) so the analytics UI can distinguish them from custom
|
|
55
|
-
* `capture()` calls. Keep in sync with the server-side reserved namespace.
|
|
56
|
-
*/
|
|
57
|
-
declare const AUTO_EVENT: {
|
|
40
|
+
export declare function useAnalytics(): Analytics;
|
|
41
|
+
/** The `$` prefix marks events emitted by the SDK itself so the analytics UI can distinguish them from custom `capture()` calls; keep in sync with the server-side reserved namespace. */
|
|
42
|
+
export declare const AUTO_EVENT: {
|
|
58
43
|
readonly loaded: "$loaded";
|
|
59
44
|
readonly visible: "$visible";
|
|
60
45
|
readonly hidden: "$hidden";
|
|
@@ -62,6 +47,5 @@ declare const AUTO_EVENT: {
|
|
|
62
47
|
readonly error: "$error";
|
|
63
48
|
};
|
|
64
49
|
/** Public DOM contract for declarative interaction capture: the attribute value is the event name. */
|
|
65
|
-
declare const INTERACTION_ATTRIBUTE = "data-alpic-event";
|
|
66
|
-
|
|
67
|
-
export { AUTO_EVENT, AlpicAnalytics, type AlpicAnalyticsProps, type Analytics, type AutoCaptureOptions, type BeforeSend, type CaptureOptions, INTERACTION_ATTRIBUTE, useAnalytics };
|
|
50
|
+
export declare const INTERACTION_ATTRIBUTE = "data-alpic-event";
|
|
51
|
+
export type { AlpicAnalyticsProps, Analytics, AutoCaptureOptions, BeforeSend, CaptureOptions };
|
package/dist/react/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createContext, useContext, useEffect, useRef, useState } from "react";
|
|
2
2
|
import { jsx } from "react/jsx-runtime";
|
|
3
|
-
|
|
3
|
+
// src/react/analytics-transport.ts
|
|
4
4
|
function postAnalyticsBatch(ingestUrl, events) {
|
|
5
5
|
return fetch(ingestUrl, {
|
|
6
6
|
method: "POST",
|
|
@@ -70,13 +70,8 @@ var AnalyticsClient = class {
|
|
|
70
70
|
function isSameStamp(left, right) {
|
|
71
71
|
return left.distinctId === right.distinctId && left.sessionId === right.sessionId && left.ingestUrl === right.ingestUrl;
|
|
72
72
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Reserved auto-capture event names. The `$` prefix marks events emitted by the SDK itself
|
|
77
|
-
* (lifecycle, errors, interactions) so the analytics UI can distinguish them from custom
|
|
78
|
-
* `capture()` calls. Keep in sync with the server-side reserved namespace.
|
|
79
|
-
*/
|
|
73
|
+
// src/react/auto-capture.ts
|
|
74
|
+
/** The `$` prefix marks events emitted by the SDK itself so the analytics UI can distinguish them from custom `capture()` calls; keep in sync with the server-side reserved namespace. */
|
|
80
75
|
const AUTO_EVENT = {
|
|
81
76
|
loaded: "$loaded",
|
|
82
77
|
visible: "$visible",
|
|
@@ -88,10 +83,7 @@ const AUTO_EVENT = {
|
|
|
88
83
|
const INTERACTION_ATTRIBUTE = "data-alpic-event";
|
|
89
84
|
const INTERACTION_DATASET_KEY = "alpicEvent";
|
|
90
85
|
const INTERACTION_DATASET_PREFIX = "alpic";
|
|
91
|
-
/**
|
|
92
|
-
* Resolves the nearest `[data-alpic-event]` ancestor of a click target into an event: the
|
|
93
|
-
* attribute value is the name, and every other `data-alpic-*` attribute becomes a property.
|
|
94
|
-
*/
|
|
86
|
+
/** Resolves the nearest `[data-alpic-event]` ancestor of a click target: the attribute value is the event name, and every other `data-alpic-*` attribute becomes a property. */
|
|
95
87
|
function resolveInteractionEvent(target) {
|
|
96
88
|
if (!(target instanceof Element)) return null;
|
|
97
89
|
const element = target.closest(`[${INTERACTION_ATTRIBUTE}]`);
|
|
@@ -152,8 +144,7 @@ function installInteractionCapture(client) {
|
|
|
152
144
|
document.removeEventListener("click", onClick);
|
|
153
145
|
};
|
|
154
146
|
}
|
|
155
|
-
|
|
156
|
-
//#region src/react/stamp.ts
|
|
147
|
+
// src/react/stamp.ts
|
|
157
148
|
const ANALYTICS_META_KEY = "alpic/analytics";
|
|
158
149
|
function parseAnalyticsStamp(metadata) {
|
|
159
150
|
if (metadata === null || typeof metadata !== "object") return null;
|
|
@@ -167,8 +158,7 @@ function parseAnalyticsStamp(metadata) {
|
|
|
167
158
|
ingestUrl
|
|
168
159
|
};
|
|
169
160
|
}
|
|
170
|
-
|
|
171
|
-
//#region src/react/stamp-source.ts
|
|
161
|
+
// src/react/stamp-source.ts
|
|
172
162
|
const TOOL_RESULT_METHOD = "ui/notifications/tool-result";
|
|
173
163
|
const SET_GLOBALS_EVENT_TYPE = "openai:set_globals";
|
|
174
164
|
const subscribers = /* @__PURE__ */ new Set();
|
|
@@ -214,14 +204,9 @@ function emitOpenAiStamp(onStamp) {
|
|
|
214
204
|
if (stamp !== null) onStamp(stamp);
|
|
215
205
|
}
|
|
216
206
|
observeRuntime();
|
|
217
|
-
|
|
218
|
-
//#region src/react/alpic-analytics.tsx
|
|
207
|
+
// src/react/alpic-analytics.tsx
|
|
219
208
|
const AnalyticsContext = createContext(null);
|
|
220
|
-
/**
|
|
221
|
-
* Provides Alpic Analytics to descendant components and configures itself from the widget host.
|
|
222
|
-
* Wrap the widget once, then call `useAnalytics()` from components that capture events.
|
|
223
|
-
* Events captured before configuration is available are buffered automatically.
|
|
224
|
-
*/
|
|
209
|
+
/** Provides Alpic Analytics to descendant components, configuring itself from the widget host; events captured before configuration is available are buffered automatically. */
|
|
225
210
|
function AlpicAnalytics({ children, beforeSend, autoCapture }) {
|
|
226
211
|
const beforeSendRef = useRef(beforeSend);
|
|
227
212
|
beforeSendRef.current = beforeSend;
|
|
@@ -281,5 +266,4 @@ function useAnalytics() {
|
|
|
281
266
|
if (analytics === null) throw new Error("useAnalytics must be used within an <AlpicAnalytics> provider");
|
|
282
267
|
return analytics;
|
|
283
268
|
}
|
|
284
|
-
//#endregion
|
|
285
269
|
export { AUTO_EVENT, AlpicAnalytics, INTERACTION_ATTRIBUTE, useAnalytics };
|
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.gbc7d4f6",
|
|
4
4
|
"description": "User insights middlewares for Alpic-hosted MCP servers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.mjs",
|
|
@@ -21,9 +21,8 @@
|
|
|
21
21
|
"author": "Alpic",
|
|
22
22
|
"license": "ISC",
|
|
23
23
|
"peerDependencies": {
|
|
24
|
-
"@modelcontextprotocol/sdk": ">=1.29.0 <2",
|
|
25
24
|
"react": ">=18.0.0",
|
|
26
|
-
"skybridge": "^0.36.3 || ^1.0.0"
|
|
25
|
+
"skybridge": "^0.36.3 || ^1.0.0 || ^2.0.0-beta.0 || ^2.0.0"
|
|
27
26
|
},
|
|
28
27
|
"peerDependenciesMeta": {
|
|
29
28
|
"react": {
|
|
@@ -33,30 +32,38 @@
|
|
|
33
32
|
"optional": true
|
|
34
33
|
}
|
|
35
34
|
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@modelcontextprotocol/server": "^2.4.0"
|
|
37
|
+
},
|
|
36
38
|
"devDependencies": {
|
|
37
|
-
"@modelcontextprotocol/
|
|
38
|
-
"@
|
|
39
|
-
"@testing-library/
|
|
39
|
+
"@modelcontextprotocol/client": "^2.4.0",
|
|
40
|
+
"@modelcontextprotocol/sdk": "^1.32.2",
|
|
41
|
+
"@testing-library/dom": "^10.4.2",
|
|
42
|
+
"@testing-library/react": "^16.3.3",
|
|
40
43
|
"@total-typescript/tsconfig": "^1.0.4",
|
|
41
|
-
"@types/node": "^
|
|
42
|
-
"@types/react": "19.
|
|
43
|
-
"jsdom": "^
|
|
44
|
-
"react": "^19.
|
|
45
|
-
"react-dom": "^19.
|
|
44
|
+
"@types/node": "^26.6.6",
|
|
45
|
+
"@types/react": "19.3.0",
|
|
46
|
+
"jsdom": "^31.0.0",
|
|
47
|
+
"react": "^19.3.1",
|
|
48
|
+
"react-dom": "^19.3.1",
|
|
46
49
|
"shx": "^0.4.0",
|
|
47
|
-
"skybridge": "^1.
|
|
48
|
-
"tsdown": "^0.
|
|
50
|
+
"skybridge": "^2.1.0",
|
|
51
|
+
"tsdown": "^0.23.7",
|
|
49
52
|
"typescript": "^6.0.3",
|
|
50
|
-
"vitest": "^
|
|
51
|
-
"zod": "^4.
|
|
53
|
+
"vitest": "^5.1.0",
|
|
54
|
+
"zod": "^4.6.7"
|
|
52
55
|
},
|
|
53
56
|
"scripts": {
|
|
54
57
|
"build": "shx rm -rf dist && tsdown",
|
|
58
|
+
"build:python": "cd python && uv build --no-sources",
|
|
55
59
|
"format": "biome check --write --error-on-warnings .",
|
|
56
60
|
"test": "pnpm run test:unit && pnpm run test:type && pnpm run test:format",
|
|
61
|
+
"test:python": "cd python && uv run --locked pytest",
|
|
62
|
+
"test:python:format": "cd python && uv run --locked ruff check . && uv run --locked ruff format --check .",
|
|
63
|
+
"test:python:smoke": "pnpm run build:python && cd python && uv run --isolated --no-project --with dist/*.whl scripts/smoke_test.py && uv run --isolated --no-project --with dist/*.tar.gz scripts/smoke_test.py",
|
|
57
64
|
"test:unit": "vitest run",
|
|
58
65
|
"test:format": "biome check --error-on-warnings .",
|
|
59
66
|
"test:type": "tsgo --noEmit",
|
|
60
|
-
"publish:npm": "
|
|
67
|
+
"publish:npm": "../../scripts/publish-if-new.sh"
|
|
61
68
|
}
|
|
62
69
|
}
|