@alpic-ai/insights 0.0.0-dev.gde89265 → 0.0.0-dev.gde915af

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 CHANGED
@@ -1,5 +1,4 @@
1
- import { Server } from "@modelcontextprotocol/sdk/server/index.js";
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
- /** Shape of the handler `extra` once `analyticsMiddleware()` (or `track(server)`) is installed; cast the handler's `extra` to this to access `analytics` with types. */
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: McpServer | Server, options?: FeedbackMiddlewareOptions) => void;
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: McpServer | Server, options?: IntentMiddlewareOptions) => void;
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: McpServer | Server, options?: AnalyticsMiddlewareOptions) => void;
78
+ export declare const track: (server: object, options?: AnalyticsMiddlewareOptions) => void;
81
79
  //#endregion
82
- 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 };
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 { CallToolRequestSchema, CallToolResultSchema, ListToolsResultSchema } from "@modelcontextprotocol/sdk/types.js";
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 (!CallToolResultSchema.safeParse(rawResult).success) return rawResult;
73
- const result = rawResult;
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
- ...result,
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
- const parsed = ListToolsResultSchema.safeParse(rawResult);
106
- if (!parsed.success) return rawResult;
107
- if (parsed.data.tools.some((tool) => tool.name === "send_feedback")) return parsed.data;
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: {
@@ -125,18 +128,20 @@ function feedbackMiddleware(options) {
125
128
  },
126
129
  outputSchema: FEEDBACK_OUTPUT_SCHEMA,
127
130
  annotations: {
128
- readOnlyHint: true,
131
+ readOnlyHint: false,
129
132
  destructiveHint: false,
130
133
  openWorldHint: false,
131
134
  idempotentHint: true
132
135
  }
133
- });
134
- return parsed.data;
136
+ };
137
+ return {
138
+ ...rawResult,
139
+ tools: [...rawResult.tools, feedbackTool]
140
+ };
135
141
  }
136
142
  if (request.method !== "tools/call") return next();
137
- const parsedRequest = CallToolRequestSchema.safeParse(request);
138
- if (!parsedRequest.success || parsedRequest.data.params.name !== "send_feedback") return next();
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
- const parsed = ListToolsResultSchema.safeParse(rawResult);
225
- if (!parsed.success) return rawResult;
226
- for (const tool of parsed.data.tools) {
227
- if (toolsFilter && !toolsFilter.has(tool.name)) continue;
228
- if (tool.name === "send_feedback") continue;
229
- if (argumentNameOverride[tool.name] != null) continue;
230
- tool.inputSchema.properties = {
231
- ...tool.inputSchema.properties,
232
- [USER_INTENT_FIELD]: {
233
- type: "string",
234
- description: `A concise, natural-language summary of what the user is trying to accomplish, derived from the conversation that triggered this tool call. Base it on the user's own words.
235
-
236
- This is context that helps understand the user's request and improve the tool's response; it does not trigger any destructive action.
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 parsed.data;
245
+ });
246
+ return {
247
+ ...rawResult,
248
+ tools
249
+ };
249
250
  }
250
251
  if (request.method === "tools/call") {
251
- const parsedRequest = CallToolRequestSchema.safeParse(request);
252
- if (!parsedRequest.success) return next();
253
- const toolName = parsedRequest.data.params.name;
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
- delete args[USER_INTENT_FIELD];
258
- request.params.arguments = args;
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
- delete args[USER_INTENT_FIELD];
268
- request.params.arguments = args;
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: parsedRequest.data.params.name,
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
- const parsedResult = CallToolResultSchema.safeParse(rawResult);
280
- if (!parsedResult.success) return rawResult;
281
- if (metaKeyName && hasUserPrompt) parsedResult.data._meta = {
282
- ...parsedResult.data._meta,
283
- [metaKeyName]: userPrompt
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 parsedResult.data;
288
+ return rawResult;
286
289
  }
287
290
  return next();
288
291
  };
@@ -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 { AUTO_EVENT, AlpicAnalytics, type AlpicAnalyticsProps, type Analytics, type AutoCaptureOptions, type BeforeSend, type CaptureOptions, INTERACTION_ATTRIBUTE, useAnalytics };
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.gde89265",
3
+ "version": "0.0.0-dev.gde915af",
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/sdk": "^1.30.0",
38
- "@testing-library/dom": "^10.4.1",
39
- "@testing-library/react": "^16.3.2",
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.5",
42
- "@types/react": "19.2.18",
43
- "jsdom": "^30.0.1",
44
- "react": "^19.2.8",
45
- "react-dom": "^19.2.8",
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": "^1.3.2",
48
- "tsdown": "^0.22.14",
50
+ "skybridge": "^2.0.4",
51
+ "tsdown": "^0.23.1",
49
52
  "typescript": "^6.0.3",
50
- "vitest": "^4.1.10",
51
- "zod": "^4.4.3"
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": "pnpm publish --tag \"${NPM_TAG}\" --access public --no-git-checks"
67
+ "publish:npm": "../../scripts/publish-if-new.sh"
65
68
  }
66
69
  }