Skip to content

feat(mcp): Build the scope rules from Sentry's x-sentry-project-scope - #2552

Open
sentry-junior[bot] wants to merge 7 commits into
fix/mcp-commands-central-scopefrom
feat/scope-rules-from-catalog
Open

sentry-junior[bot] wants to merge 7 commits into
fix/mcp-commands-central-scopefrom
feat/scope-rules-from-catalog

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Refs #2237. Stacked on #2549.

This replaces most of the hand-written endpoint table behind limitToTarget with rules Sentry declares in its own schema.

It bumps @sentry/api to 3.5.0, the first release that carries Sentry's x-sentry-project-scope extension (getsentry/sentry#132722) as ApiOperation.projectScope.

What changes

target-scope-rules.ts reads projectScope from @sentry/api 3.5.0's operations catalog, which the CLI already bundles, so no generated copy of the declarations is needed. It builds a rule for each operation whose scope isn't path:

  • org-metadata: an org read that the project scope may send.
  • filter: an org endpoint that gets filtered to the scope's project.
  • response:<pointer>: a lookup by an ID alone. The response is checked against the numeric project IDs at that JSON pointer, where * matches each array element. A write to the same path, or any request below it, looks the resource up first.

What stays hand-written, because the schema can't say it:

  • project paths and account reads;
  • getsentry's customers/:org/;
  • endpoints the schema doesn't publish: short IDs, event IDs, share links, trace/ and trace-meta/;
  • trace-logs, which ignores project, so it looks the trace up first;
  • release writes, which stay refused because a release spans projects.

Behavior changes

  • Issue, replay, monitor, detector and release lookups now compare numeric project IDs instead of slugs, since slugs are only unique within an org. The scope project's ID is looked up once per process.
  • Sentry now derives filter from an endpoint's project parameter, so a project scope sends these with ?project=<id> instead of refusing them:
    • organizations/:org/stats_v2/
    • organizations/:org/sessions/
    • organizations/:org/agents/conversations/{conversation_id}/

via @BYK.

--

View Session [Sentry]

@sentry-junior
sentry-junior Bot added this pull request to stack #1926 November 27, 2026 00:23
@vercel

vercel Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
cli Ready Ready Preview Nov 28, 2026 12:00am UTC
sentry-local Ready Ready Preview Nov 28, 2026 12:00am UTC

Request Review

@sentry-junior
sentry-junior Bot requested a review from BYK November 27, 2026 00:23
@vercel
vercel Bot temporarily deployed to Preview – sentry-local November 27, 2026 00:31 Inactive
@vercel
vercel Bot temporarily deployed to Preview – sentry-local November 27, 2026 00:37 Inactive
@vercel
vercel Bot temporarily deployed to Preview – sentry-local November 27, 2026 00:46 Inactive
BYK added a commit to getsentry/sentry that referenced this pull request Nov 27, 2026
…132723)

`@sentry/api/operations` now carries Sentry's `x-sentry-project-scope`
OpenAPI extension, added in #132722, as
`ApiOperation.projectScope`. Clients that limit a caller to some
projects can read it instead of keeping their own endpoint table.
getsentry/toolkit#2552 consumes it for the Sentry CLI and MCP project
scope.

```ts
export type ApiProjectScope = "path" | "filter" | "org-metadata" | `response:${string}`;

// on ApiOperation
readonly projectScope?: ApiProjectScope;
```

The values:

- `path`: the path names the project.
- `filter`: an organization endpoint that takes the `project` query
parameter (numeric project IDs) and returns or changes only those
projects' data.
- `org-metadata`: a read-only organization endpoint whose response holds
no project's telemetry. `GET` only.
- `response:<pointer>`: a `GET` that looks something up by ID alone. The
numeric ID of its project is at `<pointer>` in the response body.
`<pointer>` is an RFC 6901 JSON pointer (`~0`/`~1` escapes) in which a
`*` segment matches every array element, such as
`response:/projects/*/id`.

The field is left out when an operation has no extension, which means
the operation is not known to respect a project limit. `ApiProjectScope`
is exported from `@sentry/api/operations` next to `ApiOperation`. Like
the `x-sentry-routing` check, the build throws on a non-string value, on
an unknown value, on a malformed pointer, and on `org-metadata` or
`response:` on a method other than `GET`. These are the same rules that
#132722's schema build enforces.

The released catalog has no `projectScope` values until
#132722 merges and the schema is rebuilt.

Refs getsentry/toolkit#2237

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3Aee8e94869cd8853bb9ac177d49cf5de5 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3Aee8e94869cd8853bb9ac177d49cf5de5)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3Aee8e94869cd8853bb9ac177d49cf5de5/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <126780+BYK@users.noreply.github.com>
@sentry-junior
sentry-junior Bot force-pushed the feat/scope-rules-from-catalog branch from 84b0a46 to 7dd2a00 Compare November 27, 2026 11:56
@vercel
vercel Bot temporarily deployed to Preview – sentry-local November 27, 2026 11:56 Inactive
@sentry-junior
sentry-junior Bot force-pushed the feat/scope-rules-from-catalog branch from 7dd2a00 to 3dc3e75 Compare November 27, 2026 12:08
@vercel
vercel Bot temporarily deployed to Preview – sentry-local November 27, 2026 12:08 Inactive
@sentry-junior
sentry-junior Bot removed this pull request from stack #1926 November 27, 2026 12:09
sentry-junior Bot and others added 3 commits November 27, 2026 12:13
The scope rules of `limitToTarget` were a hand-written endpoint table.
Sentry now declares them with the `x-sentry-project-scope` OpenAPI
extension, which `@sentry/api` carries as `ApiOperation.projectScope`.
`script/generate-api-schema.ts` extracts them at build time into
`src/generated/project-scope-operations.json`, as it does the idempotent
writes, and `target-scope-rules.ts` builds its rules from them:

- `org-metadata`: an organization read the project scope may send.
- `filter`: an organization endpoint filtered to the scope's project.
- `response:<pointer>`: a lookup by an ID alone, checked by the project
  IDs at the pointer of its response; requests under its path look it
  up first.

Hand rules remain only where the schema can't say it: project paths,
account reads, getsentry's `customers/`, endpoints the schema doesn't
publish (short IDs, event IDs, share links, traces), `trace-meta` and
`trace-logs` (they ignore `project`), and release writes (a release
spans projects).

This needs a `@sentry/api` release with `projectScope`. Until then the
generated list is empty and a project scope refuses the endpoints it
would cover. The scope tests run on a fixture of the declarations, and
`target-scope-catalog.test.ts` checks the fixture against the catalog
once it has them.

Refs #2237

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
The installed `@sentry/api` (1.74) has no `projectScope`, so the
generated catalog list is empty and a project scope refused every
endpoint the catalog covers, `chunk-upload/` included. A scoped
`sourcemap upload` was refused before its token check.

Until the `@sentry/api` 3.x bump, the rules come from
`interim-project-scope-operations.json`, a copy of what
getsentry/sentry#132722 declares. It was the tests' fixture, so the
tests now run on the same rules the CLI enforces. Once the catalog has
any declarations it wins, and a guard test checks that every interim
entry is in it. The copy and `projectScopeDeclarations` are marked for
deletion with the bump.

Refs #2237

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
On `main`, `@sentry/api`'s operation catalog gives paths relative to
`/api/0`, and the CLI prefixes `API_BASE_PATH` itself. The scope rules
and the interim declarations match full request paths, so the extracted
`projectScope` operations get the same prefix.

Refs #2237

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
@sentry-junior
sentry-junior Bot force-pushed the feat/scope-rules-from-catalog branch from 3dc3e75 to 5709ba8 Compare November 27, 2026 12:14
@vercel
vercel Bot temporarily deployed to Preview – sentry-local November 27, 2026 12:14 Inactive
The interim declarations as a list of objects put the @sentry/mcp-server
bundle 906 bytes over its 3.75 MB budget. Keying them by method, then by
path relative to /api/0, cuts 1.5 KB; the generated catalog file uses the
same shape.
Comment thread packages/cli/src/global.d.ts Resolved
… missing

target-scope-rules.ts imports it, but ensure-generated and the schema
step's outputs listed only the other two schema files, so a checkout
missing just this one skipped generation and failed on the import.
@BYK
BYK marked this pull request as ready for review November 27, 2026 23:27
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 27, 2026
@BYK
BYK enabled stacked auto-merge November 27, 2026 23:28
3.5.0 carries Sentry's x-sentry-project-scope declarations
(getsentry/sentry#132722) as each operation's projectScope.
@sentry/api 3.5.0 carries Sentry's x-sentry-project-scope declarations
on each operation, and the CLI already bundles that catalog. Build the
rules from it directly, and delete the interim copy of the declarations
and the generated project-scope-operations.json.

Sentry now derives `filter` for stats_v2, sessions and AI conversation
details from their `project` parameter, so a project scope filters them
instead of refusing them.
@github-actions

Copy link
Copy Markdown
Contributor

📊 MCP Server Token Cost Report

Summary

  • Total Tokens: 68772 tokens
  • Tool Count: 77 tools
  • Average: 893 tokens/tool

Per-Tool Breakdown

Tool Tokens % of Total
get_issue_details 3967 5.8%
get_alert_rule 2245 3.3%
find_releases 1805 2.6%
get_issue_activity 1759 2.6%
update_alert_rule 1653 2.4%
get_replay_details 1602 2.3%
create_alert_rule 1506 2.2%
get_monitor_details 1501 2.2%
update_metric_monitor 1479 2.2%
create_metric_monitor 1458 2.1%
get_event_stacktrace 1381 2%
update_issue 1380 2%
render_sentry_chart 1362 2%
get_snapshot 1308 1.9%
get_profile 1291 1.9%
get_agent_conversation_details 1278 1.9%
find_monitors 1261 1.8%
search_logs 1248 1.8%
search_traces 1246 1.8%
get_alert_options 1236 1.8%
search_profiles 1228 1.8%
search_metrics 1216 1.8%
search_errors 1214 1.8%
get_profile_details 1200 1.7%
find_related_issues 1171 1.7%
get_uptime_monitor_details 1163 1.7%
create_monitor 1135 1.7%
onboarding_status_update 1109 1.6%
search_issue_events 1103 1.6%
search_replays 1095 1.6%
render_issue_chart 1061 1.5%
get_release_details 1056 1.5%
get_dashboard_details 1052 1.5%
search_attributes 1012 1.5%
search_issues 977 1.4%
search_docs 973 1.4%
find_alert_rules 908 1.3%
update_uptime_monitor 873 1.3%
create_uptime_monitor 871 1.3%
search_agent_conversations 830 1.2%
find_dropped_events 819 1.2%
get_metric_monitor_details 797 1.2%
create_project 793 1.2%
update_dsn 765 1.1%
get_issue_user_reports 630 0.9%
get_latest_base_snapshot 623 0.9%
find_dashboards 553 0.8%
link_issue 545 0.8%
find_uptime_monitors 537 0.8%
get_issue_breadcrumbs 492 0.7%
unlink_issue 476 0.7%
find_metric_monitors 471 0.7%
get_issue_tag_values 469 0.7%
update_project 441 0.6%
get_sentry_resource 429 0.6%
submit_feedback_to_sentry_team 426 0.6%
add_issue_note 404 0.6%
delete_monitor_environment 363 0.5%
find_teams 351 0.5%
find_projects 346 0.5%
delete_alert_rule 345 0.5%
delete_uptime_monitor 333 0.5%
delete_metric_monitor 331 0.5%
create_dsn 326 0.5%
add_team_to_project 324 0.5%
remove_team_from_project 320 0.5%
find_dsns 319 0.5%
find_organizations 311 0.5%
get_snapshot_image 302 0.4%
analyze_issue_with_seer 300 0.4%
get_event_attachment 299 0.4%
get_trace_details 282 0.4%
create_team 266 0.4%
get_span_details 263 0.4%
whoami 261 0.4%
get_sentry_mcp_info 142 0.2%
get_doc 102 0.1%

Note: This measures the static overhead of tool definitions sent to LLM clients with every request. Lower is better.

MCP Apps views

Each ui:// view as a host receives it from resources/read.

View MIME type Raw Gzip Brotli Budget % of budget
ui://sentry/chart/v5.html text/html;profile=mcp-app 19,234 B 7,915 B 7,020 B 24,576 B 78.3%

The budget applies to raw bytes, and the view's generator fails over it.

This branch was successfully deployed

2 active deployments
Preview – sentry-local — be03a600 Deployed Nov 28, 2026 by vercel[bot]
Preview – cli — be03a600 Deployed Nov 28, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant