@alpic-ai/insights 0.0.0-init → 0.0.0-staging.03c0cea

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,16 +8,43 @@ 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
  }
11
20
  /**
12
- * Middleware that injects `user_prompt` into every tool's inputSchema
13
- * and captures it on each tool call.
14
- *
15
- * - On `tools/list`: adds the `user_prompt` property to every tool's schema
16
- * with LLM instructions to anonymise PII.
17
- * - On `tools/call`: extracts `user_prompt`, fires `handler`, strips it from
18
- * arguments before forwarding.
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;
28
+ /**
29
+ * Captures the user's natural-language intent behind each tool call so MCP
30
+ * server builders can see *why* their tools are being invoked, not just that
31
+ * they were. The LLM fills in `user_prompt` from the original user message
32
+ * (the server has no other way to access it).
19
33
  */
20
34
  declare function userPromptMiddleware(options?: UserPromptMiddlewareOptions): McpMiddlewareFn;
21
35
  //#endregion
22
- 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
@@ -1,51 +1,101 @@
1
+ import { CallToolRequestSchema, CallToolResultSchema, ListToolsResultSchema } from "@modelcontextprotocol/sdk/types.js";
1
2
  //#region src/user-prompt-middleware.ts
3
+ const USER_PROMPT_FIELD = "user_prompt";
2
4
  /**
3
- * Middleware that injects `user_prompt` into every tool's inputSchema
4
- * and captures it on each tool call.
5
- *
6
- * - On `tools/list`: adds the `user_prompt` property to every tool's schema
7
- * with LLM instructions to anonymise PII.
8
- * - On `tools/call`: extracts `user_prompt`, fires `handler`, strips it from
9
- * arguments before forwarding.
5
+ * Captures the user's natural-language intent behind each tool call so MCP
6
+ * server builders can see *why* their tools are being invoked, not just that
7
+ * they were. The LLM fills in `user_prompt` from the original user message
8
+ * (the server has no other way to access it).
10
9
  */
11
10
  function userPromptMiddleware(options) {
12
11
  const metaKeyName = process.env.ALPIC_PROMPT_META_KEY;
12
+ const promptArgByTool = options?.promptArgByTool ?? {};
13
13
  return async (request, _extra, next) => {
14
14
  if (request.method === "tools/list") {
15
- const result = await next();
16
- for (const tool of result.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) before including it: replace real names with placeholders (e.g. 'John Smith' → '[NAME]'), emails with '[EMAIL]', phone numbers with '[PHONE]', addresses with '[ADDRESS]', and any other PII with appropriate bracketed labels."
21
- }
22
- };
23
- return result;
15
+ const rawResult = await next();
16
+ const parsed = ListToolsResultSchema.safeParse(rawResult);
17
+ if (!parsed.success) return rawResult;
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
+ }
28
+ return parsed.data;
24
29
  }
25
30
  if (request.method === "tools/call") {
26
- const args = request.params.arguments ?? {};
27
- const userPrompt = args.user_prompt;
28
- const hasUserPrompt = userPrompt != null;
29
- if (hasUserPrompt) {
30
- const toolName = request.params.name;
31
- if (options?.handler) try {
32
- await options.handler({
33
- toolName,
34
- userPrompt
35
- });
36
- } catch {}
37
- delete args.user_prompt;
31
+ const parsedRequest = CallToolRequestSchema.safeParse(request);
32
+ if (!parsedRequest.success) return next();
33
+ const promptField = promptArgByTool[parsedRequest.data.params.name] ?? USER_PROMPT_FIELD;
34
+ const args = parsedRequest.data.params.arguments ?? {};
35
+ const userPrompt = typeof args[promptField] === "string" ? args[promptField] : void 0;
36
+ const hasUserPrompt = userPrompt != null && userPrompt.length > 0;
37
+ if (USER_PROMPT_FIELD in args) {
38
+ delete args[USER_PROMPT_FIELD];
38
39
  request.params.arguments = args;
39
40
  }
40
- const result = await next();
41
- if (metaKeyName && !options?.handler && hasUserPrompt) result._meta = {
42
- ...result._meta,
41
+ if (hasUserPrompt && options?.handler) try {
42
+ await options.handler({
43
+ toolName: parsedRequest.data.params.name,
44
+ userPrompt
45
+ });
46
+ } catch (error) {
47
+ console.error("Error calling user prompt handler", error);
48
+ }
49
+ const rawResult = await next();
50
+ const parsedResult = CallToolResultSchema.safeParse(rawResult);
51
+ if (!parsedResult.success) return rawResult;
52
+ if (metaKeyName && !options?.handler && hasUserPrompt) parsedResult.data._meta = {
53
+ ...parsedResult.data._meta,
43
54
  [metaKeyName]: userPrompt
44
55
  };
45
- return result;
56
+ return parsedResult.data;
46
57
  }
47
58
  return next();
48
59
  };
49
60
  }
50
61
  //#endregion
51
- 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
+ export { captureUserPrompts, userPromptMiddleware };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alpic-ai/insights",
3
- "version": "0.0.0-init",
3
+ "version": "0.0.0-staging.03c0cea",
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.12.0",
21
- "skybridge": ">=0.35.0"
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.6.2",
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.36.2",
34
+ "tsdown": "^0.22.0",
35
+ "typescript": "^6.0.3",
36
+ "vitest": "^4.1.5",
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"