Skip to content

perf(pdf-server): share cache across server instances and dedupe form parsing#637

Merged
ochafik merged 2 commits intomainfrom
perf/server-pdf-module-cache
Apr 24, 2026
Merged

perf(pdf-server): share cache across server instances and dedupe form parsing#637
ochafik merged 2 commits intomainfrom
perf/server-pdf-module-cache

Conversation

@ochafik
Copy link
Copy Markdown
Contributor

@ochafik ochafik commented Apr 23, 2026

Summary

Three perf fixes for the pdf-server when run behind a stateless HTTP transport (where createServer() is invoked on every request):

  • Module-level sharedPdfCachecreatePdfCache() is now called once at module load and reused by every server instance, instead of being created (and immediately discarded) per request. This lets read_pdf_bytes chunk reads hit the cache across requests, avoiding repeated full-body downloads when the upstream origin doesn't honor Range.
  • Memoized mcp-app.html — the ~4 MB viewer HTML is read from disk once (cachedAppHtml ??= ...) instead of on every resource read.
  • Single download + parse in display_pdfextractFormSchema and extractFormFieldInfo now accept an already-parsed PDFDocumentProxy. display_pdf fetches the bytes once, calls pdfjs.getDocument() once, passes the doc to both extractors, and destroy()s it in a finally. Previously each extractor independently downloaded the full PDF and parsed it.

Behavior note

Both extractors now run inside a single try block, so if schema extraction throws, field-info extraction is also skipped (previously each had its own try/catch). Risk is low: the only throw point inside extractFormSchema (getFieldObjects) is already caught internally and returns an empty schema rather than propagating.

Tests

bun test examples/pdf-server — 177 pass, 0 fail, 1 pre-existing skip. Build (tsc + vite + bun bundle) and prettier check both clean.

… parsing

In stateless HTTP deployments createServer() is called per request, so
the per-instance pdfCache and the 4 MB viewer HTML were discarded after
every call. Hoist both to module scope.

Also refactor extractFormSchema/extractFormFieldInfo to accept an
already-parsed PDFDocumentProxy so display_pdf downloads and parses the
PDF once instead of twice.
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 23, 2026

Open in StackBlitz

@modelcontextprotocol/ext-apps

npm i https://pkg.pr.new/@modelcontextprotocol/ext-apps@637

@modelcontextprotocol/server-basic-preact

npm i https://pkg.pr.new/@modelcontextprotocol/server-basic-preact@637

@modelcontextprotocol/server-basic-react

npm i https://pkg.pr.new/@modelcontextprotocol/server-basic-react@637

@modelcontextprotocol/server-basic-solid

npm i https://pkg.pr.new/@modelcontextprotocol/server-basic-solid@637

@modelcontextprotocol/server-basic-svelte

npm i https://pkg.pr.new/@modelcontextprotocol/server-basic-svelte@637

@modelcontextprotocol/server-basic-vanillajs

npm i https://pkg.pr.new/@modelcontextprotocol/server-basic-vanillajs@637

@modelcontextprotocol/server-basic-vue

npm i https://pkg.pr.new/@modelcontextprotocol/server-basic-vue@637

@modelcontextprotocol/server-budget-allocator

npm i https://pkg.pr.new/@modelcontextprotocol/server-budget-allocator@637

@modelcontextprotocol/server-cohort-heatmap

npm i https://pkg.pr.new/@modelcontextprotocol/server-cohort-heatmap@637

@modelcontextprotocol/server-customer-segmentation

npm i https://pkg.pr.new/@modelcontextprotocol/server-customer-segmentation@637

@modelcontextprotocol/server-debug

npm i https://pkg.pr.new/@modelcontextprotocol/server-debug@637

@modelcontextprotocol/server-map

npm i https://pkg.pr.new/@modelcontextprotocol/server-map@637

@modelcontextprotocol/server-pdf

npm i https://pkg.pr.new/@modelcontextprotocol/server-pdf@637

@modelcontextprotocol/server-scenario-modeler

npm i https://pkg.pr.new/@modelcontextprotocol/server-scenario-modeler@637

@modelcontextprotocol/server-shadertoy

npm i https://pkg.pr.new/@modelcontextprotocol/server-shadertoy@637

@modelcontextprotocol/server-sheet-music

npm i https://pkg.pr.new/@modelcontextprotocol/server-sheet-music@637

@modelcontextprotocol/server-system-monitor

npm i https://pkg.pr.new/@modelcontextprotocol/server-system-monitor@637

@modelcontextprotocol/server-threejs

npm i https://pkg.pr.new/@modelcontextprotocol/server-threejs@637

@modelcontextprotocol/server-transcript

npm i https://pkg.pr.new/@modelcontextprotocol/server-transcript@637

@modelcontextprotocol/server-video-resource

npm i https://pkg.pr.new/@modelcontextprotocol/server-video-resource@637

@modelcontextprotocol/server-wiki-explorer

npm i https://pkg.pr.new/@modelcontextprotocol/server-wiki-explorer@637

commit: 4702eb6

The module-level sharedPdfCache could grow unbounded within the 60s
lifetime window under a burst of distinct URLs. Track running total
bytes and evict least-recently-used entries on insert when it would
exceed CACHE_MAX_TOTAL_BYTES (256MB). getCacheEntry now bumps the
accessed entry to the end of insertion order so eviction targets the
LRU entry rather than the oldest insert.

createPdfCache takes an optional maxTotalBytes for testability.
@ochafik ochafik force-pushed the perf/server-pdf-module-cache branch from fd26573 to 4702eb6 Compare April 23, 2026 14:08
@ochafik ochafik merged commit 30a78b6 into main Apr 24, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant