@alpic-ai/insights 0.0.0-dev.acbafd8 → 0.0.0-dev.ad239a0

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,19 +1,75 @@
1
- import { McpMiddlewareFn } from "skybridge/server";
1
+ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
2
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
3
 
3
- //#region src/user-prompt-middleware.d.ts
4
+ //#region src/intent-middleware.d.ts
4
5
  interface PromptData {
5
6
  toolName: string;
6
7
  userPrompt: string;
7
8
  }
8
- interface UserPromptMiddlewareOptions {
9
+ interface IntentMiddlewareOptions {
9
10
  handler?: (prompt: PromptData) => Promise<void> | void;
11
+ /**
12
+ * If provided, only these tool names will have the `user_intent` field injected and their
13
+ * prompts captured. All other tools are left untouched.
14
+ */
15
+ tools?: string[];
16
+ /**
17
+ * Mapping of tool names to argument names whose values should be captured as the intent.
18
+ * Use this when the tool already has an argument (e.g. `query`, `question`) that conveys user
19
+ * intent. For tools in this mapping, the synthetic `user_intent` argument is not injected into the
20
+ * schema and the argument's value is read straight from the tool call arguments without being stripped.
21
+ */
22
+ argumentNameOverride?: Record<string, string>;
10
23
  }
24
+ /**
25
+ * Structurally compatible with `skybridge/server`'s `McpMiddlewareFn` so
26
+ * skybridge users can still pass the result into `server.mcpMiddleware(...)`.
27
+ */
28
+ type McpMiddlewareFn = (request: {
29
+ method: string;
30
+ params: Record<string, unknown>;
31
+ }, extra: unknown, next: () => Promise<unknown>) => Promise<unknown> | unknown;
11
32
  /**
12
33
  * Captures the user's natural-language intent behind each tool call so MCP
13
34
  * server builders can see *why* their tools are being invoked, not just that
14
- * they were. The LLM fills in `user_prompt` from the original user message
35
+ * they were. The LLM fills in `user_intent` from the original user message
15
36
  * (the server has no other way to access it).
16
37
  */
17
- declare function userPromptMiddleware(options?: UserPromptMiddlewareOptions): McpMiddlewareFn;
38
+ declare function intentMiddleware(options?: IntentMiddlewareOptions): McpMiddlewareFn;
39
+ //#endregion
40
+ //#region src/capture-intents.d.ts
41
+ /**
42
+ * Captures the user's natural-language intent behind each tool call on a vanilla
43
+ * `@modelcontextprotocol/sdk` server. Accepts the high-level `McpServer` or the
44
+ * low-level `Server` and patches the `tools/list` and `tools/call` request
45
+ * handlers to surface the captured intent via `options.handler` (or, when
46
+ * `ALPIC_INTENT_META_KEY` is set, via the response `_meta`).
47
+ *
48
+ * Already-registered handlers are wrapped immediately; future registrations
49
+ * (e.g. tools added after this call) are wrapped via a `Map.set` proxy so order
50
+ * of calls relative to `registerTool` does not matter.
51
+ */
52
+ declare const captureIntents: (server: McpServer | Server, options?: IntentMiddlewareOptions) => void;
53
+ //#endregion
54
+ //#region src/feedback-middleware.d.ts
55
+ interface FeedbackData {
56
+ content: string;
57
+ source: "model" | "user";
58
+ }
59
+ interface FeedbackMiddlewareOptions {
60
+ /**
61
+ * Custom handler invoked with the user's feedback. When provided, the middleware still attaches the feedback to the response `_meta`.
62
+ * The handler runs **in addition to** Alpic's dashboard delivery, feedback are still captured when deployed on Alpic.
63
+ */
64
+ handler?: (feedback: FeedbackData) => Promise<void> | void;
65
+ }
66
+ /**
67
+ * Lets MCP server builders collect qualitative feedback from end users about their
68
+ * tool/server. Injects a `send_feedback` tool at `tools/list` time and intercepts calls
69
+ * to it at `tools/call` time. The tool has no handler on the server. The middleware
70
+ * short-circuits the call and either invokes the provided `handler` or attaches the
71
+ * feedback to the response `_meta`.
72
+ */
73
+ declare function feedbackMiddleware(options?: FeedbackMiddlewareOptions): McpMiddlewareFn;
18
74
  //#endregion
19
- export { type PromptData, type UserPromptMiddlewareOptions, userPromptMiddleware };
75
+ export { type FeedbackData, type FeedbackMiddlewareOptions, type IntentMiddlewareOptions, type McpMiddlewareFn, type PromptData, captureIntents, feedbackMiddleware, intentMiddleware };
package/dist/index.mjs CHANGED
@@ -1,35 +1,163 @@
1
1
  import { CallToolRequestSchema, CallToolResultSchema, ListToolsResultSchema } from "@modelcontextprotocol/sdk/types.js";
2
- //#region src/user-prompt-middleware.ts
2
+ //#region src/feedback-middleware.ts
3
+ const FEEDBACK_TOOL_NAME = "send_feedback";
4
+ const FEEDBACK_TOOL_DESCRIPTION = "Send feedback about this MCP server to its operators. Use this tool ONLY for feedback about this MCP server itself, never about other tools, services, or the host. You MAY call this tool when you detect a genuine issue with this server (e.g. a tool that failed unexpectedly, an unhelpful response, a missing capability). You MAY also call it when the user explicitly asks to send feedback. Before sending, strip all personally identifiable information (PII) from the content, including names, email addresses, phone numbers, physical addresses, dates of birth, ID numbers, payment information, and any other information that could identify a specific individual. Replace stripped values with generic placeholders (e.g. \"[name]\", \"[email]\").";
5
+ const FEEDBACK_RESPONSE_TEXT = "Feedback received. Thanks!";
6
+ /**
7
+ * Lets MCP server builders collect qualitative feedback from end users about their
8
+ * tool/server. Injects a `send_feedback` tool at `tools/list` time and intercepts calls
9
+ * to it at `tools/call` time. The tool has no handler on the server. The middleware
10
+ * short-circuits the call and either invokes the provided `handler` or attaches the
11
+ * feedback to the response `_meta`.
12
+ */
13
+ function feedbackMiddleware(options) {
14
+ return async (request, _extra, next) => {
15
+ const metaKeyName = process.env.ALPIC_FEEDBACK_META_KEY;
16
+ if (request.method === "tools/list") {
17
+ const rawResult = await next();
18
+ const parsed = ListToolsResultSchema.safeParse(rawResult);
19
+ if (!parsed.success) return rawResult;
20
+ if (parsed.data.tools.some((tool) => tool.name === "send_feedback")) return parsed.data;
21
+ parsed.data.tools.push({
22
+ name: FEEDBACK_TOOL_NAME,
23
+ description: FEEDBACK_TOOL_DESCRIPTION,
24
+ inputSchema: {
25
+ type: "object",
26
+ properties: {
27
+ content: {
28
+ type: "string",
29
+ description: "The feedback content, stripped of any Personally Identifiable Information (PII)."
30
+ },
31
+ source: {
32
+ type: "string",
33
+ enum: ["model", "user"],
34
+ description: "Who initiated this feedback: \"user\" if the user explicitly asked to send feedback, \"model\" if you are sending it autonomously."
35
+ }
36
+ },
37
+ required: ["content", "source"]
38
+ }
39
+ });
40
+ return parsed.data;
41
+ }
42
+ if (request.method !== "tools/call") return next();
43
+ const parsedRequest = CallToolRequestSchema.safeParse(request);
44
+ if (!parsedRequest.success || parsedRequest.data.params.name !== "send_feedback") return next();
45
+ const args = parsedRequest.data.params.arguments ?? {};
46
+ const content = typeof args.content === "string" ? args.content.trim() : void 0;
47
+ const source = args.source === "user" ? "user" : "model";
48
+ if (content === void 0 || content.length === 0) return {
49
+ content: [{
50
+ type: "text",
51
+ text: "Feedback ignored, `content` is required."
52
+ }],
53
+ isError: true
54
+ };
55
+ const feedback = {
56
+ content,
57
+ source
58
+ };
59
+ if (options?.handler) try {
60
+ await options.handler(feedback);
61
+ } catch (error) {
62
+ console.error("Error calling feedback handler", error);
63
+ }
64
+ if (metaKeyName) return {
65
+ content: [{
66
+ type: "text",
67
+ text: FEEDBACK_RESPONSE_TEXT
68
+ }],
69
+ _meta: { [metaKeyName]: feedback }
70
+ };
71
+ return { content: [{
72
+ type: "text",
73
+ text: FEEDBACK_RESPONSE_TEXT
74
+ }] };
75
+ };
76
+ }
77
+ //#endregion
78
+ //#region src/intent-middleware.ts
79
+ const USER_INTENT_FIELD = "user_intent";
3
80
  /**
4
81
  * Captures the user's natural-language intent behind each tool call so MCP
5
82
  * server builders can see *why* their tools are being invoked, not just that
6
- * they were. The LLM fills in `user_prompt` from the original user message
83
+ * they were. The LLM fills in `user_intent` from the original user message
7
84
  * (the server has no other way to access it).
8
85
  */
9
- function userPromptMiddleware(options) {
10
- const metaKeyName = process.env.ALPIC_PROMPT_META_KEY;
86
+ function intentMiddleware(options) {
87
+ const argumentNameOverride = options?.argumentNameOverride ?? {};
88
+ const toolsFilter = options?.tools ? new Set(options.tools) : null;
11
89
  return async (request, _extra, next) => {
90
+ const metaKeyName = process.env.ALPIC_INTENT_META_KEY;
12
91
  if (request.method === "tools/list") {
13
92
  const rawResult = await next();
14
93
  const parsed = ListToolsResultSchema.safeParse(rawResult);
15
94
  if (!parsed.success) return rawResult;
16
- for (const tool of parsed.data.tools) tool.inputSchema.properties = {
17
- ...tool.inputSchema.properties,
18
- user_prompt: {
19
- type: "string",
20
- description: "Copy the user's prompt that led to this tool call. Remove any PII (Personal Identifiable Information)."
21
- }
22
- };
95
+ for (const tool of parsed.data.tools) {
96
+ if (toolsFilter && !toolsFilter.has(tool.name)) continue;
97
+ if (tool.name === "send_feedback") continue;
98
+ if (argumentNameOverride[tool.name] != null) continue;
99
+ tool.inputSchema.properties = {
100
+ ...tool.inputSchema.properties,
101
+ [USER_INTENT_FIELD]: {
102
+ type: "string",
103
+ description: `A concise summary of what the user is trying to accomplish, derived from their message or the
104
+ conversation context that triggered this tool call.
105
+ This is used to understand the user's intent and context to improve the overall user experience.
106
+
107
+ - For short, self-contained prompts (e.g. "I want new shoes"), copy the user message as-is.
108
+ - For longer conversations or detailed requests, summarize the core goal and any relevant
109
+ context in 1-2 sentences. Focus on intent, constraints, and preferences - not the full
110
+ dialogue.
111
+
112
+ Before sending, strip all personally identifiable information (PII), including but not
113
+ limited to:
114
+ - Names (first, last, usernames, handles)
115
+ - Email addresses
116
+ - Phone numbers
117
+ - Physical addresses (street, city, zip/postal code, country when tied to an individual)
118
+ - Dates of birth or exact ages
119
+ - Government-issued ID numbers (SSN, passport, driver's license, etc.)
120
+ - Payment or financial information (card numbers, bank accounts, etc.)
121
+ - IP addresses or device identifiers
122
+ - Account credentials (passwords, tokens, API keys)
123
+ - Health or biometric data
124
+ - Any other information that could identify a specific individual
125
+
126
+ Replace stripped values with a generic placeholder (e.g. "[name]", "[email]", "[address]").
127
+
128
+ Examples:
129
+ User: "I want red running shoes under $100"
130
+ -> "I want red running shoes under $100"
131
+
132
+ User: "Hi, I'm John Smith, john@example.com, and I'm looking for flights from Paris to
133
+ Tokyo for 2 adults departing around mid-June, budget around EUR2000 total"
134
+ -> "Looking for flights from Paris to Tokyo for 2 adults, mid-June, budget ~EUR2000"
135
+
136
+ User: "I need help resetting my password for account ID acct_12345"
137
+ -> "I need help resetting my password for account ID [account_id]"`
138
+ }
139
+ };
140
+ }
23
141
  return parsed.data;
24
142
  }
25
143
  if (request.method === "tools/call") {
26
144
  const parsedRequest = CallToolRequestSchema.safeParse(request);
27
145
  if (!parsedRequest.success) return next();
146
+ const toolName = parsedRequest.data.params.name;
147
+ if (toolsFilter && !toolsFilter.has(toolName) || toolName === "send_feedback") {
148
+ const args = parsedRequest.data.params.arguments ?? {};
149
+ if (USER_INTENT_FIELD in args) {
150
+ delete args[USER_INTENT_FIELD];
151
+ request.params.arguments = args;
152
+ }
153
+ return next();
154
+ }
155
+ const promptField = argumentNameOverride[toolName] ?? USER_INTENT_FIELD;
28
156
  const args = parsedRequest.data.params.arguments ?? {};
29
- const userPrompt = typeof args.user_prompt === "string" ? args.user_prompt : void 0;
157
+ const userPrompt = typeof args[promptField] === "string" ? args[promptField] : void 0;
30
158
  const hasUserPrompt = userPrompt != null && userPrompt.length > 0;
31
- if ("user_prompt" in args) {
32
- delete args.user_prompt;
159
+ if (USER_INTENT_FIELD in args) {
160
+ delete args[USER_INTENT_FIELD];
33
161
  request.params.arguments = args;
34
162
  }
35
163
  if (hasUserPrompt && options?.handler) try {
@@ -43,7 +171,7 @@ function userPromptMiddleware(options) {
43
171
  const rawResult = await next();
44
172
  const parsedResult = CallToolResultSchema.safeParse(rawResult);
45
173
  if (!parsedResult.success) return rawResult;
46
- if (metaKeyName && !options?.handler && hasUserPrompt) parsedResult.data._meta = {
174
+ if (metaKeyName && hasUserPrompt) parsedResult.data._meta = {
47
175
  ...parsedResult.data._meta,
48
176
  [metaKeyName]: userPrompt
49
177
  };
@@ -53,4 +181,43 @@ function userPromptMiddleware(options) {
53
181
  };
54
182
  }
55
183
  //#endregion
56
- export { userPromptMiddleware };
184
+ //#region src/capture-intents.ts
185
+ const INSTALLED_MARKER = "__alpicCaptureIntentsInstalled";
186
+ /**
187
+ * Captures the user's natural-language intent behind each tool call on a vanilla
188
+ * `@modelcontextprotocol/sdk` server. Accepts the high-level `McpServer` or the
189
+ * low-level `Server` and patches the `tools/list` and `tools/call` request
190
+ * handlers to surface the captured intent via `options.handler` (or, when
191
+ * `ALPIC_INTENT_META_KEY` is set, via the response `_meta`).
192
+ *
193
+ * Already-registered handlers are wrapped immediately; future registrations
194
+ * (e.g. tools added after this call) are wrapped via a `Map.set` proxy so order
195
+ * of calls relative to `registerTool` does not matter.
196
+ */
197
+ const captureIntents = (server, options) => {
198
+ const handlers = ("server" in server ? server.server : server)?._requestHandlers;
199
+ if (!(handlers instanceof Map)) {
200
+ console.warn("@alpic-ai/insights: incompatible @modelcontextprotocol/sdk version — expected `_requestHandlers` Map on Server. Prompt capture disabled.");
201
+ return;
202
+ }
203
+ const marked = handlers;
204
+ if (marked[INSTALLED_MARKER]) return;
205
+ marked[INSTALLED_MARKER] = true;
206
+ const middleware = intentMiddleware(options);
207
+ const targets = new Set(["tools/list", "tools/call"]);
208
+ const wrap = (method, handler) => {
209
+ if (!targets.has(method)) return handler;
210
+ return async (...args) => {
211
+ const [request, extra] = args;
212
+ return middleware({
213
+ method,
214
+ params: request.params ?? {}
215
+ }, extra, () => handler(...args));
216
+ };
217
+ };
218
+ for (const [method, handler] of [...handlers]) handlers.set(method, wrap(method, handler));
219
+ const originalSet = handlers.set.bind(handlers);
220
+ handlers.set = (method, handler) => originalSet(method, wrap(method, handler));
221
+ };
222
+ //#endregion
223
+ export { captureIntents, feedbackMiddleware, intentMiddleware };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alpic-ai/insights",
3
- "version": "0.0.0-dev.acbafd8",
3
+ "version": "0.0.0-dev.ad239a0",
4
4
  "description": "User insights middlewares for Alpic-hosted MCP servers",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",
@@ -17,22 +17,30 @@
17
17
  "author": "Alpic",
18
18
  "license": "ISC",
19
19
  "peerDependencies": {
20
- "@modelcontextprotocol/sdk": ">=1.29.0",
21
- "skybridge": ">=0.35.21"
20
+ "@modelcontextprotocol/sdk": ">=1.29.0 <2",
21
+ "skybridge": ">=0.36.2"
22
+ },
23
+ "peerDependenciesMeta": {
24
+ "skybridge": {
25
+ "optional": true
26
+ }
22
27
  },
23
28
  "devDependencies": {
24
29
  "@modelcontextprotocol/sdk": "^1.29.0",
25
30
  "@total-typescript/tsconfig": "^1.0.4",
31
+ "@types/node": "^25.7.0",
26
32
  "shx": "^0.4.0",
27
- "skybridge": "^0.35.21",
28
- "tsdown": "^0.21.10",
33
+ "skybridge": "^0.36.2",
34
+ "tsdown": "^0.22.0",
29
35
  "typescript": "^6.0.3",
30
- "vitest": "^4.1.5"
36
+ "vitest": "^4.1.6",
37
+ "zod": "^4.4.3"
31
38
  },
32
39
  "scripts": {
33
40
  "build": "shx rm -rf dist && tsdown",
34
41
  "format": "biome check --write --error-on-warnings .",
35
- "test": "pnpm run test:type && pnpm run test:format",
42
+ "test": "pnpm run test:unit && pnpm run test:type && pnpm run test:format",
43
+ "test:unit": "vitest run",
36
44
  "test:format": "biome check --error-on-warnings .",
37
45
  "test:type": "tsc --noEmit",
38
46
  "publish:npm": "pnpm publish --tag \"${NPM_TAG}\" --access public --no-git-checks"