@alpic-ai/insights 0.0.0-dev.gd55c3f1 → 0.0.0-dev.gd5df388

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;
@@ -7,34 +6,18 @@ interface PromptData {
7
6
  }
8
7
  interface IntentMiddlewareOptions {
9
8
  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
- */
9
+ /** When provided, only these tool names have the `user_intent` field injected and their prompts captured; all other tools are left untouched. */
14
10
  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
- */
11
+ /** 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
12
  argumentNameOverride?: Record<string, string>;
22
13
  }
23
- /**
24
- * Structurally compatible with `skybridge/server`'s `McpMiddlewareFn` so
25
- * skybridge users can still pass the result into `server.mcpMiddleware(...)`.
26
- */
14
+ /** Structurally compatible with `skybridge/server`'s `McpMiddlewareFn` so skybridge users can still pass the result into `server.mcpMiddleware(...)`. */
27
15
  type McpMiddlewareFn = (request: {
28
16
  method: string;
29
17
  params: Record<string, unknown>;
30
18
  }, extra: unknown, next: () => Promise<unknown>) => Promise<unknown> | unknown;
31
- /**
32
- * Captures the user's natural-language intent behind each tool call so MCP
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;
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. */
20
+ export declare function intentMiddleware(options?: IntentMiddlewareOptions): McpMiddlewareFn;
38
21
  //#endregion
39
22
  //#region src/analytics-middleware.d.ts
40
23
  interface CaptureOptions {
@@ -49,17 +32,10 @@ interface CaptureOptions {
49
32
  interface Analytics {
50
33
  /** Records a custom event on the current session's timeline, interleaved with tool calls. */
51
34
  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
- */
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. */
56
36
  identify(traits: Record<string, string>): void;
57
37
  }
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 {
38
+ interface AnalyticsExtra extends ServerContext {
63
39
  analytics: Analytics;
64
40
  }
65
41
  interface AnalyticsEvent extends CaptureOptions {
@@ -74,14 +50,8 @@ interface AnalyticsMiddlewareOptions {
74
50
  /** Receives each request's analytics locally, whether or not the server is hosted by Alpic. */
75
51
  handler?: (batch: AnalyticsBatch) => Promise<void> | void;
76
52
  }
77
- /**
78
- * Lets MCP server builders record custom analytics events and user traits from inside tool
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;
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. */
54
+ export declare function analyticsMiddleware(options?: AnalyticsMiddlewareOptions): McpMiddlewareFn;
85
55
  //#endregion
86
56
  //#region src/feedback-middleware.d.ts
87
57
  interface FeedbackData {
@@ -89,48 +59,22 @@ interface FeedbackData {
89
59
  source: "model" | "user";
90
60
  }
91
61
  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
- */
62
+ /** Invoked with the user's feedback in addition to Alpic's dashboard delivery — the middleware still attaches the feedback to the response `_meta`. */
96
63
  handler?: (feedback: FeedbackData) => Promise<void> | void;
97
64
  }
98
- /**
99
- * Lets MCP server builders collect qualitative feedback from end users about their
100
- * tool/server. Injects a `send_feedback` tool at `tools/list` time and intercepts calls
101
- * to it at `tools/call` time. The tool has no handler on the server. The middleware
102
- * short-circuits the call and either invokes the provided `handler` or attaches the
103
- * feedback to the response `_meta`.
104
- */
105
- declare function feedbackMiddleware(options?: FeedbackMiddlewareOptions): McpMiddlewareFn;
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`. */
66
+ export declare function feedbackMiddleware(options?: FeedbackMiddlewareOptions): McpMiddlewareFn;
106
67
  //#endregion
107
68
  //#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;
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). */
70
+ export declare const captureFeedback: (server: object, options?: FeedbackMiddlewareOptions) => void;
116
71
  //#endregion
117
72
  //#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;
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). */
74
+ export declare const captureIntents: (server: object, options?: IntentMiddlewareOptions) => void;
126
75
  //#endregion
127
76
  //#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;
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. */
78
+ export declare const track: (server: object, options?: AnalyticsMiddlewareOptions) => void;
135
79
  //#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 };
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) {
@@ -44,13 +49,7 @@ function createAnalyticsBuffer() {
44
49
  }
45
50
  };
46
51
  }
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
- */
52
+ /** 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
53
  function analyticsMiddleware(options) {
55
54
  return async (request, extra, next) => {
56
55
  if (request.method !== "tools/call" || extra === null || typeof extra !== "object") return next();
@@ -75,13 +74,12 @@ function analyticsMiddleware(options) {
75
74
  const eventsMetaKey = process.env.ALPIC_EVENTS_META_KEY || void 0;
76
75
  const identifyMetaKey = process.env.ALPIC_IDENTIFY_META_KEY || void 0;
77
76
  if (eventsMetaKey === void 0 && identifyMetaKey === void 0) return rawResult;
78
- if (!CallToolResultSchema.safeParse(rawResult).success) return rawResult;
79
- const result = rawResult;
80
- const meta = { ...result._meta };
77
+ if (!isSpecType.CallToolResult(rawResult)) return rawResult;
78
+ const meta = { ...isRecord(rawResult._meta) ? rawResult._meta : {} };
81
79
  if (eventsMetaKey !== void 0 && buffer.events.length > 0) meta[eventsMetaKey] = buffer.events;
82
80
  if (identifyMetaKey !== void 0 && buffer.traits !== void 0) meta[identifyMetaKey] = buffer.traits;
83
81
  return {
84
- ...result,
82
+ ...rawResult,
85
83
  _meta: meta
86
84
  };
87
85
  };
@@ -102,22 +100,15 @@ const FEEDBACK_OUTPUT_SCHEMA = {
102
100
  additionalProperties: false
103
101
  };
104
102
  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
- */
103
+ /** 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
104
  function feedbackMiddleware(options) {
113
105
  return async (request, _extra, next) => {
114
106
  const metaKeyName = process.env.ALPIC_FEEDBACK_META_KEY;
115
107
  if (request.method === "tools/list") {
116
108
  const rawResult = await next();
117
- const parsed = ListToolsResultSchema.safeParse(rawResult);
118
- if (!parsed.success) return rawResult;
119
- if (parsed.data.tools.some((tool) => tool.name === "send_feedback")) return parsed.data;
120
- 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 = {
121
112
  name: FEEDBACK_TOOL_NAME,
122
113
  description: FEEDBACK_TOOL_DESCRIPTION,
123
114
  inputSchema: {
@@ -137,18 +128,20 @@ function feedbackMiddleware(options) {
137
128
  },
138
129
  outputSchema: FEEDBACK_OUTPUT_SCHEMA,
139
130
  annotations: {
140
- readOnlyHint: true,
131
+ readOnlyHint: false,
141
132
  destructiveHint: false,
142
133
  openWorldHint: false,
143
134
  idempotentHint: true
144
135
  }
145
- });
146
- return parsed.data;
136
+ };
137
+ return {
138
+ ...rawResult,
139
+ tools: [...rawResult.tools, feedbackTool]
140
+ };
147
141
  }
148
142
  if (request.method !== "tools/call") return next();
149
- const parsedRequest = CallToolRequestSchema.safeParse(request);
150
- if (!parsedRequest.success || parsedRequest.data.params.name !== "send_feedback") return next();
151
- 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 : {};
152
145
  const content = typeof args.content === "string" ? args.content.trim() : void 0;
153
146
  const source = args.source === "user" ? "user" : "model";
154
147
  if (content === void 0 || content.length === 0) return {
@@ -187,15 +180,7 @@ function feedbackMiddleware(options) {
187
180
  }
188
181
  //#endregion
189
182
  //#region src/install-capture-middleware.ts
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
- */
183
+ /** 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
184
  const installCaptureMiddleware = (server, { middleware, installedMarker, disabledWarning }) => {
200
185
  const handlers = ("server" in server ? server.server : server)?._requestHandlers;
201
186
  if (!(handlers instanceof Map)) {
@@ -222,13 +207,7 @@ const installCaptureMiddleware = (server, { middleware, installedMarker, disable
222
207
  };
223
208
  //#endregion
224
209
  //#region src/capture-feedback.ts
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
- */
210
+ /** 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
211
  const captureFeedback = (server, options) => {
233
212
  installCaptureMiddleware(server, {
234
213
  middleware: feedbackMiddleware(options),
@@ -239,12 +218,7 @@ const captureFeedback = (server, options) => {
239
218
  //#endregion
240
219
  //#region src/intent-middleware.ts
241
220
  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
- */
221
+ /** 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
222
  function intentMiddleware(options) {
249
223
  const argumentNameOverride = options?.argumentNameOverride ?? {};
250
224
  const toolsFilter = options?.tools ? new Set(options.tools) : null;
@@ -252,17 +226,18 @@ function intentMiddleware(options) {
252
226
  const metaKeyName = process.env.ALPIC_INTENT_META_KEY;
253
227
  if (request.method === "tools/list") {
254
228
  const rawResult = await next();
255
- const parsed = ListToolsResultSchema.safeParse(rawResult);
256
- if (!parsed.success) return rawResult;
257
- for (const tool of parsed.data.tools) {
258
- if (toolsFilter && !toolsFilter.has(tool.name)) continue;
259
- if (tool.name === "send_feedback") continue;
260
- if (argumentNameOverride[tool.name] != null) continue;
261
- tool.inputSchema.properties = {
262
- ...tool.inputSchema.properties,
263
- [USER_INTENT_FIELD]: {
264
- type: "string",
265
- 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.
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, 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.
266
241
 
267
242
  This is context that helps understand the user's request and improve the tool's response; it does not trigger any destructive action.
268
243
 
@@ -273,60 +248,61 @@ Before sending, strip all personally identifiable information (names, emails, ph
273
248
  - Longer conversation: summarize the core goal, constraints, and preferences in 1-2 sentences.
274
249
  "Hi, I'm Jane (jane@mail.com), I'd love a gift under EUR1000 for my mum's birthday"
275
250
  -> "looking for a gift under EUR1000 for my mum's birthday"`
251
+ }
252
+ }
276
253
  }
277
254
  };
278
- }
279
- return parsed.data;
255
+ });
256
+ return {
257
+ ...rawResult,
258
+ tools
259
+ };
280
260
  }
281
261
  if (request.method === "tools/call") {
282
- const parsedRequest = CallToolRequestSchema.safeParse(request);
283
- if (!parsedRequest.success) return next();
284
- const toolName = parsedRequest.data.params.name;
262
+ if (typeof request.params.name !== "string") return next();
263
+ const toolName = request.params.name;
264
+ const args = isRecord(request.params.arguments) ? request.params.arguments : {};
285
265
  if (toolsFilter && !toolsFilter.has(toolName) || toolName === "send_feedback") {
286
- const args = parsedRequest.data.params.arguments ?? {};
287
266
  if (USER_INTENT_FIELD in args) {
288
- delete args[USER_INTENT_FIELD];
289
- request.params.arguments = args;
267
+ const toolArgs = { ...args };
268
+ delete toolArgs[USER_INTENT_FIELD];
269
+ request.params.arguments = toolArgs;
290
270
  }
291
271
  return next();
292
272
  }
293
273
  const promptField = argumentNameOverride[toolName] ?? USER_INTENT_FIELD;
294
- const args = parsedRequest.data.params.arguments ?? {};
295
274
  const userPrompt = typeof args[promptField] === "string" ? args[promptField] : void 0;
296
275
  const hasUserPrompt = userPrompt != null && userPrompt.length > 0;
297
276
  if (USER_INTENT_FIELD in args) {
298
- delete args[USER_INTENT_FIELD];
299
- request.params.arguments = args;
277
+ const toolArgs = { ...args };
278
+ delete toolArgs[USER_INTENT_FIELD];
279
+ request.params.arguments = toolArgs;
300
280
  }
301
281
  if (hasUserPrompt && options?.handler) try {
302
282
  await options.handler({
303
- toolName: parsedRequest.data.params.name,
283
+ toolName,
304
284
  userPrompt
305
285
  });
306
286
  } catch (error) {
307
287
  console.error("Error calling user prompt handler", error);
308
288
  }
309
289
  const rawResult = await next();
310
- const parsedResult = CallToolResultSchema.safeParse(rawResult);
311
- if (!parsedResult.success) return rawResult;
312
- if (metaKeyName && hasUserPrompt) parsedResult.data._meta = {
313
- ...parsedResult.data._meta,
314
- [metaKeyName]: userPrompt
290
+ if (!isSpecType.CallToolResult(rawResult)) return rawResult;
291
+ if (metaKeyName && hasUserPrompt) return {
292
+ ...rawResult,
293
+ _meta: {
294
+ ...isRecord(rawResult._meta) ? rawResult._meta : {},
295
+ [metaKeyName]: userPrompt
296
+ }
315
297
  };
316
- return parsedResult.data;
298
+ return rawResult;
317
299
  }
318
300
  return next();
319
301
  };
320
302
  }
321
303
  //#endregion
322
304
  //#region src/capture-intents.ts
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
- */
305
+ /** 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
306
  const captureIntents = (server, options) => {
331
307
  installCaptureMiddleware(server, {
332
308
  middleware: intentMiddleware(options),
@@ -336,12 +312,7 @@ const captureIntents = (server, options) => {
336
312
  };
337
313
  //#endregion
338
314
  //#region src/track.ts
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
- */
315
+ /** 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
316
  const track = (server, options) => {
346
317
  installCaptureMiddleware(server, {
347
318
  middleware: analyticsMiddleware(options),
@@ -39,22 +39,14 @@ interface AlpicAnalyticsProps {
39
39
  beforeSend?: BeforeSend;
40
40
  autoCapture?: AutoCaptureOptions;
41
41
  }
42
- /**
43
- * Provides Alpic Analytics to descendant components and configures itself from the widget host.
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;
42
+ /** Provides Alpic Analytics to descendant components, configuring itself from the widget host; events captured before configuration is available are buffered automatically. */
43
+ export declare function AlpicAnalytics({ children, beforeSend, autoCapture }: AlpicAnalyticsProps): import("react").JSX.Element;
48
44
  /** Returns the analytics client from the nearest `AlpicAnalytics` provider. */
49
- declare function useAnalytics(): Analytics;
45
+ export declare function useAnalytics(): Analytics;
50
46
  //#endregion
51
47
  //#region src/react/auto-capture.d.ts
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: {
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
+ export declare const AUTO_EVENT: {
58
50
  readonly loaded: "$loaded";
59
51
  readonly visible: "$visible";
60
52
  readonly hidden: "$hidden";
@@ -62,6 +54,6 @@ declare const AUTO_EVENT: {
62
54
  readonly error: "$error";
63
55
  };
64
56
  /** Public DOM contract for declarative interaction capture: the attribute value is the event name. */
65
- declare const INTERACTION_ATTRIBUTE = "data-alpic-event";
57
+ export declare const INTERACTION_ATTRIBUTE = "data-alpic-event";
66
58
  //#endregion
67
- 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 };
@@ -72,11 +72,7 @@ function isSameStamp(left, right) {
72
72
  }
73
73
  //#endregion
74
74
  //#region src/react/auto-capture.ts
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
- */
75
+ /** 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
76
  const AUTO_EVENT = {
81
77
  loaded: "$loaded",
82
78
  visible: "$visible",
@@ -88,10 +84,7 @@ const AUTO_EVENT = {
88
84
  const INTERACTION_ATTRIBUTE = "data-alpic-event";
89
85
  const INTERACTION_DATASET_KEY = "alpicEvent";
90
86
  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
- */
87
+ /** 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
88
  function resolveInteractionEvent(target) {
96
89
  if (!(target instanceof Element)) return null;
97
90
  const element = target.closest(`[${INTERACTION_ATTRIBUTE}]`);
@@ -217,11 +210,7 @@ observeRuntime();
217
210
  //#endregion
218
211
  //#region src/react/alpic-analytics.tsx
219
212
  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
- */
213
+ /** Provides Alpic Analytics to descendant components, configuring itself from the widget host; events captured before configuration is available are buffered automatically. */
225
214
  function AlpicAnalytics({ children, beforeSend, autoCapture }) {
226
215
  const beforeSendRef = useRef(beforeSend);
227
216
  beforeSendRef.current = beforeSend;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alpic-ai/insights",
3
- "version": "0.0.0-dev.gd55c3f1",
3
+ "version": "0.0.0-dev.gd5df388",
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.3.1"
37
+ },
36
38
  "devDependencies": {
37
- "@modelcontextprotocol/sdk": "^1.29.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.17",
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.2",
47
+ "react": "^19.3.0",
48
+ "react-dom": "^19.3.0",
46
49
  "shx": "^0.4.0",
47
- "skybridge": "^1.3.0",
48
- "tsdown": "^0.22.14",
50
+ "skybridge": "^2.0.3",
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.3",
54
+ "zod": "^4.5.4"
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": "pnpm publish --tag \"${NPM_TAG}\" --access public --no-git-checks"
67
+ "publish:npm": "../../scripts/publish-if-new.sh"
61
68
  }
62
69
  }