@shuji-bonji/pdf-reader-mcp 0.11.2 → 0.12.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/CHANGELOG.md +130 -0
- package/README.ja.md +90 -5
- package/README.md +91 -5
- package/dist/constants.d.ts +20 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +20 -0
- package/dist/constants.js.map +1 -1
- package/dist/index.js +12 -1
- package/dist/index.js.map +1 -1
- package/dist/schemas/tier1.d.ts +53 -1
- package/dist/schemas/tier1.d.ts.map +1 -1
- package/dist/schemas/tier1.js +80 -2
- package/dist/schemas/tier1.js.map +1 -1
- package/dist/services/content-stream-service.d.ts +34 -1
- package/dist/services/content-stream-service.d.ts.map +1 -1
- package/dist/services/content-stream-service.js +92 -9
- package/dist/services/content-stream-service.js.map +1 -1
- package/dist/services/image-encoder.d.ts +33 -0
- package/dist/services/image-encoder.d.ts.map +1 -0
- package/dist/services/image-encoder.js +435 -0
- package/dist/services/image-encoder.js.map +1 -0
- package/dist/services/image-resampler.d.ts +36 -0
- package/dist/services/image-resampler.d.ts.map +1 -0
- package/dist/services/image-resampler.js +78 -0
- package/dist/services/image-resampler.js.map +1 -0
- package/dist/services/page-renderer.d.ts +71 -0
- package/dist/services/page-renderer.d.ts.map +1 -0
- package/dist/services/page-renderer.js +147 -0
- package/dist/services/page-renderer.js.map +1 -0
- package/dist/services/pdfjs-service.d.ts +24 -3
- package/dist/services/pdfjs-service.d.ts.map +1 -1
- package/dist/services/pdfjs-service.js +119 -24
- package/dist/services/pdfjs-service.js.map +1 -1
- package/dist/services/pdflib-service.d.ts +4 -0
- package/dist/services/pdflib-service.d.ts.map +1 -1
- package/dist/services/pdflib-service.js +6 -1
- package/dist/services/pdflib-service.js.map +1 -1
- package/dist/services/text-extractability-service.d.ts +44 -0
- package/dist/services/text-extractability-service.d.ts.map +1 -0
- package/dist/services/text-extractability-service.js +302 -0
- package/dist/services/text-extractability-service.js.map +1 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +2 -0
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/tier1/read-images.d.ts +1 -1
- package/dist/tools/tier1/read-images.d.ts.map +1 -1
- package/dist/tools/tier1/read-images.js +73 -43
- package/dist/tools/tier1/read-images.js.map +1 -1
- package/dist/tools/tier1/read-text.d.ts.map +1 -1
- package/dist/tools/tier1/read-text.js +22 -7
- package/dist/tools/tier1/read-text.js.map +1 -1
- package/dist/tools/tier1/read-url.d.ts.map +1 -1
- package/dist/tools/tier1/read-url.js +24 -6
- package/dist/tools/tier1/read-url.js.map +1 -1
- package/dist/tools/tier1/render-page.d.ts +6 -0
- package/dist/tools/tier1/render-page.d.ts.map +1 -0
- package/dist/tools/tier1/render-page.js +93 -0
- package/dist/tools/tier1/render-page.js.map +1 -0
- package/dist/tools/tier1/search-text.d.ts.map +1 -1
- package/dist/tools/tier1/search-text.js +17 -2
- package/dist/tools/tier1/search-text.js.map +1 -1
- package/dist/tools/tier1/summarize.d.ts.map +1 -1
- package/dist/tools/tier1/summarize.js +73 -2
- package/dist/tools/tier1/summarize.js.map +1 -1
- package/dist/tools/tier2/extract-structured-text.d.ts.map +1 -1
- package/dist/tools/tier2/extract-structured-text.js +19 -6
- package/dist/tools/tier2/extract-structured-text.js.map +1 -1
- package/dist/types.d.ts +96 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/formatter.d.ts.map +1 -1
- package/dist/utils/formatter.js +36 -1
- package/dist/utils/formatter.js.map +1 -1
- package/package.json +4 -1
|
@@ -1,28 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* read_images - Extract images from PDF pages.
|
|
2
|
+
* read_images - Extract images from PDF pages as PNG or JPEG (#22).
|
|
3
3
|
*/
|
|
4
|
+
import { MAX_IMAGE_RESPONSE_BYTES } from '../../constants.js';
|
|
4
5
|
import { ReadImagesSchema } from '../../schemas/tier1.js';
|
|
5
6
|
import { extractImages } from '../../services/pdfjs-service.js';
|
|
6
7
|
import { handleStructuredError } from '../../utils/error-handler.js';
|
|
7
8
|
export function registerReadImages(server) {
|
|
8
9
|
server.registerTool('read_images', {
|
|
9
10
|
title: 'Read PDF Images',
|
|
10
|
-
description: `Extract images from a PDF document as
|
|
11
|
+
description: `Extract embedded images from a PDF document as PNG or JPEG files.
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
Each image is returned as an MCP image content block, so a vision-capable model can look at it directly. A text block lists the metadata for all of them (page, index, size in the file, size returned, colour space, encoded bytes).
|
|
14
|
+
|
|
15
|
+
These are the image XObjects the page draws, not a picture of the page. A page whose content is vector drawing, or whose text is what you want to see, is not covered by this tool.
|
|
16
|
+
|
|
17
|
+
Response size is bounded: at most ${(MAX_IMAGE_RESPONSE_BYTES / (1024 * 1024)).toFixed(0)} MB of encoded image data per call. Images beyond the budget are named in the text block with the reason and are not returned — nothing is dropped silently. A 200 dpi A4 scan is ~11.6 MB of pixels on its own, so pass \`pages\`, \`max_width\` or \`max_height\` when working with scans.
|
|
13
18
|
|
|
14
19
|
Args:
|
|
15
20
|
- file_path (string): Absolute path to a local PDF file
|
|
16
|
-
- pages (string, optional): Page range. Format: "1-5", "3", or "1,3,5-7".
|
|
21
|
+
- pages (string, optional): Page range. Format: "1-5", "3", or "1,3,5-7". **Omitting it means ALL pages** — on an image-heavy document that exhausts the byte budget immediately. Name the pages you need.
|
|
22
|
+
- format ('png' | 'jpeg', optional): Output encoding. Default 'png' (lossless). 'jpeg' is smaller and composites alpha over white.
|
|
23
|
+
- quality (1-100, optional): JPEG quality. Ignored for PNG.
|
|
24
|
+
- max_width (number, optional): Downscale images wider than this (area average). Never enlarges.
|
|
25
|
+
- max_height (number, optional): Downscale images taller than this.
|
|
17
26
|
|
|
18
27
|
Returns:
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
Note: Large images may produce very large responses. Use the pages parameter to limit scope.
|
|
28
|
+
A text block with the metadata table and any omissions, then one image content block per returned image.
|
|
22
29
|
|
|
23
30
|
Examples:
|
|
24
31
|
- Extract all images: { file_path: "/path/to/doc.pdf" }
|
|
25
|
-
-
|
|
32
|
+
- A scanned page, small enough to look at: { file_path: "/path/to/scan.pdf", pages: "1", max_width: 1200, format: "jpeg" }`,
|
|
26
33
|
inputSchema: ReadImagesSchema,
|
|
27
34
|
annotations: {
|
|
28
35
|
readOnlyHint: true,
|
|
@@ -32,47 +39,70 @@ Examples:
|
|
|
32
39
|
},
|
|
33
40
|
}, async (params) => {
|
|
34
41
|
try {
|
|
35
|
-
const result = await extractImages(params.file_path, params.pages
|
|
42
|
+
const result = await extractImages(params.file_path, params.pages, {
|
|
43
|
+
format: params.format,
|
|
44
|
+
quality: params.quality,
|
|
45
|
+
maxWidth: params.max_width,
|
|
46
|
+
maxHeight: params.max_height,
|
|
47
|
+
});
|
|
36
48
|
if (result.extractedCount === 0) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
const lines = [];
|
|
50
|
+
if (result.detectedCount === 0) {
|
|
51
|
+
lines.push('No images found in the specified pages.');
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
lines.push(`${result.detectedCount} image(s) detected in the PDF.`);
|
|
55
|
+
if (result.skippedCount > 0) {
|
|
56
|
+
// Don't guess at a cause. The previous wording blamed "an
|
|
57
|
+
// encoding format that is not directly accessible", which was
|
|
58
|
+
// reported even for ordinary images pdfjs decodes perfectly —
|
|
59
|
+
// the real cause was a bug in this server, and the confident
|
|
60
|
+
// explanation made it look like a property of the file.
|
|
61
|
+
lines.push(`${result.skippedCount} could not be decoded. This can happen with image types ` +
|
|
62
|
+
'pdf.js does not decode outside a rendering context (for example some masks ' +
|
|
63
|
+
'and JPX/JBIG2 streams).');
|
|
64
|
+
}
|
|
65
|
+
for (const omitted of result.omitted) {
|
|
66
|
+
lines.push(`- page ${omitted.page}, image ${omitted.index} ` +
|
|
67
|
+
`(${omitted.sourceWidth}×${omitted.sourceHeight}): ${omitted.reason}`);
|
|
68
|
+
}
|
|
54
69
|
}
|
|
55
|
-
return {
|
|
56
|
-
|
|
57
|
-
|
|
70
|
+
return { content: [{ type: 'text', text: lines.join('\n') }] };
|
|
71
|
+
}
|
|
72
|
+
const lines = [
|
|
73
|
+
`Returning ${result.extractedCount} image(s), ` +
|
|
74
|
+
`${result.totalEncodedBytes.toLocaleString()} bytes encoded.`,
|
|
75
|
+
'',
|
|
76
|
+
'| Page | Index | In file | Returned | Colour space | Bits | Type | Bytes |',
|
|
77
|
+
'|---|---|---|---|---|---|---|---|',
|
|
78
|
+
];
|
|
79
|
+
for (const image of result.images) {
|
|
80
|
+
const returnedSize = image.downscaled
|
|
81
|
+
? `${image.width}×${image.height} (downscaled)`
|
|
82
|
+
: `${image.width}×${image.height}`;
|
|
83
|
+
lines.push(`| ${image.page} | ${image.index} | ${image.sourceWidth}×${image.sourceHeight} | ` +
|
|
84
|
+
`${returnedSize} | ${image.colorSpace} | ${image.bitsPerComponent} | ` +
|
|
85
|
+
`${image.mimeType} | ${image.encodedBytes.toLocaleString()} |`);
|
|
58
86
|
}
|
|
59
|
-
// Build response with full image data including base64
|
|
60
|
-
const imagesWithData = result.images.map((img) => ({
|
|
61
|
-
page: img.page,
|
|
62
|
-
index: img.index,
|
|
63
|
-
width: img.width,
|
|
64
|
-
height: img.height,
|
|
65
|
-
colorSpace: img.colorSpace,
|
|
66
|
-
bitsPerComponent: img.bitsPerComponent,
|
|
67
|
-
dataBase64: img.dataBase64,
|
|
68
|
-
}));
|
|
69
|
-
let text = `Found ${result.extractedCount} image(s).`;
|
|
70
87
|
if (result.skippedCount > 0) {
|
|
71
|
-
|
|
88
|
+
lines.push('', `${result.skippedCount} further image(s) could not be decoded by pdf.js.`);
|
|
89
|
+
}
|
|
90
|
+
if (result.omitted.length > 0) {
|
|
91
|
+
lines.push('', '## Not returned', '');
|
|
92
|
+
for (const omitted of result.omitted) {
|
|
93
|
+
lines.push(`- page ${omitted.page}, image ${omitted.index} ` +
|
|
94
|
+
`(${omitted.sourceWidth}×${omitted.sourceHeight}): ${omitted.reason}`);
|
|
95
|
+
}
|
|
72
96
|
}
|
|
73
|
-
text += `\n\n${JSON.stringify(imagesWithData, null, 2)}`;
|
|
74
97
|
return {
|
|
75
|
-
content: [
|
|
98
|
+
content: [
|
|
99
|
+
{ type: 'text', text: lines.join('\n') },
|
|
100
|
+
...result.images.map((image) => ({
|
|
101
|
+
type: 'image',
|
|
102
|
+
data: image.dataBase64,
|
|
103
|
+
mimeType: image.mimeType,
|
|
104
|
+
})),
|
|
105
|
+
],
|
|
76
106
|
};
|
|
77
107
|
}
|
|
78
108
|
catch (error) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read-images.js","sourceRoot":"","sources":["../../../src/tools/tier1/read-images.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAwB,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAChF,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAErE,MAAM,UAAU,kBAAkB,CAAC,MAAiB;IAClD,MAAM,CAAC,YAAY,CACjB,aAAa,EACb;QACE,KAAK,EAAE,iBAAiB;QACxB,WAAW,EAAE;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"read-images.js","sourceRoot":"","sources":["../../../src/tools/tier1/read-images.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EAAwB,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAChF,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAA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AC;IACH,CAAC,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read-text.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/read-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;
|
|
1
|
+
{"version":3,"file":"read-text.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/read-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAYzE,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAkFxD"}
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import { ResponseFormat } from '../../constants.js';
|
|
5
5
|
import { ReadTextSchema } from '../../schemas/tier1.js';
|
|
6
6
|
import { extractText } from '../../services/pdfjs-service.js';
|
|
7
|
+
import { byPage, observeExtractability, summarizeExtractability, } from '../../services/text-extractability-service.js';
|
|
7
8
|
import { handleStructuredError } from '../../utils/error-handler.js';
|
|
8
9
|
import { formatPageTextsMarkdown, truncateIfNeeded } from '../../utils/formatter.js';
|
|
9
10
|
export function registerReadText(server) {
|
|
@@ -23,13 +24,15 @@ For Japanese form-style PDFs (帳票・様式) where U+3000 fullwidth spaces are
|
|
|
23
24
|
|
|
24
25
|
Args:
|
|
25
26
|
- file_path (string): Absolute path to a local PDF file
|
|
26
|
-
- pages (string, optional): Page range to extract. Format: "1-5", "3", or "1,3,5-7".
|
|
27
|
+
- pages (string, optional): Page range to extract. Format: "1-5", "3", or "1,3,5-7". **Omitting it means ALL pages** — on a large document that consumes the whole response budget in one call. Past ~50 pages, search_text first and pass an explicit range here.
|
|
27
28
|
- response_format ('markdown' | 'json'): Output format (default: 'markdown')
|
|
28
29
|
- split_columns (1 | 2 | 3, optional): Column-aware reordering for untagged multi-column PDFs. Default 1 = existing Y-sort.
|
|
29
30
|
- compact_whitespace (boolean, optional): Collapse whitespace runs (incl. U+3000) to one ASCII space and trim each line. Default false.
|
|
30
31
|
|
|
32
|
+
Every page also reports how much of its text could be converted to Unicode (ISO 32000-2 §9.10.1): \`extracted\`, \`no_text_layer\` (no text-showing operator, image content present — reading it needs OCR or a rendered page), \`not_extractable\` (a font used here offers no route to Unicode, so text is missing or wrong), or \`not_observed\` (encrypted or unreadable content stream). An empty result is therefore never by itself evidence that a page has no text.
|
|
33
|
+
|
|
31
34
|
Returns:
|
|
32
|
-
Extracted text organized by page number. With \`split_columns >= 2\`, columns are separated by a blank line so a downstream LLM can tell them apart.
|
|
35
|
+
Extracted text organized by page number, preceded by the extractability tally. With \`split_columns >= 2\`, columns are separated by a blank line so a downstream LLM can tell them apart.
|
|
33
36
|
|
|
34
37
|
Examples:
|
|
35
38
|
- Extract all text: { file_path: "/path/to/doc.pdf" }
|
|
@@ -44,16 +47,28 @@ Examples:
|
|
|
44
47
|
},
|
|
45
48
|
}, async (params) => {
|
|
46
49
|
try {
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
const [extracted, observations] = await Promise.all([
|
|
51
|
+
extractText(params.file_path, params.pages, {
|
|
52
|
+
splitColumns: params.split_columns,
|
|
53
|
+
compactWhitespace: params.compact_whitespace,
|
|
54
|
+
}),
|
|
55
|
+
// #21: an empty (or shortened) result is not evidence that the page
|
|
56
|
+
// has no text. The state says which of the three §9.10.1 conditions
|
|
57
|
+
// produced what is above it.
|
|
58
|
+
observeExtractability(params.file_path, params.pages),
|
|
59
|
+
]);
|
|
60
|
+
const observed = byPage(observations);
|
|
61
|
+
const pages = extracted.map((page) => ({
|
|
62
|
+
...page,
|
|
63
|
+
...(observed.has(page.page) ? { extractability: observed.get(page.page) } : {}),
|
|
64
|
+
}));
|
|
51
65
|
let text;
|
|
52
66
|
if (params.response_format === ResponseFormat.JSON) {
|
|
53
67
|
text = JSON.stringify(pages, null, 2);
|
|
54
68
|
}
|
|
55
69
|
else {
|
|
56
|
-
|
|
70
|
+
const banner = summarizeExtractability(observations);
|
|
71
|
+
text = [...banner, '', formatPageTextsMarkdown(pages)].join('\n');
|
|
57
72
|
}
|
|
58
73
|
const { text: finalText } = truncateIfNeeded(text);
|
|
59
74
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read-text.js","sourceRoot":"","sources":["../../../src/tools/tier1/read-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAsB,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC5E,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAErF,MAAM,UAAU,gBAAgB,CAAC,MAAiB;IAChD,MAAM,CAAC,YAAY,CACjB,WAAW,EACX;QACE,KAAK,EAAE,eAAe;QACtB,WAAW,EAAE
|
|
1
|
+
{"version":3,"file":"read-text.js","sourceRoot":"","sources":["../../../src/tools/tier1/read-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAsB,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC5E,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EACL,MAAM,EACN,qBAAqB,EACrB,uBAAuB,GACxB,MAAM,+CAA+C,CAAC;AACvD,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAErF,MAAM,UAAU,gBAAgB,CAAC,MAAiB;IAChD,MAAM,CAAC,YAAY,CACjB,WAAW,EACX;QACE,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ACN,MAAM,MAAM,GAAG,uBAAuB,CAAC,YAAY,CAAC,CAAC;gBACrD,IAAI,GAAG,CAAC,GAAG,MAAM,EAAE,EAAE,EAAE,uBAAuB,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACpE,CAAC;YAED,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;YAEnD,OAAO;gBACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;aACtD,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,GAAG,GAAG,qBAAqB,CAAC,KAAK,CAAC,CAAC;YACzC,OAAO;gBACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;gBACxE,OAAO,EAAE,IAAI;aACd,CAAC;QACJ,CAAC;IACH,CAAC,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read-url.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/read-url.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;
|
|
1
|
+
{"version":3,"file":"read-url.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/read-url.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAazE,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAqFvD"}
|
|
@@ -4,16 +4,19 @@
|
|
|
4
4
|
import { ResponseFormat } from '../../constants.js';
|
|
5
5
|
import { ReadUrlSchema } from '../../schemas/tier1.js';
|
|
6
6
|
import { extractTextFromDoc, loadDocumentFromData } from '../../services/pdfjs-service.js';
|
|
7
|
+
import { byPage, observeExtractabilityFromData, summarizeExtractability, } from '../../services/text-extractability-service.js';
|
|
7
8
|
import { fetchPdfFromUrl } from '../../services/url-fetcher.js';
|
|
8
9
|
import { handleStructuredError } from '../../utils/error-handler.js';
|
|
9
10
|
import { formatPageTextsMarkdown, truncateIfNeeded } from '../../utils/formatter.js';
|
|
10
11
|
export function registerReadUrl(server) {
|
|
11
12
|
server.registerTool('read_url', {
|
|
12
13
|
title: 'Read PDF from URL',
|
|
13
|
-
description: `Fetch a PDF from a URL and extract its text content.
|
|
14
|
+
description: `Fetch a PDF from a URL and extract its text content. Text is ALL this tool returns — see the scope note below.
|
|
14
15
|
|
|
15
16
|
Downloads the PDF from the specified URL, then extracts text with Y-coordinate-based reading order. Supports HTTP and HTTPS. Maximum file size: 50MB. Timeout: 30 seconds.
|
|
16
17
|
|
|
18
|
+
**Scope (#25):** the fetched bytes are discarded after extraction; this tool deliberately does not save them. Every other tool of this server takes a \`file_path\`, so to use search_text, inspect_structure, extract_tables, render_page or anything else on a URL's PDF, download the file to local disk FIRST (with whatever fetch capability the calling environment has) and pass its path. This keeps every tool of this server read-only with respect to the file system — writing files is not a reader's job. read_url exists for the one-shot case: "what does the document at this URL say?"
|
|
19
|
+
|
|
17
20
|
Like \`read_text\`, accepts \`split_columns: 2 | 3\` for **untagged** multi-column PDFs and \`compact_whitespace: true\` to collapse U+3000 / ASCII whitespace runs. Tagged PDFs should use \`extract_tables\` instead.
|
|
18
21
|
|
|
19
22
|
Args:
|
|
@@ -42,16 +45,31 @@ Examples:
|
|
|
42
45
|
const data = await fetchPdfFromUrl(params.url);
|
|
43
46
|
const doc = await loadDocumentFromData(data);
|
|
44
47
|
try {
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
const [extracted, observations] = await Promise.all([
|
|
49
|
+
extractTextFromDoc(doc, params.pages, {
|
|
50
|
+
splitColumns: params.split_columns,
|
|
51
|
+
compactWhitespace: params.compact_whitespace,
|
|
52
|
+
}),
|
|
53
|
+
// #21: the same three-state answer as read_text — the fetched bytes
|
|
54
|
+
// are already here, so there is no reason for this path to be the
|
|
55
|
+
// one that still returns a bare empty string.
|
|
56
|
+
observeExtractabilityFromData(data, params.pages),
|
|
57
|
+
]);
|
|
58
|
+
const observed = byPage(observations);
|
|
59
|
+
const results = extracted.map((page) => ({
|
|
60
|
+
...page,
|
|
61
|
+
...(observed.has(page.page) ? { extractability: observed.get(page.page) } : {}),
|
|
62
|
+
}));
|
|
49
63
|
let text;
|
|
50
64
|
if (params.response_format === ResponseFormat.JSON) {
|
|
51
65
|
text = JSON.stringify(results, null, 2);
|
|
52
66
|
}
|
|
53
67
|
else {
|
|
54
|
-
text =
|
|
68
|
+
text = [
|
|
69
|
+
...summarizeExtractability(observations),
|
|
70
|
+
'',
|
|
71
|
+
formatPageTextsMarkdown(results),
|
|
72
|
+
].join('\n');
|
|
55
73
|
}
|
|
56
74
|
const { text: finalText } = truncateIfNeeded(text);
|
|
57
75
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read-url.js","sourceRoot":"","sources":["../../../src/tools/tier1/read-url.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAqB,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC1E,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAC3F,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAChE,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAErF,MAAM,UAAU,eAAe,CAAC,MAAiB;IAC/C,MAAM,CAAC,YAAY,CACjB,UAAU,EACV;QACE,KA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K,EAAE,CAAC;YACf,MAAM,GAAG,GAAG,qBAAqB,CAAC,KAAK,CAAC,CAAC;YACzC,OAAO;gBACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;gBACxE,OAAO,EAAE,IAAI;aACd,CAAC;QACJ,CAAC;IACH,CAAC,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render-page.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/render-page.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAWzE,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAgG1D"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* render_page - Rasterise PDF pages to PNG or JPEG (#23).
|
|
3
|
+
*/
|
|
4
|
+
import { MAX_IMAGE_RESPONSE_BYTES } from '../../constants.js';
|
|
5
|
+
import { RenderPageSchema } from '../../schemas/tier1.js';
|
|
6
|
+
import { DEFAULT_RENDER_DPI, RENDERER_MISSING_MESSAGE, rendererAvailable, renderPages, } from '../../services/page-renderer.js';
|
|
7
|
+
import { handleStructuredError } from '../../utils/error-handler.js';
|
|
8
|
+
export function registerRenderPage(server) {
|
|
9
|
+
server.registerTool('render_page', {
|
|
10
|
+
title: 'Render PDF Page',
|
|
11
|
+
description: `Rasterise pages of a PDF to PNG or JPEG images, returned as MCP image content blocks.
|
|
12
|
+
|
|
13
|
+
This is the tool for documents whose text cannot be read as text: pages \`read_text\` reports as \`no_text_layer\` or \`not_extractable\`, vector drawings, forms, handwriting, stamps. It draws the PAGE — everything on it — where \`read_images\` only extracts the image XObjects a page happens to embed.
|
|
14
|
+
|
|
15
|
+
Rendering uses PDFium compiled to WebAssembly (optional dependency \`@hyzyla/pdfium\`). Note this is a different engine from the pdf.js this server reads text with; where their behaviour on a damaged file differs, neither output is evidence about the other. If the dependency is not installed, this tool says so and every other tool works normally.
|
|
16
|
+
|
|
17
|
+
\`pages\` is required — rendering is the most expensive operation here, and "all pages" of a large scan should be a decision, not a default. The response carries at most ${(MAX_IMAGE_RESPONSE_BYTES / (1024 * 1024)).toFixed(0)} MB of encoded images; pages past the budget are named with the reason, not dropped.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
- file_path (string): Absolute path to a local PDF file
|
|
21
|
+
- pages (string, REQUIRED): Page range: "1-3", "5", "1,3,5-7".
|
|
22
|
+
- dpi (36-600, optional): Density, default ${DEFAULT_RENDER_DPI}. 150 reads well; 300 for small print.
|
|
23
|
+
- max_width (number, optional): Pixel-width cap; wins over dpi when smaller.
|
|
24
|
+
- format ('png' | 'jpeg', optional): Default 'png'. 'jpeg' is usually right for scans.
|
|
25
|
+
- quality (1-100, optional): JPEG quality.
|
|
26
|
+
|
|
27
|
+
Returns:
|
|
28
|
+
A text block with per-page metadata (point size, pixel size, effective dpi, bytes) and any omissions, then one image content block per rendered page.
|
|
29
|
+
|
|
30
|
+
Examples:
|
|
31
|
+
- A scanned page: { file_path: "/path/to/scan.pdf", pages: "1", format: "jpeg" }
|
|
32
|
+
- A diagram at high detail: { file_path: "/path/to/doc.pdf", pages: "3", dpi: 300 }`,
|
|
33
|
+
inputSchema: RenderPageSchema,
|
|
34
|
+
annotations: {
|
|
35
|
+
readOnlyHint: true,
|
|
36
|
+
destructiveHint: false,
|
|
37
|
+
idempotentHint: true,
|
|
38
|
+
openWorldHint: false,
|
|
39
|
+
},
|
|
40
|
+
}, async (params) => {
|
|
41
|
+
try {
|
|
42
|
+
if (!(await rendererAvailable())) {
|
|
43
|
+
return {
|
|
44
|
+
content: [{ type: 'text', text: RENDERER_MISSING_MESSAGE }],
|
|
45
|
+
isError: true,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
const result = await renderPages(params.file_path, params.pages, {
|
|
49
|
+
dpi: params.dpi,
|
|
50
|
+
maxWidth: params.max_width,
|
|
51
|
+
format: params.format,
|
|
52
|
+
quality: params.quality,
|
|
53
|
+
});
|
|
54
|
+
const lines = [];
|
|
55
|
+
if (result.pages.length === 0) {
|
|
56
|
+
lines.push('No pages were rendered.');
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
lines.push(`Rendered ${result.pages.length} page(s), ` +
|
|
60
|
+
`${result.totalEncodedBytes.toLocaleString()} bytes encoded.`, '', '| Page | Size (pt) | Rendered (px) | Effective dpi | Type | Bytes |', '|---|---|---|---|---|---|');
|
|
61
|
+
for (const page of result.pages) {
|
|
62
|
+
lines.push(`| ${page.page} | ${page.pointWidth}×${page.pointHeight} | ` +
|
|
63
|
+
`${page.width}×${page.height} | ${page.effectiveDpi} | ${page.mimeType} | ` +
|
|
64
|
+
`${page.encodedBytes.toLocaleString()} |`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (result.omitted.length > 0) {
|
|
68
|
+
lines.push('', '## Not rendered', '');
|
|
69
|
+
for (const omitted of result.omitted) {
|
|
70
|
+
lines.push(`- page ${omitted.page}: ${omitted.reason}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
content: [
|
|
75
|
+
{ type: 'text', text: lines.join('\n') },
|
|
76
|
+
...result.pages.map((page) => ({
|
|
77
|
+
type: 'image',
|
|
78
|
+
data: page.dataBase64,
|
|
79
|
+
mimeType: page.mimeType,
|
|
80
|
+
})),
|
|
81
|
+
],
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
const err = handleStructuredError(error);
|
|
86
|
+
return {
|
|
87
|
+
content: [{ type: 'text', text: JSON.stringify(err, null, 2) }],
|
|
88
|
+
isError: true,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=render-page.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render-page.js","sourceRoot":"","sources":["../../../src/tools/tier1/render-page.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EAAwB,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAChF,OAAO,EACL,kBAAkB,EAClB,wBAAwB,EACxB,iBAAiB,EACjB,WAAW,GACZ,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAErE,MAAM,UAAU,kBAAkB,CAAC,MAAiB;IAClD,MAAM,CAAC,YAAY,CACjB,aAAa,EACb;QACE,KAAK,EAAE,iBAAiB;QACxB,WAAW,EAAE;;;;;;4KAMyJ,CAAC,wBAAwB,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC;;;;;+CAKlL,kBAAkB;;;;;;;;;;sFAUqB;QAChF,WAAW,EAAE,gBAAgB;QAC7B,WAAW,EAAE;YACX,YAAY,EAAE,IAAI;YAClB,eAAe,EAAE,KAAK;YACtB,cAAc,EAAE,IAAI;YACpB,aAAa,EAAE,KAAK;SACrB;KACF,EACD,KAAK,EAAE,MAAuB,EAAE,EAAE;QAChC,IAAI,CAAC;YACH,IAAI,CAAC,CAAC,MAAM,iBAAiB,EAAE,CAAC,EAAE,CAAC;gBACjC,OAAO;oBACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,wBAAwB,EAAE,CAAC;oBACpE,OAAO,EAAE,IAAI;iBACd,CAAC;YACJ,CAAC;YAED,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,KAAK,EAAE;gBAC/D,GAAG,EAAE,MAAM,CAAC,GAAG;gBACf,QAAQ,EAAE,MAAM,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EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;oBACjD,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;wBAC7B,IAAI,EAAE,OAAgB;wBACtB,IAAI,EAAE,IAAI,CAAC,UAAU;wBACrB,QAAQ,EAAE,IAAI,CAAC,QAAQ;qBACxB,CAAC,CAAC;iBACJ;aACF,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,GAAG,GAAG,qBAAqB,CAAC,KAAK,CAAC,CAAC;YACzC,OAAO;gBACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;gBACxE,OAAO,EAAE,IAAI;aACd,CAAC;QACJ,CAAC;IACH,CAAC,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search-text.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/search-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;
|
|
1
|
+
{"version":3,"file":"search-text.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/search-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AASzE,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAkG1D"}
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import { ResponseFormat } from '../../constants.js';
|
|
5
5
|
import { SearchTextSchema } from '../../schemas/tier1.js';
|
|
6
6
|
import { searchText } from '../../services/pdfjs-service.js';
|
|
7
|
+
import { observeExtractability } from '../../services/text-extractability-service.js';
|
|
7
8
|
import { handleStructuredError } from '../../utils/error-handler.js';
|
|
8
9
|
import { formatSearchResultMarkdown } from '../../utils/formatter.js';
|
|
9
10
|
export function registerSearchText(server) {
|
|
@@ -38,7 +39,13 @@ Examples:
|
|
|
38
39
|
},
|
|
39
40
|
}, async (params) => {
|
|
40
41
|
try {
|
|
41
|
-
const { matches: allMatches, unresolvedPages, unresolvedReason
|
|
42
|
+
const [{ matches: allMatches, unresolvedPages, unresolvedReason }, observations] = await Promise.all([
|
|
43
|
+
searchText(params.file_path, params.query, params.context_chars, params.pages),
|
|
44
|
+
// #21: zero hits on a page whose characters do not convert to Unicode
|
|
45
|
+
// is "not found in what could be read", not "not in the document".
|
|
46
|
+
observeExtractability(params.file_path, params.pages),
|
|
47
|
+
]);
|
|
48
|
+
const unsearchable = observations.filter((page) => page.state !== 'extracted');
|
|
42
49
|
const truncated = allMatches.length > params.max_results;
|
|
43
50
|
const matches = allMatches.slice(0, params.max_results);
|
|
44
51
|
const result = {
|
|
@@ -46,6 +53,7 @@ Examples:
|
|
|
46
53
|
totalMatches: allMatches.length,
|
|
47
54
|
matches,
|
|
48
55
|
truncated,
|
|
56
|
+
...(unsearchable.length > 0 ? { unsearchablePages: unsearchable } : {}),
|
|
49
57
|
};
|
|
50
58
|
// #18 resolves /ActualText, so the blanket #15 warning no longer
|
|
51
59
|
// applies — but the resolution can still be skipped. Keep saying so,
|
|
@@ -53,7 +61,14 @@ Examples:
|
|
|
53
61
|
// it is: they call for different next steps, and pointing an encrypted
|
|
54
62
|
// document at extract_structured_text would waste the caller's time
|
|
55
63
|
// (that tool reads its replacement text through pdf-lib as well).
|
|
56
|
-
if (allMatches.length === 0 && unresolvedPages.length > 0) {
|
|
64
|
+
if (allMatches.length === 0 && unresolvedPages.length === 0 && unsearchable.length > 0) {
|
|
65
|
+
result.note =
|
|
66
|
+
`No matches. ${unsearchable.length} of the ${observations.length} page(s) searched ` +
|
|
67
|
+
'do not yield text that can be converted to Unicode (ISO 32000-2 §9.10.1), so this ' +
|
|
68
|
+
'result says the query was not found in what could be read — not that it is absent ' +
|
|
69
|
+
'from the document. See the per-page states above.';
|
|
70
|
+
}
|
|
71
|
+
else if (allMatches.length === 0 && unresolvedPages.length > 0) {
|
|
57
72
|
result.note =
|
|
58
73
|
unresolvedReason === 'encrypted'
|
|
59
74
|
? 'No matches. This document is encrypted, so replacement text (/ActualText, ' +
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search-text.js","sourceRoot":"","sources":["../../../src/tools/tier1/search-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAwB,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAChF,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;
|
|
1
|
+
{"version":3,"file":"search-text.js","sourceRoot":"","sources":["../../../src/tools/tier1/search-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAwB,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAChF,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC7D,OAAO,EAAE,qBAAqB,EAAE,MAAM,+CAA+C,CAAC;AAEtF,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAE,0BAA0B,EAAE,MAAM,0BAA0B,CAAC;AAEtE,MAAM,UAAU,kBAAkB,CAAC,MAAiB;IAClD,MAAM,CAAC,YAAY,CACjB,aAAa,EACb;QACE,KAAK,EAAE,iBAAiB;QACxB,WAAW,EAAE;;;;;;;;;;;;;;;;;;;4FAmByE;QACtF,WAAW,EAAE,gBAAgB;QAC7B,WAAW,EAAE;YACX,YAAY,EAAE,IAAI;YAClB,eAAe,EAAE,KAAK;YACtB,cAAc,EAAE,IAAI;YACpB,aAAa,EAAE,KAAK;SACrB;KACF,EACD,K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,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;
|
|
1
|
+
{"version":3,"file":"summarize.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/summarize.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAkGzE,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAqFzD"}
|