From db0bbabb400f682c85afcbf1012adcec97ad2900 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Wed, 18 Nov 2026 09:18:04 +0000 Subject: [PATCH] fix(tools): Find a project by numeric ID in find_projects Sentry's projects endpoint matches free-text queries against name and slug only, so find_projects returned nothing when an agent passed a numeric project ID. Send a numeric query as `id:` first and fall back to the name/slug search when no project has that ID. --- packages/mcp-core/src/toolDefinitions.json | 2 +- .../src/tools/catalog/find-projects.test.ts | 70 +++++++++++++++++++ .../src/tools/catalog/find-projects.ts | 28 ++++++-- 3 files changed, 92 insertions(+), 8 deletions(-) diff --git a/packages/mcp-core/src/toolDefinitions.json b/packages/mcp-core/src/toolDefinitions.json index 0965114a6..07b13f243 100644 --- a/packages/mcp-core/src/toolDefinitions.json +++ b/packages/mcp-core/src/toolDefinitions.json @@ -2985,7 +2985,7 @@ "anyOf": [ { "type": "string", - "description": "Filter by name or slug." + "description": "Filter by name or slug, or pass a numeric project ID." }, { "type": "null" diff --git a/packages/mcp-core/src/tools/catalog/find-projects.test.ts b/packages/mcp-core/src/tools/catalog/find-projects.test.ts index 95d38f5e5..4f186febb 100644 --- a/packages/mcp-core/src/tools/catalog/find-projects.test.ts +++ b/packages/mcp-core/src/tools/catalog/find-projects.test.ts @@ -155,6 +155,76 @@ describe("find_projects", () => { }); }); + describe("numeric project ID query", () => { + // Mirrors the Sentry endpoint: free text matches only name or slug, and + // `id:` matches the numeric ID (organization_projects.py). + const projects = [ + { id: "4509062593708032", slug: "mcp-server", name: "mcp-server" }, + { id: "7", slug: "web-2024", name: "Web 2024" }, + ]; + const sentQueries: string[] = []; + + function useProjectsEndpoint() { + sentQueries.length = 0; + mswServer.use( + http.get( + "https://sentry.io/api/0/organizations/sentry-mcp-evals/projects/", + ({ request }) => { + const query = new URL(request.url).searchParams.get("query") ?? ""; + sentQueries.push(query); + const id = query.match(/^id:(\d+)$/)?.[1]; + return HttpResponse.json( + projects.filter((project) => + id + ? project.id === id + : project.name.includes(query) || + project.slug.includes(query), + ), + ); + }, + ), + ); + } + + function findByQuery(query: string) { + return findProjects.handler( + { + organizationSlug: "sentry-mcp-evals", + regionUrl: null, + query, + cursor: null, + }, + getServerContext(), + ); + } + + it("finds the project with that ID", async () => { + useProjectsEndpoint(); + + const result = await findByQuery("4509062593708032"); + + expect(getStructuredContent(result)).toEqual({ + projects: [{ slug: "mcp-server" }], + hasMore: false, + nextCursor: null, + }); + expect(sentQueries).toEqual(["id:4509062593708032"]); + }); + + it("falls back to name and slug search when no project has that ID", async () => { + useProjectsEndpoint(); + + const result = await findByQuery("2024"); + + expect(getStructuredContent(result)).toEqual({ + projects: [{ slug: "web-2024" }], + hasMore: false, + nextCursor: null, + }); + expect(sentQueries).toEqual(["id:2024", "2024"]); + }); + }); + it("preserves mixed-case organization slug in the API path", async () => { const context = getServerContext(); diff --git a/packages/mcp-core/src/tools/catalog/find-projects.ts b/packages/mcp-core/src/tools/catalog/find-projects.ts index ce98fbc95..6a03e4a72 100644 --- a/packages/mcp-core/src/tools/catalog/find-projects.ts +++ b/packages/mcp-core/src/tools/catalog/find-projects.ts @@ -34,7 +34,11 @@ export default defineTool({ inputSchema: { organizationSlug: ParamOrganizationSlug, regionUrl: ParamRegionUrl.nullable().default(null), - query: ParamSearchQuery.nullable().default(null), + query: ParamSearchQuery.describe( + "Filter by name or slug, or pass a numeric project ID.", + ) + .nullable() + .default(null), cursor: ParamCursor.nullable().default(null), }, annotations: { @@ -57,14 +61,24 @@ export default defineTool({ setTargetTagsAndAttributes({ organizationSlug }); - const { projects, nextCursor } = await apiService.listProjects( - organizationSlug, - { - query: params.query ?? undefined, + const listProjects = (query: string | undefined) => + apiService.listProjects(organizationSlug, { + query, limit: RESULT_LIMIT, cursor: params.cursor ?? undefined, - }, - ); + }); + + // Sentry matches free text against name and slug only, so a bare + // numeric ID finds nothing unless it is sent as `id:`. + const query = params.query ?? undefined; + let result: Awaited> | undefined; + if (query && /^\d+$/.test(query) && !params.cursor) { + result = await listProjects(`id:${query}`); + } + if (!result?.projects.length) { + result = await listProjects(query); + } + const { projects, nextCursor } = result; return structuredResult({ projects: projects.map((project) => ({ slug: project.slug })),