Skip to content

Commit 0ca0e21

Browse files
wmakclaudecursoragent
committed
feat(api): Add MCP referrer to search_events API calls
Set referrer=api.mcp.search-events on Discover and EAP /events/ requests so Sentry can attribute search_events traffic. The validate endpoint does not accept referrer, so it is omitted there. Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 450cd4b commit 0ca0e21

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

packages/mcp-core/src/api-client/client.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ describe("API query builders", () => {
10581058
});
10591059

10601060
expect(params.toString()).toMatchInlineSnapshot(
1061-
`"per_page=50&query=level%3Aerror&dataset=errors&statsPeriod=24h&project=backend&sort=-count&field=title&field=project&field=count%28%29"`,
1061+
`"per_page=50&query=level%3Aerror&dataset=errors&statsPeriod=24h&project=backend&sort=-count&field=title&field=project&field=count%28%29&referrer=api.mcp.search-events"`,
10621062
);
10631063
});
10641064

@@ -1143,7 +1143,7 @@ describe("API query builders", () => {
11431143
});
11441144

11451145
expect(params.toString()).toMatchInlineSnapshot(
1146-
`"per_page=20&query=span.op%3Adb&dataset=spans&statsPeriod=1h&project=frontend&sampling=NORMAL&sort=-span.duration&field=span.op&field=span.description&field=span.duration"`,
1146+
`"per_page=20&query=span.op%3Adb&dataset=spans&statsPeriod=1h&project=frontend&sampling=NORMAL&sort=-span.duration&field=span.op&field=span.description&field=span.duration&referrer=api.mcp.search-events"`,
11471147
);
11481148
});
11491149

@@ -1160,7 +1160,7 @@ describe("API query builders", () => {
11601160
});
11611161

11621162
expect(params.toString()).toMatchInlineSnapshot(
1163-
`"per_page=30&query=severity%3Aerror&dataset=logs&sort=-timestamp&field=timestamp&field=message&field=severity"`,
1163+
`"per_page=30&query=severity%3Aerror&dataset=logs&sort=-timestamp&field=timestamp&field=message&field=severity&referrer=api.mcp.search-events"`,
11641164
);
11651165

11661166
// Verify sampling is not added for logs
@@ -1217,6 +1217,10 @@ describe("API query builders", () => {
12171217
expect.stringContaining("sort=-count"),
12181218
expect.any(Object),
12191219
);
1220+
expect(globalThis.fetch).toHaveBeenCalledWith(
1221+
expect.stringContaining("referrer=api.mcp.search-events"),
1222+
expect.any(Object),
1223+
);
12201224
});
12211225

12221226
it("should route spans dataset to EAP API builder with sampling", async () => {
@@ -1251,6 +1255,10 @@ describe("API query builders", () => {
12511255
expect.stringContaining("sampling=NORMAL"),
12521256
expect.any(Object),
12531257
);
1258+
expect(globalThis.fetch).toHaveBeenCalledWith(
1259+
expect.stringContaining("referrer=api.mcp.search-events"),
1260+
expect.any(Object),
1261+
);
12541262
});
12551263

12561264
it("should normalize metrics dataset to tracemetrics for Discover queries", async () => {

packages/mcp-core/src/api-client/client.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ import type {
131131
// logger isnt exposed (or rather, it is, but its not the right logger)
132132
// import { logger } from "@sentry/node";
133133

134+
const SENTRY_MCP_SEARCH_EVENTS_REFERRER = "api.mcp.search-events";
135+
134136
/**
135137
* Mapping of common network error codes to user-friendly messages.
136138
* These help users understand and resolve connection issues.
@@ -3735,6 +3737,8 @@ export class SentryApiService {
37353737
queryParams.append("field", field);
37363738
}
37373739

3740+
queryParams.set("referrer", SENTRY_MCP_SEARCH_EVENTS_REFERRER);
3741+
37383742
return queryParams;
37393743
}
37403744

@@ -3802,6 +3806,8 @@ export class SentryApiService {
38023806
queryParams.append("field", field);
38033807
}
38043808

3809+
queryParams.set("referrer", SENTRY_MCP_SEARCH_EVENTS_REFERRER);
3810+
38053811
return queryParams;
38063812
}
38073813

packages/mcp-core/src/api-client/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ export const EventsResponseSchema = z.object({
10831083
.passthrough(),
10841084
});
10851085

1086-
// https://us.sentry.io/api/0/organizations/sentry/events/?dataset=errors&field=issue&field=title&field=project&field=timestamp&field=trace&per_page=5&query=event.type%3Aerror&referrer=sentry-mcp&sort=-timestamp&statsPeriod=1w
1086+
// https://us.sentry.io/api/0/organizations/sentry/events/?dataset=errors&field=issue&field=title&field=project&field=timestamp&field=trace&per_page=5&query=event.type%3Aerror&referrer=api.mcp.search-events&sort=-timestamp&statsPeriod=1w
10871087
export const ErrorsSearchResponseSchema = EventsResponseSchema.extend({
10881088
data: z.array(
10891089
z.object({

0 commit comments

Comments
 (0)