@research-copilot/mcp-pdf 1.0.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/LICENSE +21 -0
- package/README.md +60 -0
- package/dist/mcp-pdf.js +103 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ldm2060
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# @research-copilot/mcp-pdf
|
|
2
|
+
|
|
3
|
+
MCP server for PDF text and metadata extraction using [unpdf](https://github.com/unjs/unpdf).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npx @research-copilot/mcp-pdf
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or via MCP configuration:
|
|
12
|
+
|
|
13
|
+
```json
|
|
14
|
+
{
|
|
15
|
+
"mcpServers": {
|
|
16
|
+
"research-pdf": {
|
|
17
|
+
"command": "npx",
|
|
18
|
+
"args": ["-y", "@research-copilot/mcp-pdf"]
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Tools
|
|
25
|
+
|
|
26
|
+
### `pdf_extract_text`
|
|
27
|
+
|
|
28
|
+
Extract text content from a PDF file.
|
|
29
|
+
|
|
30
|
+
**Inputs:**
|
|
31
|
+
- `path` (string, required) - Absolute path to PDF file
|
|
32
|
+
- `mergePages` (boolean, optional) - Merge all pages into single text block (default: true)
|
|
33
|
+
|
|
34
|
+
**Returns:**
|
|
35
|
+
- `text` (string) - Extracted text content
|
|
36
|
+
- `totalPages` (number) - Total page count
|
|
37
|
+
|
|
38
|
+
**Note:** unpdf extracts text in content-stream order. For two-column academic papers, text from both columns may be interleaved. Page numbers are advisory.
|
|
39
|
+
|
|
40
|
+
### `pdf_extract_metadata`
|
|
41
|
+
|
|
42
|
+
Extract PDF metadata (page count, document info).
|
|
43
|
+
|
|
44
|
+
**Inputs:**
|
|
45
|
+
- `path` (string, required) - Absolute path to PDF file
|
|
46
|
+
|
|
47
|
+
**Returns:**
|
|
48
|
+
- `totalPages` (number) - Total page count
|
|
49
|
+
- `info` (object) - Document metadata (title, author, creation date, etc.)
|
|
50
|
+
|
|
51
|
+
## Implementation
|
|
52
|
+
|
|
53
|
+
- Built with [unpdf](https://github.com/unjs/unpdf) for PDF parsing
|
|
54
|
+
- Uses Mozilla PDF.js under the hood
|
|
55
|
+
- Text extraction is coarse (content-stream order, not reading order)
|
|
56
|
+
- Suitable for simple text extraction; complex layouts may need specialized tools
|
|
57
|
+
|
|
58
|
+
## License
|
|
59
|
+
|
|
60
|
+
MIT
|
package/dist/mcp-pdf.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// bin/mcp-pdf.ts
|
|
4
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
5
|
+
|
|
6
|
+
// src/server.ts
|
|
7
|
+
import { readFileSync } from "fs";
|
|
8
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
|
|
11
|
+
// src/extract.ts
|
|
12
|
+
import { extractText as unpdfExtractText, getDocumentProxy, getMeta } from "unpdf";
|
|
13
|
+
async function extractText(data, opts) {
|
|
14
|
+
const mergePages = opts?.mergePages ?? true;
|
|
15
|
+
const bytes = data.slice();
|
|
16
|
+
if (mergePages) {
|
|
17
|
+
const { text: text2, totalPages: totalPages2 } = await unpdfExtractText(bytes, { mergePages: true });
|
|
18
|
+
return { text: text2, totalPages: totalPages2 };
|
|
19
|
+
}
|
|
20
|
+
const { text, totalPages } = await unpdfExtractText(bytes, { mergePages: false });
|
|
21
|
+
return { text: text.join("\n\n"), totalPages };
|
|
22
|
+
}
|
|
23
|
+
async function extractMetadata(data) {
|
|
24
|
+
const pdf = await getDocumentProxy(data.slice());
|
|
25
|
+
const totalPages = pdf.numPages;
|
|
26
|
+
const { info } = await getMeta(data.slice());
|
|
27
|
+
return { totalPages, info: info ?? {} };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// src/server.ts
|
|
31
|
+
function readPdf(path) {
|
|
32
|
+
const buf = readFileSync(path);
|
|
33
|
+
return new Uint8Array(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength));
|
|
34
|
+
}
|
|
35
|
+
function createServer() {
|
|
36
|
+
const server = new McpServer({
|
|
37
|
+
name: "mcp-pdf",
|
|
38
|
+
version: "0.0.0"
|
|
39
|
+
});
|
|
40
|
+
server.registerTool(
|
|
41
|
+
"pdf_extract_text",
|
|
42
|
+
{
|
|
43
|
+
title: "Extract PDF text",
|
|
44
|
+
description: "Extract the text of a local PDF file. Coarse extraction (content-stream order); two-column layouts may interleave. Extracted numbers are advisory.",
|
|
45
|
+
inputSchema: { path: z.string().describe("Absolute or relative path to a local PDF file") }
|
|
46
|
+
},
|
|
47
|
+
async ({ path }) => {
|
|
48
|
+
try {
|
|
49
|
+
const data = readPdf(path);
|
|
50
|
+
const { text, totalPages } = await extractText(data);
|
|
51
|
+
return {
|
|
52
|
+
content: [{ type: "text", text }],
|
|
53
|
+
structuredContent: { text, totalPages }
|
|
54
|
+
};
|
|
55
|
+
} catch (err) {
|
|
56
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
57
|
+
return {
|
|
58
|
+
isError: true,
|
|
59
|
+
content: [{ type: "text", text: `pdf_extract_text failed for "${path}": ${message}` }]
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
server.registerTool(
|
|
65
|
+
"pdf_extract_metadata",
|
|
66
|
+
{
|
|
67
|
+
title: "Extract PDF metadata",
|
|
68
|
+
description: "Extract metadata (page count and the PDF info dictionary) from a local PDF file.",
|
|
69
|
+
inputSchema: { path: z.string().describe("Absolute or relative path to a local PDF file") }
|
|
70
|
+
},
|
|
71
|
+
async ({ path }) => {
|
|
72
|
+
try {
|
|
73
|
+
const data = readPdf(path);
|
|
74
|
+
const { totalPages, info } = await extractMetadata(data);
|
|
75
|
+
return {
|
|
76
|
+
content: [{ type: "text", text: JSON.stringify({ totalPages, info }, null, 2) }],
|
|
77
|
+
structuredContent: { totalPages, info }
|
|
78
|
+
};
|
|
79
|
+
} catch (err) {
|
|
80
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
81
|
+
return {
|
|
82
|
+
isError: true,
|
|
83
|
+
content: [
|
|
84
|
+
{ type: "text", text: `pdf_extract_metadata failed for "${path}": ${message}` }
|
|
85
|
+
]
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
return server;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// bin/mcp-pdf.ts
|
|
94
|
+
async function main() {
|
|
95
|
+
const server = createServer();
|
|
96
|
+
const transport = new StdioServerTransport();
|
|
97
|
+
await server.connect(transport);
|
|
98
|
+
}
|
|
99
|
+
main().catch((err) => {
|
|
100
|
+
process.stderr.write(`mcp-pdf: fatal: ${err instanceof Error ? err.stack ?? err.message : String(err)}
|
|
101
|
+
`);
|
|
102
|
+
process.exit(1);
|
|
103
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@research-copilot/mcp-pdf",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "MCP server for PDF extraction",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"mcp-pdf": "./dist/mcp-pdf.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"keywords": [
|
|
14
|
+
"mcp",
|
|
15
|
+
"pdf",
|
|
16
|
+
"extraction"
|
|
17
|
+
],
|
|
18
|
+
"author": "ldm2060",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/ldm2060/research_copilot.git",
|
|
23
|
+
"directory": "packages/mcp-pdf"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=18.0.0"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@modelcontextprotocol/sdk": "^1.0.0",
|
|
30
|
+
"unpdf": "^0.12.0",
|
|
31
|
+
"zod": "^3.25.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"pdf-lib": "^1.17.1"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsup bin/mcp-pdf.ts --format esm"
|
|
38
|
+
}
|
|
39
|
+
}
|