@alpic-ai/insights 0.0.0-dev.bdeed34 → 0.0.0-dev.bf144c2

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,4 +1,5 @@
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
4
  //#region src/user-prompt-middleware.d.ts
4
5
  interface PromptData {
@@ -7,7 +8,23 @@ interface PromptData {
7
8
  }
8
9
  interface UserPromptMiddlewareOptions {
9
10
  handler?: (prompt: PromptData) => Promise<void> | void;
11
+ /**
12
+ * Mapping of tool names to input field names whose values should be captured as the prompt.
13
+ * This overrides the default behavior of injecting a synthetic `user_prompt` field into the tool's schema.
14
+ * Use this when the tool already has a parameter (e.g. `query`, `question`) that conveys user intent.
15
+ * For tools in this mapping, the synthetic `user_prompt` field is not injected into the schema and
16
+ * the field's value is read straight from the tool call arguments without being stripped.
17
+ */
18
+ promptArgByTool?: Record<string, string>;
10
19
  }
20
+ /**
21
+ * Structurally compatible with `skybridge/server`'s `McpMiddlewareFn` so
22
+ * skybridge users can still pass the result into `server.mcpMiddleware(...)`.
23
+ */
24
+ type McpMiddlewareFn = (request: {
25
+ method: string;
26
+ params: Record<string, unknown>;
27
+ }, extra: unknown, next: () => Promise<unknown>) => Promise<unknown> | unknown;
11
28
  /**
12
29
  * Captures the user's natural-language intent behind each tool call so MCP
13
30
  * server builders can see *why* their tools are being invoked, not just that
@@ -16,4 +33,41 @@ interface UserPromptMiddlewareOptions {
16
33
  */
17
34
  declare function userPromptMiddleware(options?: UserPromptMiddlewareOptions): McpMiddlewareFn;
18
35
  //#endregion
19
- export { type PromptData, type UserPromptMiddlewareOptions, userPromptMiddleware };
36
+ //#region src/capture-user-prompts.d.ts
37
+ /**
38
+ * Captures the user's natural-language prompt behind each tool call on a vanilla
39
+ * `@modelcontextprotocol/sdk` server. Accepts the high-level `McpServer` or the
40
+ * low-level `Server` and patches the `tools/list` and `tools/call` request
41
+ * handlers to surface the captured prompt via `options.handler` (or, when
42
+ * `ALPIC_PROMPT_META_KEY` is set, via the response `_meta`).
43
+ *
44
+ * Already-registered handlers are wrapped immediately; future registrations
45
+ * (e.g. tools added after this call) are wrapped via a `Map.set` proxy so order
46
+ * of calls relative to `registerTool` does not matter.
47
+ */
48
+ declare const captureUserPrompts: (server: McpServer | Server, options?: UserPromptMiddlewareOptions) => void;
49
+ //#endregion
50
+ //#region src/feedback-middleware.d.ts
51
+ interface FeedbackData {
52
+ toolName?: string;
53
+ message: string;
54
+ }
55
+ interface FeedbackMiddlewareOptions {
56
+ /** Tool name to register. Defaults to `send_feedback`. */
57
+ toolName?: string;
58
+ /**
59
+ * Custom handler invoked with the user's feedback. When provided, the middleware does NOT
60
+ * attach data to the response `_meta`. The handler takes full ownership of delivery.
61
+ */
62
+ handler?: (feedback: FeedbackData) => Promise<void> | void;
63
+ }
64
+ /**
65
+ * Lets MCP server builders collect qualitative feedback from end users about their
66
+ * tool/server. Injects a `send_feedback` tool at `tools/list` time and intercepts calls
67
+ * to it at `tools/call` time. The tool has no handler on the server. The middleware
68
+ * short-circuits the call and either invokes the provided `handler` or attaches the
69
+ * feedback to the response `_meta`.
70
+ */
71
+ declare function feedbackMiddleware(options?: FeedbackMiddlewareOptions): McpMiddlewareFn;
72
+ //#endregion
73
+ export { type FeedbackData, type FeedbackMiddlewareOptions, type McpMiddlewareFn, type PromptData, type UserPromptMiddlewareOptions, captureUserPrompts, feedbackMiddleware, userPromptMiddleware };
package/dist/index.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  import { CallToolRequestSchema, CallToolResultSchema, ListToolsResultSchema } from "@modelcontextprotocol/sdk/types.js";
2
2
  //#region src/user-prompt-middleware.ts
3
+ const USER_PROMPT_FIELD = "user_prompt";
3
4
  /**
4
5
  * Captures the user's natural-language intent behind each tool call so MCP
5
6
  * server builders can see *why* their tools are being invoked, not just that
@@ -8,28 +9,33 @@ import { CallToolRequestSchema, CallToolResultSchema, ListToolsResultSchema } fr
8
9
  */
9
10
  function userPromptMiddleware(options) {
10
11
  const metaKeyName = process.env.ALPIC_PROMPT_META_KEY;
12
+ const promptArgByTool = options?.promptArgByTool ?? {};
11
13
  return async (request, _extra, next) => {
12
14
  if (request.method === "tools/list") {
13
15
  const rawResult = await next();
14
16
  const parsed = ListToolsResultSchema.safeParse(rawResult);
15
17
  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
- };
18
+ for (const tool of parsed.data.tools) {
19
+ if (promptArgByTool[tool.name] != null) continue;
20
+ tool.inputSchema.properties = {
21
+ ...tool.inputSchema.properties,
22
+ [USER_PROMPT_FIELD]: {
23
+ type: "string",
24
+ description: "Copy the user's prompt that led to this tool call. Remove any PII (Personal Identifiable Information)."
25
+ }
26
+ };
27
+ }
23
28
  return parsed.data;
24
29
  }
25
30
  if (request.method === "tools/call") {
26
31
  const parsedRequest = CallToolRequestSchema.safeParse(request);
27
32
  if (!parsedRequest.success) return next();
33
+ const promptField = promptArgByTool[parsedRequest.data.params.name] ?? USER_PROMPT_FIELD;
28
34
  const args = parsedRequest.data.params.arguments ?? {};
29
- const userPrompt = typeof args.user_prompt === "string" ? args.user_prompt : void 0;
35
+ const userPrompt = typeof args[promptField] === "string" ? args[promptField] : void 0;
30
36
  const hasUserPrompt = userPrompt != null && userPrompt.length > 0;
31
- if ("user_prompt" in args) {
32
- delete args.user_prompt;
37
+ if (USER_PROMPT_FIELD in args) {
38
+ delete args[USER_PROMPT_FIELD];
33
39
  request.params.arguments = args;
34
40
  }
35
41
  if (hasUserPrompt && options?.handler) try {
@@ -53,4 +59,126 @@ function userPromptMiddleware(options) {
53
59
  };
54
60
  }
55
61
  //#endregion
56
- export { userPromptMiddleware };
62
+ //#region src/capture-user-prompts.ts
63
+ const INSTALLED_MARKER = "__alpicCaptureUserPromptsInstalled";
64
+ /**
65
+ * Captures the user's natural-language prompt behind each tool call on a vanilla
66
+ * `@modelcontextprotocol/sdk` server. Accepts the high-level `McpServer` or the
67
+ * low-level `Server` and patches the `tools/list` and `tools/call` request
68
+ * handlers to surface the captured prompt via `options.handler` (or, when
69
+ * `ALPIC_PROMPT_META_KEY` is set, via the response `_meta`).
70
+ *
71
+ * Already-registered handlers are wrapped immediately; future registrations
72
+ * (e.g. tools added after this call) are wrapped via a `Map.set` proxy so order
73
+ * of calls relative to `registerTool` does not matter.
74
+ */
75
+ const captureUserPrompts = (server, options) => {
76
+ const handlers = ("server" in server ? server.server : server)?._requestHandlers;
77
+ if (!(handlers instanceof Map)) {
78
+ console.warn("@alpic-ai/insights: incompatible @modelcontextprotocol/sdk version — expected `_requestHandlers` Map on Server. Prompt capture disabled.");
79
+ return;
80
+ }
81
+ const marked = handlers;
82
+ if (marked[INSTALLED_MARKER]) return;
83
+ marked[INSTALLED_MARKER] = true;
84
+ const middleware = userPromptMiddleware(options);
85
+ const targets = new Set(["tools/list", "tools/call"]);
86
+ const wrap = (method, handler) => {
87
+ if (!targets.has(method)) return handler;
88
+ return async (...args) => {
89
+ const [request, extra] = args;
90
+ return middleware({
91
+ method,
92
+ params: request.params ?? {}
93
+ }, extra, () => handler(...args));
94
+ };
95
+ };
96
+ for (const [method, handler] of [...handlers]) handlers.set(method, wrap(method, handler));
97
+ const originalSet = handlers.set.bind(handlers);
98
+ handlers.set = (method, handler) => originalSet(method, wrap(method, handler));
99
+ };
100
+ //#endregion
101
+ //#region src/feedback-middleware.ts
102
+ const DEFAULT_FEEDBACK_TOOL_NAME = "send_feedback";
103
+ const FEEDBACK_TOOL_DESCRIPTION = "Send the user's 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 MUST ask the user for explicit consent before calling this tool. You can proactively suggest sending feedback when something didn't work well, but never call without consent. ";
104
+ const FEEDBACK_RESPONSE_TEXT = "Feedback received. Thanks!";
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
+ */
112
+ function feedbackMiddleware(options) {
113
+ const feedbackToolName = options?.toolName ?? DEFAULT_FEEDBACK_TOOL_NAME;
114
+ return async (request, _extra, next) => {
115
+ const metaKeyName = process.env.ALPIC_FEEDBACK_META_KEY;
116
+ if (request.method === "tools/list") {
117
+ const rawResult = await next();
118
+ const parsed = ListToolsResultSchema.safeParse(rawResult);
119
+ if (!parsed.success) return rawResult;
120
+ if (parsed.data.tools.some((tool) => tool.name === feedbackToolName)) return parsed.data;
121
+ parsed.data.tools.push({
122
+ name: feedbackToolName,
123
+ description: FEEDBACK_TOOL_DESCRIPTION,
124
+ inputSchema: {
125
+ type: "object",
126
+ properties: {
127
+ tool_name: {
128
+ type: "string",
129
+ description: "Name of the tool the feedback is about, when applicable."
130
+ },
131
+ message: {
132
+ type: "string",
133
+ description: "The user's feedback content. Remove any PII (Personal Identifiable Information)."
134
+ }
135
+ },
136
+ required: ["message"]
137
+ }
138
+ });
139
+ return parsed.data;
140
+ }
141
+ if (request.method !== "tools/call") return next();
142
+ const parsedRequest = CallToolRequestSchema.safeParse(request);
143
+ if (!parsedRequest.success || parsedRequest.data.params.name !== feedbackToolName) return next();
144
+ const args = parsedRequest.data.params.arguments ?? {};
145
+ const message = typeof args.message === "string" ? args.message.trim() : void 0;
146
+ const rawToolName = typeof args.tool_name === "string" ? args.tool_name.trim() : "";
147
+ const toolName = rawToolName.length > 0 ? rawToolName : void 0;
148
+ if (message === void 0 || message.length === 0) return {
149
+ content: [{
150
+ type: "text",
151
+ text: "Feedback ignored — `message` is required."
152
+ }],
153
+ isError: true
154
+ };
155
+ const feedback = toolName !== void 0 ? {
156
+ toolName,
157
+ message
158
+ } : { message };
159
+ if (options?.handler) {
160
+ try {
161
+ await options.handler(feedback);
162
+ } catch (error) {
163
+ console.error("Error calling feedback handler", error);
164
+ }
165
+ return { content: [{
166
+ type: "text",
167
+ text: FEEDBACK_RESPONSE_TEXT
168
+ }] };
169
+ }
170
+ if (metaKeyName) return {
171
+ content: [{
172
+ type: "text",
173
+ text: FEEDBACK_RESPONSE_TEXT
174
+ }],
175
+ _meta: { [metaKeyName]: feedback }
176
+ };
177
+ return { content: [{
178
+ type: "text",
179
+ text: FEEDBACK_RESPONSE_TEXT
180
+ }] };
181
+ };
182
+ }
183
+ //#endregion
184
+ export { captureUserPrompts, feedbackMiddleware, userPromptMiddleware };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alpic-ai/insights",
3
- "version": "0.0.0-dev.bdeed34",
3
+ "version": "0.0.0-dev.bf144c2",
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",
20
+ "@modelcontextprotocol/sdk": ">=1.29.0 <2",
21
21
  "skybridge": ">=0.35.21"
22
22
  },
23
+ "peerDependenciesMeta": {
24
+ "skybridge": {
25
+ "optional": true
26
+ }
27
+ },
23
28
  "devDependencies": {
24
29
  "@modelcontextprotocol/sdk": "^1.29.0",
25
30
  "@total-typescript/tsconfig": "^1.0.4",
31
+ "@types/node": "^25.6.0",
26
32
  "shx": "^0.4.0",
27
33
  "skybridge": "^0.35.21",
28
- "tsdown": "^0.21.9",
34
+ "tsdown": "^0.21.10",
29
35
  "typescript": "^6.0.3",
30
- "vitest": "^4.1.4"
36
+ "vitest": "^4.1.5",
37
+ "zod": "^4.4.1"
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"