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

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.
Files changed (2) hide show
  1. package/dist/index.mjs +16 -8
  2. package/package.json +7 -6
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
@@ -15,10 +16,10 @@ function userPromptMiddleware(options) {
15
16
  const parsed = ListToolsResultSchema.safeParse(rawResult);
16
17
  if (!parsed.success) return rawResult;
17
18
  for (const tool of parsed.data.tools) {
18
- if (promptArgByTool[tool.name]) continue;
19
+ if (promptArgByTool[tool.name] != null) continue;
19
20
  tool.inputSchema.properties = {
20
21
  ...tool.inputSchema.properties,
21
- user_prompt: {
22
+ [USER_PROMPT_FIELD]: {
22
23
  type: "string",
23
24
  description: "Copy the user's prompt that led to this tool call. Remove any PII (Personal Identifiable Information)."
24
25
  }
@@ -29,12 +30,12 @@ function userPromptMiddleware(options) {
29
30
  if (request.method === "tools/call") {
30
31
  const parsedRequest = CallToolRequestSchema.safeParse(request);
31
32
  if (!parsedRequest.success) return next();
32
- const promptField = promptArgByTool[parsedRequest.data.params.name] ?? "user_prompt";
33
+ const promptField = promptArgByTool[parsedRequest.data.params.name] ?? USER_PROMPT_FIELD;
33
34
  const args = parsedRequest.data.params.arguments ?? {};
34
35
  const userPrompt = typeof args[promptField] === "string" ? args[promptField] : void 0;
35
36
  const hasUserPrompt = userPrompt != null && userPrompt.length > 0;
36
- if ("user_prompt" in args) {
37
- delete args.user_prompt;
37
+ if (USER_PROMPT_FIELD in args) {
38
+ delete args[USER_PROMPT_FIELD];
38
39
  request.params.arguments = args;
39
40
  }
40
41
  if (hasUserPrompt && options?.handler) try {
@@ -59,6 +60,7 @@ function userPromptMiddleware(options) {
59
60
  }
60
61
  //#endregion
61
62
  //#region src/capture-user-prompts.ts
63
+ const INSTALLED_MARKER = "__alpicCaptureUserPromptsInstalled";
62
64
  /**
63
65
  * Captures the user's natural-language prompt behind each tool call on a vanilla
64
66
  * `@modelcontextprotocol/sdk` server. Accepts the high-level `McpServer` or the
@@ -71,8 +73,14 @@ function userPromptMiddleware(options) {
71
73
  * of calls relative to `registerTool` does not matter.
72
74
  */
73
75
  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 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;
76
84
  const middleware = userPromptMiddleware(options);
77
85
  const targets = new Set(["tools/list", "tools/call"]);
78
86
  const wrap = (method, handler) => {
@@ -85,7 +93,7 @@ const captureUserPrompts = (server, options) => {
85
93
  }, extra, () => handler(...args));
86
94
  };
87
95
  };
88
- for (const [method, handler] of handlers) handlers.set(method, wrap(method, handler));
96
+ for (const [method, handler] of [...handlers]) handlers.set(method, wrap(method, handler));
89
97
  const originalSet = handlers.set.bind(handlers);
90
98
  handlers.set = (method, handler) => originalSet(method, wrap(method, handler));
91
99
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alpic-ai/insights",
3
- "version": "0.0.0-dev.c649622",
3
+ "version": "0.0.0-dev.c70543a",
4
4
  "description": "User insights middlewares for Alpic-hosted MCP servers",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",
@@ -17,8 +17,8 @@
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.1"
22
22
  },
23
23
  "peerDependenciesMeta": {
24
24
  "skybridge": {
@@ -30,16 +30,17 @@
30
30
  "@total-typescript/tsconfig": "^1.0.4",
31
31
  "@types/node": "^25.6.0",
32
32
  "shx": "^0.4.0",
33
- "skybridge": "^0.35.21",
33
+ "skybridge": "^0.36.1",
34
34
  "tsdown": "^0.21.10",
35
35
  "typescript": "^6.0.3",
36
36
  "vitest": "^4.1.5",
37
- "zod": "^4.3.6"
37
+ "zod": "^4.4.3"
38
38
  },
39
39
  "scripts": {
40
40
  "build": "shx rm -rf dist && tsdown",
41
41
  "format": "biome check --write --error-on-warnings .",
42
- "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",
43
44
  "test:format": "biome check --error-on-warnings .",
44
45
  "test:type": "tsc --noEmit",
45
46
  "publish:npm": "pnpm publish --tag \"${NPM_TAG}\" --access public --no-git-checks"