Skip to content

Open source alternative to Semrush and Ahrefs

Return report HTML to Claude Code when get_report includes it (EVE-221) (#631)

Ben Senescuffdcdf99ed05
diff --git a/src/server/mcp/tools/report-tools.test.ts b/src/server/mcp/tools/report-tools.test.ts
index 6c4f3f46..4be2f7d7 100644
--- a/src/server/mcp/tools/report-tools.test.ts
+++ b/src/server/mcp/tools/report-tools.test.ts
@@ -181,7 +181,10 @@ describe("list_reports", () => {
describe("get_report", () => {
it("returns the summary without the document, and the document on request", async () => {
mocks.getReport.mockResolvedValue(storedReport());
- mocks.getReportWithHtml.mockResolvedValue({ ...storedReport(), html });
+ mocks.getReportWithHtml.mockResolvedValue({
+ ...storedReport({ sizeBytes: new TextEncoder().encode(html).length }),
+ html,
+ });
const metadataOnly = await getReportTool.handler(
{ projectId, reportId },
@@ -190,12 +193,19 @@ describe("get_report", () => {
expect(mocks.getReportWithHtml).not.toHaveBeenCalled();
expect(textContent(metadataOnly)).not.toContain(html);
expect(metadataOnly.structuredContent.report.shareUrl).toBeNull();
+ expect(metadataOnly.structuredContent.report).not.toHaveProperty("html");
const withHtml = await getReportTool.handler(
{ projectId, reportId, includeHtml: true },
toolContext,
);
expect(textContent(withHtml)).toContain(html);
+ // Clients that render structuredContent never see the text block, so the
+ // document has to reach them here too, whole: the tool tells agents to
+ // compare what they received against htmlBytes.
+ const { html: returned, htmlBytes } = withHtml.structuredContent.report;
+ expect(returned).toBe(html);
+ expect(new TextEncoder().encode(returned).length).toBe(htmlBytes);
});
});
diff --git a/src/server/mcp/tools/report-tools.ts b/src/server/mcp/tools/report-tools.ts
index 556816a6..e15e3e02 100644
--- a/src/server/mcp/tools/report-tools.ts
+++ b/src/server/mcp/tools/report-tools.ts
@@ -297,7 +297,12 @@ const getInputSchema = {
} as const;
const getOutputSchema = z.looseObject({
- report: z.looseObject({ shareUrl: z.string().nullable() }),
+ // `html` is declared so clients see it in the tool's output schema; it is
+ // present only when includeHtml was set.
+ report: z.looseObject({
+ shareUrl: z.string().nullable(),
+ html: z.string().optional(),
+ }),
...optionalMetaOutputSchema,
});
@@ -334,9 +339,11 @@ export const getReportTool = {
const shareUrl = await reportShareUrl(report, context.baseUrl);
return mcpResponse({
- // The document goes in `text` only. It is what the agent actually
- // reads, and repeating it in structuredContent would double a payload
- // that is already the largest thing these tools return.
+ // The document rides in both blocks because clients read one or the
+ // other: Claude Code and other structuredContent-aware clients render
+ // structuredContent and never see the text block, while text-only
+ // clients see nothing else. Sending it once would silently drop the
+ // report for half of them.
text: [
`${report.title} (${report.id})`,
metaLine(report),
@@ -359,6 +366,7 @@ export const getReportTool = {
htmlBytes: report.sizeBytes,
url,
shareUrl,
+ ...(html ? { html } : {}),
},
},
});