sentry-mcp icon indicating copy to clipboard operation
sentry-mcp copied to clipboard

feat: adopt @sentry/api SDK for 38 of 40 API call sites

Open MathurAditya724 opened this issue 7 months ago • 4 comments

Adds @sentry/api@^0.175.0 and migrates 38 of 40 API methods in SentryApiService from raw fetch (via requestJSON) to the typed SDK functions. The remaining 2 methods use internal/undocumented endpoints with no SDK equivalent.

What changed

Two helper methods added to SentryApiService:

  • getSdkConfig(opts?) — builds baseUrl + auth headers (including X-Sentry-MCP-Client-* identity headers) per call, supporting multi-region host overrides
  • unwrapSdkResult(result, context) — converts SDK discriminated-union results to the existing MCP error hierarchy (ApiError, ApiNotFoundError, ApiServerError, etc.), using the API detail text as the user-facing message when available

17 direct replacements (no casts)

listOrganizations, getOrganization, listTeams, createTeam, listProjects, getProject, createProject, updateProject, addTeamToProject, createClientKey, listClientKeys, listReleases, listIssues, getEventForIssue, getLatestEventForIssue, listEventsForIssue, startAutofix, getAutofixState

11 replacements with type casts

searchReplays, getIssue, getIssueTagValues, getIssueExternalLinks, getReplayDetails, listReplayIdsForIssue, getReplayRecordingSegments, updateIssue, searchErrors, searchSpans, searchEvents

10 new replacements (from SDK v0.171.0+)

  • listTags → listAnOrganization_sTags (with cast for useCache param)
  • listTraceItemAttributes → listTraceItemAttributes (replaces fetchTraceItemAttributesByType private helper)
  • listEventAttachments → listAnEvent_sAttachments
  • getEventAttachment — metadata via listEventAttachments; binary download stays on raw request()
  • getTraceMeta → retrieveTraceMetadata
  • getTrace → retrieveATrace (with cast for limit/project params)
  • getFlamegraph → retrieveAFlamegraphForAnOrganization
  • getTransactionProfile → retrieveAProfile
  • getProfileChunk → retrieveProfileChunksForAnOrganization
  • listReleases (project-level) → listAProject_sReleases
  • listIssues (project-level) → listAProject_sIssues (with cast for extra query params)

2 methods still on requestJSON

getAuthenticatedUser, regions endpoint — no SDK equivalent exists for these.

Schema improvements

  • TagSchema / TagListSchema rebased on zTagKeyDetailsDict from @sentry/api/zod
  • EventsResponseSchema, ExternalIssueListSchema, AutofixRunSchema use SDK-generated Zod schemas

Preserved behavior

  • Zod validation: All responses still pass through existing Zod schemas after SDK unwrapping
  • Error handling: unwrapSdkResult maps HTTP errors to the existing ApiError/ApiNotFoundError/ApiServerError types, using API detail text as the message
  • Multi-region: getSdkConfig builds per-call base URLs from opts.host overrides
  • Client identity: X-Sentry-MCP-Client-Id/Name/Family headers forwarded through SDK calls

Testing

  • TypeScript: clean (no new errors in client.ts)
  • Full test suite: 1033 tests passed, 4 skipped
  • Lint: clean

Dependencies

  • @sentry/api@^0.175.0 — latest version, includes SDK wrappers for organization tags, trace, profiling, project-level issues/releases, and event attachment endpoints

MathurAditya724 avatar May 05 '26 00:05 MathurAditya724

fix-ci: starting attempt 1/3

MathurAditya724 avatar May 05 '26 02:05 MathurAditya724

fix-ci: result — fixed 7 test failures across 5 test files.

Root causes:

  1. Agent provider conflict: ANTHROPIC_API_KEY env var leaked into tests alongside OPENAI_API_KEY, causing hasAgentProvider() to return false and routing tests through the direct (non-agent) code path. Fixed by clearing the env var in beforeEach/afterEach.
  2. MSW handler assertions: SDK sends limit not per_page for issues, and doesn't expose sort/per_page as query params on issue events. Updated handler assertions.
  3. Path traversal tests: SDK URL-encodes path params, neutralizing traversal attacks. Updated tests to verify the encoding works correctly instead of expecting errors.
  4. addTeamToProject mock: SDK sends POST with no body; removed request.json() call that threw SyntaxError.

All 69 tests in the 5 affected files pass. Client unit tests (72/72) still pass.

MathurAditya724 avatar May 05 '26 02:05 MathurAditya724

One f/u - i was hoping we'd be able to remove a bunch of typed schemas. Im not sure what we need to do to solve it, but it'd be great if @sentry/api had native zod structs that we could use that were either autogenerated or at minimum, we maintain them in one place vs here and cli etc

dcramer avatar May 05 '26 22:05 dcramer

Bumped @sentry/api to ^0.141.0 — this picks up the new @sentry/api/zod subpath export with auto-generated Zod v3 schemas from the OpenAPI spec (sentry-api-schema#70). These schemas can eventually replace the hand-written ones in packages/mcp-core/src/api-client/schema.ts.

MathurAditya724 avatar May 06 '26 00:05 MathurAditya724