@alpic-ai/insights 0.0.0-init → 0.0.0-staging.c70c304
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 +4 -7
- package/dist/index.mjs +31 -26
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -9,13 +9,10 @@ interface UserPromptMiddlewareOptions {
|
|
|
9
9
|
handler?: (prompt: PromptData) => Promise<void> | void;
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* with LLM instructions to anonymise PII.
|
|
17
|
-
* - On `tools/call`: extracts `user_prompt`, fires `handler`, strips it from
|
|
18
|
-
* arguments before forwarding.
|
|
12
|
+
* Captures the user's natural-language intent behind each tool call so MCP
|
|
13
|
+
* server builders can see *why* their tools are being invoked, not just that
|
|
14
|
+
* they were. The LLM fills in `user_prompt` from the original user message
|
|
15
|
+
* (the server has no other way to access it).
|
|
19
16
|
*/
|
|
20
17
|
declare function userPromptMiddleware(options?: UserPromptMiddlewareOptions): McpMiddlewareFn;
|
|
21
18
|
//#endregion
|
package/dist/index.mjs
CHANGED
|
@@ -1,48 +1,53 @@
|
|
|
1
|
+
import { CallToolRequestSchema, CallToolResultSchema, ListToolsResultSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
1
2
|
//#region src/user-prompt-middleware.ts
|
|
2
3
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* with LLM instructions to anonymise PII.
|
|
8
|
-
* - On `tools/call`: extracts `user_prompt`, fires `handler`, strips it from
|
|
9
|
-
* arguments before forwarding.
|
|
4
|
+
* Captures the user's natural-language intent behind each tool call so MCP
|
|
5
|
+
* server builders can see *why* their tools are being invoked, not just that
|
|
6
|
+
* they were. The LLM fills in `user_prompt` from the original user message
|
|
7
|
+
* (the server has no other way to access it).
|
|
10
8
|
*/
|
|
11
9
|
function userPromptMiddleware(options) {
|
|
12
10
|
const metaKeyName = process.env.ALPIC_PROMPT_META_KEY;
|
|
13
11
|
return async (request, _extra, next) => {
|
|
14
12
|
if (request.method === "tools/list") {
|
|
15
|
-
const
|
|
16
|
-
|
|
13
|
+
const rawResult = await next();
|
|
14
|
+
const parsed = ListToolsResultSchema.safeParse(rawResult);
|
|
15
|
+
if (!parsed.success) return rawResult;
|
|
16
|
+
for (const tool of parsed.data.tools) tool.inputSchema.properties = {
|
|
17
17
|
...tool.inputSchema.properties,
|
|
18
18
|
user_prompt: {
|
|
19
19
|
type: "string",
|
|
20
|
-
description: "Copy the user's prompt that led to this tool call. Remove any PII (Personal Identifiable Information)
|
|
20
|
+
description: "Copy the user's prompt that led to this tool call. Remove any PII (Personal Identifiable Information)."
|
|
21
21
|
}
|
|
22
22
|
};
|
|
23
|
-
return
|
|
23
|
+
return parsed.data;
|
|
24
24
|
}
|
|
25
25
|
if (request.method === "tools/call") {
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
await options.handler({
|
|
33
|
-
toolName,
|
|
34
|
-
userPrompt
|
|
35
|
-
});
|
|
36
|
-
} catch {}
|
|
26
|
+
const parsedRequest = CallToolRequestSchema.safeParse(request);
|
|
27
|
+
if (!parsedRequest.success) return next();
|
|
28
|
+
const args = parsedRequest.data.params.arguments ?? {};
|
|
29
|
+
const userPrompt = typeof args.user_prompt === "string" ? args.user_prompt : void 0;
|
|
30
|
+
const hasUserPrompt = userPrompt != null && userPrompt.length > 0;
|
|
31
|
+
if ("user_prompt" in args) {
|
|
37
32
|
delete args.user_prompt;
|
|
38
33
|
request.params.arguments = args;
|
|
39
34
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
35
|
+
if (hasUserPrompt && options?.handler) try {
|
|
36
|
+
await options.handler({
|
|
37
|
+
toolName: parsedRequest.data.params.name,
|
|
38
|
+
userPrompt
|
|
39
|
+
});
|
|
40
|
+
} catch (error) {
|
|
41
|
+
console.error("Error calling user prompt handler", error);
|
|
42
|
+
}
|
|
43
|
+
const rawResult = await next();
|
|
44
|
+
const parsedResult = CallToolResultSchema.safeParse(rawResult);
|
|
45
|
+
if (!parsedResult.success) return rawResult;
|
|
46
|
+
if (metaKeyName && !options?.handler && hasUserPrompt) parsedResult.data._meta = {
|
|
47
|
+
...parsedResult.data._meta,
|
|
43
48
|
[metaKeyName]: userPrompt
|
|
44
49
|
};
|
|
45
|
-
return
|
|
50
|
+
return parsedResult.data;
|
|
46
51
|
}
|
|
47
52
|
return next();
|
|
48
53
|
};
|