perf(pdf-server): share cache across server instances and dedupe form parsing by ochafik · Pull Request #637 · modelcontextprotocol/ext-apps · GitHub
Skip to content

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

Open
ochafik wants to merge 2 commits intomainfrom
perf/server-pdf-module-cache
Open

perf(pdf-server): share cache across server instances and dedupe form parsing#637
ochafik wants to merge 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

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
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