Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,32 @@ describe("resolveResourceParams", () => {
"https://sentry.io/api/0/projects/my-org/my-project/events/abc123/",
"This is a Sentry API URL",
],
[
"https://my-org.sentry.io/explore/errors/homepage/?dataset=errors&field=title&sort=-timestamp&referrer=weekly_report",
"Use `search_errors` to query error events",
],
[
"https://my-org.sentry.io/explore/discover/results/?dataset=errors",
"Use `search_errors` to query error events",
],
[
"https://sentry.io/organizations/my-org/discover/homepage/",
"Use `search_errors` to query error events",
],
])("explains unsupported URL %s", (url, message) => {
expect(() => resolveResourceParams({ url })).toThrow(message);
});

it("lists the page filters of an unsupported list URL", () => {
expect(() =>
resolveResourceParams({
url: "https://my-org.sentry.io/explore/errors/homepage/?dataset=errors&project=1&project=2&environment=prod&statsPeriod=7d&query=level%3Aerror&referrer=weekly_report",
}),
).toThrow(
"The URL is scoped to: project=1, project=2, environment=prod, statsPeriod=7d, query=level:error.",
);
});

it("throws for unknown URL path (settings)", () => {
expect(() =>
resolveResourceParams({
Expand Down
30 changes: 27 additions & 3 deletions packages/mcp-core/src/tools/catalog/get-sentry-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,42 @@ const UNSUPPORTED_URL_GUIDANCE: Array<[RegExp, string]> = [
/(^|\/)(explore\/traces|performance|traces)\/?$/,
"This is a trace search URL. Use `search_traces` to query traces, or pass a URL for one trace.",
],
[
/(^|\/)(explore\/(errors|errors-v2|discover)|discover)(\/(homepage|results|queries))?\/?$/,
"This is an errors search URL. Use `search_errors` to query error events, or pass a URL for one issue or event.",
],
];

// Page filters that a search tool needs to reproduce a list URL's scope.
const URL_SCOPE_PARAMS = [
"project",
"environment",
"statsPeriod",
"start",
"end",
"query",
];

function getUnsupportedUrlGuidance(url: string): string | undefined {
let pathname: string;
let parsedUrl: URL;
try {
pathname = new URL(url).pathname.replace(/^\/+|\/+$/g, "");
parsedUrl = new URL(url);
} catch {
return undefined;
}
return UNSUPPORTED_URL_GUIDANCE.find(([pattern]) =>
const pathname = parsedUrl.pathname.replace(/^\/+|\/+$/g, "");
const guidance = UNSUPPORTED_URL_GUIDANCE.find(([pattern]) =>
pattern.test(pathname),
)?.[1];
if (!guidance) {
return undefined;
}
const scope = URL_SCOPE_PARAMS.flatMap((name) =>
parsedUrl.searchParams.getAll(name).map((value) => `${name}=${value}`),
);
return scope.length > 0
? `${guidance} The URL is scoped to: ${scope.join(", ")}.`
: guidance;
}

export function resolveResourceParams(params: {
Expand Down
Loading