@alpic-ai/insights 0.0.0-dev.ad3076e → 0.0.0-dev.ae87f1b
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 +22 -1
- package/dist/index.mjs +80 -3
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -51,4 +51,25 @@ declare function intentMiddleware(options?: IntentMiddlewareOptions): McpMiddlew
|
|
|
51
51
|
*/
|
|
52
52
|
declare const captureIntents: (server: McpServer | Server, options?: IntentMiddlewareOptions) => void;
|
|
53
53
|
//#endregion
|
|
54
|
-
|
|
54
|
+
//#region src/feedback-middleware.d.ts
|
|
55
|
+
interface FeedbackData {
|
|
56
|
+
content: string;
|
|
57
|
+
source: "model" | "user";
|
|
58
|
+
}
|
|
59
|
+
interface FeedbackMiddlewareOptions {
|
|
60
|
+
/**
|
|
61
|
+
* Custom handler invoked with the user's feedback. When provided, the middleware still attaches the feedback to the response `_meta`.
|
|
62
|
+
* The handler runs **in addition to** Alpic's dashboard delivery, feedback are still captured when deployed on Alpic.
|
|
63
|
+
*/
|
|
64
|
+
handler?: (feedback: FeedbackData) => Promise<void> | void;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Lets MCP server builders collect qualitative feedback from end users about their
|
|
68
|
+
* tool/server. Injects a `send_feedback` tool at `tools/list` time and intercepts calls
|
|
69
|
+
* to it at `tools/call` time. The tool has no handler on the server. The middleware
|
|
70
|
+
* short-circuits the call and either invokes the provided `handler` or attaches the
|
|
71
|
+
* feedback to the response `_meta`.
|
|
72
|
+
*/
|
|
73
|
+
declare function feedbackMiddleware(options?: FeedbackMiddlewareOptions): McpMiddlewareFn;
|
|
74
|
+
//#endregion
|
|
75
|
+
export { type FeedbackData, type FeedbackMiddlewareOptions, type IntentMiddlewareOptions, type McpMiddlewareFn, type PromptData, captureIntents, feedbackMiddleware, intentMiddleware };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,80 @@
|
|
|
1
1
|
import { CallToolRequestSchema, CallToolResultSchema, ListToolsResultSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
2
|
+
//#region src/feedback-middleware.ts
|
|
3
|
+
const FEEDBACK_TOOL_NAME = "send_feedback";
|
|
4
|
+
const FEEDBACK_TOOL_DESCRIPTION = "Send feedback about this MCP server to its operators. Use this tool ONLY for feedback about this MCP server itself, never about other tools, services, or the host. You MAY call this tool when you detect a genuine issue with this server (e.g. a tool that failed unexpectedly, an unhelpful response, a missing capability). You MAY also call it when the user explicitly asks to send feedback. Before sending, strip all personally identifiable information (PII) from the content, including names, email addresses, phone numbers, physical addresses, dates of birth, ID numbers, payment information, and any other information that could identify a specific individual. Replace stripped values with generic placeholders (e.g. \"[name]\", \"[email]\").";
|
|
5
|
+
const FEEDBACK_RESPONSE_TEXT = "Feedback received. Thanks!";
|
|
6
|
+
/**
|
|
7
|
+
* Lets MCP server builders collect qualitative feedback from end users about their
|
|
8
|
+
* tool/server. Injects a `send_feedback` tool at `tools/list` time and intercepts calls
|
|
9
|
+
* to it at `tools/call` time. The tool has no handler on the server. The middleware
|
|
10
|
+
* short-circuits the call and either invokes the provided `handler` or attaches the
|
|
11
|
+
* feedback to the response `_meta`.
|
|
12
|
+
*/
|
|
13
|
+
function feedbackMiddleware(options) {
|
|
14
|
+
return async (request, _extra, next) => {
|
|
15
|
+
const metaKeyName = process.env.ALPIC_FEEDBACK_META_KEY;
|
|
16
|
+
if (request.method === "tools/list") {
|
|
17
|
+
const rawResult = await next();
|
|
18
|
+
const parsed = ListToolsResultSchema.safeParse(rawResult);
|
|
19
|
+
if (!parsed.success) return rawResult;
|
|
20
|
+
if (parsed.data.tools.some((tool) => tool.name === "send_feedback")) return parsed.data;
|
|
21
|
+
parsed.data.tools.push({
|
|
22
|
+
name: FEEDBACK_TOOL_NAME,
|
|
23
|
+
description: FEEDBACK_TOOL_DESCRIPTION,
|
|
24
|
+
inputSchema: {
|
|
25
|
+
type: "object",
|
|
26
|
+
properties: {
|
|
27
|
+
content: {
|
|
28
|
+
type: "string",
|
|
29
|
+
description: "The feedback content, stripped of any Personally Identifiable Information (PII)."
|
|
30
|
+
},
|
|
31
|
+
source: {
|
|
32
|
+
type: "string",
|
|
33
|
+
enum: ["model", "user"],
|
|
34
|
+
description: "Who initiated this feedback: \"user\" if the user explicitly asked to send feedback, \"model\" if you are sending it autonomously."
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
required: ["content", "source"]
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
return parsed.data;
|
|
41
|
+
}
|
|
42
|
+
if (request.method !== "tools/call") return next();
|
|
43
|
+
const parsedRequest = CallToolRequestSchema.safeParse(request);
|
|
44
|
+
if (!parsedRequest.success || parsedRequest.data.params.name !== "send_feedback") return next();
|
|
45
|
+
const args = parsedRequest.data.params.arguments ?? {};
|
|
46
|
+
const content = typeof args.content === "string" ? args.content.trim() : void 0;
|
|
47
|
+
const source = args.source === "user" ? "user" : "model";
|
|
48
|
+
if (content === void 0 || content.length === 0) return {
|
|
49
|
+
content: [{
|
|
50
|
+
type: "text",
|
|
51
|
+
text: "Feedback ignored, `content` is required."
|
|
52
|
+
}],
|
|
53
|
+
isError: true
|
|
54
|
+
};
|
|
55
|
+
const feedback = {
|
|
56
|
+
content,
|
|
57
|
+
source
|
|
58
|
+
};
|
|
59
|
+
if (options?.handler) try {
|
|
60
|
+
await options.handler(feedback);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
console.error("Error calling feedback handler", error);
|
|
63
|
+
}
|
|
64
|
+
if (metaKeyName) return {
|
|
65
|
+
content: [{
|
|
66
|
+
type: "text",
|
|
67
|
+
text: FEEDBACK_RESPONSE_TEXT
|
|
68
|
+
}],
|
|
69
|
+
_meta: { [metaKeyName]: feedback }
|
|
70
|
+
};
|
|
71
|
+
return { content: [{
|
|
72
|
+
type: "text",
|
|
73
|
+
text: FEEDBACK_RESPONSE_TEXT
|
|
74
|
+
}] };
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
//#endregion
|
|
2
78
|
//#region src/intent-middleware.ts
|
|
3
79
|
const USER_INTENT_FIELD = "user_intent";
|
|
4
80
|
/**
|
|
@@ -8,16 +84,17 @@ const USER_INTENT_FIELD = "user_intent";
|
|
|
8
84
|
* (the server has no other way to access it).
|
|
9
85
|
*/
|
|
10
86
|
function intentMiddleware(options) {
|
|
11
|
-
const metaKeyName = process.env.ALPIC_INTENT_META_KEY;
|
|
12
87
|
const argumentNameOverride = options?.argumentNameOverride ?? {};
|
|
13
88
|
const toolsFilter = options?.tools ? new Set(options.tools) : null;
|
|
14
89
|
return async (request, _extra, next) => {
|
|
90
|
+
const metaKeyName = process.env.ALPIC_INTENT_META_KEY;
|
|
15
91
|
if (request.method === "tools/list") {
|
|
16
92
|
const rawResult = await next();
|
|
17
93
|
const parsed = ListToolsResultSchema.safeParse(rawResult);
|
|
18
94
|
if (!parsed.success) return rawResult;
|
|
19
95
|
for (const tool of parsed.data.tools) {
|
|
20
96
|
if (toolsFilter && !toolsFilter.has(tool.name)) continue;
|
|
97
|
+
if (tool.name === "send_feedback") continue;
|
|
21
98
|
if (argumentNameOverride[tool.name] != null) continue;
|
|
22
99
|
tool.inputSchema.properties = {
|
|
23
100
|
...tool.inputSchema.properties,
|
|
@@ -67,7 +144,7 @@ Examples:
|
|
|
67
144
|
const parsedRequest = CallToolRequestSchema.safeParse(request);
|
|
68
145
|
if (!parsedRequest.success) return next();
|
|
69
146
|
const toolName = parsedRequest.data.params.name;
|
|
70
|
-
if (toolsFilter && !toolsFilter.has(toolName)) {
|
|
147
|
+
if (toolsFilter && !toolsFilter.has(toolName) || toolName === "send_feedback") {
|
|
71
148
|
const args = parsedRequest.data.params.arguments ?? {};
|
|
72
149
|
if (USER_INTENT_FIELD in args) {
|
|
73
150
|
delete args[USER_INTENT_FIELD];
|
|
@@ -143,4 +220,4 @@ const captureIntents = (server, options) => {
|
|
|
143
220
|
handlers.set = (method, handler) => originalSet(method, wrap(method, handler));
|
|
144
221
|
};
|
|
145
222
|
//#endregion
|
|
146
|
-
export { captureIntents, intentMiddleware };
|
|
223
|
+
export { captureIntents, feedbackMiddleware, intentMiddleware };
|