-
Notifications
You must be signed in to change notification settings - Fork 6
feat(mcp): Add send_feedback tool wired to Sentry User Feedback #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
|
|
||
| const sentry = vi.hoisted(() => ({ | ||
| captureFeedback: vi.fn(() => "feedback-event-id"), | ||
| withScope: vi.fn(), | ||
| setTag: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@sentry/cloudflare", () => ({ | ||
| captureFeedback: sentry.captureFeedback, | ||
| withScope: (cb: (scope: { setTag: typeof sentry.setTag }) => unknown) => | ||
| cb({ setTag: sentry.setTag }), | ||
| })); | ||
|
|
||
| import { register } from "../../src/tools/send-feedback.js"; | ||
|
|
||
| function getToolHandler(server: McpServer, toolName: string) { | ||
| const tools = (server as any)._registeredTools as Record<string, any>; | ||
| const tool = tools?.[toolName]; | ||
| if (!tool) throw new Error(`Tool ${toolName} not registered`); | ||
| return async (args: Record<string, unknown>) => tool.handler(args, {} as any); | ||
| } | ||
|
|
||
| describe("send_feedback tool", () => { | ||
| let server: McpServer; | ||
|
|
||
| beforeEach(() => { | ||
| sentry.captureFeedback.mockClear(); | ||
| sentry.setTag.mockClear(); | ||
| server = new McpServer({ name: "test", version: "0.0.1" }); | ||
| register(server); | ||
| }); | ||
|
|
||
| it("captures the message as Sentry feedback and returns the feedback id", async () => { | ||
| const handler = getToolHandler(server, "send_feedback"); | ||
| const result = await handler({ | ||
| message: "get_breakdown rejected my dimension and the error did not say which values are valid", | ||
| category: "confusing_error", | ||
| tool_name: "get_breakdown", | ||
| }); | ||
|
|
||
| expect(sentry.captureFeedback).toHaveBeenCalledWith({ | ||
| message: | ||
| "get_breakdown rejected my dimension and the error did not say which values are valid", | ||
| }); | ||
| expect(sentry.setTag).toHaveBeenCalledWith("feedback.category", "confusing_error"); | ||
| expect(sentry.setTag).toHaveBeenCalledWith("feedback.tool", "get_breakdown"); | ||
| expect(result.structuredContent).toEqual({ | ||
| recorded: true, | ||
| feedback_id: "feedback-event-id", | ||
| }); | ||
| expect(result.isError).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("defaults the category tag when none is given", async () => { | ||
| const handler = getToolHandler(server, "send_feedback"); | ||
| await handler({ message: "something long enough to pass validation" }); | ||
|
|
||
| expect(sentry.setTag).toHaveBeenCalledWith("feedback.category", "bug"); | ||
| expect(sentry.setTag).not.toHaveBeenCalledWith("feedback.tool", expect.anything()); | ||
| }); | ||
|
|
||
| it("is not registered as read-only", () => { | ||
| const tool = ((server as any)._registeredTools as Record<string, any>)["send_feedback"]; | ||
| expect(tool.annotations.readOnlyHint).toBe(false); | ||
| expect(tool.annotations.idempotentHint).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { z } from "zod"; | ||
| import * as Sentry from "@sentry/cloudflare"; | ||
| import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
|
|
||
| export const FEEDBACK_CATEGORIES = [ | ||
| "bug", | ||
| "confusing_error", | ||
| "unexpected_results", | ||
| "feature_request", | ||
| "praise", | ||
| ] as const; | ||
|
|
||
| /** | ||
| * Lets the calling agent (or its user) file feedback about this MCP server into | ||
| * Sentry User Feedback, linked to the active trace. Registered only when the | ||
| * hosting entry point runs with Sentry (`ServerConfig.enableFeedbackTool`) — | ||
| * without an initialized SDK the feedback would go nowhere, so the tool is | ||
| * hidden rather than silently dropping submissions. | ||
| * | ||
| * The message is untrusted caller input: it is bounded by the schema and passed | ||
| * to Sentry as data, never interpreted. On `/internal` the feedback inherits the | ||
| * authenticated user from the isolation scope; BYOK `/mcp` feedback stays | ||
| * anonymous, matching the endpoint's privacy posture. | ||
| */ | ||
| export function register(server: McpServer) { | ||
| server.registerTool( | ||
| "send_feedback", | ||
| { | ||
| title: "Send Feedback", | ||
| description: | ||
| "Report feedback about this MCP server to its maintainers: a confusing error, a query you could not express, results that did not match expectations, or something that worked well. Use this after hitting friction with the other tools so the server can improve.", | ||
| annotations: { readOnlyHint: false, idempotentHint: false, openWorldHint: true }, | ||
| outputSchema: { | ||
| recorded: z.boolean(), | ||
| feedback_id: z.string().optional(), | ||
| }, | ||
| inputSchema: { | ||
| message: z | ||
| .string() | ||
| .min(10) | ||
| .max(4000) | ||
| .describe( | ||
| "What happened and what you expected. Name the tool and the arguments that caused friction; never include API keys or secrets." | ||
| ), | ||
| category: z | ||
| .enum(FEEDBACK_CATEGORIES) | ||
| .default("bug") | ||
| .describe("The kind of feedback"), | ||
| tool_name: z | ||
| .string() | ||
| .max(64) | ||
| .optional() | ||
| .describe("Which tool the feedback is about, if any (e.g. get_breakdown)"), | ||
| }, | ||
| }, | ||
| async (args) => { | ||
| const feedbackId = Sentry.withScope((scope) => { | ||
| scope.setTag("feedback.category", args.category ?? "bug"); | ||
| if (args.tool_name) { | ||
| scope.setTag("feedback.tool", args.tool_name); | ||
| } | ||
| return Sentry.captureFeedback({ message: args.message }); | ||
| }); | ||
|
|
||
| return { | ||
| content: [ | ||
| { | ||
| type: "text" as const, | ||
| text: "Feedback recorded — thank you. The maintainers review submissions in Sentry User Feedback.", | ||
| }, | ||
| ], | ||
| structuredContent: { recorded: true, feedback_id: feedbackId }, | ||
| }; | ||
| } | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.