@alpic-ai/insights 0.0.0-dev.93179f5 → 0.0.0-dev.9398b42
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 +19 -15
- package/dist/index.mjs +27 -16
- package/package.json +1 -1
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_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.
|
|
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
|
|
@@ -31,20 +35,20 @@ type McpMiddlewareFn = (request: {
|
|
|
31
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,5 +1,5 @@
|
|
|
1
1
|
import { CallToolRequestSchema, CallToolResultSchema, ListToolsResultSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
-
//#region src/
|
|
2
|
+
//#region src/intent-middleware.ts
|
|
3
3
|
const USER_INTENT_FIELD = "user_intent";
|
|
4
4
|
/**
|
|
5
5
|
* Captures the user's natural-language intent behind each tool call so MCP
|
|
@@ -7,22 +7,24 @@ const USER_INTENT_FIELD = "user_intent";
|
|
|
7
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
26
|
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.
|
|
27
|
+
conversation context that triggered this tool call.
|
|
26
28
|
This is used to understand the user's intent and context to improve the overall user experience.
|
|
27
29
|
|
|
28
30
|
- For short, self-contained prompts (e.g. "I want new shoes"), copy the user message as-is.
|
|
@@ -64,7 +66,16 @@ Examples:
|
|
|
64
66
|
if (request.method === "tools/call") {
|
|
65
67
|
const parsedRequest = CallToolRequestSchema.safeParse(request);
|
|
66
68
|
if (!parsedRequest.success) return next();
|
|
67
|
-
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;
|
|
68
79
|
const args = parsedRequest.data.params.arguments ?? {};
|
|
69
80
|
const userPrompt = typeof args[promptField] === "string" ? args[promptField] : void 0;
|
|
70
81
|
const hasUserPrompt = userPrompt != null && userPrompt.length > 0;
|
|
@@ -83,7 +94,7 @@ Examples:
|
|
|
83
94
|
const rawResult = await next();
|
|
84
95
|
const parsedResult = CallToolResultSchema.safeParse(rawResult);
|
|
85
96
|
if (!parsedResult.success) return rawResult;
|
|
86
|
-
if (metaKeyName &&
|
|
97
|
+
if (metaKeyName && hasUserPrompt) parsedResult.data._meta = {
|
|
87
98
|
...parsedResult.data._meta,
|
|
88
99
|
[metaKeyName]: userPrompt
|
|
89
100
|
};
|
|
@@ -93,20 +104,20 @@ Examples:
|
|
|
93
104
|
};
|
|
94
105
|
}
|
|
95
106
|
//#endregion
|
|
96
|
-
//#region src/capture-
|
|
97
|
-
const INSTALLED_MARKER = "
|
|
107
|
+
//#region src/capture-intents.ts
|
|
108
|
+
const INSTALLED_MARKER = "__alpicCaptureIntentsInstalled";
|
|
98
109
|
/**
|
|
99
|
-
* Captures the user's natural-language
|
|
110
|
+
* Captures the user's natural-language intent behind each tool call on a vanilla
|
|
100
111
|
* `@modelcontextprotocol/sdk` server. Accepts the high-level `McpServer` or the
|
|
101
112
|
* low-level `Server` and patches the `tools/list` and `tools/call` request
|
|
102
|
-
* handlers to surface the captured
|
|
103
|
-
* `
|
|
113
|
+
* handlers to surface the captured intent via `options.handler` (or, when
|
|
114
|
+
* `ALPIC_INTENT_META_KEY` is set, via the response `_meta`).
|
|
104
115
|
*
|
|
105
116
|
* Already-registered handlers are wrapped immediately; future registrations
|
|
106
117
|
* (e.g. tools added after this call) are wrapped via a `Map.set` proxy so order
|
|
107
118
|
* of calls relative to `registerTool` does not matter.
|
|
108
119
|
*/
|
|
109
|
-
const
|
|
120
|
+
const captureIntents = (server, options) => {
|
|
110
121
|
const handlers = ("server" in server ? server.server : server)?._requestHandlers;
|
|
111
122
|
if (!(handlers instanceof Map)) {
|
|
112
123
|
console.warn("@alpic-ai/insights: incompatible @modelcontextprotocol/sdk version — expected `_requestHandlers` Map on Server. Prompt capture disabled.");
|
|
@@ -115,7 +126,7 @@ const captureUserPrompts = (server, options) => {
|
|
|
115
126
|
const marked = handlers;
|
|
116
127
|
if (marked[INSTALLED_MARKER]) return;
|
|
117
128
|
marked[INSTALLED_MARKER] = true;
|
|
118
|
-
const middleware =
|
|
129
|
+
const middleware = intentMiddleware(options);
|
|
119
130
|
const targets = new Set(["tools/list", "tools/call"]);
|
|
120
131
|
const wrap = (method, handler) => {
|
|
121
132
|
if (!targets.has(method)) return handler;
|
|
@@ -132,4 +143,4 @@ const captureUserPrompts = (server, options) => {
|
|
|
132
143
|
handlers.set = (method, handler) => originalSet(method, wrap(method, handler));
|
|
133
144
|
};
|
|
134
145
|
//#endregion
|
|
135
|
-
export {
|
|
146
|
+
export { captureIntents, intentMiddleware };
|