@alpic-ai/insights 0.0.0-dev.bf5b1e9 → 0.0.0-dev.c0130ae
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 +20 -16
- package/dist/index.mjs +66 -21
- package/package.json +7 -7
package/dist/index.d.mts
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
2
2
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
3
|
|
|
4
|
-
//#region src/
|
|
4
|
+
//#region src/intent-middleware.d.ts
|
|
5
5
|
interface PromptData {
|
|
6
6
|
toolName: string;
|
|
7
7
|
userPrompt: string;
|
|
8
8
|
}
|
|
9
|
-
interface
|
|
9
|
+
interface IntentMiddlewareOptions {
|
|
10
10
|
handler?: (prompt: PromptData) => Promise<void> | void;
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
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.
|
|
12
|
+
* If provided, only these tool names will have the `user_intent` field injected and their
|
|
13
|
+
* prompts captured. All other tools are left untouched.
|
|
17
14
|
*/
|
|
18
|
-
|
|
15
|
+
tools?: string[];
|
|
16
|
+
/**
|
|
17
|
+
* Mapping of tool names to argument names whose values should be captured as the intent.
|
|
18
|
+
* Use this when the tool already has an argument (e.g. `query`, `question`) that conveys user
|
|
19
|
+
* intent. For tools in this mapping, the synthetic `user_intent` argument is not injected into the
|
|
20
|
+
* schema and the argument's value is read straight from the tool call arguments without being stripped.
|
|
21
|
+
*/
|
|
22
|
+
argumentNameOverride?: Record<string, string>;
|
|
19
23
|
}
|
|
20
24
|
/**
|
|
21
25
|
* Structurally compatible with `skybridge/server`'s `McpMiddlewareFn` so
|
|
@@ -28,23 +32,23 @@ type McpMiddlewareFn = (request: {
|
|
|
28
32
|
/**
|
|
29
33
|
* Captures the user's natural-language intent behind each tool call so MCP
|
|
30
34
|
* server builders can see *why* their tools are being invoked, not just that
|
|
31
|
-
* they were. The LLM fills in `
|
|
35
|
+
* they were. The LLM fills in `user_intent` from the original user message
|
|
32
36
|
* (the server has no other way to access it).
|
|
33
37
|
*/
|
|
34
|
-
declare function
|
|
38
|
+
declare function intentMiddleware(options?: IntentMiddlewareOptions): McpMiddlewareFn;
|
|
35
39
|
//#endregion
|
|
36
|
-
//#region src/capture-
|
|
40
|
+
//#region src/capture-intents.d.ts
|
|
37
41
|
/**
|
|
38
|
-
* Captures the user's natural-language
|
|
42
|
+
* Captures the user's natural-language intent behind each tool call on a vanilla
|
|
39
43
|
* `@modelcontextprotocol/sdk` server. Accepts the high-level `McpServer` or the
|
|
40
44
|
* low-level `Server` and patches the `tools/list` and `tools/call` request
|
|
41
|
-
* handlers to surface the captured
|
|
42
|
-
* `
|
|
45
|
+
* handlers to surface the captured intent via `options.handler` (or, when
|
|
46
|
+
* `ALPIC_INTENT_META_KEY` is set, via the response `_meta`).
|
|
43
47
|
*
|
|
44
48
|
* Already-registered handlers are wrapped immediately; future registrations
|
|
45
49
|
* (e.g. tools added after this call) are wrapped via a `Map.set` proxy so order
|
|
46
50
|
* of calls relative to `registerTool` does not matter.
|
|
47
51
|
*/
|
|
48
|
-
declare const
|
|
52
|
+
declare const captureIntents: (server: McpServer | Server, options?: IntentMiddlewareOptions) => void;
|
|
49
53
|
//#endregion
|
|
50
|
-
export { type
|
|
54
|
+
export { type IntentMiddlewareOptions, type McpMiddlewareFn, type PromptData, captureIntents, intentMiddleware };
|
package/dist/index.mjs
CHANGED
|
@@ -1,27 +1,63 @@
|
|
|
1
1
|
import { CallToolRequestSchema, CallToolResultSchema, ListToolsResultSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
-
//#region src/
|
|
3
|
-
const
|
|
2
|
+
//#region src/intent-middleware.ts
|
|
3
|
+
const USER_INTENT_FIELD = "user_intent";
|
|
4
4
|
/**
|
|
5
5
|
* Captures the user's natural-language intent behind each tool call so MCP
|
|
6
6
|
* server builders can see *why* their tools are being invoked, not just that
|
|
7
|
-
* they were. The LLM fills in `
|
|
7
|
+
* they were. The LLM fills in `user_intent` from the original user message
|
|
8
8
|
* (the server has no other way to access it).
|
|
9
9
|
*/
|
|
10
|
-
function
|
|
11
|
-
const metaKeyName = process.env.
|
|
12
|
-
const
|
|
10
|
+
function intentMiddleware(options) {
|
|
11
|
+
const metaKeyName = process.env.ALPIC_INTENT_META_KEY;
|
|
12
|
+
const argumentNameOverride = options?.argumentNameOverride ?? {};
|
|
13
|
+
const toolsFilter = options?.tools ? new Set(options.tools) : null;
|
|
13
14
|
return async (request, _extra, next) => {
|
|
14
15
|
if (request.method === "tools/list") {
|
|
15
16
|
const rawResult = await next();
|
|
16
17
|
const parsed = ListToolsResultSchema.safeParse(rawResult);
|
|
17
18
|
if (!parsed.success) return rawResult;
|
|
18
19
|
for (const tool of parsed.data.tools) {
|
|
19
|
-
if (
|
|
20
|
+
if (toolsFilter && !toolsFilter.has(tool.name)) continue;
|
|
21
|
+
if (argumentNameOverride[tool.name] != null) continue;
|
|
20
22
|
tool.inputSchema.properties = {
|
|
21
23
|
...tool.inputSchema.properties,
|
|
22
|
-
[
|
|
24
|
+
[USER_INTENT_FIELD]: {
|
|
23
25
|
type: "string",
|
|
24
|
-
description:
|
|
26
|
+
description: `A concise summary of what the user is trying to accomplish, derived from their message or the
|
|
27
|
+
conversation context that triggered this tool call.
|
|
28
|
+
This is used to understand the user's intent and context to improve the overall user experience.
|
|
29
|
+
|
|
30
|
+
- For short, self-contained prompts (e.g. "I want new shoes"), copy the user message as-is.
|
|
31
|
+
- For longer conversations or detailed requests, summarize the core goal and any relevant
|
|
32
|
+
context in 1-2 sentences. Focus on intent, constraints, and preferences - not the full
|
|
33
|
+
dialogue.
|
|
34
|
+
|
|
35
|
+
Before sending, strip all personally identifiable information (PII), including but not
|
|
36
|
+
limited to:
|
|
37
|
+
- Names (first, last, usernames, handles)
|
|
38
|
+
- Email addresses
|
|
39
|
+
- Phone numbers
|
|
40
|
+
- Physical addresses (street, city, zip/postal code, country when tied to an individual)
|
|
41
|
+
- Dates of birth or exact ages
|
|
42
|
+
- Government-issued ID numbers (SSN, passport, driver's license, etc.)
|
|
43
|
+
- Payment or financial information (card numbers, bank accounts, etc.)
|
|
44
|
+
- IP addresses or device identifiers
|
|
45
|
+
- Account credentials (passwords, tokens, API keys)
|
|
46
|
+
- Health or biometric data
|
|
47
|
+
- Any other information that could identify a specific individual
|
|
48
|
+
|
|
49
|
+
Replace stripped values with a generic placeholder (e.g. "[name]", "[email]", "[address]").
|
|
50
|
+
|
|
51
|
+
Examples:
|
|
52
|
+
User: "I want red running shoes under $100"
|
|
53
|
+
-> "I want red running shoes under $100"
|
|
54
|
+
|
|
55
|
+
User: "Hi, I'm John Smith, john@example.com, and I'm looking for flights from Paris to
|
|
56
|
+
Tokyo for 2 adults departing around mid-June, budget around EUR2000 total"
|
|
57
|
+
-> "Looking for flights from Paris to Tokyo for 2 adults, mid-June, budget ~EUR2000"
|
|
58
|
+
|
|
59
|
+
User: "I need help resetting my password for account ID acct_12345"
|
|
60
|
+
-> "I need help resetting my password for account ID [account_id]"`
|
|
25
61
|
}
|
|
26
62
|
};
|
|
27
63
|
}
|
|
@@ -30,12 +66,21 @@ function userPromptMiddleware(options) {
|
|
|
30
66
|
if (request.method === "tools/call") {
|
|
31
67
|
const parsedRequest = CallToolRequestSchema.safeParse(request);
|
|
32
68
|
if (!parsedRequest.success) return next();
|
|
33
|
-
const
|
|
69
|
+
const toolName = parsedRequest.data.params.name;
|
|
70
|
+
if (toolsFilter && !toolsFilter.has(toolName)) {
|
|
71
|
+
const args = parsedRequest.data.params.arguments ?? {};
|
|
72
|
+
if (USER_INTENT_FIELD in args) {
|
|
73
|
+
delete args[USER_INTENT_FIELD];
|
|
74
|
+
request.params.arguments = args;
|
|
75
|
+
}
|
|
76
|
+
return next();
|
|
77
|
+
}
|
|
78
|
+
const promptField = argumentNameOverride[toolName] ?? USER_INTENT_FIELD;
|
|
34
79
|
const args = parsedRequest.data.params.arguments ?? {};
|
|
35
80
|
const userPrompt = typeof args[promptField] === "string" ? args[promptField] : void 0;
|
|
36
81
|
const hasUserPrompt = userPrompt != null && userPrompt.length > 0;
|
|
37
|
-
if (
|
|
38
|
-
delete args[
|
|
82
|
+
if (USER_INTENT_FIELD in args) {
|
|
83
|
+
delete args[USER_INTENT_FIELD];
|
|
39
84
|
request.params.arguments = args;
|
|
40
85
|
}
|
|
41
86
|
if (hasUserPrompt && options?.handler) try {
|
|
@@ -49,7 +94,7 @@ function userPromptMiddleware(options) {
|
|
|
49
94
|
const rawResult = await next();
|
|
50
95
|
const parsedResult = CallToolResultSchema.safeParse(rawResult);
|
|
51
96
|
if (!parsedResult.success) return rawResult;
|
|
52
|
-
if (metaKeyName &&
|
|
97
|
+
if (metaKeyName && hasUserPrompt) parsedResult.data._meta = {
|
|
53
98
|
...parsedResult.data._meta,
|
|
54
99
|
[metaKeyName]: userPrompt
|
|
55
100
|
};
|
|
@@ -59,20 +104,20 @@ function userPromptMiddleware(options) {
|
|
|
59
104
|
};
|
|
60
105
|
}
|
|
61
106
|
//#endregion
|
|
62
|
-
//#region src/capture-
|
|
63
|
-
const INSTALLED_MARKER = "
|
|
107
|
+
//#region src/capture-intents.ts
|
|
108
|
+
const INSTALLED_MARKER = "__alpicCaptureIntentsInstalled";
|
|
64
109
|
/**
|
|
65
|
-
* Captures the user's natural-language
|
|
110
|
+
* Captures the user's natural-language intent behind each tool call on a vanilla
|
|
66
111
|
* `@modelcontextprotocol/sdk` server. Accepts the high-level `McpServer` or the
|
|
67
112
|
* low-level `Server` and patches the `tools/list` and `tools/call` request
|
|
68
|
-
* handlers to surface the captured
|
|
69
|
-
* `
|
|
113
|
+
* handlers to surface the captured intent via `options.handler` (or, when
|
|
114
|
+
* `ALPIC_INTENT_META_KEY` is set, via the response `_meta`).
|
|
70
115
|
*
|
|
71
116
|
* Already-registered handlers are wrapped immediately; future registrations
|
|
72
117
|
* (e.g. tools added after this call) are wrapped via a `Map.set` proxy so order
|
|
73
118
|
* of calls relative to `registerTool` does not matter.
|
|
74
119
|
*/
|
|
75
|
-
const
|
|
120
|
+
const captureIntents = (server, options) => {
|
|
76
121
|
const handlers = ("server" in server ? server.server : server)?._requestHandlers;
|
|
77
122
|
if (!(handlers instanceof Map)) {
|
|
78
123
|
console.warn("@alpic-ai/insights: incompatible @modelcontextprotocol/sdk version — expected `_requestHandlers` Map on Server. Prompt capture disabled.");
|
|
@@ -81,7 +126,7 @@ const captureUserPrompts = (server, options) => {
|
|
|
81
126
|
const marked = handlers;
|
|
82
127
|
if (marked[INSTALLED_MARKER]) return;
|
|
83
128
|
marked[INSTALLED_MARKER] = true;
|
|
84
|
-
const middleware =
|
|
129
|
+
const middleware = intentMiddleware(options);
|
|
85
130
|
const targets = new Set(["tools/list", "tools/call"]);
|
|
86
131
|
const wrap = (method, handler) => {
|
|
87
132
|
if (!targets.has(method)) return handler;
|
|
@@ -98,4 +143,4 @@ const captureUserPrompts = (server, options) => {
|
|
|
98
143
|
handlers.set = (method, handler) => originalSet(method, wrap(method, handler));
|
|
99
144
|
};
|
|
100
145
|
//#endregion
|
|
101
|
-
export {
|
|
146
|
+
export { captureIntents, intentMiddleware };
|
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.c0130ae",
|
|
4
4
|
"description": "User insights middlewares for Alpic-hosted MCP servers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.mjs",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"license": "ISC",
|
|
19
19
|
"peerDependencies": {
|
|
20
20
|
"@modelcontextprotocol/sdk": ">=1.29.0 <2",
|
|
21
|
-
"skybridge": ">=0.
|
|
21
|
+
"skybridge": ">=0.36.2"
|
|
22
22
|
},
|
|
23
23
|
"peerDependenciesMeta": {
|
|
24
24
|
"skybridge": {
|
|
@@ -28,13 +28,13 @@
|
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
30
30
|
"@total-typescript/tsconfig": "^1.0.4",
|
|
31
|
-
"@types/node": "^25.
|
|
31
|
+
"@types/node": "^25.7.0",
|
|
32
32
|
"shx": "^0.4.0",
|
|
33
|
-
"skybridge": "^0.
|
|
34
|
-
"tsdown": "^0.
|
|
33
|
+
"skybridge": "^0.36.2",
|
|
34
|
+
"tsdown": "^0.22.0",
|
|
35
35
|
"typescript": "^6.0.3",
|
|
36
|
-
"vitest": "^4.1.
|
|
37
|
-
"zod": "^4.4.
|
|
36
|
+
"vitest": "^4.1.6",
|
|
37
|
+
"zod": "^4.4.3"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "shx rm -rf dist && tsdown",
|