Skip to content

Commit 1c80bb7

Browse files
feat(mcp-cloudflare): Share allowlisted cache entries across isolates through R2
Add the R2 tier of the hosted response cache behind MCP_RESPONSE_CACHE=tiered. TieredStore reads memory, then R2, for organization event details (without averageColumn) and project trace-item details (without debug). It writes to R2 only when the response carries Cache-Control: private with a positive max-age, and expires objects at the lesser of max-age and 5 minutes, checked on read. Writes run in waitUntil, and a 1-day bucket lifecycle rule collects garbage. The shared cache policy in @sentry/api/client keeps only Link in entry headers, so R2 eligibility reads Cache-Control from the response headers in the entry's serialized http-cache-semantics policy. Authorization values kept for Vary matching are stored as SHA-256 hashes, never as tokens. Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
1 parent 00f28ad commit 1c80bb7

11 files changed

Lines changed: 716 additions & 28 deletions

File tree

‎docs/cloudflare/architecture.md‎

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ const result = streamText({
145145
- **Pages**: Static asset hosting for React app
146146
- **KV Namespace**: OAuth token storage
147147
- **AI Binding**: Access to Cloudflare AI models (AutoRAG for docs search)
148-
- **R2**: File storage (future)
148+
- **R2**: The response cache's tier for allowlisted detail endpoints
149+
(`MCP_RESPONSE_CACHE_BUCKET`, see
150+
[Sentry API Response Cache](#sentry-api-response-cache))
149151

150152
### Environment Variables
151153

@@ -172,11 +174,11 @@ The MCP handler can cache Sentry API GET responses through the shared
172174
response cache in `@sentry/api/client`. It is
173175
behind `MCP_RESPONSE_CACHE`:
174176

175-
| Value | Behavior |
176-
| ------------------- | -------------------------------------------------------------------------- |
177-
| unset, `off`, other | No caching. The default in every deployed environment. |
178-
| `memory` | Caches every tier in the isolate's memory. |
179-
| `tiered` | Memory only for now; the R2 tier for allowlisted detail endpoints follows. |
177+
| Value | Behavior |
178+
| ------------------- | ---------------------------------------------------------------------------- |
179+
| unset, `off`, other | No caching. The default in every deployed environment. |
180+
| `memory` | Caches every tier in the isolate's memory. |
181+
| `tiered` | `memory`, plus R2 for allowlisted detail endpoints (memory only without R2). |
180182

181183
How it works (`src/server/lib/response-cache.ts`):
182184

@@ -197,9 +199,53 @@ How it works (`src/server/lib/response-cache.ts`):
197199
has zstd; a deployed Worker is not confirmed yet. The
198200
`app.response_cache.write` metric records the encoding each write used.
199201

202+
#### R2 tier
203+
204+
`tiered` shares a few detail responses across isolates through the
205+
`MCP_RESPONSE_CACHE_BUCKET` R2 binding (`src/server/lib/r2-cache-store.ts`):
206+
207+
- **Allowlist.** Only organization event details
208+
(`/api/0/organizations/{org}/events/{project}:{event_id}/`) without
209+
`averageColumn`, and project trace-item details
210+
(`/api/0/projects/{org}/{project}/trace-items/{id}/`) without `debug`.
211+
Every other URL stays in memory.
212+
- **Server opt-in.** An allowlisted response is written to R2 only when it
213+
carries `Cache-Control: private` with a positive `max-age` (and no
214+
`no-store` or `no-cache`). Until Sentry sends that header
215+
(getsentry/sentry#128828), R2 stores nothing.
216+
- **Expiry.** Each object records an expiry, the lesser of `max-age` and 5
217+
minutes, and reads treat a later object as a miss. An R2 hit is copied
218+
into memory for the time it has left.
219+
- **Keys.** Objects are keyed `<identity>/<HMAC cache key>`, with the
220+
identity hash above, and a read also checks the identity stored in the
221+
entry. Authorization values the policy keeps for `Vary` are stored as
222+
SHA-256 hashes, never as tokens.
223+
- **Writes** run in `ctx.waitUntil()`. Mutation invalidation only touches
224+
memory, so a write under the same path, such as deleting the project, does
225+
not clear R2. The 5-minute expiry bounds how long a deleted item can still
226+
be served.
227+
- **Garbage collection.** A 1-day bucket lifecycle rule deletes old objects.
228+
It only frees space; reads enforce the expiry.
229+
230+
The buckets are `sentry-mcp-response-cache` (production) and
231+
`sentry-mcp-canary-response-cache` (canary). Deploys run with
232+
`--experimental-auto-create=false`, so each bucket must exist before a
233+
config that binds it deploys:
234+
235+
```bash
236+
pnpm exec wrangler r2 bucket create sentry-mcp-response-cache
237+
pnpm exec wrangler r2 bucket lifecycle add sentry-mcp-response-cache \
238+
expire-1d --expire-days 1 --force
239+
pnpm exec wrangler r2 bucket create sentry-mcp-canary-response-cache
240+
pnpm exec wrangler r2 bucket lifecycle add sentry-mcp-canary-response-cache \
241+
expire-1d --expire-days 1 --force
242+
```
243+
244+
The binding is inert while `MCP_RESPONSE_CACHE` is not `tiered`.
245+
200246
Turning it on in production is a separate decision: revoked access or deleted
201247
data can still be served from the cache until the entry expires (up to 5
202-
minutes for stable entries). Leave `MCP_RESPONSE_CACHE` unset in production
248+
minutes for stable entries, including from R2). Leave `MCP_RESPONSE_CACHE` unset in production
203249
until that window has privacy and security sign-off. Setting it back to `off`
204250
restores uncached behavior. Metrics are listed in
205251
[Monitoring](../operations/monitoring.md#response-cache-metrics).

‎docs/operations/monitoring.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ off.
358358
charges against its budget, after each write
359359
- `app.response_cache.memory.entries` - Gauge of entries in the isolate's
360360
store, after each write
361+
- `app.response_cache.r2.lookup` - Count of R2 reads after a memory miss on an
362+
allowlisted URL (`MCP_RESPONSE_CACHE=tiered` only), with
363+
`app.response_cache.result`: `hit`, `miss`, or `expired` (past its recorded
364+
expiry)
365+
- `app.response_cache.r2.write` - Count of entries written to R2
361366

362367
The first eviction in an isolate also logs `Response cache reached its memory
363368
budget` at info level, with the budget, bytes, and entry count.
@@ -368,6 +373,11 @@ Interpretation:
368373
the unfiltered sum
369374
- A steady eviction rate with `memory.bytes` at the budget means the budget is
370375
too small for the traffic an isolate serves
376+
- R2 hit rate: `app.response_cache.r2.lookup` filtered to `result=hit`, over
377+
the unfiltered sum. No `r2.write` at all means Sentry is not sending
378+
`Cache-Control: private, max-age` on the allowlisted endpoints.
379+
- R2 read and write failures log `Response cache R2 read failed` and
380+
`Response cache R2 write failed` at warning level; a failed read is a miss.
371381

372382
### Traces Configuration
373383

‎packages/mcp-cloudflare/src/server/lib/mcp-handler.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ async function handleAuthenticatedMcpRequest(
451451
auth.kind === "oauth" && auth.grantId
452452
? await getResponseCache({
453453
mode: resolveResponseCacheMode(env.MCP_RESPONSE_CACHE),
454+
bucket: env.MCP_RESPONSE_CACHE_BUCKET,
454455
userId: auth.userId,
455456
grantId: auth.grantId,
456457
sentryHost,

0 commit comments

Comments
 (0)