@alpic-ai/insights 0.0.0-dev.ge3eb38c → 0.0.0-dev.ge40c969
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 +9 -11
- package/dist/index.mjs +56 -53
- package/dist/react/index.d.mts +5 -5
- package/package.json +19 -16
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
1
|
+
import { ServerContext } from "@modelcontextprotocol/server";
|
|
3
2
|
//#region src/intent-middleware.d.ts
|
|
4
3
|
interface PromptData {
|
|
5
4
|
toolName: string;
|
|
@@ -18,7 +17,7 @@ type McpMiddlewareFn = (request: {
|
|
|
18
17
|
params: Record<string, unknown>;
|
|
19
18
|
}, extra: unknown, next: () => Promise<unknown>) => Promise<unknown> | unknown;
|
|
20
19
|
/** 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. */
|
|
21
|
-
declare function intentMiddleware(options?: IntentMiddlewareOptions): McpMiddlewareFn;
|
|
20
|
+
export declare function intentMiddleware(options?: IntentMiddlewareOptions): McpMiddlewareFn;
|
|
22
21
|
//#endregion
|
|
23
22
|
//#region src/analytics-middleware.d.ts
|
|
24
23
|
interface CaptureOptions {
|
|
@@ -36,8 +35,7 @@ interface Analytics {
|
|
|
36
35
|
/** 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. */
|
|
37
36
|
identify(traits: Record<string, string>): void;
|
|
38
37
|
}
|
|
39
|
-
|
|
40
|
-
interface AnalyticsExtra {
|
|
38
|
+
interface AnalyticsExtra extends ServerContext {
|
|
41
39
|
analytics: Analytics;
|
|
42
40
|
}
|
|
43
41
|
interface AnalyticsEvent extends CaptureOptions {
|
|
@@ -53,7 +51,7 @@ interface AnalyticsMiddlewareOptions {
|
|
|
53
51
|
handler?: (batch: AnalyticsBatch) => Promise<void> | void;
|
|
54
52
|
}
|
|
55
53
|
/** 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. */
|
|
56
|
-
declare function analyticsMiddleware(options?: AnalyticsMiddlewareOptions): McpMiddlewareFn;
|
|
54
|
+
export declare function analyticsMiddleware(options?: AnalyticsMiddlewareOptions): McpMiddlewareFn;
|
|
57
55
|
//#endregion
|
|
58
56
|
//#region src/feedback-middleware.d.ts
|
|
59
57
|
interface FeedbackData {
|
|
@@ -65,18 +63,18 @@ interface FeedbackMiddlewareOptions {
|
|
|
65
63
|
handler?: (feedback: FeedbackData) => Promise<void> | void;
|
|
66
64
|
}
|
|
67
65
|
/** 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`. */
|
|
68
|
-
declare function feedbackMiddleware(options?: FeedbackMiddlewareOptions): McpMiddlewareFn;
|
|
66
|
+
export declare function feedbackMiddleware(options?: FeedbackMiddlewareOptions): McpMiddlewareFn;
|
|
69
67
|
//#endregion
|
|
70
68
|
//#region src/capture-feedback.d.ts
|
|
71
69
|
/** 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). */
|
|
72
|
-
declare const captureFeedback: (server:
|
|
70
|
+
export declare const captureFeedback: (server: object, options?: FeedbackMiddlewareOptions) => void;
|
|
73
71
|
//#endregion
|
|
74
72
|
//#region src/capture-intents.d.ts
|
|
75
73
|
/** 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). */
|
|
76
|
-
declare const captureIntents: (server:
|
|
74
|
+
export declare const captureIntents: (server: object, options?: IntentMiddlewareOptions) => void;
|
|
77
75
|
//#endregion
|
|
78
76
|
//#region src/track.d.ts
|
|
79
77
|
/** 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. */
|
|
80
|
-
declare const track: (server:
|
|
78
|
+
export declare const track: (server: object, options?: AnalyticsMiddlewareOptions) => void;
|
|
81
79
|
//#endregion
|
|
82
|
-
export {
|
|
80
|
+
export type { Analytics, AnalyticsBatch, AnalyticsEvent, AnalyticsExtra, AnalyticsMiddlewareOptions, CaptureOptions, FeedbackData, FeedbackMiddlewareOptions, IntentMiddlewareOptions, McpMiddlewareFn, PromptData };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isSpecType } from "@modelcontextprotocol/server";
|
|
2
|
+
//#region src/is-record.ts
|
|
3
|
+
function isRecord(value) {
|
|
4
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
5
|
+
}
|
|
6
|
+
//#endregion
|
|
2
7
|
//#region src/analytics-middleware.ts
|
|
3
8
|
const MAX_EVENTS_PER_REQUEST = 50;
|
|
4
9
|
function warnInDev(message) {
|
|
@@ -69,13 +74,12 @@ function analyticsMiddleware(options) {
|
|
|
69
74
|
const eventsMetaKey = process.env.ALPIC_EVENTS_META_KEY || void 0;
|
|
70
75
|
const identifyMetaKey = process.env.ALPIC_IDENTIFY_META_KEY || void 0;
|
|
71
76
|
if (eventsMetaKey === void 0 && identifyMetaKey === void 0) return rawResult;
|
|
72
|
-
if (!
|
|
73
|
-
const
|
|
74
|
-
const meta = { ...result._meta };
|
|
77
|
+
if (!isSpecType.CallToolResult(rawResult)) return rawResult;
|
|
78
|
+
const meta = { ...isRecord(rawResult._meta) ? rawResult._meta : {} };
|
|
75
79
|
if (eventsMetaKey !== void 0 && buffer.events.length > 0) meta[eventsMetaKey] = buffer.events;
|
|
76
80
|
if (identifyMetaKey !== void 0 && buffer.traits !== void 0) meta[identifyMetaKey] = buffer.traits;
|
|
77
81
|
return {
|
|
78
|
-
...
|
|
82
|
+
...rawResult,
|
|
79
83
|
_meta: meta
|
|
80
84
|
};
|
|
81
85
|
};
|
|
@@ -102,10 +106,9 @@ function feedbackMiddleware(options) {
|
|
|
102
106
|
const metaKeyName = process.env.ALPIC_FEEDBACK_META_KEY;
|
|
103
107
|
if (request.method === "tools/list") {
|
|
104
108
|
const rawResult = await next();
|
|
105
|
-
|
|
106
|
-
if (
|
|
107
|
-
|
|
108
|
-
parsed.data.tools.push({
|
|
109
|
+
if (!isSpecType.ListToolsResult(rawResult)) return rawResult;
|
|
110
|
+
if (rawResult.tools.some((tool) => tool.name === "send_feedback")) return rawResult;
|
|
111
|
+
const feedbackTool = {
|
|
109
112
|
name: FEEDBACK_TOOL_NAME,
|
|
110
113
|
description: FEEDBACK_TOOL_DESCRIPTION,
|
|
111
114
|
inputSchema: {
|
|
@@ -130,13 +133,15 @@ function feedbackMiddleware(options) {
|
|
|
130
133
|
openWorldHint: false,
|
|
131
134
|
idempotentHint: true
|
|
132
135
|
}
|
|
133
|
-
}
|
|
134
|
-
return
|
|
136
|
+
};
|
|
137
|
+
return {
|
|
138
|
+
...rawResult,
|
|
139
|
+
tools: [...rawResult.tools, feedbackTool]
|
|
140
|
+
};
|
|
135
141
|
}
|
|
136
142
|
if (request.method !== "tools/call") return next();
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
const args = parsedRequest.data.params.arguments ?? {};
|
|
143
|
+
if (request.params.name !== "send_feedback") return next();
|
|
144
|
+
const args = isRecord(request.params.arguments) ? request.params.arguments : {};
|
|
140
145
|
const content = typeof args.content === "string" ? args.content.trim() : void 0;
|
|
141
146
|
const source = args.source === "user" ? "user" : "model";
|
|
142
147
|
if (content === void 0 || content.length === 0) return {
|
|
@@ -221,68 +226,66 @@ function intentMiddleware(options) {
|
|
|
221
226
|
const metaKeyName = process.env.ALPIC_INTENT_META_KEY;
|
|
222
227
|
if (request.method === "tools/list") {
|
|
223
228
|
const rawResult = await next();
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
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).
|
|
239
|
-
|
|
240
|
-
- Short, self-contained request: copy it as-is.
|
|
241
|
-
"I want new shoes" -> "I want new shoes"
|
|
242
|
-
- Longer conversation: summarize the core goal, constraints, and preferences in 1-2 sentences.
|
|
243
|
-
"Hi, I'm Jane (jane@mail.com), I'd love a gift under EUR1000 for my mum's birthday"
|
|
244
|
-
-> "looking for a gift under EUR1000 for my mum's birthday"`
|
|
229
|
+
if (!isSpecType.ListToolsResult(rawResult)) return rawResult;
|
|
230
|
+
const tools = rawResult.tools.map((tool) => {
|
|
231
|
+
if (toolsFilter && !toolsFilter.has(tool.name) || tool.name === "send_feedback" || argumentNameOverride[tool.name] != null) return tool;
|
|
232
|
+
return {
|
|
233
|
+
...tool,
|
|
234
|
+
inputSchema: {
|
|
235
|
+
...tool.inputSchema,
|
|
236
|
+
properties: {
|
|
237
|
+
...tool.inputSchema.properties,
|
|
238
|
+
[USER_INTENT_FIELD]: {
|
|
239
|
+
type: "string",
|
|
240
|
+
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."
|
|
241
|
+
}
|
|
242
|
+
}
|
|
245
243
|
}
|
|
246
244
|
};
|
|
247
|
-
}
|
|
248
|
-
return
|
|
245
|
+
});
|
|
246
|
+
return {
|
|
247
|
+
...rawResult,
|
|
248
|
+
tools
|
|
249
|
+
};
|
|
249
250
|
}
|
|
250
251
|
if (request.method === "tools/call") {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const
|
|
252
|
+
if (typeof request.params.name !== "string") return next();
|
|
253
|
+
const toolName = request.params.name;
|
|
254
|
+
const args = isRecord(request.params.arguments) ? request.params.arguments : {};
|
|
254
255
|
if (toolsFilter && !toolsFilter.has(toolName) || toolName === "send_feedback") {
|
|
255
|
-
const args = parsedRequest.data.params.arguments ?? {};
|
|
256
256
|
if (USER_INTENT_FIELD in args) {
|
|
257
|
-
|
|
258
|
-
|
|
257
|
+
const toolArgs = { ...args };
|
|
258
|
+
delete toolArgs[USER_INTENT_FIELD];
|
|
259
|
+
request.params.arguments = toolArgs;
|
|
259
260
|
}
|
|
260
261
|
return next();
|
|
261
262
|
}
|
|
262
263
|
const promptField = argumentNameOverride[toolName] ?? USER_INTENT_FIELD;
|
|
263
|
-
const args = parsedRequest.data.params.arguments ?? {};
|
|
264
264
|
const userPrompt = typeof args[promptField] === "string" ? args[promptField] : void 0;
|
|
265
265
|
const hasUserPrompt = userPrompt != null && userPrompt.length > 0;
|
|
266
266
|
if (USER_INTENT_FIELD in args) {
|
|
267
|
-
|
|
268
|
-
|
|
267
|
+
const toolArgs = { ...args };
|
|
268
|
+
delete toolArgs[USER_INTENT_FIELD];
|
|
269
|
+
request.params.arguments = toolArgs;
|
|
269
270
|
}
|
|
270
271
|
if (hasUserPrompt && options?.handler) try {
|
|
271
272
|
await options.handler({
|
|
272
|
-
toolName
|
|
273
|
+
toolName,
|
|
273
274
|
userPrompt
|
|
274
275
|
});
|
|
275
276
|
} catch (error) {
|
|
276
277
|
console.error("Error calling user prompt handler", error);
|
|
277
278
|
}
|
|
278
279
|
const rawResult = await next();
|
|
279
|
-
|
|
280
|
-
if (
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
280
|
+
if (!isSpecType.CallToolResult(rawResult)) return rawResult;
|
|
281
|
+
if (metaKeyName && hasUserPrompt) return {
|
|
282
|
+
...rawResult,
|
|
283
|
+
_meta: {
|
|
284
|
+
...isRecord(rawResult._meta) ? rawResult._meta : {},
|
|
285
|
+
[metaKeyName]: userPrompt
|
|
286
|
+
}
|
|
284
287
|
};
|
|
285
|
-
return
|
|
288
|
+
return rawResult;
|
|
286
289
|
}
|
|
287
290
|
return next();
|
|
288
291
|
};
|
package/dist/react/index.d.mts
CHANGED
|
@@ -40,13 +40,13 @@ interface AlpicAnalyticsProps {
|
|
|
40
40
|
autoCapture?: AutoCaptureOptions;
|
|
41
41
|
}
|
|
42
42
|
/** Provides Alpic Analytics to descendant components, configuring itself from the widget host; events captured before configuration is available are buffered automatically. */
|
|
43
|
-
declare function AlpicAnalytics({ children, beforeSend, autoCapture }: AlpicAnalyticsProps): import("react").JSX.Element;
|
|
43
|
+
export declare function AlpicAnalytics({ children, beforeSend, autoCapture }: AlpicAnalyticsProps): import("react").JSX.Element;
|
|
44
44
|
/** Returns the analytics client from the nearest `AlpicAnalytics` provider. */
|
|
45
|
-
declare function useAnalytics(): Analytics;
|
|
45
|
+
export declare function useAnalytics(): Analytics;
|
|
46
46
|
//#endregion
|
|
47
47
|
//#region src/react/auto-capture.d.ts
|
|
48
48
|
/** 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. */
|
|
49
|
-
declare const AUTO_EVENT: {
|
|
49
|
+
export declare const AUTO_EVENT: {
|
|
50
50
|
readonly loaded: "$loaded";
|
|
51
51
|
readonly visible: "$visible";
|
|
52
52
|
readonly hidden: "$hidden";
|
|
@@ -54,6 +54,6 @@ declare const AUTO_EVENT: {
|
|
|
54
54
|
readonly error: "$error";
|
|
55
55
|
};
|
|
56
56
|
/** Public DOM contract for declarative interaction capture: the attribute value is the event name. */
|
|
57
|
-
declare const INTERACTION_ATTRIBUTE = "data-alpic-event";
|
|
57
|
+
export declare const INTERACTION_ATTRIBUTE = "data-alpic-event";
|
|
58
58
|
//#endregion
|
|
59
|
-
export {
|
|
59
|
+
export type { AlpicAnalyticsProps, Analytics, AutoCaptureOptions, BeforeSend, CaptureOptions };
|
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.ge40c969",
|
|
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,22 +32,26 @@
|
|
|
33
32
|
"optional": true
|
|
34
33
|
}
|
|
35
34
|
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@modelcontextprotocol/server": "^2.3.1"
|
|
37
|
+
},
|
|
36
38
|
"devDependencies": {
|
|
37
|
-
"@modelcontextprotocol/
|
|
38
|
-
"@
|
|
39
|
-
"@testing-library/
|
|
39
|
+
"@modelcontextprotocol/client": "^2.3.1",
|
|
40
|
+
"@modelcontextprotocol/sdk": "^1.32.1",
|
|
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": "^25.9.
|
|
42
|
-
"@types/react": "19.
|
|
43
|
-
"jsdom": "^30.
|
|
44
|
-
"react": "^19.
|
|
45
|
-
"react-dom": "^19.
|
|
44
|
+
"@types/node": "^25.9.9",
|
|
45
|
+
"@types/react": "19.3.0",
|
|
46
|
+
"jsdom": "^30.1.3",
|
|
47
|
+
"react": "^19.3.1",
|
|
48
|
+
"react-dom": "^19.3.1",
|
|
46
49
|
"shx": "^0.4.0",
|
|
47
|
-
"skybridge": "^
|
|
48
|
-
"tsdown": "^0.
|
|
50
|
+
"skybridge": "^2.0.4",
|
|
51
|
+
"tsdown": "^0.23.1",
|
|
49
52
|
"typescript": "^6.0.3",
|
|
50
|
-
"vitest": "^
|
|
51
|
-
"zod": "^4.
|
|
53
|
+
"vitest": "^5.0.5",
|
|
54
|
+
"zod": "^4.6.6"
|
|
52
55
|
},
|
|
53
56
|
"scripts": {
|
|
54
57
|
"build": "shx rm -rf dist && tsdown",
|
|
@@ -61,6 +64,6 @@
|
|
|
61
64
|
"test:unit": "vitest run",
|
|
62
65
|
"test:format": "biome check --error-on-warnings .",
|
|
63
66
|
"test:type": "tsgo --noEmit",
|
|
64
|
-
"publish:npm": "
|
|
67
|
+
"publish:npm": "../../scripts/publish-if-new.sh"
|
|
65
68
|
}
|
|
66
69
|
}
|