Cache full PDF bodies from servers without Range support#411
Merged
Conversation
When a remote server ignores the Range header and returns HTTP 200 with the full body, readPdfRange previously passed the entire file (potentially 10MB+) through as a single chunk. This bypassed the 512KB limit because: 1. The error check `!response.ok && status !== 206` short-circuits on 200 (ok is true), so the full body is read via arrayBuffer() 2. No Content-Range header on a 200 response leaves totalBytes at 0 3. hasMore becomes `offset + fullSize < 0` = false, so the client stops after one oversized message Fix: detect HTTP 200, cache the full body in memory (to avoid re-downloading on every subsequent chunk request), then slice to the requested range. The 512KB per-message limit is now enforced for all remote URLs regardless of Range request support.
@modelcontextprotocol/ext-apps
@modelcontextprotocol/server-arcade
@modelcontextprotocol/server-basic-react
@modelcontextprotocol/server-basic-vanillajs
@modelcontextprotocol/server-budget-allocator
@modelcontextprotocol/server-cohort-heatmap
@modelcontextprotocol/server-customer-segmentation
@modelcontextprotocol/server-map
@modelcontextprotocol/server-pdf
@modelcontextprotocol/server-scenario-modeler
@modelcontextprotocol/server-shadertoy
@modelcontextprotocol/server-sheet-music
@modelcontextprotocol/server-system-monitor
@modelcontextprotocol/server-threejs
@modelcontextprotocol/server-transcript
@modelcontextprotocol/server-video-resource
@modelcontextprotocol/server-wiki-explorer
commit: |
- Add dual timeout strategy for cache cleanup: - 10s inactivity timeout (resets on each access) - 60s max lifetime (absolute timeout from creation) - Add 50MB max size limit with both Content-Length and actual size checks - Add unit tests for caching behavior including: - Cache on HTTP 200 response (no range support) - No cache on HTTP 206 response (range supported) - Slice cached data for subsequent range requests - Reject PDFs exceeding size limits - Export getCacheSize() and clearCache() for testing
jerome3o-anthropic
previously approved these changes
Jan 30, 2026
- Replace module-level global cache with createPdfCache() factory - Each server instance now gets its own isolated cache - Export PdfCache interface for type-safe usage - Update tests to use per-test cache instances - Add test verifying cache isolation between sessions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Improve performance when fetching PDFs from servers that don't support HTTP Range requests by caching the full file body on first request, eliminating redundant full-file downloads for subsequent chunk requests.
Changes
remoteFullBodyCacheMap to store full PDF bodies from servers that return HTTP 200 instead of 206 Partial ContentsliceToChunk()helper function to extract requested byte ranges from cached or fetched full datareadPdfRange()to:Implementation Details
sliceToChunk()safely handles edge cases (offset beyond file size, clamped byte counts)https://preview.claude.ai/code/session_01JutQT8VqbMmfC1n1438hjP