ext-apps/examples/pdf-server/pdfjs-polyfill.ts
Olivier Chafik 04fed524a9
fix(pdf-server): npx DOMMatrix crash + broken MCPB bundle (#584)
* fix(pdf-server): stub DOMMatrix/ImageData/Path2D for npx without @napi-rs/canvas

pdfjs-dist/legacy/build/pdf.mjs does `new DOMMatrix()` at module scope.
Its own polyfill loads @napi-rs/canvas (an optionalDependency), but npx
frequently misses the platform-native binary (npm/cli#4828), so the
server crashed on `npx @modelcontextprotocol/server-pdf --stdio`:

    ReferenceError: DOMMatrix is not defined
        at pdfjs-dist/legacy/build/pdf.mjs:17078

The server only uses pdfjs for text/metadata/form-field extraction —
never canvas rendering — so no-op stubs suffice and avoid shipping
~130MB of native binaries.

The polyfill is a separate ESM module kept --external in the bun bundle
so it executes before pdfjs's hoisted static import; inlined body code
would run too late.

Broken since 1.3.0 (#506). Thanks to Bryan Thompson for the analysis
and fix shape from the npm-to-mcpb pipeline.

* fix(pdf-server): MCPB bundle missing runtime deps + display_name with spaces

`mcpb pack` zips whatever is on disk; in this monorepo all runtime deps
are hoisted to root node_modules, so the published .mcpb shipped without
pdfjs-dist/ajv/express/etc. and crashed on import in Claude Desktop.

build-mcpb.mjs stages dist/ + manifest into a clean temp dir, runs a
non-workspace `npm install --omit=dev --omit=optional` (the new
DOMMatrix polyfill makes @napi-rs/canvas's ~130MB of native binaries
unnecessary), syncs manifest.version to package.json, then packs.
Result: 12.3MB / 38.8MB unpacked, full MCP handshake + tools/list pass.

Also rename display_name "PDF (By Anthropic)" → "pdf-viewer" — spaces
in display_name break MCP App UI rendering for MCPBs in Claude Desktop.

CI now smoke-tests the extracted bundle starts (`dist/index.js --stdio`
prints "Ready") so a deps-less bundle can't ship again.

* fix(pdf-server): list interact in MCPB manifest tools (was stale since #491)
2026-04-02 04:16:33 +01:00

26 lines
1.1 KiB
TypeScript

/**
* Stub polyfills for pdfjs-dist's legacy build under Node.js.
*
* pdfjs-dist/legacy/build/pdf.mjs eagerly does `new DOMMatrix()` at module
* scope. Its own polyfill loads `@napi-rs/canvas` (an optionalDependency),
* but `npx` / fresh installs frequently miss the platform-native binary
* (npm optional-deps bug, see https://github.com/npm/cli/issues/4828),
* so the import crashes before our server code runs.
*
* The server only uses pdfjs for text/metadata/form-field extraction —
* never canvas rendering — so no-op stubs are sufficient and avoid
* shipping ~130MB of native binaries we don't need.
*
* MUST be a separate ESM module imported before pdfjs-dist (and kept
* `--external` in the bun bundle) so it executes before pdfjs's
* module-level initializer. Inlined body code would run too late
* because static imports are hoisted.
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
const g = globalThis as any;
// Only stub if missing — let a real @napi-rs/canvas (or jsdom) win.
g.DOMMatrix ??= class DOMMatrix {};
g.ImageData ??= class ImageData {};
g.Path2D ??= class Path2D {};