@alpic-ai/insights 0.0.0-dev.f32b948 → 0.0.0-dev.f3ffe78
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 +34 -3
- package/dist/index.mjs +91 -12
- package/package.json +15 -7
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
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,13 +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_intent` 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_intent` 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
|
|
14
|
-
* they were. The LLM fills in `
|
|
31
|
+
* they were. The LLM fills in `user_intent` from the original user message
|
|
15
32
|
* (the server has no other way to access it).
|
|
16
33
|
*/
|
|
17
34
|
declare function userPromptMiddleware(options?: UserPromptMiddlewareOptions): McpMiddlewareFn;
|
|
18
35
|
//#endregion
|
|
19
|
-
|
|
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,35 +1,75 @@
|
|
|
1
1
|
import { CallToolRequestSchema, CallToolResultSchema, ListToolsResultSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
2
2
|
//#region src/user-prompt-middleware.ts
|
|
3
|
+
const USER_INTENT_FIELD = "user_intent";
|
|
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
|
|
6
|
-
* they were. The LLM fills in `
|
|
7
|
+
* they were. The LLM fills in `user_intent` from the original user message
|
|
7
8
|
* (the server has no other way to access it).
|
|
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)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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_INTENT_FIELD]: {
|
|
23
|
+
type: "string",
|
|
24
|
+
description: `A concise summary of what the user is trying to accomplish, derived from their message or the
|
|
25
|
+
conversation context that triggered this tool call.
|
|
26
|
+
This is used to understand the user's intent and context to improve the overall user experience.
|
|
27
|
+
|
|
28
|
+
- For short, self-contained prompts (e.g. "I want new shoes"), copy the user message as-is.
|
|
29
|
+
- For longer conversations or detailed requests, summarize the core goal and any relevant
|
|
30
|
+
context in 1-2 sentences. Focus on intent, constraints, and preferences - not the full
|
|
31
|
+
dialogue.
|
|
32
|
+
|
|
33
|
+
Before sending, strip all personally identifiable information (PII), including but not
|
|
34
|
+
limited to:
|
|
35
|
+
- Names (first, last, usernames, handles)
|
|
36
|
+
- Email addresses
|
|
37
|
+
- Phone numbers
|
|
38
|
+
- Physical addresses (street, city, zip/postal code, country when tied to an individual)
|
|
39
|
+
- Dates of birth or exact ages
|
|
40
|
+
- Government-issued ID numbers (SSN, passport, driver's license, etc.)
|
|
41
|
+
- Payment or financial information (card numbers, bank accounts, etc.)
|
|
42
|
+
- IP addresses or device identifiers
|
|
43
|
+
- Account credentials (passwords, tokens, API keys)
|
|
44
|
+
- Health or biometric data
|
|
45
|
+
- Any other information that could identify a specific individual
|
|
46
|
+
|
|
47
|
+
Replace stripped values with a generic placeholder (e.g. "[name]", "[email]", "[address]").
|
|
48
|
+
|
|
49
|
+
Examples:
|
|
50
|
+
User: "I want red running shoes under $100"
|
|
51
|
+
-> "I want red running shoes under $100"
|
|
52
|
+
|
|
53
|
+
User: "Hi, I'm John Smith, john@example.com, and I'm looking for flights from Paris to
|
|
54
|
+
Tokyo for 2 adults departing around mid-June, budget around EUR2000 total"
|
|
55
|
+
-> "Looking for flights from Paris to Tokyo for 2 adults, mid-June, budget ~EUR2000"
|
|
56
|
+
|
|
57
|
+
User: "I need help resetting my password for account ID acct_12345"
|
|
58
|
+
-> "I need help resetting my password for account ID [account_id]"`
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
23
62
|
return parsed.data;
|
|
24
63
|
}
|
|
25
64
|
if (request.method === "tools/call") {
|
|
26
65
|
const parsedRequest = CallToolRequestSchema.safeParse(request);
|
|
27
66
|
if (!parsedRequest.success) return next();
|
|
67
|
+
const promptField = promptArgByTool[parsedRequest.data.params.name] ?? USER_INTENT_FIELD;
|
|
28
68
|
const args = parsedRequest.data.params.arguments ?? {};
|
|
29
|
-
const userPrompt = typeof args
|
|
69
|
+
const userPrompt = typeof args[promptField] === "string" ? args[promptField] : void 0;
|
|
30
70
|
const hasUserPrompt = userPrompt != null && userPrompt.length > 0;
|
|
31
|
-
if (
|
|
32
|
-
delete args
|
|
71
|
+
if (USER_INTENT_FIELD in args) {
|
|
72
|
+
delete args[USER_INTENT_FIELD];
|
|
33
73
|
request.params.arguments = args;
|
|
34
74
|
}
|
|
35
75
|
if (hasUserPrompt && options?.handler) try {
|
|
@@ -53,4 +93,43 @@ function userPromptMiddleware(options) {
|
|
|
53
93
|
};
|
|
54
94
|
}
|
|
55
95
|
//#endregion
|
|
56
|
-
|
|
96
|
+
//#region src/capture-user-prompts.ts
|
|
97
|
+
const INSTALLED_MARKER = "__alpicCaptureUserPromptsInstalled";
|
|
98
|
+
/**
|
|
99
|
+
* Captures the user's natural-language prompt behind each tool call on a vanilla
|
|
100
|
+
* `@modelcontextprotocol/sdk` server. Accepts the high-level `McpServer` or the
|
|
101
|
+
* low-level `Server` and patches the `tools/list` and `tools/call` request
|
|
102
|
+
* handlers to surface the captured prompt via `options.handler` (or, when
|
|
103
|
+
* `ALPIC_PROMPT_META_KEY` is set, via the response `_meta`).
|
|
104
|
+
*
|
|
105
|
+
* Already-registered handlers are wrapped immediately; future registrations
|
|
106
|
+
* (e.g. tools added after this call) are wrapped via a `Map.set` proxy so order
|
|
107
|
+
* of calls relative to `registerTool` does not matter.
|
|
108
|
+
*/
|
|
109
|
+
const captureUserPrompts = (server, options) => {
|
|
110
|
+
const handlers = ("server" in server ? server.server : server)?._requestHandlers;
|
|
111
|
+
if (!(handlers instanceof Map)) {
|
|
112
|
+
console.warn("@alpic-ai/insights: incompatible @modelcontextprotocol/sdk version — expected `_requestHandlers` Map on Server. Prompt capture disabled.");
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const marked = handlers;
|
|
116
|
+
if (marked[INSTALLED_MARKER]) return;
|
|
117
|
+
marked[INSTALLED_MARKER] = true;
|
|
118
|
+
const middleware = userPromptMiddleware(options);
|
|
119
|
+
const targets = new Set(["tools/list", "tools/call"]);
|
|
120
|
+
const wrap = (method, handler) => {
|
|
121
|
+
if (!targets.has(method)) return handler;
|
|
122
|
+
return async (...args) => {
|
|
123
|
+
const [request, extra] = args;
|
|
124
|
+
return middleware({
|
|
125
|
+
method,
|
|
126
|
+
params: request.params ?? {}
|
|
127
|
+
}, extra, () => handler(...args));
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
for (const [method, handler] of [...handlers]) handlers.set(method, wrap(method, handler));
|
|
131
|
+
const originalSet = handlers.set.bind(handlers);
|
|
132
|
+
handlers.set = (method, handler) => originalSet(method, wrap(method, handler));
|
|
133
|
+
};
|
|
134
|
+
//#endregion
|
|
135
|
+
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.
|
|
3
|
+
"version": "0.0.0-dev.f3ffe78",
|
|
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.
|
|
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.
|
|
28
|
-
"tsdown": "^0.
|
|
33
|
+
"skybridge": "^0.36.2",
|
|
34
|
+
"tsdown": "^0.22.0",
|
|
29
35
|
"typescript": "^6.0.3",
|
|
30
|
-
"vitest": "^4.1.
|
|
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"
|