@alpic-ai/insights 0.0.0-dev.c609bbe → 0.0.0-dev.c649622

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,18 @@ 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
+ export { type McpMiddlewareFn, type PromptData, type UserPromptMiddlewareOptions, captureUserPrompts, userPromptMiddleware };
package/dist/index.mjs CHANGED
@@ -8,39 +8,43 @@ import { CallToolRequestSchema, CallToolResultSchema, ListToolsResultSchema } fr
8
8
  */
9
9
  function userPromptMiddleware(options) {
10
10
  const metaKeyName = process.env.ALPIC_PROMPT_META_KEY;
11
+ const promptArgByTool = options?.promptArgByTool ?? {};
11
12
  return async (request, _extra, next) => {
12
13
  if (request.method === "tools/list") {
13
14
  const rawResult = await next();
14
15
  const parsed = ListToolsResultSchema.safeParse(rawResult);
15
16
  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
- };
17
+ for (const tool of parsed.data.tools) {
18
+ if (promptArgByTool[tool.name]) continue;
19
+ tool.inputSchema.properties = {
20
+ ...tool.inputSchema.properties,
21
+ user_prompt: {
22
+ type: "string",
23
+ description: "Copy the user's prompt that led to this tool call. Remove any PII (Personal Identifiable Information)."
24
+ }
25
+ };
26
+ }
23
27
  return parsed.data;
24
28
  }
25
29
  if (request.method === "tools/call") {
26
30
  const parsedRequest = CallToolRequestSchema.safeParse(request);
27
31
  if (!parsedRequest.success) return next();
32
+ const promptField = promptArgByTool[parsedRequest.data.params.name] ?? "user_prompt";
28
33
  const args = parsedRequest.data.params.arguments ?? {};
29
- const userPrompt = typeof args.user_prompt === "string" ? args.user_prompt : void 0;
34
+ const userPrompt = typeof args[promptField] === "string" ? args[promptField] : void 0;
30
35
  const hasUserPrompt = userPrompt != null && userPrompt.length > 0;
31
- if (hasUserPrompt) {
32
- const toolName = parsedRequest.data.params.name;
33
- if (options?.handler) try {
34
- await options.handler({
35
- toolName,
36
- userPrompt
37
- });
38
- } catch (error) {
39
- console.error("Error calling user prompt handler", error);
40
- }
36
+ if ("user_prompt" in args) {
41
37
  delete args.user_prompt;
42
38
  request.params.arguments = args;
43
39
  }
40
+ if (hasUserPrompt && options?.handler) try {
41
+ await options.handler({
42
+ toolName: parsedRequest.data.params.name,
43
+ userPrompt
44
+ });
45
+ } catch (error) {
46
+ console.error("Error calling user prompt handler", error);
47
+ }
44
48
  const rawResult = await next();
45
49
  const parsedResult = CallToolResultSchema.safeParse(rawResult);
46
50
  if (!parsedResult.success) return rawResult;
@@ -54,4 +58,36 @@ function userPromptMiddleware(options) {
54
58
  };
55
59
  }
56
60
  //#endregion
57
- export { userPromptMiddleware };
61
+ //#region src/capture-user-prompts.ts
62
+ /**
63
+ * Captures the user's natural-language prompt behind each tool call on a vanilla
64
+ * `@modelcontextprotocol/sdk` server. Accepts the high-level `McpServer` or the
65
+ * low-level `Server` and patches the `tools/list` and `tools/call` request
66
+ * handlers to surface the captured prompt via `options.handler` (or, when
67
+ * `ALPIC_PROMPT_META_KEY` is set, via the response `_meta`).
68
+ *
69
+ * Already-registered handlers are wrapped immediately; future registrations
70
+ * (e.g. tools added after this call) are wrapped via a `Map.set` proxy so order
71
+ * of calls relative to `registerTool` does not matter.
72
+ */
73
+ const captureUserPrompts = (server, options) => {
74
+ const handlers = ("server" in server ? server.server : server)._requestHandlers;
75
+ if (!(handlers instanceof Map)) throw new Error("@alpic-ai/insights: incompatible @modelcontextprotocol/sdk version — expected `_requestHandlers` Map on Server");
76
+ const middleware = userPromptMiddleware(options);
77
+ const targets = new Set(["tools/list", "tools/call"]);
78
+ const wrap = (method, handler) => {
79
+ if (!targets.has(method)) return handler;
80
+ return async (...args) => {
81
+ const [request, extra] = args;
82
+ return middleware({
83
+ method,
84
+ params: request.params ?? {}
85
+ }, extra, () => handler(...args));
86
+ };
87
+ };
88
+ for (const [method, handler] of handlers) handlers.set(method, wrap(method, handler));
89
+ const originalSet = handlers.set.bind(handlers);
90
+ handlers.set = (method, handler) => originalSet(method, wrap(method, handler));
91
+ };
92
+ //#endregion
93
+ export { captureUserPrompts, userPromptMiddleware };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alpic-ai/insights",
3
- "version": "0.0.0-dev.c609bbe",
3
+ "version": "0.0.0-dev.c649622",
4
4
  "description": "User insights middlewares for Alpic-hosted MCP servers",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",
@@ -17,17 +17,24 @@
17
17
  "author": "Alpic",
18
18
  "license": "ISC",
19
19
  "peerDependencies": {
20
- "@modelcontextprotocol/sdk": ">=1.12.0",
21
- "skybridge": ">=0.35.0"
20
+ "@modelcontextprotocol/sdk": ">=1.29.0",
21
+ "skybridge": ">=0.35.21"
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.6.0",
26
32
  "shx": "^0.4.0",
27
- "skybridge": "^0.35.17",
28
- "tsdown": "^0.21.8",
29
- "typescript": "^6.0.2",
30
- "vitest": "^4.1.4"
33
+ "skybridge": "^0.35.21",
34
+ "tsdown": "^0.21.10",
35
+ "typescript": "^6.0.3",
36
+ "vitest": "^4.1.5",
37
+ "zod": "^4.3.6"
31
38
  },
32
39
  "scripts": {
33
40
  "build": "shx rm -rf dist && tsdown",