From c861e8bd614e8361ce402790b30af89ee618b77a Mon Sep 17 00:00:00 2001 From: Nithin RG <2275985+nithinrgaddam@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:16:55 -0600 Subject: [PATCH 1/5] feat(mcp): Attach videos to issue notes and list them in activity add_issue_note takes attachmentPaths: local videos uploaded through the issue's comment-attachments endpoint and linked after the note text. Only hosts that provide a readLocalFile reader (the CLI's stdio server) can attach; the hosted server explains that it cannot. Files must have a video extension and start with matching MP4/MOV, WebM or Ogg bytes, so a renamed key or document is never uploaded. get_issue_activity lists the videos attached to each comment with their API URL. --- packages/mcp-core/package.json | 4 + packages/mcp-core/src/api-client/client.ts | 62 ++++++ packages/mcp-core/src/api-client/schema.ts | 9 + packages/mcp-core/src/api-client/types.ts | 4 + .../mcp-core/src/comment-attachments.test.ts | 81 ++++++++ packages/mcp-core/src/comment-attachments.ts | 119 +++++++++++ packages/mcp-core/src/skillDefinitions.json | 4 +- packages/mcp-core/src/toolDefinitions.json | 84 +++++++- .../src/tools/catalog/add-issue-note.test.ts | 188 +++++++++++++++++- .../src/tools/catalog/add-issue-note.ts | 91 ++++++++- .../tools/catalog/get-issue-activity.test.ts | 27 +++ .../src/tools/catalog/get-issue-activity.ts | 30 ++- packages/mcp-core/src/types.ts | 19 ++ 13 files changed, 707 insertions(+), 15 deletions(-) create mode 100644 packages/mcp-core/src/comment-attachments.test.ts create mode 100644 packages/mcp-core/src/comment-attachments.ts diff --git a/packages/mcp-core/package.json b/packages/mcp-core/package.json index ea61352bd0..d4e84738f3 100644 --- a/packages/mcp-core/package.json +++ b/packages/mcp-core/package.json @@ -29,6 +29,10 @@ "types": "./dist/api-client/index.d.ts", "default": "./dist/api-client/index.js" }, + "./comment-attachments": { + "types": "./dist/comment-attachments.d.ts", + "default": "./dist/comment-attachments.js" + }, "./constants": { "types": "./dist/constants.d.ts", "default": "./dist/constants.js" diff --git a/packages/mcp-core/src/api-client/client.ts b/packages/mcp-core/src/api-client/client.ts index 1b2d5f3dc3..c76896aca4 100644 --- a/packages/mcp-core/src/api-client/client.ts +++ b/packages/mcp-core/src/api-client/client.ts @@ -23,11 +23,13 @@ import { type ApiTarget, ApiValidationError, applyTimeParams, + buildSentryApiUrl, createApiClientCore, createApiError, createApiTransport, createIdempotentOperationMatcher, nextPageCursor, + readErrorResponse, readJsonResponse, type RetryPolicy, selectIdempotentWrites, @@ -149,6 +151,7 @@ import { IssueActivityListResponseSchema, IssueAlertRuleListSchema, IssueAlertRuleSchema, + IssueCommentAttachmentSchema, IssueCommentListSchema, IssueCommentSchema, IssueIntegrationListSchema, @@ -231,6 +234,7 @@ import type { IssueAlertRule, IssueAlertRuleList, IssueComment, + IssueCommentAttachment, IssueCommentList, IssueIntegrationList, IssueList, @@ -838,6 +842,14 @@ export class SentryApiService { * apiService.getIssueUrl("my-org", "PROJ-123") * ``` */ + /** + * Full URL for an API path that already carries the `/api/0` prefix, such + * as a comment attachment link read from comment text. + */ + getAbsoluteApiUrl(path: string): string { + return `${this.origin()}${path}`; + } + getIssueUrl(organizationSlug: string, issueId: string): string { return getIssueUrlUtil(this.host, organizationSlug, issueId, this.protocol); } @@ -4378,6 +4390,56 @@ export class SentryApiService { return IssueCommentSchema.parse(body); } + /** + * Uploads a video for an issue comment. The comment links the returned + * `url`; Sentry enforces the size limit and accepted video types. + */ + async uploadIssueCommentAttachment( + { + organizationSlug, + issueId, + fileName, + contentType, + data, + }: { + organizationSlug: string; + issueId: string; + fileName: string; + contentType: string; + data: Uint8Array; + }, + opts?: RequestOptions, + ): Promise { + const path = apiPath`/organizations/${organizationSlug}/issues/${issueId}/comment-attachments/`; + const form = new FormData(); + form.append( + "file", + new Blob([new Uint8Array(data)], { type: contentType }), + fileName, + ); + // The core sends every body as JSON, so a multipart upload goes straight + // to the transport and lets fetch set the multipart boundary. + const response = await transport.request( + buildSentryApiUrl(this.origin(opts?.host), path), + { + method: "POST", + headers: this.requestHeaders(), + body: form, + ...(this.signal ? { signal: this.signal } : {}), + }, + ); + if (!response.ok) { + const body = await readErrorResponse(response); + throw createApiError( + body.detail ?? `Video upload failed (${response.status})`, + response.status, + body.detail, + body.json, + ); + } + return IssueCommentAttachmentSchema.parse(await readJsonResponse(response)); + } + async getIssueActivity( { organizationSlug, diff --git a/packages/mcp-core/src/api-client/schema.ts b/packages/mcp-core/src/api-client/schema.ts index 79b4a826f5..45b904838c 100644 --- a/packages/mcp-core/src/api-client/schema.ts +++ b/packages/mcp-core/src/api-client/schema.ts @@ -798,6 +798,15 @@ export const IssueCommentSchema = IssueActivitySchema.extend({ export const IssueCommentListSchema = z.array(IssueCommentSchema); +/** A video uploaded through an issue's `comment-attachments/` endpoint. */ +export const IssueCommentAttachmentSchema = z.object({ + id: z.string(), + name: z.string(), + size: z.number(), + contentType: z.string(), + url: z.string(), +}); + /** * Organization tag lists are backed by `TagKeySerializerResponse`, which only * guarantees `key` and `name`. Count fields are backend-dependent and may come diff --git a/packages/mcp-core/src/api-client/types.ts b/packages/mcp-core/src/api-client/types.ts index 505c8cd1f1..e9fd3027a8 100644 --- a/packages/mcp-core/src/api-client/types.ts +++ b/packages/mcp-core/src/api-client/types.ts @@ -86,6 +86,7 @@ import type { IssueAlertRuleListSchema, IssueAlertRuleSchema, IssueCommentListSchema, + IssueCommentAttachmentSchema, IssueCommentSchema, IssueIntegrationListSchema, IssueIntegrationSchema, @@ -218,6 +219,9 @@ export type Commit = z.infer; export type Issue = z.infer; export type IssueActivity = z.infer; export type IssueComment = z.infer; +export type IssueCommentAttachment = z.infer< + typeof IssueCommentAttachmentSchema +>; type IssueUpdateStatusDetails = NonNullable< NonNullable["statusDetails"] diff --git a/packages/mcp-core/src/comment-attachments.test.ts b/packages/mcp-core/src/comment-attachments.test.ts new file mode 100644 index 0000000000..41adf60a5f --- /dev/null +++ b/packages/mcp-core/src/comment-attachments.test.ts @@ -0,0 +1,81 @@ +import { describe, expect, it } from "vitest"; +import { + appendCommentAttachments, + commentAttachmentContentType, + commentAttachmentMarkdown, + parseCommentAttachments, +} from "./comment-attachments"; + +const MP4 = new Uint8Array([0, 0, 0, 0x18, 0x66, 0x74, 0x79, 0x70, 0x69]); +const WEBM = new Uint8Array([0x1a, 0x45, 0xdf, 0xa3, 0x01]); +const OGG = new Uint8Array([0x4f, 0x67, 0x67, 0x53, 0x00]); +const TEXT = new TextEncoder().encode("-----BEGIN OPENSSH PRIVATE KEY-----"); + +const PATH = "/api/0/organizations/my-org/issues/123/comment-attachments/5/"; + +describe("commentAttachmentContentType", () => { + it.each([ + ["clip.mp4", MP4, "video/mp4"], + ["clip.M4V", MP4, "video/mp4"], + ["clip.mov", MP4, "video/quicktime"], + ["clip.webm", WEBM, "video/webm"], + ["clip.ogv", OGG, "video/ogg"], + ])("accepts %s", (name, bytes, expected) => { + expect(commentAttachmentContentType(name, bytes)).toBe(expected); + }); + + it.each([ + ["a key renamed to a video", "id_rsa.mp4", TEXT], + ["a video with the wrong extension", "clip.webm", MP4], + ["an unsupported extension", "notes.txt", MP4], + ["a file without an extension", "clip", MP4], + ["an empty file", "clip.mp4", new Uint8Array()], + ])("rejects %s", (_label, name, bytes) => { + expect(commentAttachmentContentType(name, bytes)).toBeNull(); + }); +}); + +describe("parseCommentAttachments", () => { + it("reads attachment links and ignores other links", () => { + expect( + parseCommentAttachments( + `Repro below\n[repro.mp4](${PATH})\nSee [docs](https://docs.sentry.io/)`, + ), + ).toEqual([{ id: "5", name: "repro.mp4", path: PATH }]); + }); + + it("unescapes brackets in the file name", () => { + expect(parseCommentAttachments(`[take \\[2\\].mp4](${PATH})`)).toEqual([ + { id: "5", name: "take [2].mp4", path: PATH }, + ]); + }); + + it("returns nothing for empty text", () => { + expect(parseCommentAttachments(null)).toEqual([]); + expect(parseCommentAttachments("")).toEqual([]); + }); +}); + +describe("appendCommentAttachments", () => { + it("adds one link per video after the text", () => { + expect( + appendCommentAttachments("Repro ", [ + { name: "a.mp4", url: PATH }, + { name: "b].mp4", url: PATH }, + ]), + ).toBe(`Repro\n[a.mp4](${PATH})\n[b\\].mp4](${PATH})`); + }); + + it("round-trips through parseCommentAttachments", () => { + const text = appendCommentAttachments("x", [ + { name: "we[ir]d\\.mp4", url: PATH }, + ]); + expect(parseCommentAttachments(text)[0]?.name).toBe("we[ir]d\\.mp4"); + }); + + it("escapes link text", () => { + expect(commentAttachmentMarkdown({ name: "[x]", url: PATH })).toBe( + `[\\[x\\]](${PATH})`, + ); + }); +}); diff --git a/packages/mcp-core/src/comment-attachments.ts b/packages/mcp-core/src/comment-attachments.ts new file mode 100644 index 0000000000..9a8983a3eb --- /dev/null +++ b/packages/mcp-core/src/comment-attachments.ts @@ -0,0 +1,119 @@ +/** + * Videos attached to issue comments. + * + * Sentry stores each upload through the issue's `comment-attachments/` + * endpoint and the comment text links to it as Markdown. The CLI and the MCP + * server share these helpers so both accept the same files and read the same + * links back out of comment text. + */ + +/** Video types Sentry accepts for comment attachments, keyed by extension. */ +const VIDEO_TYPES_BY_EXTENSION: Readonly> = { + mp4: "video/mp4", + m4v: "video/mp4", + mov: "video/quicktime", + webm: "video/webm", + ogv: "video/ogg", + ogg: "video/ogg", +}; + +/** File extensions accepted for comment attachments, for help text. */ +export const COMMENT_ATTACHMENT_EXTENSIONS = Object.keys( + VIDEO_TYPES_BY_EXTENSION, +); + +function startsWith( + bytes: Uint8Array, + signature: readonly number[], + offset = 0, +) { + return signature.every((byte, index) => bytes[offset + index] === byte); +} + +/** True when the bytes begin like a container of the given video type. */ +function hasVideoSignature(contentType: string, bytes: Uint8Array): boolean { + switch (contentType) { + case "video/mp4": + case "video/quicktime": + // ISO base media: a box size, then the `ftyp` box type. + return startsWith(bytes, [0x66, 0x74, 0x79, 0x70], 4); + case "video/webm": + // EBML header. + return startsWith(bytes, [0x1a, 0x45, 0xdf, 0xa3]); + case "video/ogg": + return startsWith(bytes, [0x4f, 0x67, 0x67, 0x53]); // "OggS" + default: + return false; + } +} + +/** + * Content type to upload a file as, or null when it is not a supported video. + * + * Both the extension and the file's leading bytes must match, so a renamed + * text file, key or document is never sent as a video. + */ +export function commentAttachmentContentType( + fileName: string, + bytes: Uint8Array, +): string | null { + const extension = fileName.split(".").pop()?.toLowerCase() ?? ""; + const contentType = VIDEO_TYPES_BY_EXTENSION[extension]; + if (!contentType || !hasVideoSignature(contentType, bytes)) { + return null; + } + return contentType; +} + +/** A video uploaded to an issue, as the upload endpoint returns it. */ +export type CommentAttachment = { + id: string; + name: string; + size: number; + contentType: string; + /** API path, e.g. `/api/0/organizations/org/issues/1/comment-attachments/5/`. */ + url: string; +}; + +/** A video linked from comment text. */ +export type CommentAttachmentLink = { + id: string; + name: string; + /** API path of the video, as written in the comment. */ + path: string; +}; + +const ATTACHMENT_LINK = + /\[((?:\\.|[^\]\\])*)\]\((\/api\/0\/organizations\/[^/\s)]+\/issues\/[^/\s)]+\/comment-attachments\/(\d+)\/)\)/g; + +/** Markdown link for an uploaded video, with link-text brackets escaped. */ +export function commentAttachmentMarkdown(attachment: { + name: string; + url: string; +}): string { + const label = attachment.name.replace(/([\\[\]])/g, "\\$1"); + return `[${label}](${attachment.url})`; +} + +/** Videos linked from a comment's text, in the order they appear. */ +export function parseCommentAttachments( + text: string | null | undefined, +): CommentAttachmentLink[] { + if (!text) { + return []; + } + return Array.from(text.matchAll(ATTACHMENT_LINK), (match) => ({ + id: match[3] ?? "", + name: (match[1] ?? "").replace(/\\([\\[\]])/g, "$1"), + path: match[2] ?? "", + })); +} + +/** Comment text with each uploaded video's link appended on its own line. */ +export function appendCommentAttachments( + text: string, + attachments: ReadonlyArray<{ name: string; url: string }>, +): string { + const links = attachments.map(commentAttachmentMarkdown); + return [text.trimEnd(), ...links].filter((line) => line !== "").join("\n"); +} diff --git a/packages/mcp-core/src/skillDefinitions.json b/packages/mcp-core/src/skillDefinitions.json index cd770a91c3..9ac19a01f2 100644 --- a/packages/mcp-core/src/skillDefinitions.json +++ b/packages/mcp-core/src/skillDefinitions.json @@ -99,7 +99,7 @@ }, { "name": "get_issue_activity", - "description": "Get the activity feed and comments of a Sentry issue: who resolved, ignored, assigned, or commented on it, and when.", + "description": "Get the activity feed and comments of a Sentry issue: who resolved, ignored, assigned, or commented on it, and when, plus any videos attached to comments.", "requiredScopes": ["event:read"] }, { @@ -450,7 +450,7 @@ }, { "name": "get_issue_activity", - "description": "Get the activity feed and comments of a Sentry issue: who resolved, ignored, assigned, or commented on it, and when.", + "description": "Get the activity feed and comments of a Sentry issue: who resolved, ignored, assigned, or commented on it, and when, plus any videos attached to comments.", "requiredScopes": ["event:read"] }, { diff --git a/packages/mcp-core/src/toolDefinitions.json b/packages/mcp-core/src/toolDefinitions.json index 79b26b062e..263410c2ef 100644 --- a/packages/mcp-core/src/toolDefinitions.json +++ b/packages/mcp-core/src/toolDefinitions.json @@ -35,7 +35,16 @@ "type": "string", "minLength": 1, "maxLength": 4096, - "description": "The comment text, posted as is." + "description": "The comment text, posted as is. Links to attached videos are added after it." + }, + "attachmentPaths": { + "description": "Local paths of videos to attach (MP4, MOV, WebM or Ogg), such as a screen recording of the bug. Only works when the MCP server runs on the user's machine (`sentry mcp`). Only attach files the user asked for.", + "maxItems": 5, + "type": "array", + "items": { + "type": "string", + "minLength": 1 + } } }, "required": ["text"] @@ -69,9 +78,36 @@ }, "text": { "type": ["string", "null"] + }, + "attachments": { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "name": { + "type": "string" + }, + "url": { + "type": "string" + } + }, + "required": ["id", "name", "url"], + "additionalProperties": false + }, + "description": "Videos uploaded with the note and linked from its text." } }, - "required": ["id", "type", "author", "dateCreated", "text"], + "required": [ + "id", + "type", + "author", + "dateCreated", + "text", + "attachments" + ], "additionalProperties": false } }, @@ -6521,7 +6557,7 @@ { "name": "get_issue_activity", "title": "Get Issue Activity", - "description": "Get the activity feed and comments of a Sentry issue: who resolved, ignored, assigned, or commented on it, and when.", + "description": "Get the activity feed and comments of a Sentry issue: who resolved, ignored, assigned, or commented on it, and when, plus any videos attached to comments.", "inputSchema": { "type": "object", "properties": { @@ -6602,6 +6638,27 @@ "text": { "type": ["string", "null"] }, + "attachments": { + "description": "Videos attached to a comment. Download one with `sentry issue comments --download `.", + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "name": { + "type": "string" + }, + "url": { + "type": "string", + "description": "API URL that streams the video." + } + }, + "required": ["id", "name", "url"], + "additionalProperties": false + } + }, "assignee": { "type": "object", "properties": { @@ -6855,6 +6912,27 @@ "text": { "type": ["string", "null"] }, + "attachments": { + "description": "Videos attached to a comment. Download one with `sentry issue comments --download `.", + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "name": { + "type": "string" + }, + "url": { + "type": "string", + "description": "API URL that streams the video." + } + }, + "required": ["id", "name", "url"], + "additionalProperties": false + } + }, "assignee": { "type": "object", "properties": { diff --git a/packages/mcp-core/src/tools/catalog/add-issue-note.test.ts b/packages/mcp-core/src/tools/catalog/add-issue-note.test.ts index b11e877d79..e666463f36 100644 --- a/packages/mcp-core/src/tools/catalog/add-issue-note.test.ts +++ b/packages/mcp-core/src/tools/catalog/add-issue-note.test.ts @@ -1,12 +1,28 @@ -import { afterEach, describe, expect, it } from "vitest"; +import { afterEach, assert, describe, expect, it } from "vitest"; import { mswServer } from "@sentry/mcp-server-mocks"; import { http, HttpResponse } from "msw"; import { assertStructuredOnlyResult, getStructuredContent, } from "../../test-utils/structured-content.js"; +import { UserInputError } from "../../errors.js"; +import type { LocalFile } from "../../types.js"; import addIssueNote, { addIssueNoteOutputSchema } from "./add-issue-note.js"; +const MP4 = new Uint8Array([0, 0, 0, 0x18, 0x66, 0x74, 0x79, 0x70, 0x69]); +const ATTACHMENTS_PATH = + "*/api/0/organizations/:org/issues/:issueId/comment-attachments/"; + +function localFiles(files: Record) { + return async (path: string) => { + const file = files[path]; + if (!file) { + throw new UserInputError(`No such file: ${path}`); + } + return file; + }; +} + const context = { constraints: { organizationSlug: null, @@ -57,6 +73,7 @@ describe("add_issue_note", () => { "issueId": "CLOUDFLARE-MCP-41", "issueUrl": "https://sentry-mcp-evals.sentry.io/issues/CLOUDFLARE-MCP-41", "note": { + "attachments": [], "author": "Jane Developer (Claude via MCP)", "dateCreated": "2025-04-14T12:34:56.000Z", "id": "comment-1", @@ -89,6 +106,7 @@ describe("add_issue_note", () => { "issueId": "CLOUDFLARE-MCP-41", "issueUrl": "https://sentry-mcp-evals.sentry.io/issues/CLOUDFLARE-MCP-41", "note": { + "attachments": [], "author": "Test User", "dateCreated": "2025-04-14T12:34:56.000Z", "id": "12345", @@ -99,4 +117,172 @@ describe("add_issue_note", () => { } `); }); + + it("uploads local videos and links them in the note", async () => { + const uploads: Array<{ name: string; type: string; size: number }> = []; + let postedText: unknown; + mswServer.use( + http.post(ATTACHMENTS_PATH, async ({ request, params }) => { + const file = (await request.formData()).get("file"); + assert(file instanceof File); + uploads.push({ name: file.name, type: file.type, size: file.size }); + return HttpResponse.json( + { + id: String(uploads.length), + name: file.name, + size: file.size, + contentType: file.type, + url: `/api/0/organizations/${String(params["org"])}/issues/${String(params["issueId"])}/comment-attachments/${uploads.length}/`, + }, + { status: 201 }, + ); + }), + http.post( + "*/api/0/organizations/:org/issues/:issueId/notes/", + async ({ request }) => { + postedText = ((await request.json()) as { text: unknown }).text; + return HttpResponse.json({ + id: "activity-2", + commentId: "comment-2", + type: "note", + user: { name: "Jane Developer" }, + dateCreated: "2025-04-14T12:34:56.000Z", + data: { text: postedText }, + }); + }, + ), + ); + + const result = await addIssueNote.handler( + { + organizationSlug: "sentry-mcp-evals", + regionUrl: null, + issueId: "CLOUDFLARE-MCP-41", + text: "Screen recording of the crash.", + attachmentPaths: ["/tmp/repro.mp4", "/tmp/take2.mov"], + }, + { + ...context, + readLocalFile: localFiles({ + "/tmp/repro.mp4": { name: "repro.mp4", data: MP4 }, + "/tmp/take2.mov": { name: "take2.mov", data: MP4 }, + }), + }, + ); + + expect(uploads).toEqual([ + { name: "repro.mp4", type: "video/mp4", size: MP4.length }, + { name: "take2.mov", type: "video/quicktime", size: MP4.length }, + ]); + expect(postedText).toBe( + "Screen recording of the crash.\n" + + "[repro.mp4](/api/0/organizations/sentry-mcp-evals/issues/CLOUDFLARE-MCP-41/comment-attachments/1/)\n" + + "[take2.mov](/api/0/organizations/sentry-mcp-evals/issues/CLOUDFLARE-MCP-41/comment-attachments/2/)", + ); + expect(getStructuredContent(result)).toMatchInlineSnapshot(` + { + "issueId": "CLOUDFLARE-MCP-41", + "issueUrl": "https://sentry-mcp-evals.sentry.io/issues/CLOUDFLARE-MCP-41", + "note": { + "attachments": [ + { + "id": "1", + "name": "repro.mp4", + "url": "https://sentry.io/api/0/organizations/sentry-mcp-evals/issues/CLOUDFLARE-MCP-41/comment-attachments/1/", + }, + { + "id": "2", + "name": "take2.mov", + "url": "https://sentry.io/api/0/organizations/sentry-mcp-evals/issues/CLOUDFLARE-MCP-41/comment-attachments/2/", + }, + ], + "author": "Jane Developer", + "dateCreated": "2025-04-14T12:34:56.000Z", + "id": "comment-2", + "text": "Screen recording of the crash. + [repro.mp4](/api/0/organizations/sentry-mcp-evals/issues/CLOUDFLARE-MCP-41/comment-attachments/1/) + [take2.mov](/api/0/organizations/sentry-mcp-evals/issues/CLOUDFLARE-MCP-41/comment-attachments/2/)", + "type": "note", + }, + "organizationSlug": "sentry-mcp-evals", + } + `); + }); + + it("refuses files that are not videos before uploading anything", async () => { + let uploaded = false; + mswServer.use( + http.post(ATTACHMENTS_PATH, () => { + uploaded = true; + return HttpResponse.json({}, { status: 201 }); + }), + ); + + await expect( + addIssueNote.handler( + { + organizationSlug: "sentry-mcp-evals", + regionUrl: null, + issueId: "CLOUDFLARE-MCP-41", + text: "Recording attached.", + attachmentPaths: ["/tmp/repro.mp4", "/home/me/.ssh/id_rsa.mp4"], + }, + { + ...context, + readLocalFile: localFiles({ + "/tmp/repro.mp4": { name: "repro.mp4", data: MP4 }, + "/home/me/.ssh/id_rsa.mp4": { + name: "id_rsa.mp4", + data: new TextEncoder().encode("-----BEGIN OPENSSH PRIVATE KEY"), + }, + }), + }, + ), + ).rejects.toThrow("id_rsa.mp4 is not a supported video"); + expect(uploaded).toBe(false); + }); + + it("explains that the hosted server cannot attach local videos", async () => { + await expect( + addIssueNote.handler( + { + organizationSlug: "sentry-mcp-evals", + regionUrl: null, + issueId: "CLOUDFLARE-MCP-41", + text: "Recording attached.", + attachmentPaths: ["/tmp/repro.mp4"], + }, + context, + ), + ).rejects.toThrow(UserInputError); + }); + + it("surfaces Sentry's size limit when an upload is too large", async () => { + mswServer.use( + http.post(ATTACHMENTS_PATH, () => + HttpResponse.json( + { detail: "File is too large. The maximum size is 100 MB." }, + { status: 413 }, + ), + ), + ); + + await expect( + addIssueNote.handler( + { + organizationSlug: "sentry-mcp-evals", + regionUrl: null, + issueId: "CLOUDFLARE-MCP-41", + text: "Recording attached.", + attachmentPaths: ["/tmp/repro.mp4"], + }, + { + ...context, + readLocalFile: localFiles({ + "/tmp/repro.mp4": { name: "repro.mp4", data: MP4 }, + }), + }, + ), + ).rejects.toThrow("File is too large. The maximum size is 100 MB."); + }); }); diff --git a/packages/mcp-core/src/tools/catalog/add-issue-note.ts b/packages/mcp-core/src/tools/catalog/add-issue-note.ts index 726a8fea7c..d9c939caf4 100644 --- a/packages/mcp-core/src/tools/catalog/add-issue-note.ts +++ b/packages/mcp-core/src/tools/catalog/add-issue-note.ts @@ -1,4 +1,12 @@ import { z } from "zod"; +import type { IssueCommentAttachment } from "../../api-client/types"; +import { + appendCommentAttachments, + COMMENT_ATTACHMENT_EXTENSIONS, + commentAttachmentContentType, + parseCommentAttachments, +} from "../../comment-attachments"; +import { UserInputError } from "../../errors"; import type { IssueComment } from "../../api-client/types"; import { apiServiceFromContext } from "../../internal/tool-helpers/api"; import { defineTool } from "../../internal/tool-helpers/define"; @@ -14,7 +22,7 @@ import { ParamRegionUrl, } from "../../schema"; import { setTargetTagsAndAttributes } from "../../telem/scope"; -import type { ServerContext } from "../../types"; +import type { LocalFile, LocalFileReader, ServerContext } from "../../types"; import { formatDate, formatId, readString } from "./support/api-formatting"; import { formatActivityAuthor } from "./support/activity-formatting"; @@ -28,16 +36,62 @@ export const addIssueNoteOutputSchema = z.object({ author: z.string(), dateCreated: z.string().nullable(), text: z.string().nullable(), + attachments: z + .array(z.object({ id: z.string(), name: z.string(), url: z.string() })) + .describe("Videos uploaded with the note and linked from its text."), }), }); -function mapNote(comment: IssueComment) { +/** Most videos one note can carry. */ +const MAX_ATTACHMENTS = 5; + +/** + * Reads each path and checks it is a video before anything is uploaded, so a + * bad path never leaves a half-uploaded note. + */ +async function readVideos( + paths: readonly string[], + readLocalFile: LocalFileReader | undefined, +) { + if (!readLocalFile) { + throw new UserInputError( + "Attaching videos needs the local MCP server (`sentry mcp`), which can read files on this machine. This server cannot.", + ); + } + return await Promise.all( + paths.map(async (path) => { + let file: LocalFile; + try { + file = await readLocalFile(path); + } catch (error) { + throw new UserInputError( + `Could not read ${path}: ${error instanceof Error ? error.message : String(error)}`, + ); + } + const contentType = commentAttachmentContentType(file.name, file.data); + if (!contentType) { + throw new UserInputError( + `${file.name} is not a supported video. Attach ${COMMENT_ATTACHMENT_EXTENSIONS.join(", ")} files.`, + ); + } + return { ...file, contentType }; + }), + ); +} + +function mapNote(comment: IssueComment, absoluteUrl: (path: string) => string) { + const text = comment.data ? readString(comment.data, "text") : null; return { id: formatId(comment.commentId ?? comment.id), type: comment.type ?? "note", author: formatActivityAuthor(comment, "current user"), dateCreated: formatDate(comment.dateCreated), - text: comment.data ? readString(comment.data, "text") : null, + text, + attachments: parseCommentAttachments(text).map((link) => ({ + id: link.id, + name: link.name, + url: absoluteUrl(link.path), + })), }; } @@ -58,7 +112,16 @@ export default defineTool({ .trim() .min(1) .max(4096) - .describe("The comment text, posted as is."), + .describe( + "The comment text, posted as is. Links to attached videos are added after it.", + ), + attachmentPaths: z + .array(z.string().trim().min(1)) + .max(MAX_ATTACHMENTS) + .optional() + .describe( + "Local paths of videos to attach (MP4, MOV, WebM or Ogg), such as a screen recording of the bug. Only works when the MCP server runs on the user's machine (`sentry mcp`). Only attach files the user asked for.", + ), }, annotations: { readOnlyHint: false, @@ -89,17 +152,33 @@ export default defineTool({ projectSlug: context.constraints.projectSlug, }); + const videos = params.attachmentPaths?.length + ? await readVideos(params.attachmentPaths, context.readLocalFile) + : []; + const uploaded: IssueCommentAttachment[] = []; + for (const video of videos) { + uploaded.push( + await apiService.uploadIssueCommentAttachment({ + organizationSlug: parsed.organizationSlug, + issueId: parsed.issueId, + fileName: video.name, + contentType: video.contentType, + data: video.data, + }), + ); + } + const comment = await apiService.createIssueComment({ organizationSlug: parsed.organizationSlug, issueId: parsed.issueId, - text: params.text, + text: appendCommentAttachments(params.text, uploaded), }); return structuredResult({ organizationSlug: parsed.organizationSlug, issueId: parsed.issueId, issueUrl: apiService.getIssueUrl(parsed.organizationSlug, parsed.issueId), - note: mapNote(comment), + note: mapNote(comment, (path) => apiService.getAbsoluteApiUrl(path)), }); }, }); diff --git a/packages/mcp-core/src/tools/catalog/get-issue-activity.test.ts b/packages/mcp-core/src/tools/catalog/get-issue-activity.test.ts index c9ee784ade..eaf0ab3c79 100644 --- a/packages/mcp-core/src/tools/catalog/get-issue-activity.test.ts +++ b/packages/mcp-core/src/tools/catalog/get-issue-activity.test.ts @@ -94,6 +94,33 @@ describe("get_issue_activity", () => { `); }); + it("lists videos attached to comments", async () => { + const path = + "/api/0/organizations/sentry-mcp-evals/issues/CLOUDFLARE-MCP-41/comment-attachments/7/"; + mockActivity({ + id: "100", + type: "note", + user: { name: "Jane Developer" }, + dateCreated: "2025-04-14T12:34:56.000Z", + data: { + text: `Crash on save\n[repro.mp4](${path})\n[docs](https://docs.sentry.io/)`, + }, + }); + + const result = await getIssueActivity.handler(params, context); + + const structuredContent = getStructuredContent(result); + expect(getIssueActivityOutputSchema.parse(structuredContent)).toEqual( + structuredContent, + ); + expect( + (structuredContent as { comments: Array<{ attachments?: unknown }> }) + .comments[0]?.attachments, + ).toEqual([ + { id: "7", name: "repro.mp4", url: `https://sentry.io${path}` }, + ]); + }); + it("returns null comments when comments are not requested", async () => { const result = await getIssueActivity.handler( { ...params, includeComments: false }, diff --git a/packages/mcp-core/src/tools/catalog/get-issue-activity.ts b/packages/mcp-core/src/tools/catalog/get-issue-activity.ts index a353cb9329..5927fc8818 100644 --- a/packages/mcp-core/src/tools/catalog/get-issue-activity.ts +++ b/packages/mcp-core/src/tools/catalog/get-issue-activity.ts @@ -1,5 +1,6 @@ import { z } from "zod"; import type { IssueActivity, IssueComment } from "../../api-client/types"; +import { parseCommentAttachments } from "../../comment-attachments"; import { apiServiceFromContext } from "../../internal/tool-helpers/api"; import { defineTool } from "../../internal/tool-helpers/define"; import { @@ -54,6 +55,18 @@ const issueActivityEntrySchema = z.object({ .nullable() .describe("Set when the comment ID differs from the activity ID."), text: z.string().nullable(), + attachments: z + .array( + z.object({ + id: z.string(), + name: z.string(), + url: z.string().describe("API URL that streams the video."), + }), + ) + .optional() + .describe( + "Videos attached to a comment. Download one with `sentry issue comments --download `.", + ), assignee: z .object({ id: z.string(), @@ -273,6 +286,7 @@ function mapExternalIssue(type: string, data: Record) { function mapActivity( activity: IssueActivity | IssueComment, + absoluteUrl: (path: string) => string, ): IssueActivityEntry { const text = getActivityText(activity); const commentId = activity.commentId; @@ -287,6 +301,14 @@ function mapActivity( : null, text, }; + const attachments = parseCommentAttachments(text); + if (attachments.length > 0) { + entry.attachments = attachments.map((link) => ({ + id: link.id, + name: link.name, + url: absoluteUrl(link.path), + })); + } const data = activity.data; if (text || !isPlainObject(data) || Object.keys(data).length === 0) { @@ -323,7 +345,7 @@ export default defineTool({ skills: ["inspect", "triage"], requiredScopes: ["event:read"], description: - "Get the activity feed and comments of a Sentry issue: who resolved, ignored, assigned, or commented on it, and when.", + "Get the activity feed and comments of a Sentry issue: who resolved, ignored, assigned, or commented on it, and when, plus any videos attached to comments.", inputSchema: { organizationSlug: ParamOrganizationSlug.optional(), regionUrl: ParamRegionUrl.nullable().default(null), @@ -375,13 +397,15 @@ export default defineTool({ : Promise.resolve([]), ]); + const toEntry = (item: IssueActivity | IssueComment) => + mapActivity(item, (path) => apiService.getAbsoluteApiUrl(path)); return structuredResult({ organizationSlug: parsed.organizationSlug, issueId: parsed.issueId, issueUrl: apiService.getIssueUrl(parsed.organizationSlug, parsed.issueId), - activity: activity.slice(0, params.limit).map(mapActivity), + activity: activity.slice(0, params.limit).map(toEntry), comments: params.includeComments - ? comments.slice(0, params.limit).map(mapActivity) + ? comments.slice(0, params.limit).map(toEntry) : null, }); }, diff --git a/packages/mcp-core/src/types.ts b/packages/mcp-core/src/types.ts index 885a6dc308..3b4e32971c 100644 --- a/packages/mcp-core/src/types.ts +++ b/packages/mcp-core/src/types.ts @@ -77,6 +77,20 @@ export type CredentialRouter = { list(): Promise; }; +/** A file read from the machine the MCP server runs on. */ +export type LocalFile = { + /** File name without its directory, e.g. `repro.mp4`. */ + name: string; + data: Uint8Array; +}; + +/** + * Reads a local file the user named in a tool call. Only hosts that run on the + * user's machine (the CLI's stdio server) provide one; the hosted server has + * no local files. + */ +export type LocalFileReader = (path: string) => Promise; + export type ServerContext = { sentryHost?: string; sentryProtocol?: SentryProtocol; @@ -119,6 +133,11 @@ export type ServerContext = { * default credential's token. */ credentialRouter?: CredentialRouter; + /** + * Reads local files for tools that upload them, such as videos attached to + * issue comments. Unset on hosts without local file access. + */ + readLocalFile?: LocalFileReader; /** * Aborts when the MCP client cancels the tool call. Set only on a per-call * copy of the context (see `withCallSignal`), never on the session context. From 315752f2c74bb6bf2099f7fbbefea9b4d7731a3a Mon Sep 17 00:00:00 2001 From: Nithin RG <2275985+nithinrgaddam@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:17:40 -0600 Subject: [PATCH 2/5] feat(cli): Add issue comment and issue comments commands - sentry issue comment [--attach