@modelcontextprotocol/server-pdf 1.2.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +147 -18
- package/dist/index.js +21 -5
- package/dist/mcp-app.html +167 -52
- package/dist/server.d.ts +67 -2
- package/dist/server.js +1223 -64
- package/dist/src/commands.d.ts +81 -0
- package/dist/src/pdf-annotations.d.ts +186 -0
- package/package.json +3 -2
package/dist/server.js
CHANGED
|
@@ -24040,7 +24040,6 @@ function date4(params) {
|
|
|
24040
24040
|
|
|
24041
24041
|
// ../../node_modules/zod/v4/classic/external.js
|
|
24042
24042
|
config(en_default2());
|
|
24043
|
-
|
|
24044
24043
|
// ../../node_modules/@modelcontextprotocol/sdk/dist/esm/types.js
|
|
24045
24044
|
var LATEST_PROTOCOL_VERSION = "2025-11-25";
|
|
24046
24045
|
var SUPPORTED_PROTOCOL_VERSIONS = [LATEST_PROTOCOL_VERSION, "2025-06-18", "2025-03-26", "2024-11-05", "2024-10-07"];
|
|
@@ -28267,8 +28266,8 @@ class j {
|
|
|
28267
28266
|
sessionId;
|
|
28268
28267
|
setProtocolVersion;
|
|
28269
28268
|
}
|
|
28270
|
-
var
|
|
28271
|
-
var
|
|
28269
|
+
var g = exports_external.union([exports_external.literal("light"), exports_external.literal("dark")]).describe("Color theme preference for the host environment.");
|
|
28270
|
+
var G = exports_external.union([exports_external.literal("inline"), exports_external.literal("fullscreen"), exports_external.literal("pip")]).describe("Display mode for UI presentation.");
|
|
28272
28271
|
var i = exports_external.union([exports_external.literal("--color-background-primary"), exports_external.literal("--color-background-secondary"), exports_external.literal("--color-background-tertiary"), exports_external.literal("--color-background-inverse"), exports_external.literal("--color-background-ghost"), exports_external.literal("--color-background-info"), exports_external.literal("--color-background-danger"), exports_external.literal("--color-background-success"), exports_external.literal("--color-background-warning"), exports_external.literal("--color-background-disabled"), exports_external.literal("--color-text-primary"), exports_external.literal("--color-text-secondary"), exports_external.literal("--color-text-tertiary"), exports_external.literal("--color-text-inverse"), exports_external.literal("--color-text-ghost"), exports_external.literal("--color-text-info"), exports_external.literal("--color-text-danger"), exports_external.literal("--color-text-success"), exports_external.literal("--color-text-warning"), exports_external.literal("--color-text-disabled"), exports_external.literal("--color-border-primary"), exports_external.literal("--color-border-secondary"), exports_external.literal("--color-border-tertiary"), exports_external.literal("--color-border-inverse"), exports_external.literal("--color-border-ghost"), exports_external.literal("--color-border-info"), exports_external.literal("--color-border-danger"), exports_external.literal("--color-border-success"), exports_external.literal("--color-border-warning"), exports_external.literal("--color-border-disabled"), exports_external.literal("--color-ring-primary"), exports_external.literal("--color-ring-secondary"), exports_external.literal("--color-ring-inverse"), exports_external.literal("--color-ring-info"), exports_external.literal("--color-ring-danger"), exports_external.literal("--color-ring-success"), exports_external.literal("--color-ring-warning"), exports_external.literal("--font-sans"), exports_external.literal("--font-mono"), exports_external.literal("--font-weight-normal"), exports_external.literal("--font-weight-medium"), exports_external.literal("--font-weight-semibold"), exports_external.literal("--font-weight-bold"), exports_external.literal("--font-text-xs-size"), exports_external.literal("--font-text-sm-size"), exports_external.literal("--font-text-md-size"), exports_external.literal("--font-text-lg-size"), exports_external.literal("--font-heading-xs-size"), exports_external.literal("--font-heading-sm-size"), exports_external.literal("--font-heading-md-size"), exports_external.literal("--font-heading-lg-size"), exports_external.literal("--font-heading-xl-size"), exports_external.literal("--font-heading-2xl-size"), exports_external.literal("--font-heading-3xl-size"), exports_external.literal("--font-text-xs-line-height"), exports_external.literal("--font-text-sm-line-height"), exports_external.literal("--font-text-md-line-height"), exports_external.literal("--font-text-lg-line-height"), exports_external.literal("--font-heading-xs-line-height"), exports_external.literal("--font-heading-sm-line-height"), exports_external.literal("--font-heading-md-line-height"), exports_external.literal("--font-heading-lg-line-height"), exports_external.literal("--font-heading-xl-line-height"), exports_external.literal("--font-heading-2xl-line-height"), exports_external.literal("--font-heading-3xl-line-height"), exports_external.literal("--border-radius-xs"), exports_external.literal("--border-radius-sm"), exports_external.literal("--border-radius-md"), exports_external.literal("--border-radius-lg"), exports_external.literal("--border-radius-xl"), exports_external.literal("--border-radius-full"), exports_external.literal("--border-width-regular"), exports_external.literal("--shadow-hairline"), exports_external.literal("--shadow-sm"), exports_external.literal("--shadow-md"), exports_external.literal("--shadow-lg")]).describe("CSS variable keys available to MCP apps for theming.");
|
|
28273
28272
|
var o = exports_external.record(i.describe(`Style variables for theming MCP apps.
|
|
28274
28273
|
|
|
@@ -28303,15 +28302,16 @@ var t = exports_external.object({ method: exports_external.literal("ui/notificat
|
|
|
28303
28302
|
var A = exports_external.object({ method: exports_external.literal("ui/notifications/tool-input"), params: exports_external.object({ arguments: exports_external.record(exports_external.string(), exports_external.unknown().describe("Complete tool call arguments as key-value pairs.")).optional().describe("Complete tool call arguments as key-value pairs.") }) });
|
|
28304
28303
|
var P = exports_external.object({ method: exports_external.literal("ui/notifications/tool-input-partial"), params: exports_external.object({ arguments: exports_external.record(exports_external.string(), exports_external.unknown().describe("Partial tool call arguments (incomplete, may change).")).optional().describe("Partial tool call arguments (incomplete, may change).") }) });
|
|
28305
28304
|
var H = exports_external.object({ method: exports_external.literal("ui/notifications/tool-cancelled"), params: exports_external.object({ reason: exports_external.string().optional().describe('Optional reason for the cancellation (e.g., "user action", "timeout").') }) });
|
|
28306
|
-
var
|
|
28307
|
-
var
|
|
28305
|
+
var S = exports_external.object({ fonts: exports_external.string().optional() });
|
|
28306
|
+
var y = exports_external.object({ variables: o.optional().describe("CSS variables for theming the app."), css: S.optional().describe("CSS blocks that apps can inject.") });
|
|
28308
28307
|
var _ = exports_external.object({ method: exports_external.literal("ui/resource-teardown"), params: exports_external.object({}) });
|
|
28309
28308
|
var e = exports_external.record(exports_external.string(), exports_external.unknown());
|
|
28310
28309
|
var q = exports_external.object({ text: exports_external.object({}).optional().describe("Host supports text content blocks."), image: exports_external.object({}).optional().describe("Host supports image content blocks."), audio: exports_external.object({}).optional().describe("Host supports audio content blocks."), resource: exports_external.object({}).optional().describe("Host supports resource content blocks."), resourceLink: exports_external.object({}).optional().describe("Host supports resource link content blocks."), structuredContent: exports_external.object({}).optional().describe("Host supports structured content.") });
|
|
28311
|
-
var
|
|
28312
|
-
var
|
|
28313
|
-
var
|
|
28314
|
-
var ZQ = exports_external.object({
|
|
28310
|
+
var QQ = exports_external.object({ method: exports_external.literal("ui/notifications/request-teardown"), params: exports_external.object({}).optional() });
|
|
28311
|
+
var C = exports_external.object({ experimental: exports_external.object({}).optional().describe("Experimental features (structure TBD)."), openLinks: exports_external.object({}).optional().describe("Host supports opening external URLs."), downloadFile: exports_external.object({}).optional().describe("Host supports file downloads via ui/download-file."), serverTools: exports_external.object({ listChanged: exports_external.boolean().optional().describe("Host supports tools/list_changed notifications.") }).optional().describe("Host can proxy tool calls to the MCP server."), serverResources: exports_external.object({ listChanged: exports_external.boolean().optional().describe("Host supports resources/list_changed notifications.") }).optional().describe("Host can proxy resource reads to the MCP server."), logging: exports_external.object({}).optional().describe("Host accepts log messages."), sandbox: exports_external.object({ permissions: K.optional().describe("Permissions granted by the host (camera, microphone, geolocation)."), csp: B.optional().describe("CSP domains approved by the host.") }).optional().describe("Sandbox configuration applied by the host."), updateModelContext: q.optional().describe("Host accepts context updates (ui/update-model-context) to be included in the model's context for future turns."), message: q.optional().describe("Host supports receiving content messages (ui/message) from the view.") });
|
|
28312
|
+
var f = exports_external.object({ experimental: exports_external.object({}).optional().describe("Experimental features (structure TBD)."), tools: exports_external.object({ listChanged: exports_external.boolean().optional().describe("App supports tools/list_changed notifications.") }).optional().describe("App exposes MCP-style tools that the host can call."), availableDisplayModes: exports_external.array(G).optional().describe("Display modes the app supports.") });
|
|
28313
|
+
var ZQ = exports_external.object({ method: exports_external.literal("ui/notifications/initialized"), params: exports_external.object({}).optional() });
|
|
28314
|
+
var $Q = exports_external.object({ csp: B.optional().describe("Content Security Policy configuration for UI resources."), permissions: K.optional().describe("Sandbox permissions requested by the UI resource."), domain: exports_external.string().optional().describe(`Dedicated origin for view sandbox.
|
|
28315
28315
|
|
|
28316
28316
|
Useful when views need stable, dedicated origins for OAuth callbacks, CORS policies, or API key allowlists.
|
|
28317
28317
|
|
|
@@ -28326,27 +28326,27 @@ Boolean requesting whether a visible border and background is provided by the ho
|
|
|
28326
28326
|
- \`true\`: request visible border + background
|
|
28327
28327
|
- \`false\`: request no visible border + background
|
|
28328
28328
|
- omitted: host decides border`) });
|
|
28329
|
-
var
|
|
28330
|
-
var T = exports_external.object({ mode:
|
|
28331
|
-
var
|
|
28332
|
-
var
|
|
28329
|
+
var JQ = exports_external.object({ method: exports_external.literal("ui/request-display-mode"), params: exports_external.object({ mode: G.describe("The display mode being requested.") }) });
|
|
28330
|
+
var T = exports_external.object({ mode: G.describe("The display mode that was actually set. May differ from requested if not supported.") }).passthrough();
|
|
28331
|
+
var u = exports_external.union([exports_external.literal("model"), exports_external.literal("app")]).describe("Tool visibility scope - who can access the tool.");
|
|
28332
|
+
var XQ = exports_external.object({ resourceUri: exports_external.string().optional(), visibility: exports_external.array(u).optional().describe(`Who can access this tool. Default: ["model", "app"]
|
|
28333
28333
|
- "model": Tool visible to and callable by the agent
|
|
28334
28334
|
- "app": Tool callable by the app from this server only`) });
|
|
28335
|
-
var
|
|
28336
|
-
var
|
|
28337
|
-
var
|
|
28338
|
-
var
|
|
28339
|
-
var
|
|
28340
|
-
var k = exports_external.object({ toolInfo: exports_external.object({ id: RequestIdSchema.optional().describe("JSON-RPC id of the tools/call request."), tool: ToolSchema.describe("Tool definition including name, inputSchema, etc.") }).optional().describe("Metadata of the tool call that instantiated this App."), theme:
|
|
28335
|
+
var RQ = exports_external.object({ mimeTypes: exports_external.array(exports_external.string()).optional().describe('Array of supported MIME types for UI resources.\nMust include `"text/html;profile=mcp-app"` for MCP Apps support.') });
|
|
28336
|
+
var VQ = exports_external.object({ method: exports_external.literal("ui/download-file"), params: exports_external.object({ contents: exports_external.array(exports_external.union([EmbeddedResourceSchema, ResourceLinkSchema])).describe("Resource contents to download — embedded (inline data) or linked (host fetches). Uses standard MCP resource types.") }) });
|
|
28337
|
+
var DQ = exports_external.object({ method: exports_external.literal("ui/message"), params: exports_external.object({ role: exports_external.literal("user").describe('Message role, currently only "user" is supported.'), content: exports_external.array(ContentBlockSchema).describe("Message content blocks (text, image, etc.).") }) });
|
|
28338
|
+
var GQ = exports_external.object({ method: exports_external.literal("ui/notifications/sandbox-resource-ready"), params: exports_external.object({ html: exports_external.string().describe("HTML content to load into the inner iframe."), sandbox: exports_external.string().optional().describe("Optional override for the inner iframe's sandbox attribute."), csp: B.optional().describe("CSP configuration from resource metadata."), permissions: K.optional().describe("Sandbox permissions from resource metadata.") }) });
|
|
28339
|
+
var E = exports_external.object({ method: exports_external.literal("ui/notifications/tool-result"), params: CallToolResultSchema.describe("Standard MCP tool execution result.") });
|
|
28340
|
+
var k = exports_external.object({ toolInfo: exports_external.object({ id: RequestIdSchema.optional().describe("JSON-RPC id of the tools/call request."), tool: ToolSchema.describe("Tool definition including name, inputSchema, etc.") }).optional().describe("Metadata of the tool call that instantiated this App."), theme: g.optional().describe("Current color theme preference."), styles: y.optional().describe("Style configuration for theming the app."), displayMode: G.optional().describe("How the UI is currently displayed."), availableDisplayModes: exports_external.array(G).optional().describe("Display modes the host supports."), containerDimensions: exports_external.union([exports_external.object({ height: exports_external.number().describe("Fixed container height in pixels.") }), exports_external.object({ maxHeight: exports_external.union([exports_external.number(), exports_external.undefined()]).optional().describe("Maximum container height in pixels.") })]).and(exports_external.union([exports_external.object({ width: exports_external.number().describe("Fixed container width in pixels.") }), exports_external.object({ maxWidth: exports_external.union([exports_external.number(), exports_external.undefined()]).optional().describe("Maximum container width in pixels.") })])).optional().describe(`Container dimensions. Represents the dimensions of the iframe or other
|
|
28341
28341
|
container holding the app. Specify either width or maxWidth, and either height or maxHeight.`), locale: exports_external.string().optional().describe("User's language and region preference in BCP 47 format."), timeZone: exports_external.string().optional().describe("User's timezone in IANA format."), userAgent: exports_external.string().optional().describe("Host application identifier."), platform: exports_external.union([exports_external.literal("web"), exports_external.literal("desktop"), exports_external.literal("mobile")]).optional().describe("Platform type for responsive design decisions."), deviceCapabilities: exports_external.object({ touch: exports_external.boolean().optional().describe("Whether the device supports touch input."), hover: exports_external.boolean().optional().describe("Whether the device supports hover interactions.") }).optional().describe("Device input capabilities."), safeAreaInsets: exports_external.object({ top: exports_external.number().describe("Top safe area inset in pixels."), right: exports_external.number().describe("Right safe area inset in pixels."), bottom: exports_external.number().describe("Bottom safe area inset in pixels."), left: exports_external.number().describe("Left safe area inset in pixels.") }).optional().describe("Mobile safe area boundaries in pixels.") }).passthrough();
|
|
28342
|
-
var
|
|
28342
|
+
var R = exports_external.object({ method: exports_external.literal("ui/notifications/host-context-changed"), params: k.describe("Partial context update containing only changed fields.") });
|
|
28343
28343
|
var LQ = exports_external.object({ method: exports_external.literal("ui/update-model-context"), params: exports_external.object({ content: exports_external.array(ContentBlockSchema).optional().describe("Context content blocks (text, image, etc.)."), structuredContent: exports_external.record(exports_external.string(), exports_external.unknown().describe("Structured content for machine-readable context data.")).optional().describe("Structured content for machine-readable context data.") }) });
|
|
28344
|
-
var
|
|
28345
|
-
var
|
|
28344
|
+
var WQ = exports_external.object({ method: exports_external.literal("ui/initialize"), params: exports_external.object({ appInfo: ImplementationSchema.describe("App identification (name and version)."), appCapabilities: f.describe("Features and capabilities this app provides."), protocolVersion: exports_external.string().describe("Protocol version this app supports.") }) });
|
|
28345
|
+
var U = exports_external.object({ protocolVersion: exports_external.string().describe('Negotiated protocol version string (e.g., "2025-11-21").'), hostInfo: ImplementationSchema.describe("Host application identification and version."), hostCapabilities: C.describe("Features and capabilities provided by the host."), hostContext: k.describe("Rich context about the host environment.") }).passthrough();
|
|
28346
28346
|
var v = "ui/resourceUri";
|
|
28347
28347
|
var d = "text/html;profile=mcp-app";
|
|
28348
28348
|
|
|
28349
|
-
class
|
|
28349
|
+
class OQ extends Protocol {
|
|
28350
28350
|
_appInfo;
|
|
28351
28351
|
_capabilities;
|
|
28352
28352
|
options;
|
|
@@ -28378,13 +28378,13 @@ class qQ extends Protocol {
|
|
|
28378
28378
|
this.setNotificationHandler(P, ($) => Z($.params));
|
|
28379
28379
|
}
|
|
28380
28380
|
set ontoolresult(Z) {
|
|
28381
|
-
this.setNotificationHandler(
|
|
28381
|
+
this.setNotificationHandler(E, ($) => Z($.params));
|
|
28382
28382
|
}
|
|
28383
28383
|
set ontoolcancelled(Z) {
|
|
28384
28384
|
this.setNotificationHandler(H, ($) => Z($.params));
|
|
28385
28385
|
}
|
|
28386
28386
|
set onhostcontextchanged(Z) {
|
|
28387
|
-
this.setNotificationHandler(
|
|
28387
|
+
this.setNotificationHandler(R, ($) => {
|
|
28388
28388
|
this._hostContext = { ...this._hostContext, ...$.params }, Z($.params);
|
|
28389
28389
|
});
|
|
28390
28390
|
}
|
|
@@ -28446,6 +28446,9 @@ class qQ extends Protocol {
|
|
|
28446
28446
|
downloadFile(Z, $) {
|
|
28447
28447
|
return this.request({ method: "ui/download-file", params: Z }, I, $);
|
|
28448
28448
|
}
|
|
28449
|
+
requestTeardown(Z = {}) {
|
|
28450
|
+
return this.notification({ method: "ui/notifications/request-teardown", params: Z });
|
|
28451
|
+
}
|
|
28449
28452
|
requestDisplayMode(Z, $) {
|
|
28450
28453
|
return this.request({ method: "ui/request-display-mode", params: Z }, T, $);
|
|
28451
28454
|
}
|
|
@@ -28458,11 +28461,11 @@ class qQ extends Protocol {
|
|
|
28458
28461
|
return;
|
|
28459
28462
|
Z = true, requestAnimationFrame(() => {
|
|
28460
28463
|
Z = false;
|
|
28461
|
-
let V = document.documentElement,
|
|
28464
|
+
let V = document.documentElement, L = V.style.width, W = V.style.height;
|
|
28462
28465
|
V.style.width = "fit-content", V.style.height = "max-content";
|
|
28463
|
-
let
|
|
28464
|
-
V.style.width =
|
|
28465
|
-
let h = window.innerWidth - V.clientWidth, N = Math.ceil(
|
|
28466
|
+
let x = V.getBoundingClientRect();
|
|
28467
|
+
V.style.width = L, V.style.height = W;
|
|
28468
|
+
let h = window.innerWidth - V.clientWidth, N = Math.ceil(x.width + h), Y = Math.ceil(x.height);
|
|
28466
28469
|
if (N !== $ || Y !== J)
|
|
28467
28470
|
$ = N, J = Y, this.sendSizeChanged({ width: N, height: Y });
|
|
28468
28471
|
});
|
|
@@ -28476,7 +28479,7 @@ class qQ extends Protocol {
|
|
|
28476
28479
|
throw Error("App is already connected. Call close() before connecting again.");
|
|
28477
28480
|
await super.connect(Z);
|
|
28478
28481
|
try {
|
|
28479
|
-
let J = await this.request({ method: "ui/initialize", params: { appCapabilities: this._capabilities, appInfo: this._appInfo, protocolVersion: F } },
|
|
28482
|
+
let J = await this.request({ method: "ui/initialize", params: { appCapabilities: this._capabilities, appInfo: this._appInfo, protocolVersion: F } }, U, $);
|
|
28480
28483
|
if (J === undefined)
|
|
28481
28484
|
throw Error(`Server sent invalid initialize result: ${J}`);
|
|
28482
28485
|
if (this._hostCapabilities = J.hostCapabilities, this._hostInfo = J.hostInfo, this._hostContext = J.hostContext, await this.notification({ method: "ui/notifications/initialized" }), this.options?.autoResize)
|
|
@@ -28486,19 +28489,42 @@ class qQ extends Protocol {
|
|
|
28486
28489
|
}
|
|
28487
28490
|
}
|
|
28488
28491
|
}
|
|
28489
|
-
function
|
|
28490
|
-
let D = J._meta, V = D.ui,
|
|
28491
|
-
if (V?.resourceUri && !
|
|
28492
|
+
function hZ(Z, $, J, X) {
|
|
28493
|
+
let D = J._meta, V = D.ui, L = D[v], W = D;
|
|
28494
|
+
if (V?.resourceUri && !L)
|
|
28492
28495
|
W = { ...D, [v]: V.resourceUri };
|
|
28493
|
-
else if (
|
|
28494
|
-
W = { ...D, ui: { ...V, resourceUri:
|
|
28496
|
+
else if (L && !V?.resourceUri)
|
|
28497
|
+
W = { ...D, ui: { ...V, resourceUri: L } };
|
|
28495
28498
|
return Z.registerTool($, { ...J, _meta: W }, X);
|
|
28496
28499
|
}
|
|
28497
|
-
function
|
|
28500
|
+
function mZ(Z, $, J, X, D) {
|
|
28498
28501
|
return Z.registerResource($, J, { mimeType: d, ...X }, D);
|
|
28499
28502
|
}
|
|
28500
28503
|
|
|
28501
28504
|
// server.ts
|
|
28505
|
+
import {
|
|
28506
|
+
getDocument,
|
|
28507
|
+
VerbosityLevel,
|
|
28508
|
+
version as PDFJS_VERSION
|
|
28509
|
+
} from "pdfjs-dist/legacy/build/pdf.mjs";
|
|
28510
|
+
var STANDARD_FONT_DATA_URL = `https://unpkg.com/pdfjs-dist@${PDFJS_VERSION}/standard_fonts/`;
|
|
28511
|
+
var STANDARD_FONT_ORIGIN = "https://unpkg.com";
|
|
28512
|
+
|
|
28513
|
+
class FetchStandardFontDataFactory {
|
|
28514
|
+
baseUrl;
|
|
28515
|
+
constructor({ baseUrl = null }) {
|
|
28516
|
+
this.baseUrl = baseUrl;
|
|
28517
|
+
}
|
|
28518
|
+
async fetch({ filename }) {
|
|
28519
|
+
if (!this.baseUrl)
|
|
28520
|
+
throw new Error("standardFontDataUrl not provided");
|
|
28521
|
+
const url2 = `${this.baseUrl}${filename}`;
|
|
28522
|
+
const res = await globalThis.fetch(url2);
|
|
28523
|
+
if (!res.ok)
|
|
28524
|
+
throw new Error(`Failed to fetch ${url2}: ${res.status}`);
|
|
28525
|
+
return new Uint8Array(await res.arrayBuffer());
|
|
28526
|
+
}
|
|
28527
|
+
}
|
|
28502
28528
|
var DEFAULT_PDF = "https://arxiv.org/pdf/1706.03762";
|
|
28503
28529
|
var MAX_CHUNK_BYTES = 512 * 1024;
|
|
28504
28530
|
var RESOURCE_URI = "ui://pdf-viewer/mcp-app.html";
|
|
@@ -28507,7 +28533,158 @@ var CACHE_MAX_LIFETIME_MS = 60000;
|
|
|
28507
28533
|
var CACHE_MAX_PDF_SIZE_BYTES = 50 * 1024 * 1024;
|
|
28508
28534
|
var allowedLocalFiles = new Set;
|
|
28509
28535
|
var allowedLocalDirs = new Set;
|
|
28536
|
+
var cliLocalFiles = new Set;
|
|
28537
|
+
var writeFlags = {
|
|
28538
|
+
allowUploadsRoot: false
|
|
28539
|
+
};
|
|
28540
|
+
function isWritablePath(resolved) {
|
|
28541
|
+
if (cliLocalFiles.has(resolved))
|
|
28542
|
+
return true;
|
|
28543
|
+
if (allowedLocalFiles.has(resolved))
|
|
28544
|
+
return false;
|
|
28545
|
+
return [...allowedLocalDirs].some((dir) => {
|
|
28546
|
+
if (!isAncestorDir(dir, resolved))
|
|
28547
|
+
return false;
|
|
28548
|
+
if (!writeFlags.allowUploadsRoot && path.basename(dir) === "uploads") {
|
|
28549
|
+
return false;
|
|
28550
|
+
}
|
|
28551
|
+
return true;
|
|
28552
|
+
});
|
|
28553
|
+
}
|
|
28510
28554
|
var DIST_DIR = import.meta.filename.endsWith(".ts") ? path.join(import.meta.dirname, "dist") : import.meta.dirname;
|
|
28555
|
+
var COMMAND_TTL_MS = 60000;
|
|
28556
|
+
var SWEEP_INTERVAL_MS = 30000;
|
|
28557
|
+
var POLL_BATCH_WAIT_MS = 200;
|
|
28558
|
+
var LONG_POLL_TIMEOUT_MS = 30000;
|
|
28559
|
+
var FormField = exports_external.object({
|
|
28560
|
+
name: exports_external.string(),
|
|
28561
|
+
value: exports_external.union([exports_external.string(), exports_external.boolean()])
|
|
28562
|
+
});
|
|
28563
|
+
var PageInterval = exports_external.object({
|
|
28564
|
+
start: exports_external.number().min(1).optional(),
|
|
28565
|
+
end: exports_external.number().min(1).optional()
|
|
28566
|
+
});
|
|
28567
|
+
var GET_PAGES_TIMEOUT_MS = 45000;
|
|
28568
|
+
var pendingPageRequests = new Map;
|
|
28569
|
+
function waitForPageData(requestId, signal) {
|
|
28570
|
+
return new Promise((resolve, reject) => {
|
|
28571
|
+
const settle = (v2) => {
|
|
28572
|
+
clearTimeout(timer);
|
|
28573
|
+
signal?.removeEventListener("abort", onAbort);
|
|
28574
|
+
pendingPageRequests.delete(requestId);
|
|
28575
|
+
v2 instanceof Error ? reject(v2) : resolve(v2);
|
|
28576
|
+
};
|
|
28577
|
+
const onAbort = () => settle(new Error("interact request cancelled"));
|
|
28578
|
+
const timer = setTimeout(() => settle(new Error("Timeout waiting for page data from viewer")), GET_PAGES_TIMEOUT_MS);
|
|
28579
|
+
signal?.addEventListener("abort", onAbort);
|
|
28580
|
+
pendingPageRequests.set(requestId, settle);
|
|
28581
|
+
});
|
|
28582
|
+
}
|
|
28583
|
+
var commandQueues = new Map;
|
|
28584
|
+
var pollWaiters = new Map;
|
|
28585
|
+
var viewFieldNames = new Map;
|
|
28586
|
+
var viewFieldInfo = new Map;
|
|
28587
|
+
var viewFileWatches = new Map;
|
|
28588
|
+
var viewLastActivity = new Map;
|
|
28589
|
+
function touchView(uuid3) {
|
|
28590
|
+
viewLastActivity.set(uuid3, Date.now());
|
|
28591
|
+
}
|
|
28592
|
+
function pruneStaleQueues() {
|
|
28593
|
+
const now = Date.now();
|
|
28594
|
+
for (const [uuid3, lastActivity] of viewLastActivity) {
|
|
28595
|
+
if (now - lastActivity > COMMAND_TTL_MS) {
|
|
28596
|
+
viewLastActivity.delete(uuid3);
|
|
28597
|
+
commandQueues.delete(uuid3);
|
|
28598
|
+
viewFieldNames.delete(uuid3);
|
|
28599
|
+
viewFieldInfo.delete(uuid3);
|
|
28600
|
+
stopFileWatch(uuid3);
|
|
28601
|
+
}
|
|
28602
|
+
}
|
|
28603
|
+
}
|
|
28604
|
+
setInterval(pruneStaleQueues, SWEEP_INTERVAL_MS).unref();
|
|
28605
|
+
function enqueueCommand(viewUUID, command) {
|
|
28606
|
+
let entry = commandQueues.get(viewUUID);
|
|
28607
|
+
if (!entry) {
|
|
28608
|
+
entry = { commands: [], lastActivity: Date.now() };
|
|
28609
|
+
commandQueues.set(viewUUID, entry);
|
|
28610
|
+
}
|
|
28611
|
+
entry.commands.push(command);
|
|
28612
|
+
entry.lastActivity = Date.now();
|
|
28613
|
+
touchView(viewUUID);
|
|
28614
|
+
const waiter = pollWaiters.get(viewUUID);
|
|
28615
|
+
if (waiter) {
|
|
28616
|
+
pollWaiters.delete(viewUUID);
|
|
28617
|
+
waiter();
|
|
28618
|
+
}
|
|
28619
|
+
}
|
|
28620
|
+
function dequeueCommands(viewUUID) {
|
|
28621
|
+
touchView(viewUUID);
|
|
28622
|
+
const entry = commandQueues.get(viewUUID);
|
|
28623
|
+
if (!entry)
|
|
28624
|
+
return [];
|
|
28625
|
+
const commands = entry.commands;
|
|
28626
|
+
commandQueues.delete(viewUUID);
|
|
28627
|
+
return commands;
|
|
28628
|
+
}
|
|
28629
|
+
var FILE_WATCH_DEBOUNCE_MS = 150;
|
|
28630
|
+
function startFileWatch(viewUUID, filePath) {
|
|
28631
|
+
const resolved = path.resolve(filePath);
|
|
28632
|
+
let stat;
|
|
28633
|
+
try {
|
|
28634
|
+
stat = fs.statSync(resolved);
|
|
28635
|
+
} catch {
|
|
28636
|
+
return;
|
|
28637
|
+
}
|
|
28638
|
+
stopFileWatch(viewUUID);
|
|
28639
|
+
const entry = {
|
|
28640
|
+
filePath: resolved,
|
|
28641
|
+
watcher: null,
|
|
28642
|
+
lastMtimeMs: stat.mtimeMs,
|
|
28643
|
+
debounce: null
|
|
28644
|
+
};
|
|
28645
|
+
const onEvent = (eventType) => {
|
|
28646
|
+
if (entry.debounce)
|
|
28647
|
+
clearTimeout(entry.debounce);
|
|
28648
|
+
entry.debounce = setTimeout(() => {
|
|
28649
|
+
entry.debounce = null;
|
|
28650
|
+
let s2;
|
|
28651
|
+
try {
|
|
28652
|
+
s2 = fs.statSync(resolved);
|
|
28653
|
+
} catch {
|
|
28654
|
+
return;
|
|
28655
|
+
}
|
|
28656
|
+
if (s2.mtimeMs === entry.lastMtimeMs)
|
|
28657
|
+
return;
|
|
28658
|
+
entry.lastMtimeMs = s2.mtimeMs;
|
|
28659
|
+
enqueueCommand(viewUUID, { type: "file_changed", mtimeMs: s2.mtimeMs });
|
|
28660
|
+
}, FILE_WATCH_DEBOUNCE_MS);
|
|
28661
|
+
if (eventType === "rename") {
|
|
28662
|
+
try {
|
|
28663
|
+
entry.watcher.close();
|
|
28664
|
+
} catch {}
|
|
28665
|
+
try {
|
|
28666
|
+
entry.watcher = fs.watch(resolved, onEvent);
|
|
28667
|
+
} catch {}
|
|
28668
|
+
}
|
|
28669
|
+
};
|
|
28670
|
+
try {
|
|
28671
|
+
entry.watcher = fs.watch(resolved, onEvent);
|
|
28672
|
+
} catch {
|
|
28673
|
+
return;
|
|
28674
|
+
}
|
|
28675
|
+
viewFileWatches.set(viewUUID, entry);
|
|
28676
|
+
}
|
|
28677
|
+
function stopFileWatch(viewUUID) {
|
|
28678
|
+
const entry = viewFileWatches.get(viewUUID);
|
|
28679
|
+
if (!entry)
|
|
28680
|
+
return;
|
|
28681
|
+
if (entry.debounce)
|
|
28682
|
+
clearTimeout(entry.debounce);
|
|
28683
|
+
try {
|
|
28684
|
+
entry.watcher.close();
|
|
28685
|
+
} catch {}
|
|
28686
|
+
viewFileWatches.delete(viewUUID);
|
|
28687
|
+
}
|
|
28511
28688
|
function isFileUrl(url2) {
|
|
28512
28689
|
return url2.startsWith("file://") || url2.startsWith("computer://");
|
|
28513
28690
|
}
|
|
@@ -28718,8 +28895,133 @@ async function refreshRoots(server) {
|
|
|
28718
28895
|
console.error(`[pdf-server] Failed to list roots: ${err instanceof Error ? err.message : err}`);
|
|
28719
28896
|
}
|
|
28720
28897
|
}
|
|
28898
|
+
async function extractFormFieldInfo(url2, readRange) {
|
|
28899
|
+
const { totalBytes } = await readRange(url2, 0, 1);
|
|
28900
|
+
const { data } = await readRange(url2, 0, totalBytes);
|
|
28901
|
+
const loadingTask = getDocument({
|
|
28902
|
+
data,
|
|
28903
|
+
standardFontDataUrl: STANDARD_FONT_DATA_URL,
|
|
28904
|
+
StandardFontDataFactory: FetchStandardFontDataFactory,
|
|
28905
|
+
verbosity: VerbosityLevel.ERRORS
|
|
28906
|
+
});
|
|
28907
|
+
const pdfDoc = await loadingTask.promise;
|
|
28908
|
+
const fields = [];
|
|
28909
|
+
try {
|
|
28910
|
+
for (let i2 = 1;i2 <= pdfDoc.numPages; i2++) {
|
|
28911
|
+
const page = await pdfDoc.getPage(i2);
|
|
28912
|
+
const pageHeight = page.getViewport({ scale: 1 }).height;
|
|
28913
|
+
const annotations = await page.getAnnotations();
|
|
28914
|
+
for (const ann of annotations) {
|
|
28915
|
+
if (ann.annotationType !== 20)
|
|
28916
|
+
continue;
|
|
28917
|
+
if (!ann.rect)
|
|
28918
|
+
continue;
|
|
28919
|
+
const fieldName = ann.fieldName || "";
|
|
28920
|
+
const fieldType = ann.fieldType || "unknown";
|
|
28921
|
+
const x1 = Math.min(ann.rect[0], ann.rect[2]);
|
|
28922
|
+
const y1 = Math.min(ann.rect[1], ann.rect[3]);
|
|
28923
|
+
const x2 = Math.max(ann.rect[0], ann.rect[2]);
|
|
28924
|
+
const y2 = Math.max(ann.rect[1], ann.rect[3]);
|
|
28925
|
+
const width = x2 - x1;
|
|
28926
|
+
const height = y2 - y1;
|
|
28927
|
+
const modelY = pageHeight - y2;
|
|
28928
|
+
fields.push({
|
|
28929
|
+
name: fieldName,
|
|
28930
|
+
type: fieldType,
|
|
28931
|
+
page: i2,
|
|
28932
|
+
x: Math.round(x1),
|
|
28933
|
+
y: Math.round(modelY),
|
|
28934
|
+
width: Math.round(width),
|
|
28935
|
+
height: Math.round(height),
|
|
28936
|
+
...ann.alternativeText ? { label: ann.alternativeText } : undefined
|
|
28937
|
+
});
|
|
28938
|
+
}
|
|
28939
|
+
}
|
|
28940
|
+
} finally {
|
|
28941
|
+
pdfDoc.destroy();
|
|
28942
|
+
}
|
|
28943
|
+
return fields;
|
|
28944
|
+
}
|
|
28945
|
+
async function extractFormSchema(url2, readRange) {
|
|
28946
|
+
const { totalBytes } = await readRange(url2, 0, 1);
|
|
28947
|
+
const { data } = await readRange(url2, 0, totalBytes);
|
|
28948
|
+
const loadingTask = getDocument({
|
|
28949
|
+
data,
|
|
28950
|
+
standardFontDataUrl: STANDARD_FONT_DATA_URL,
|
|
28951
|
+
StandardFontDataFactory: FetchStandardFontDataFactory,
|
|
28952
|
+
verbosity: VerbosityLevel.ERRORS
|
|
28953
|
+
});
|
|
28954
|
+
const pdfDoc = await loadingTask.promise;
|
|
28955
|
+
let fieldObjects;
|
|
28956
|
+
try {
|
|
28957
|
+
fieldObjects = await pdfDoc.getFieldObjects();
|
|
28958
|
+
} catch {
|
|
28959
|
+
pdfDoc.destroy();
|
|
28960
|
+
return null;
|
|
28961
|
+
}
|
|
28962
|
+
if (!fieldObjects || Object.keys(fieldObjects).length === 0) {
|
|
28963
|
+
pdfDoc.destroy();
|
|
28964
|
+
return null;
|
|
28965
|
+
}
|
|
28966
|
+
const properties = {};
|
|
28967
|
+
for (const [name, fields] of Object.entries(fieldObjects)) {
|
|
28968
|
+
const field = fields[0];
|
|
28969
|
+
if (!field.editable)
|
|
28970
|
+
continue;
|
|
28971
|
+
switch (field.type) {
|
|
28972
|
+
case "text":
|
|
28973
|
+
properties[name] = { type: "string", title: name };
|
|
28974
|
+
break;
|
|
28975
|
+
case "checkbox":
|
|
28976
|
+
properties[name] = { type: "boolean", title: name };
|
|
28977
|
+
break;
|
|
28978
|
+
case "radiobutton": {
|
|
28979
|
+
const options = fields.map((f2) => f2.exportValues).filter((v2) => !!v2 && v2 !== "Off");
|
|
28980
|
+
properties[name] = options.length > 0 ? { type: "string", title: name, enum: options } : { type: "string", title: name };
|
|
28981
|
+
break;
|
|
28982
|
+
}
|
|
28983
|
+
case "combobox":
|
|
28984
|
+
case "listbox": {
|
|
28985
|
+
const items = field.items?.map((i2) => i2.exportValue).filter(Boolean);
|
|
28986
|
+
properties[name] = items && items.length > 0 ? { type: "string", title: name, enum: items } : { type: "string", title: name };
|
|
28987
|
+
break;
|
|
28988
|
+
}
|
|
28989
|
+
}
|
|
28990
|
+
}
|
|
28991
|
+
const fieldLabels = new Map;
|
|
28992
|
+
try {
|
|
28993
|
+
for (let i2 = 1;i2 <= pdfDoc.numPages; i2++) {
|
|
28994
|
+
const page = await pdfDoc.getPage(i2);
|
|
28995
|
+
const annotations = await page.getAnnotations();
|
|
28996
|
+
for (const ann of annotations) {
|
|
28997
|
+
if (ann.fieldName && ann.alternativeText) {
|
|
28998
|
+
fieldLabels.set(ann.fieldName, ann.alternativeText);
|
|
28999
|
+
}
|
|
29000
|
+
}
|
|
29001
|
+
}
|
|
29002
|
+
} catch {}
|
|
29003
|
+
for (const [name, prop] of Object.entries(properties)) {
|
|
29004
|
+
const label = fieldLabels.get(name);
|
|
29005
|
+
if (label) {
|
|
29006
|
+
prop.title = label;
|
|
29007
|
+
}
|
|
29008
|
+
}
|
|
29009
|
+
const hasMechanicalNames = Object.keys(properties).some((name) => {
|
|
29010
|
+
if (fieldLabels.has(name))
|
|
29011
|
+
return false;
|
|
29012
|
+
return /[[\]().]/.test(name) || /^[A-Z0-9_]+$/.test(name);
|
|
29013
|
+
});
|
|
29014
|
+
pdfDoc.destroy();
|
|
29015
|
+
if (Object.keys(properties).length === 0)
|
|
29016
|
+
return null;
|
|
29017
|
+
if (hasMechanicalNames)
|
|
29018
|
+
return null;
|
|
29019
|
+
return { type: "object", properties };
|
|
29020
|
+
}
|
|
28721
29021
|
function createServer(options = {}) {
|
|
28722
|
-
const { useClientRoots = false } = options;
|
|
29022
|
+
const { enableInteract = false, useClientRoots = false } = options;
|
|
29023
|
+
const debug = options.debug ?? false;
|
|
29024
|
+
const disableInteract = !enableInteract;
|
|
28723
29025
|
const server = new McpServer({ name: "PDF Server", version: "2.0.0" });
|
|
28724
29026
|
if (useClientRoots) {
|
|
28725
29027
|
server.server.oninitialized = () => {
|
|
@@ -28728,23 +29030,60 @@ function createServer(options = {}) {
|
|
|
28728
29030
|
server.server.setNotificationHandler(RootsListChangedNotificationSchema, async () => {
|
|
28729
29031
|
await refreshRoots(server.server);
|
|
28730
29032
|
});
|
|
28731
|
-
} else {
|
|
28732
|
-
console.error("[pdf-server] Client roots are ignored (default for remote transports). " + "Pass --use-client-roots to allow the client to expose local directories.");
|
|
28733
29033
|
}
|
|
28734
29034
|
const { readPdfRange } = createPdfCache();
|
|
28735
29035
|
server.tool("list_pdfs", "List available PDFs that can be displayed", {}, async () => {
|
|
28736
|
-
const
|
|
28737
|
-
|
|
28738
|
-
|
|
28739
|
-
|
|
29036
|
+
const seen = new Set;
|
|
29037
|
+
const localFiles = [];
|
|
29038
|
+
const addLocal = (filePath) => {
|
|
29039
|
+
const url2 = pathToFileUrl(filePath);
|
|
29040
|
+
if (seen.has(url2))
|
|
29041
|
+
return;
|
|
29042
|
+
seen.add(url2);
|
|
29043
|
+
localFiles.push(url2);
|
|
29044
|
+
};
|
|
29045
|
+
for (const filePath of allowedLocalFiles)
|
|
29046
|
+
addLocal(filePath);
|
|
29047
|
+
const WALK_MAX_DEPTH = 8;
|
|
29048
|
+
const WALK_MAX_FILES = 500;
|
|
29049
|
+
let truncated = false;
|
|
29050
|
+
const walk = async (dir, depth) => {
|
|
29051
|
+
if (depth > WALK_MAX_DEPTH || localFiles.length >= WALK_MAX_FILES) {
|
|
29052
|
+
truncated ||= localFiles.length >= WALK_MAX_FILES;
|
|
29053
|
+
return;
|
|
29054
|
+
}
|
|
29055
|
+
let entries;
|
|
29056
|
+
try {
|
|
29057
|
+
entries = await fs.promises.readdir(dir, { withFileTypes: true });
|
|
29058
|
+
} catch {
|
|
29059
|
+
return;
|
|
29060
|
+
}
|
|
29061
|
+
for (const e2 of entries) {
|
|
29062
|
+
if (localFiles.length >= WALK_MAX_FILES) {
|
|
29063
|
+
truncated = true;
|
|
29064
|
+
return;
|
|
29065
|
+
}
|
|
29066
|
+
if (e2.name.startsWith(".") || e2.name === "node_modules")
|
|
29067
|
+
continue;
|
|
29068
|
+
const full = path.join(dir, e2.name);
|
|
29069
|
+
if (e2.isDirectory()) {
|
|
29070
|
+
await walk(full, depth + 1);
|
|
29071
|
+
} else if (e2.isFile() && /\.pdf$/i.test(e2.name)) {
|
|
29072
|
+
addLocal(full);
|
|
29073
|
+
}
|
|
29074
|
+
}
|
|
29075
|
+
};
|
|
29076
|
+
for (const dir of allowedLocalDirs)
|
|
29077
|
+
await walk(dir, 0);
|
|
28740
29078
|
const parts = [];
|
|
28741
|
-
if (
|
|
28742
|
-
|
|
28743
|
-
|
|
29079
|
+
if (localFiles.length > 0) {
|
|
29080
|
+
const header = truncated ? `Available PDFs (showing first ${WALK_MAX_FILES}):` : `Available PDFs:`;
|
|
29081
|
+
parts.push(`${header}
|
|
29082
|
+
${localFiles.map((u2) => `- ${u2}`).join(`
|
|
28744
29083
|
`)}`);
|
|
28745
29084
|
}
|
|
28746
29085
|
if (allowedLocalDirs.size > 0) {
|
|
28747
|
-
parts.push(`Allowed local directories
|
|
29086
|
+
parts.push(`Allowed local directories:
|
|
28748
29087
|
${[...allowedLocalDirs].map((d2) => `- ${d2}`).join(`
|
|
28749
29088
|
`)}
|
|
28750
29089
|
Any PDF file under these directories can be displayed.`);
|
|
@@ -28755,12 +29094,13 @@ Any PDF file under these directories can be displayed.`);
|
|
|
28755
29094
|
|
|
28756
29095
|
`) }],
|
|
28757
29096
|
structuredContent: {
|
|
28758
|
-
localFiles
|
|
28759
|
-
allowedDirectories: [...allowedLocalDirs]
|
|
29097
|
+
localFiles,
|
|
29098
|
+
allowedDirectories: [...allowedLocalDirs],
|
|
29099
|
+
truncated
|
|
28760
29100
|
}
|
|
28761
29101
|
};
|
|
28762
29102
|
});
|
|
28763
|
-
|
|
29103
|
+
hZ(server, "read_pdf_bytes", {
|
|
28764
29104
|
title: "Read PDF Bytes",
|
|
28765
29105
|
description: "Read a range of bytes from a PDF (max 512KB per request)",
|
|
28766
29106
|
inputSchema: {
|
|
@@ -28818,25 +29158,50 @@ Any PDF file under these directories can be displayed.`);
|
|
|
28818
29158
|
};
|
|
28819
29159
|
}
|
|
28820
29160
|
});
|
|
28821
|
-
|
|
29161
|
+
hZ(server, "display_pdf", {
|
|
28822
29162
|
title: "Display PDF",
|
|
28823
|
-
description: `
|
|
29163
|
+
description: disableInteract ? `Show and render a PDF in a read-only viewer.
|
|
29164
|
+
|
|
29165
|
+
Use this tool when the user wants to view or read a PDF. The renderer displays the document for viewing.
|
|
29166
|
+
|
|
29167
|
+
Accepts local files (use list_pdfs), client MCP root directories, or any HTTPS URL.` : `Open a PDF in an interactive viewer. Call this ONCE per PDF.
|
|
29168
|
+
|
|
29169
|
+
**All follow-up actions go through the \`interact\` tool** with the returned viewUUID — annotating, signing, stamping, filling forms, navigating, searching, extracting text/screenshots. Calling display_pdf again creates a SEPARATE viewer with a different viewUUID — interact calls using the new UUID will not reach the viewer the user already sees.
|
|
29170
|
+
|
|
29171
|
+
Returns a viewUUID in structuredContent. Pass it to \`interact\`:
|
|
29172
|
+
- add_annotations, update_annotations, remove_annotations, highlight_text
|
|
29173
|
+
- fill_form (fill PDF form fields)
|
|
29174
|
+
- navigate, search, find, search_navigate, zoom
|
|
29175
|
+
- get_text, get_screenshot (extract content)
|
|
28824
29176
|
|
|
28825
|
-
Accepts
|
|
28826
|
-
|
|
28827
|
-
- Local files under directories provided by the client as MCP roots
|
|
28828
|
-
- Any remote PDF accessible via HTTPS`,
|
|
29177
|
+
Accepts local files (use list_pdfs), client MCP root directories, or any HTTPS URL.
|
|
29178
|
+
Set \`elicit_form_inputs\` to true to prompt the user to fill form fields before display.`,
|
|
28829
29179
|
inputSchema: {
|
|
28830
29180
|
url: exports_external.string().default(DEFAULT_PDF).describe("PDF URL or local file path"),
|
|
28831
|
-
page: exports_external.number().min(1).default(1).describe("Initial page")
|
|
29181
|
+
page: exports_external.number().min(1).default(1).describe("Initial page"),
|
|
29182
|
+
...disableInteract ? {} : {
|
|
29183
|
+
elicit_form_inputs: exports_external.boolean().default(false).describe("If true and the PDF has form fields, prompt the user to fill them before displaying")
|
|
29184
|
+
}
|
|
28832
29185
|
},
|
|
28833
29186
|
outputSchema: exports_external.object({
|
|
29187
|
+
viewUUID: exports_external.string().describe("UUID for this viewer instance" + (disableInteract ? "" : " — pass to interact tool")),
|
|
28834
29188
|
url: exports_external.string(),
|
|
28835
29189
|
initialPage: exports_external.number(),
|
|
28836
|
-
totalBytes: exports_external.number()
|
|
29190
|
+
totalBytes: exports_external.number(),
|
|
29191
|
+
formFieldValues: exports_external.record(exports_external.string(), exports_external.union([exports_external.string(), exports_external.boolean()])).optional().describe("Form field values filled by the user via elicitation"),
|
|
29192
|
+
formFields: exports_external.array(exports_external.object({
|
|
29193
|
+
name: exports_external.string(),
|
|
29194
|
+
type: exports_external.string(),
|
|
29195
|
+
page: exports_external.number(),
|
|
29196
|
+
label: exports_external.string().optional(),
|
|
29197
|
+
x: exports_external.number(),
|
|
29198
|
+
y: exports_external.number(),
|
|
29199
|
+
width: exports_external.number(),
|
|
29200
|
+
height: exports_external.number()
|
|
29201
|
+
})).optional().describe("Form fields with bounding boxes in model coordinates (top-left origin)")
|
|
28837
29202
|
}),
|
|
28838
29203
|
_meta: { ui: { resourceUri: RESOURCE_URI } }
|
|
28839
|
-
}, async ({ url: url2, page }) => {
|
|
29204
|
+
}, async ({ url: url2, page, elicit_form_inputs }) => {
|
|
28840
29205
|
const normalized = isArxivUrl(url2) ? normalizeArxivUrl(url2) : url2;
|
|
28841
29206
|
const validation = validateUrl(normalized);
|
|
28842
29207
|
if (!validation.valid) {
|
|
@@ -28846,40 +29211,834 @@ Accepts:
|
|
|
28846
29211
|
};
|
|
28847
29212
|
}
|
|
28848
29213
|
const { totalBytes } = await readPdfRange(normalized, 0, 1);
|
|
29214
|
+
const uuid3 = randomUUID();
|
|
29215
|
+
if (!disableInteract)
|
|
29216
|
+
touchView(uuid3);
|
|
29217
|
+
let writable = false;
|
|
29218
|
+
let debugResolved;
|
|
29219
|
+
if (isFileUrl(normalized) || isLocalPath(normalized)) {
|
|
29220
|
+
const localPath = isFileUrl(normalized) ? fileUrlToPath(normalized) : decodeURIComponent(normalized);
|
|
29221
|
+
const resolved = path.resolve(localPath);
|
|
29222
|
+
debugResolved = resolved;
|
|
29223
|
+
if (isWritablePath(resolved)) {
|
|
29224
|
+
try {
|
|
29225
|
+
await fs.promises.access(resolved, fs.constants.W_OK);
|
|
29226
|
+
writable = true;
|
|
29227
|
+
} catch {}
|
|
29228
|
+
}
|
|
29229
|
+
if (!disableInteract) {
|
|
29230
|
+
startFileWatch(uuid3, localPath);
|
|
29231
|
+
}
|
|
29232
|
+
}
|
|
29233
|
+
let formSchema = null;
|
|
29234
|
+
try {
|
|
29235
|
+
formSchema = await extractFormSchema(normalized, readPdfRange);
|
|
29236
|
+
} catch {}
|
|
29237
|
+
if (formSchema) {
|
|
29238
|
+
viewFieldNames.set(uuid3, new Set(Object.keys(formSchema.properties)));
|
|
29239
|
+
}
|
|
29240
|
+
let fieldInfo = [];
|
|
29241
|
+
try {
|
|
29242
|
+
fieldInfo = await extractFormFieldInfo(normalized, readPdfRange);
|
|
29243
|
+
if (fieldInfo.length > 0) {
|
|
29244
|
+
viewFieldInfo.set(uuid3, fieldInfo);
|
|
29245
|
+
if (!viewFieldNames.has(uuid3)) {
|
|
29246
|
+
viewFieldNames.set(uuid3, new Set(fieldInfo.map((f2) => f2.name).filter(Boolean)));
|
|
29247
|
+
}
|
|
29248
|
+
}
|
|
29249
|
+
} catch {}
|
|
29250
|
+
let formFieldValues;
|
|
29251
|
+
let elicitResult;
|
|
29252
|
+
if (elicit_form_inputs && formSchema) {
|
|
29253
|
+
const clientCaps = server.server.getClientCapabilities();
|
|
29254
|
+
if (clientCaps?.elicitation?.form) {
|
|
29255
|
+
try {
|
|
29256
|
+
elicitResult = await server.server.elicitInput({
|
|
29257
|
+
message: `Please fill in the PDF form fields for "${normalized.split("/").pop() || normalized}":`,
|
|
29258
|
+
requestedSchema: formSchema
|
|
29259
|
+
});
|
|
29260
|
+
if (elicitResult.action === "accept" && elicitResult.content) {
|
|
29261
|
+
formFieldValues = {};
|
|
29262
|
+
for (const [k2, v2] of Object.entries(elicitResult.content)) {
|
|
29263
|
+
if (typeof v2 === "string" || typeof v2 === "boolean") {
|
|
29264
|
+
formFieldValues[k2] = v2;
|
|
29265
|
+
}
|
|
29266
|
+
}
|
|
29267
|
+
enqueueCommand(uuid3, {
|
|
29268
|
+
type: "fill_form",
|
|
29269
|
+
fields: Object.entries(formFieldValues).map(([name, value]) => ({ name, value }))
|
|
29270
|
+
});
|
|
29271
|
+
}
|
|
29272
|
+
} catch (err) {
|
|
29273
|
+
console.error("[pdf-server] Form elicitation failed:", err);
|
|
29274
|
+
}
|
|
29275
|
+
}
|
|
29276
|
+
}
|
|
29277
|
+
const contentParts = [
|
|
29278
|
+
{
|
|
29279
|
+
type: "text",
|
|
29280
|
+
text: disableInteract ? `Displaying PDF: ${normalized}` : `PDF opened. viewUUID: ${uuid3}
|
|
29281
|
+
|
|
29282
|
+
→ To annotate, sign, stamp, fill forms, navigate, or extract: call \`interact\` with this viewUUID.
|
|
29283
|
+
→ DO NOT call display_pdf again — that spawns a separate viewer with a different viewUUID; your interact calls would target the new empty one, not the one the user is looking at.
|
|
29284
|
+
|
|
29285
|
+
URL: ${normalized}`
|
|
29286
|
+
}
|
|
29287
|
+
];
|
|
29288
|
+
if (formFieldValues && Object.keys(formFieldValues).length > 0) {
|
|
29289
|
+
const fieldSummary = Object.entries(formFieldValues).map(([name, value]) => ` ${name}: ${typeof value === "boolean" ? value ? "checked" : "unchecked" : value}`).join(`
|
|
29290
|
+
`);
|
|
29291
|
+
contentParts.push({
|
|
29292
|
+
type: "text",
|
|
29293
|
+
text: `
|
|
29294
|
+
User-provided form field values:
|
|
29295
|
+
${fieldSummary}`
|
|
29296
|
+
});
|
|
29297
|
+
} else if (elicit_form_inputs && elicitResult && elicitResult.action !== "accept") {
|
|
29298
|
+
contentParts.push({
|
|
29299
|
+
type: "text",
|
|
29300
|
+
text: `
|
|
29301
|
+
Form elicitation was ${elicitResult.action}d by the user.`
|
|
29302
|
+
});
|
|
29303
|
+
}
|
|
29304
|
+
if (fieldInfo.length > 0) {
|
|
29305
|
+
const byPage = new Map;
|
|
29306
|
+
for (const f2 of fieldInfo) {
|
|
29307
|
+
let list = byPage.get(f2.page);
|
|
29308
|
+
if (!list) {
|
|
29309
|
+
list = [];
|
|
29310
|
+
byPage.set(f2.page, list);
|
|
29311
|
+
}
|
|
29312
|
+
list.push(f2);
|
|
29313
|
+
}
|
|
29314
|
+
const lines = [
|
|
29315
|
+
`
|
|
29316
|
+
Form fields (${fieldInfo.length})${disableInteract ? "" : " — use fill_form with {name, value}"}:`
|
|
29317
|
+
];
|
|
29318
|
+
for (const [pg, fields] of [...byPage.entries()].sort((a2, b) => a2[0] - b[0])) {
|
|
29319
|
+
lines.push(` Page ${pg}:`);
|
|
29320
|
+
for (const f2 of fields) {
|
|
29321
|
+
const label = f2.label ? ` "${f2.label}"` : "";
|
|
29322
|
+
const nameStr = f2.name || "(unnamed)";
|
|
29323
|
+
lines.push(` ${nameStr}${label} [${f2.type}] at (${f2.x},${f2.y}) ${f2.width}×${f2.height}`);
|
|
29324
|
+
}
|
|
29325
|
+
}
|
|
29326
|
+
contentParts.push({ type: "text", text: lines.join(`
|
|
29327
|
+
`) });
|
|
29328
|
+
} else {
|
|
29329
|
+
const fieldNames = viewFieldNames.get(uuid3);
|
|
29330
|
+
if (fieldNames && fieldNames.size > 0) {
|
|
29331
|
+
contentParts.push({
|
|
29332
|
+
type: "text",
|
|
29333
|
+
text: `
|
|
29334
|
+
Form fields${disableInteract ? "" : " available for fill_form"}: ${[...fieldNames].join(", ")}`
|
|
29335
|
+
});
|
|
29336
|
+
}
|
|
29337
|
+
}
|
|
28849
29338
|
return {
|
|
28850
|
-
content:
|
|
29339
|
+
content: contentParts,
|
|
28851
29340
|
structuredContent: {
|
|
29341
|
+
viewUUID: uuid3,
|
|
28852
29342
|
url: normalized,
|
|
28853
29343
|
initialPage: page,
|
|
28854
|
-
totalBytes
|
|
29344
|
+
totalBytes,
|
|
29345
|
+
...formFieldValues ? { formFieldValues } : {},
|
|
29346
|
+
...fieldInfo.length > 0 ? { formFields: fieldInfo } : {}
|
|
28855
29347
|
},
|
|
28856
29348
|
_meta: {
|
|
28857
|
-
viewUUID:
|
|
29349
|
+
viewUUID: uuid3,
|
|
29350
|
+
interactEnabled: !disableInteract,
|
|
29351
|
+
writable,
|
|
29352
|
+
...debug ? {
|
|
29353
|
+
_debug: {
|
|
29354
|
+
resolved: debugResolved,
|
|
29355
|
+
writable,
|
|
29356
|
+
isWritablePath: debugResolved ? isWritablePath(debugResolved) : undefined,
|
|
29357
|
+
cliLocalFiles: [...cliLocalFiles],
|
|
29358
|
+
allowedLocalFiles: [...allowedLocalFiles],
|
|
29359
|
+
allowedLocalDirs: [...allowedLocalDirs]
|
|
29360
|
+
}
|
|
29361
|
+
} : {}
|
|
29362
|
+
}
|
|
29363
|
+
};
|
|
29364
|
+
});
|
|
29365
|
+
if (!disableInteract) {
|
|
29366
|
+
let detectImageDimensions = function(bytes) {
|
|
29367
|
+
if (bytes[0] === 137 && bytes[1] === 80 && bytes[2] === 78 && bytes[3] === 71) {
|
|
29368
|
+
if (bytes.length >= 24) {
|
|
29369
|
+
const width = bytes.readUInt32BE(16);
|
|
29370
|
+
const height = bytes.readUInt32BE(20);
|
|
29371
|
+
return { width, height };
|
|
29372
|
+
}
|
|
29373
|
+
}
|
|
29374
|
+
if (bytes[0] === 255 && bytes[1] === 216) {
|
|
29375
|
+
let offset = 2;
|
|
29376
|
+
while (offset < bytes.length - 8) {
|
|
29377
|
+
if (bytes[offset] !== 255)
|
|
29378
|
+
break;
|
|
29379
|
+
const marker = bytes[offset + 1];
|
|
29380
|
+
if (marker === 192 || marker === 194) {
|
|
29381
|
+
const height = bytes.readUInt16BE(offset + 5);
|
|
29382
|
+
const width = bytes.readUInt16BE(offset + 7);
|
|
29383
|
+
return { width, height };
|
|
29384
|
+
}
|
|
29385
|
+
const segLen = bytes.readUInt16BE(offset + 2);
|
|
29386
|
+
offset += 2 + segLen;
|
|
29387
|
+
}
|
|
28858
29388
|
}
|
|
29389
|
+
return null;
|
|
28859
29390
|
};
|
|
29391
|
+
const InteractCommandSchema = exports_external.object({
|
|
29392
|
+
action: exports_external.enum([
|
|
29393
|
+
"navigate",
|
|
29394
|
+
"search",
|
|
29395
|
+
"find",
|
|
29396
|
+
"search_navigate",
|
|
29397
|
+
"zoom",
|
|
29398
|
+
"add_annotations",
|
|
29399
|
+
"update_annotations",
|
|
29400
|
+
"remove_annotations",
|
|
29401
|
+
"highlight_text",
|
|
29402
|
+
"fill_form",
|
|
29403
|
+
"get_text",
|
|
29404
|
+
"get_screenshot"
|
|
29405
|
+
]).describe("Action to perform"),
|
|
29406
|
+
page: exports_external.number().min(1).optional().describe("Page number (for navigate, highlight_text, get_screenshot, get_text)"),
|
|
29407
|
+
query: exports_external.string().optional().describe("Search text (for search / find / highlight_text)"),
|
|
29408
|
+
matchIndex: exports_external.number().min(0).optional().describe("Match index (for search_navigate)"),
|
|
29409
|
+
scale: exports_external.number().min(0.5).max(3).optional().describe("Zoom scale, 1.0 = 100% (for zoom)"),
|
|
29410
|
+
annotations: exports_external.array(exports_external.record(exports_external.string(), exports_external.any())).optional().describe("Annotation objects (see types in description). Each needs: id, type, page. For update_annotations only id+type are required."),
|
|
29411
|
+
ids: exports_external.array(exports_external.string()).optional().describe("Annotation IDs (for remove_annotations)"),
|
|
29412
|
+
color: exports_external.string().optional().describe("Color override (for highlight_text)"),
|
|
29413
|
+
content: exports_external.string().optional().describe("Tooltip/note content (for highlight_text)"),
|
|
29414
|
+
fields: exports_external.array(FormField).optional().describe("Form fields to fill (for fill_form): { name, value } where value is string or boolean"),
|
|
29415
|
+
intervals: exports_external.array(PageInterval).optional().describe("Page ranges for get_text. Each has optional start/end. [{start:1,end:5}], [{}] = all pages. Max 20 pages.")
|
|
29416
|
+
});
|
|
29417
|
+
async function resolveImageAnnotation(ann) {
|
|
29418
|
+
if (!ann.imageData && ann.imageUrl) {
|
|
29419
|
+
const url2 = String(ann.imageUrl);
|
|
29420
|
+
const check2 = validateUrl(url2);
|
|
29421
|
+
if (!check2.valid) {
|
|
29422
|
+
throw new Error(`imageUrl rejected by validateUrl: ${check2.error ?? url2}`);
|
|
29423
|
+
}
|
|
29424
|
+
let imgBytes;
|
|
29425
|
+
if (url2.startsWith("https://")) {
|
|
29426
|
+
const resp = await fetch(url2);
|
|
29427
|
+
if (!resp.ok)
|
|
29428
|
+
throw new Error(`HTTP ${resp.status} for ${url2}`);
|
|
29429
|
+
imgBytes = new Uint8Array(await resp.arrayBuffer());
|
|
29430
|
+
} else {
|
|
29431
|
+
const filePath = isFileUrl(url2) ? fileUrlToPath(url2) : decodeURIComponent(url2);
|
|
29432
|
+
imgBytes = await fs.promises.readFile(path.resolve(filePath));
|
|
29433
|
+
}
|
|
29434
|
+
ann.imageData = Buffer.from(imgBytes).toString("base64");
|
|
29435
|
+
}
|
|
29436
|
+
if (ann.imageData && !ann.mimeType) {
|
|
29437
|
+
const bytes = Buffer.from(ann.imageData, "base64");
|
|
29438
|
+
if (bytes[0] === 137 && bytes[1] === 80 && bytes[2] === 78 && bytes[3] === 71) {
|
|
29439
|
+
ann.mimeType = "image/png";
|
|
29440
|
+
} else {
|
|
29441
|
+
ann.mimeType = "image/jpeg";
|
|
29442
|
+
}
|
|
29443
|
+
}
|
|
29444
|
+
if (ann.imageData && (ann.width == null || ann.height == null)) {
|
|
29445
|
+
const dims = detectImageDimensions(Buffer.from(ann.imageData, "base64"));
|
|
29446
|
+
if (dims) {
|
|
29447
|
+
const maxWidth = 200;
|
|
29448
|
+
const aspectRatio = dims.height / dims.width;
|
|
29449
|
+
ann.width = ann.width ?? Math.min(dims.width, maxWidth);
|
|
29450
|
+
ann.height = ann.height ?? ann.width * aspectRatio;
|
|
29451
|
+
} else {
|
|
29452
|
+
ann.width = ann.width ?? 200;
|
|
29453
|
+
ann.height = ann.height ?? 200;
|
|
29454
|
+
}
|
|
29455
|
+
}
|
|
29456
|
+
ann.x = ann.x ?? 72;
|
|
29457
|
+
ann.y = ann.y ?? 72;
|
|
29458
|
+
}
|
|
29459
|
+
async function processInteractCommand(uuid3, cmd, signal) {
|
|
29460
|
+
const {
|
|
29461
|
+
action,
|
|
29462
|
+
page,
|
|
29463
|
+
query,
|
|
29464
|
+
matchIndex,
|
|
29465
|
+
scale,
|
|
29466
|
+
annotations,
|
|
29467
|
+
ids,
|
|
29468
|
+
color,
|
|
29469
|
+
content,
|
|
29470
|
+
fields,
|
|
29471
|
+
intervals
|
|
29472
|
+
} = cmd;
|
|
29473
|
+
let description;
|
|
29474
|
+
switch (action) {
|
|
29475
|
+
case "navigate":
|
|
29476
|
+
if (page == null)
|
|
29477
|
+
return {
|
|
29478
|
+
content: [{ type: "text", text: "navigate requires `page`" }],
|
|
29479
|
+
isError: true
|
|
29480
|
+
};
|
|
29481
|
+
enqueueCommand(uuid3, { type: "navigate", page });
|
|
29482
|
+
description = `navigate to page ${page}`;
|
|
29483
|
+
break;
|
|
29484
|
+
case "search":
|
|
29485
|
+
if (!query)
|
|
29486
|
+
return {
|
|
29487
|
+
content: [{ type: "text", text: "search requires `query`" }],
|
|
29488
|
+
isError: true
|
|
29489
|
+
};
|
|
29490
|
+
enqueueCommand(uuid3, { type: "search", query });
|
|
29491
|
+
description = `search for "${query}"`;
|
|
29492
|
+
break;
|
|
29493
|
+
case "find":
|
|
29494
|
+
if (!query)
|
|
29495
|
+
return {
|
|
29496
|
+
content: [{ type: "text", text: "find requires `query`" }],
|
|
29497
|
+
isError: true
|
|
29498
|
+
};
|
|
29499
|
+
enqueueCommand(uuid3, { type: "find", query });
|
|
29500
|
+
description = `find "${query}" (silent)`;
|
|
29501
|
+
break;
|
|
29502
|
+
case "search_navigate":
|
|
29503
|
+
if (matchIndex == null)
|
|
29504
|
+
return {
|
|
29505
|
+
content: [
|
|
29506
|
+
{
|
|
29507
|
+
type: "text",
|
|
29508
|
+
text: "search_navigate requires `matchIndex`"
|
|
29509
|
+
}
|
|
29510
|
+
],
|
|
29511
|
+
isError: true
|
|
29512
|
+
};
|
|
29513
|
+
enqueueCommand(uuid3, { type: "search_navigate", matchIndex });
|
|
29514
|
+
description = `go to match #${matchIndex}`;
|
|
29515
|
+
break;
|
|
29516
|
+
case "zoom":
|
|
29517
|
+
if (scale == null)
|
|
29518
|
+
return {
|
|
29519
|
+
content: [{ type: "text", text: "zoom requires `scale`" }],
|
|
29520
|
+
isError: true
|
|
29521
|
+
};
|
|
29522
|
+
enqueueCommand(uuid3, { type: "zoom", scale });
|
|
29523
|
+
description = `zoom to ${Math.round(scale * 100)}%`;
|
|
29524
|
+
break;
|
|
29525
|
+
case "add_annotations":
|
|
29526
|
+
if (!annotations || annotations.length === 0)
|
|
29527
|
+
return {
|
|
29528
|
+
content: [
|
|
29529
|
+
{
|
|
29530
|
+
type: "text",
|
|
29531
|
+
text: "add_annotations requires `annotations` array"
|
|
29532
|
+
}
|
|
29533
|
+
],
|
|
29534
|
+
isError: true
|
|
29535
|
+
};
|
|
29536
|
+
try {
|
|
29537
|
+
for (const ann of annotations) {
|
|
29538
|
+
if (ann.type === "image") {
|
|
29539
|
+
await resolveImageAnnotation(ann);
|
|
29540
|
+
}
|
|
29541
|
+
}
|
|
29542
|
+
} catch (err) {
|
|
29543
|
+
return {
|
|
29544
|
+
content: [
|
|
29545
|
+
{
|
|
29546
|
+
type: "text",
|
|
29547
|
+
text: `add_annotations: ${err instanceof Error ? err.message : String(err)}`
|
|
29548
|
+
}
|
|
29549
|
+
],
|
|
29550
|
+
isError: true
|
|
29551
|
+
};
|
|
29552
|
+
}
|
|
29553
|
+
enqueueCommand(uuid3, {
|
|
29554
|
+
type: "add_annotations",
|
|
29555
|
+
annotations
|
|
29556
|
+
});
|
|
29557
|
+
description = `add ${annotations.length} annotation(s)`;
|
|
29558
|
+
break;
|
|
29559
|
+
case "update_annotations":
|
|
29560
|
+
if (!annotations || annotations.length === 0)
|
|
29561
|
+
return {
|
|
29562
|
+
content: [
|
|
29563
|
+
{
|
|
29564
|
+
type: "text",
|
|
29565
|
+
text: "update_annotations requires `annotations` array"
|
|
29566
|
+
}
|
|
29567
|
+
],
|
|
29568
|
+
isError: true
|
|
29569
|
+
};
|
|
29570
|
+
enqueueCommand(uuid3, {
|
|
29571
|
+
type: "update_annotations",
|
|
29572
|
+
annotations
|
|
29573
|
+
});
|
|
29574
|
+
description = `update ${annotations.length} annotation(s)`;
|
|
29575
|
+
break;
|
|
29576
|
+
case "remove_annotations":
|
|
29577
|
+
if (!ids || ids.length === 0)
|
|
29578
|
+
return {
|
|
29579
|
+
content: [
|
|
29580
|
+
{
|
|
29581
|
+
type: "text",
|
|
29582
|
+
text: "remove_annotations requires `ids` array"
|
|
29583
|
+
}
|
|
29584
|
+
],
|
|
29585
|
+
isError: true
|
|
29586
|
+
};
|
|
29587
|
+
enqueueCommand(uuid3, { type: "remove_annotations", ids });
|
|
29588
|
+
description = `remove ${ids.length} annotation(s)`;
|
|
29589
|
+
break;
|
|
29590
|
+
case "highlight_text": {
|
|
29591
|
+
if (!query)
|
|
29592
|
+
return {
|
|
29593
|
+
content: [
|
|
29594
|
+
{ type: "text", text: "highlight_text requires `query`" }
|
|
29595
|
+
],
|
|
29596
|
+
isError: true
|
|
29597
|
+
};
|
|
29598
|
+
const id = `ht_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
29599
|
+
enqueueCommand(uuid3, {
|
|
29600
|
+
type: "highlight_text",
|
|
29601
|
+
id,
|
|
29602
|
+
query,
|
|
29603
|
+
page,
|
|
29604
|
+
color,
|
|
29605
|
+
content
|
|
29606
|
+
});
|
|
29607
|
+
description = `highlight text "${query}"${page ? ` on page ${page}` : ""}`;
|
|
29608
|
+
break;
|
|
29609
|
+
}
|
|
29610
|
+
case "fill_form": {
|
|
29611
|
+
if (!fields || fields.length === 0)
|
|
29612
|
+
return {
|
|
29613
|
+
content: [
|
|
29614
|
+
{ type: "text", text: "fill_form requires `fields` array" }
|
|
29615
|
+
],
|
|
29616
|
+
isError: true
|
|
29617
|
+
};
|
|
29618
|
+
const knownFields = viewFieldNames.get(uuid3);
|
|
29619
|
+
const validFields = [];
|
|
29620
|
+
const unknownNames = [];
|
|
29621
|
+
for (const f2 of fields) {
|
|
29622
|
+
if (knownFields && !knownFields.has(f2.name)) {
|
|
29623
|
+
unknownNames.push(f2.name);
|
|
29624
|
+
} else {
|
|
29625
|
+
validFields.push(f2);
|
|
29626
|
+
}
|
|
29627
|
+
}
|
|
29628
|
+
if (validFields.length > 0) {
|
|
29629
|
+
enqueueCommand(uuid3, { type: "fill_form", fields: validFields });
|
|
29630
|
+
}
|
|
29631
|
+
const parts = [];
|
|
29632
|
+
if (validFields.length > 0) {
|
|
29633
|
+
parts.push(`Filled ${validFields.length} field(s): ${validFields.map((f2) => f2.name).join(", ")}`);
|
|
29634
|
+
}
|
|
29635
|
+
if (unknownNames.length > 0) {
|
|
29636
|
+
parts.push(`Unknown field(s) skipped: ${unknownNames.join(", ")}`);
|
|
29637
|
+
if (knownFields && knownFields.size > 0) {
|
|
29638
|
+
parts.push(`Valid field names: ${[...knownFields].join(", ")}`);
|
|
29639
|
+
}
|
|
29640
|
+
}
|
|
29641
|
+
description = parts.join(". ");
|
|
29642
|
+
if (unknownNames.length > 0 && validFields.length === 0) {
|
|
29643
|
+
return {
|
|
29644
|
+
content: [{ type: "text", text: description }],
|
|
29645
|
+
isError: true
|
|
29646
|
+
};
|
|
29647
|
+
}
|
|
29648
|
+
break;
|
|
29649
|
+
}
|
|
29650
|
+
case "get_text": {
|
|
29651
|
+
const resolvedIntervals = intervals ?? (page ? [{ start: page, end: page }] : [{}]);
|
|
29652
|
+
const requestId = randomUUID();
|
|
29653
|
+
enqueueCommand(uuid3, {
|
|
29654
|
+
type: "get_pages",
|
|
29655
|
+
requestId,
|
|
29656
|
+
intervals: resolvedIntervals,
|
|
29657
|
+
getText: true,
|
|
29658
|
+
getScreenshots: false
|
|
29659
|
+
});
|
|
29660
|
+
let pageData;
|
|
29661
|
+
try {
|
|
29662
|
+
pageData = await waitForPageData(requestId, signal);
|
|
29663
|
+
} catch (err) {
|
|
29664
|
+
return {
|
|
29665
|
+
content: [
|
|
29666
|
+
{
|
|
29667
|
+
type: "text",
|
|
29668
|
+
text: `Error: ${err instanceof Error ? err.message : String(err)}`
|
|
29669
|
+
}
|
|
29670
|
+
],
|
|
29671
|
+
isError: true
|
|
29672
|
+
};
|
|
29673
|
+
}
|
|
29674
|
+
const textParts = [];
|
|
29675
|
+
for (const entry of pageData) {
|
|
29676
|
+
if (entry.text != null) {
|
|
29677
|
+
textParts.push({
|
|
29678
|
+
type: "text",
|
|
29679
|
+
text: `--- Page ${entry.page} ---
|
|
29680
|
+
${entry.text}`
|
|
29681
|
+
});
|
|
29682
|
+
}
|
|
29683
|
+
}
|
|
29684
|
+
if (textParts.length === 0) {
|
|
29685
|
+
textParts.push({ type: "text", text: "No text content returned" });
|
|
29686
|
+
}
|
|
29687
|
+
return { content: textParts };
|
|
29688
|
+
}
|
|
29689
|
+
case "get_screenshot": {
|
|
29690
|
+
if (page == null)
|
|
29691
|
+
return {
|
|
29692
|
+
content: [
|
|
29693
|
+
{ type: "text", text: "get_screenshot requires `page`" }
|
|
29694
|
+
],
|
|
29695
|
+
isError: true
|
|
29696
|
+
};
|
|
29697
|
+
const requestId = randomUUID();
|
|
29698
|
+
enqueueCommand(uuid3, {
|
|
29699
|
+
type: "get_pages",
|
|
29700
|
+
requestId,
|
|
29701
|
+
intervals: [{ start: page, end: page }],
|
|
29702
|
+
getText: false,
|
|
29703
|
+
getScreenshots: true
|
|
29704
|
+
});
|
|
29705
|
+
let pageData;
|
|
29706
|
+
try {
|
|
29707
|
+
pageData = await waitForPageData(requestId, signal);
|
|
29708
|
+
} catch (err) {
|
|
29709
|
+
return {
|
|
29710
|
+
content: [
|
|
29711
|
+
{
|
|
29712
|
+
type: "text",
|
|
29713
|
+
text: `Error: ${err instanceof Error ? err.message : String(err)}`
|
|
29714
|
+
}
|
|
29715
|
+
],
|
|
29716
|
+
isError: true
|
|
29717
|
+
};
|
|
29718
|
+
}
|
|
29719
|
+
const entry = pageData[0];
|
|
29720
|
+
if (entry?.image) {
|
|
29721
|
+
return {
|
|
29722
|
+
content: [
|
|
29723
|
+
{
|
|
29724
|
+
type: "image",
|
|
29725
|
+
data: entry.image,
|
|
29726
|
+
mimeType: "image/jpeg"
|
|
29727
|
+
}
|
|
29728
|
+
]
|
|
29729
|
+
};
|
|
29730
|
+
}
|
|
29731
|
+
return {
|
|
29732
|
+
content: [{ type: "text", text: "No screenshot returned" }],
|
|
29733
|
+
isError: true
|
|
29734
|
+
};
|
|
29735
|
+
}
|
|
29736
|
+
default:
|
|
29737
|
+
return {
|
|
29738
|
+
content: [{ type: "text", text: `Unknown action: ${action}` }],
|
|
29739
|
+
isError: true
|
|
29740
|
+
};
|
|
29741
|
+
}
|
|
29742
|
+
return {
|
|
29743
|
+
content: [{ type: "text", text: `Queued: ${description}` }]
|
|
29744
|
+
};
|
|
29745
|
+
}
|
|
29746
|
+
server.registerTool("interact", {
|
|
29747
|
+
title: "Interact with PDF",
|
|
29748
|
+
description: `Interact with a PDF viewer: annotate, navigate, search, extract text/screenshots, fill forms.
|
|
29749
|
+
IMPORTANT: viewUUID must be the exact UUID returned by display_pdf (e.g. "a1b2c3d4-..."). Do NOT use arbitrary strings.
|
|
29750
|
+
|
|
29751
|
+
**BATCHING**: Send multiple commands in one call via \`commands\` array. Commands run sequentially. TIP: End with \`get_screenshot\` to verify your changes.
|
|
29752
|
+
|
|
29753
|
+
**ANNOTATION** — add_annotations with array of annotation objects. Each needs: id (unique string), type, page (1-indexed).
|
|
29754
|
+
|
|
29755
|
+
**COORDINATE SYSTEM**: PDF points (1pt = 1/72in), origin at page TOP-LEFT corner. X increases rightward, Y increases downward.
|
|
29756
|
+
- US Letter = 612×792pt. Margins: top≈y=50, bottom≈y=742, left≈x=72, right≈x=540, center≈(306, 396).
|
|
29757
|
+
- Rectangle/circle/stamp x,y is the TOP-LEFT corner. To place a 200×30 box at the TOP of the page: x=72, y=50, width=200, height=30.
|
|
29758
|
+
- For highlights/underlines, each rect's y is the TOP of the highlighted region.
|
|
29759
|
+
|
|
29760
|
+
Annotation types:
|
|
29761
|
+
• highlight: rects:[{x,y,width,height}], color?, content? • underline: rects:[{x,y,w,h}], color?
|
|
29762
|
+
• strikethrough: rects:[{x,y,w,h}], color? • note: x, y, content, color?
|
|
29763
|
+
• rectangle: x, y, width, height, color?, fillColor?, rotation? • circle: x, y, width, height, color?, fillColor?
|
|
29764
|
+
• line: x1, y1, x2, y2, color? • freetext: x, y, content, fontSize?, color?
|
|
29765
|
+
• stamp: x, y, label (any text, e.g. APPROVED, DRAFT, CONFIDENTIAL), color?, rotation?
|
|
29766
|
+
• image: imageUrl (required), x?, y?, width?, height?, mimeType?, rotation?, aspect? — places an image (signature, logo, etc.) on the page. Pass a local file path or HTTPS URL (NO data: URIs, NO base64). Width/height auto-detected if omitted. Users can also drag & drop images directly onto the viewer.
|
|
29767
|
+
|
|
29768
|
+
TIP: For text annotations, prefer highlight_text (auto-finds text) over manual rects.
|
|
29769
|
+
|
|
29770
|
+
Example — add a signature image and a stamp, then screenshot to verify:
|
|
29771
|
+
\`\`\`json
|
|
29772
|
+
{"viewUUID":"…","commands":[
|
|
29773
|
+
{"action":"add_annotations","annotations":[
|
|
29774
|
+
{"id":"sig1","type":"image","page":1,"x":72,"y":700,"imageUrl":"/path/to/signature.png"},
|
|
29775
|
+
{"id":"s1","type":"stamp","page":1,"x":300,"y":400,"label":"APPROVED"}
|
|
29776
|
+
]},
|
|
29777
|
+
{"action":"get_screenshot","page":1}
|
|
29778
|
+
]}
|
|
29779
|
+
\`\`\`
|
|
29780
|
+
|
|
29781
|
+
• highlight_text: auto-find and highlight text (query, page?, color?, content?)
|
|
29782
|
+
• update_annotations: partial update (id+type required) • remove_annotations: remove by ids
|
|
29783
|
+
|
|
29784
|
+
**NAVIGATION**: navigate (page), search (query), find (query, silent), search_navigate (matchIndex), zoom (scale 0.5–3.0)
|
|
29785
|
+
|
|
29786
|
+
**TEXT/SCREENSHOTS**:
|
|
29787
|
+
• get_text: extract text from pages. Optional \`page\` for single page, or \`intervals\` for ranges [{start?,end?}]. Max 20 pages.
|
|
29788
|
+
• get_screenshot: capture a single page as PNG image. Requires \`page\`.
|
|
29789
|
+
|
|
29790
|
+
**FORMS** — fill_form: fill fields with \`fields\` array of {name, value}.`,
|
|
29791
|
+
inputSchema: {
|
|
29792
|
+
viewUUID: exports_external.string().describe("The viewUUID of the PDF viewer (from display_pdf result)"),
|
|
29793
|
+
action: exports_external.enum([
|
|
29794
|
+
"navigate",
|
|
29795
|
+
"search",
|
|
29796
|
+
"find",
|
|
29797
|
+
"search_navigate",
|
|
29798
|
+
"zoom",
|
|
29799
|
+
"add_annotations",
|
|
29800
|
+
"update_annotations",
|
|
29801
|
+
"remove_annotations",
|
|
29802
|
+
"highlight_text",
|
|
29803
|
+
"fill_form",
|
|
29804
|
+
"get_text",
|
|
29805
|
+
"get_screenshot"
|
|
29806
|
+
]).optional().describe("Action to perform (for single command). Use `commands` array for batching."),
|
|
29807
|
+
page: exports_external.number().min(1).optional().describe("Page number (for navigate, highlight_text, get_screenshot, get_text)"),
|
|
29808
|
+
query: exports_external.string().optional().describe("Search text (for search / find / highlight_text)"),
|
|
29809
|
+
matchIndex: exports_external.number().min(0).optional().describe("Match index (for search_navigate)"),
|
|
29810
|
+
scale: exports_external.number().min(0.5).max(3).optional().describe("Zoom scale, 1.0 = 100% (for zoom)"),
|
|
29811
|
+
annotations: exports_external.array(exports_external.record(exports_external.string(), exports_external.any())).optional().describe("Annotation objects (see types in description). Each needs: id, type, page. For update_annotations only id+type are required."),
|
|
29812
|
+
ids: exports_external.array(exports_external.string()).optional().describe("Annotation IDs (for remove_annotations)"),
|
|
29813
|
+
color: exports_external.string().optional().describe("Color override (for highlight_text)"),
|
|
29814
|
+
content: exports_external.string().optional().describe("Tooltip/note content (for highlight_text)"),
|
|
29815
|
+
fields: exports_external.array(FormField).optional().describe("Form fields to fill (for fill_form): { name, value } where value is string or boolean"),
|
|
29816
|
+
intervals: exports_external.array(PageInterval).optional().describe("Page ranges for get_text. Each has optional start/end. [{start:1,end:5}], [{}] = all pages. Max 20 pages."),
|
|
29817
|
+
commands: exports_external.array(InteractCommandSchema).optional().describe("Array of commands to execute sequentially. More efficient than separate calls. Tip: end with get_pages+getScreenshots to verify changes.")
|
|
29818
|
+
}
|
|
29819
|
+
}, async ({
|
|
29820
|
+
viewUUID: uuid3,
|
|
29821
|
+
action,
|
|
29822
|
+
page,
|
|
29823
|
+
query,
|
|
29824
|
+
matchIndex,
|
|
29825
|
+
scale,
|
|
29826
|
+
annotations,
|
|
29827
|
+
ids,
|
|
29828
|
+
color,
|
|
29829
|
+
content,
|
|
29830
|
+
fields,
|
|
29831
|
+
intervals,
|
|
29832
|
+
commands
|
|
29833
|
+
}, extra) => {
|
|
29834
|
+
const commandList = commands ? commands : action ? [
|
|
29835
|
+
{
|
|
29836
|
+
action,
|
|
29837
|
+
page,
|
|
29838
|
+
query,
|
|
29839
|
+
matchIndex,
|
|
29840
|
+
scale,
|
|
29841
|
+
annotations,
|
|
29842
|
+
ids,
|
|
29843
|
+
color,
|
|
29844
|
+
content,
|
|
29845
|
+
fields,
|
|
29846
|
+
intervals
|
|
29847
|
+
}
|
|
29848
|
+
] : [];
|
|
29849
|
+
if (commandList.length === 0) {
|
|
29850
|
+
return {
|
|
29851
|
+
content: [
|
|
29852
|
+
{
|
|
29853
|
+
type: "text",
|
|
29854
|
+
text: "No action or commands specified. Provide either `action` (single command) or `commands` (batch)."
|
|
29855
|
+
}
|
|
29856
|
+
],
|
|
29857
|
+
isError: true
|
|
29858
|
+
};
|
|
29859
|
+
}
|
|
29860
|
+
const allContent = [];
|
|
29861
|
+
let hasError = false;
|
|
29862
|
+
for (let i2 = 0;i2 < commandList.length; i2++) {
|
|
29863
|
+
const result = await processInteractCommand(uuid3, commandList[i2], extra.signal);
|
|
29864
|
+
if (result.isError) {
|
|
29865
|
+
hasError = true;
|
|
29866
|
+
}
|
|
29867
|
+
allContent.push(...result.content);
|
|
29868
|
+
if (hasError)
|
|
29869
|
+
break;
|
|
29870
|
+
}
|
|
29871
|
+
return {
|
|
29872
|
+
content: allContent,
|
|
29873
|
+
...hasError ? { isError: true } : {}
|
|
29874
|
+
};
|
|
29875
|
+
});
|
|
29876
|
+
hZ(server, "submit_page_data", {
|
|
29877
|
+
title: "Submit Page Data",
|
|
29878
|
+
description: "Submit rendered page data for a get_pages request (used by viewer)",
|
|
29879
|
+
inputSchema: {
|
|
29880
|
+
requestId: exports_external.string().describe("The request ID from the get_pages command"),
|
|
29881
|
+
pages: exports_external.array(exports_external.object({
|
|
29882
|
+
page: exports_external.number(),
|
|
29883
|
+
text: exports_external.string().optional(),
|
|
29884
|
+
image: exports_external.string().optional().describe("Base64 PNG image data")
|
|
29885
|
+
})).describe("Page data entries")
|
|
29886
|
+
},
|
|
29887
|
+
_meta: { ui: { visibility: ["app"] } }
|
|
29888
|
+
}, async ({ requestId, pages }) => {
|
|
29889
|
+
const settle = pendingPageRequests.get(requestId);
|
|
29890
|
+
if (settle) {
|
|
29891
|
+
settle(pages);
|
|
29892
|
+
return {
|
|
29893
|
+
content: [
|
|
29894
|
+
{ type: "text", text: `Submitted ${pages.length} page(s)` }
|
|
29895
|
+
]
|
|
29896
|
+
};
|
|
29897
|
+
}
|
|
29898
|
+
return {
|
|
29899
|
+
content: [
|
|
29900
|
+
{ type: "text", text: `No pending request for ${requestId}` }
|
|
29901
|
+
],
|
|
29902
|
+
isError: true
|
|
29903
|
+
};
|
|
29904
|
+
});
|
|
29905
|
+
hZ(server, "poll_pdf_commands", {
|
|
29906
|
+
title: "Poll PDF Commands",
|
|
29907
|
+
description: "Poll for pending commands for a PDF viewer",
|
|
29908
|
+
inputSchema: {
|
|
29909
|
+
viewUUID: exports_external.string().describe("The viewUUID of the PDF viewer")
|
|
29910
|
+
},
|
|
29911
|
+
_meta: { ui: { visibility: ["app"] } }
|
|
29912
|
+
}, async ({ viewUUID: uuid3 }) => {
|
|
29913
|
+
if (commandQueues.has(uuid3)) {
|
|
29914
|
+
await new Promise((r) => setTimeout(r, POLL_BATCH_WAIT_MS));
|
|
29915
|
+
} else {
|
|
29916
|
+
await new Promise((resolve) => {
|
|
29917
|
+
const timer = setTimeout(() => {
|
|
29918
|
+
pollWaiters.delete(uuid3);
|
|
29919
|
+
resolve();
|
|
29920
|
+
}, LONG_POLL_TIMEOUT_MS);
|
|
29921
|
+
const prev = pollWaiters.get(uuid3);
|
|
29922
|
+
if (prev)
|
|
29923
|
+
prev();
|
|
29924
|
+
pollWaiters.set(uuid3, () => {
|
|
29925
|
+
clearTimeout(timer);
|
|
29926
|
+
resolve();
|
|
29927
|
+
});
|
|
29928
|
+
});
|
|
29929
|
+
if (commandQueues.has(uuid3)) {
|
|
29930
|
+
await new Promise((r) => setTimeout(r, POLL_BATCH_WAIT_MS));
|
|
29931
|
+
}
|
|
29932
|
+
}
|
|
29933
|
+
const commands = dequeueCommands(uuid3);
|
|
29934
|
+
return {
|
|
29935
|
+
content: [{ type: "text", text: `${commands.length} command(s)` }],
|
|
29936
|
+
structuredContent: { commands }
|
|
29937
|
+
};
|
|
29938
|
+
});
|
|
29939
|
+
}
|
|
29940
|
+
hZ(server, "save_pdf", {
|
|
29941
|
+
title: "Save PDF",
|
|
29942
|
+
description: "Save annotated PDF bytes back to a local file",
|
|
29943
|
+
inputSchema: {
|
|
29944
|
+
url: exports_external.string().describe("Original PDF URL or local file path"),
|
|
29945
|
+
data: exports_external.string().describe("Base64-encoded PDF bytes")
|
|
29946
|
+
},
|
|
29947
|
+
outputSchema: exports_external.object({
|
|
29948
|
+
filePath: exports_external.string(),
|
|
29949
|
+
mtimeMs: exports_external.number()
|
|
29950
|
+
}),
|
|
29951
|
+
_meta: { ui: { visibility: ["app"] } }
|
|
29952
|
+
}, async ({ url: url2, data }) => {
|
|
29953
|
+
const validation = validateUrl(url2);
|
|
29954
|
+
if (!validation.valid) {
|
|
29955
|
+
return {
|
|
29956
|
+
content: [{ type: "text", text: validation.error }],
|
|
29957
|
+
isError: true
|
|
29958
|
+
};
|
|
29959
|
+
}
|
|
29960
|
+
const filePath = isFileUrl(url2) ? fileUrlToPath(url2) : isLocalPath(url2) ? decodeURIComponent(url2) : null;
|
|
29961
|
+
if (!filePath) {
|
|
29962
|
+
return {
|
|
29963
|
+
content: [
|
|
29964
|
+
{ type: "text", text: "Save is only supported for local files" }
|
|
29965
|
+
],
|
|
29966
|
+
isError: true
|
|
29967
|
+
};
|
|
29968
|
+
}
|
|
29969
|
+
const resolved = path.resolve(filePath);
|
|
29970
|
+
if (!isWritablePath(resolved)) {
|
|
29971
|
+
return {
|
|
29972
|
+
content: [
|
|
29973
|
+
{
|
|
29974
|
+
type: "text",
|
|
29975
|
+
text: "Save refused: file is not under a mounted directory root " + "and was not passed as a CLI argument. MCP file roots are " + "read-only (typically uploaded copies the client doesn't " + "expect to change)."
|
|
29976
|
+
}
|
|
29977
|
+
],
|
|
29978
|
+
isError: true
|
|
29979
|
+
};
|
|
29980
|
+
}
|
|
29981
|
+
try {
|
|
29982
|
+
const bytes = Buffer.from(data, "base64");
|
|
29983
|
+
await fs.promises.writeFile(resolved, bytes);
|
|
29984
|
+
const { mtimeMs } = await fs.promises.stat(resolved);
|
|
29985
|
+
return {
|
|
29986
|
+
content: [{ type: "text", text: `Saved to ${filePath}` }],
|
|
29987
|
+
structuredContent: { filePath: resolved, mtimeMs }
|
|
29988
|
+
};
|
|
29989
|
+
} catch (err) {
|
|
29990
|
+
return {
|
|
29991
|
+
content: [
|
|
29992
|
+
{
|
|
29993
|
+
type: "text",
|
|
29994
|
+
text: `Failed to save: ${err instanceof Error ? err.message : String(err)}`
|
|
29995
|
+
}
|
|
29996
|
+
],
|
|
29997
|
+
isError: true
|
|
29998
|
+
};
|
|
29999
|
+
}
|
|
28860
30000
|
});
|
|
28861
|
-
|
|
30001
|
+
mZ(server, RESOURCE_URI, RESOURCE_URI, { mimeType: d }, async () => {
|
|
28862
30002
|
const html = await fs.promises.readFile(path.join(DIST_DIR, "mcp-app.html"), "utf-8");
|
|
28863
30003
|
return {
|
|
28864
30004
|
contents: [
|
|
28865
|
-
{
|
|
30005
|
+
{
|
|
30006
|
+
uri: RESOURCE_URI,
|
|
30007
|
+
mimeType: d,
|
|
30008
|
+
text: html,
|
|
30009
|
+
_meta: {
|
|
30010
|
+
ui: {
|
|
30011
|
+
permissions: { clipboardWrite: {} },
|
|
30012
|
+
csp: {
|
|
30013
|
+
connectDomains: [STANDARD_FONT_ORIGIN],
|
|
30014
|
+
resourceDomains: [STANDARD_FONT_ORIGIN]
|
|
30015
|
+
}
|
|
30016
|
+
}
|
|
30017
|
+
}
|
|
30018
|
+
}
|
|
28866
30019
|
]
|
|
28867
30020
|
};
|
|
28868
30021
|
});
|
|
28869
30022
|
return server;
|
|
28870
30023
|
}
|
|
28871
30024
|
export {
|
|
30025
|
+
writeFlags,
|
|
28872
30026
|
validateUrl,
|
|
30027
|
+
stopFileWatch,
|
|
30028
|
+
startFileWatch,
|
|
28873
30029
|
pathToFileUrl,
|
|
28874
30030
|
normalizeArxivUrl,
|
|
30031
|
+
isWritablePath,
|
|
28875
30032
|
isFileUrl,
|
|
28876
30033
|
isArxivUrl,
|
|
28877
30034
|
isAncestorDir,
|
|
28878
30035
|
fileUrlToPath,
|
|
28879
30036
|
createServer,
|
|
28880
30037
|
createPdfCache,
|
|
30038
|
+
cliLocalFiles,
|
|
28881
30039
|
allowedLocalFiles,
|
|
28882
30040
|
allowedLocalDirs,
|
|
30041
|
+
STANDARD_FONT_DATA_URL,
|
|
28883
30042
|
RESOURCE_URI,
|
|
28884
30043
|
MAX_CHUNK_BYTES,
|
|
28885
30044
|
DEFAULT_PDF,
|