|
| 1 | +/** |
| 2 | + * Entry point for running the MCP server. |
| 3 | + * Run with: npx mcp-pdf-server |
| 4 | + * Or: node dist/index.js [--stdio] [pdf-urls...] |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Shared utilities for running MCP servers with Streamable HTTP transport. |
| 9 | + */ |
| 10 | + |
| 11 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
| 12 | +import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js"; |
| 13 | +import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 14 | +import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js"; |
| 15 | +import cors from "cors"; |
| 16 | +import type { Request, Response } from "express"; |
| 17 | +import { createServer, initializePdfIndex } from "./server.js"; |
| 18 | +import { |
| 19 | + isArxivUrl, |
| 20 | + toFileUrl, |
| 21 | + normalizeArxivUrl, |
| 22 | +} from "./src/pdf-indexer.js"; |
| 23 | + |
| 24 | +export interface ServerOptions { |
| 25 | + port: number; |
| 26 | + name?: string; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Starts an MCP server with Streamable HTTP transport in stateless mode. |
| 31 | + * |
| 32 | + * @param createServer - Factory function that creates a new McpServer instance per request. |
| 33 | + * @param options - Server configuration options. |
| 34 | + */ |
| 35 | +export async function startServer( |
| 36 | + createServer: () => McpServer, |
| 37 | + options: ServerOptions, |
| 38 | +): Promise<void> { |
| 39 | + const { port, name = "MCP Server" } = options; |
| 40 | + |
| 41 | + const app = createMcpExpressApp({ host: "0.0.0.0" }); |
| 42 | + app.use(cors()); |
| 43 | + |
| 44 | + app.all("/mcp", async (req: Request, res: Response) => { |
| 45 | + const server = createServer(); |
| 46 | + const transport = new StreamableHTTPServerTransport({ |
| 47 | + sessionIdGenerator: undefined, |
| 48 | + }); |
| 49 | + |
| 50 | + res.on("close", () => { |
| 51 | + transport.close().catch(() => {}); |
| 52 | + server.close().catch(() => {}); |
| 53 | + }); |
| 54 | + |
| 55 | + try { |
| 56 | + await server.connect(transport); |
| 57 | + await transport.handleRequest(req, res, req.body); |
| 58 | + } catch (error) { |
| 59 | + console.error("MCP error:", error); |
| 60 | + if (!res.headersSent) { |
| 61 | + res.status(500).json({ |
| 62 | + jsonrpc: "2.0", |
| 63 | + error: { code: -32603, message: "Internal server error" }, |
| 64 | + id: null, |
| 65 | + }); |
| 66 | + } |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + const httpServer = app.listen(port, (err) => { |
| 71 | + if (err) { |
| 72 | + console.error("Failed to start server:", err); |
| 73 | + process.exit(1); |
| 74 | + } |
| 75 | + console.log(`${name} listening on http://localhost:${port}/mcp`); |
| 76 | + }); |
| 77 | + |
| 78 | + const shutdown = () => { |
| 79 | + console.log("\nShutting down..."); |
| 80 | + httpServer.close(() => process.exit(0)); |
| 81 | + }; |
| 82 | + |
| 83 | + process.on("SIGINT", shutdown); |
| 84 | + process.on("SIGTERM", shutdown); |
| 85 | +} |
| 86 | + |
| 87 | +const DEFAULT_PDF = "https://arxiv.org/pdf/1706.03762"; // Attention Is All You Need |
| 88 | + |
| 89 | +function parseArgs(): { urls: string[]; stdio: boolean } { |
| 90 | + const args = process.argv.slice(2); |
| 91 | + const urls: string[] = []; |
| 92 | + let stdio = false; |
| 93 | + |
| 94 | + for (const arg of args) { |
| 95 | + if (arg === "--stdio") { |
| 96 | + stdio = true; |
| 97 | + } else if (!arg.startsWith("-")) { |
| 98 | + // Convert local paths to file:// URLs, normalize arxiv URLs |
| 99 | + let url = arg; |
| 100 | + if ( |
| 101 | + !arg.startsWith("http://") && |
| 102 | + !arg.startsWith("https://") && |
| 103 | + !arg.startsWith("file://") |
| 104 | + ) { |
| 105 | + url = toFileUrl(arg); |
| 106 | + } else if (isArxivUrl(arg)) { |
| 107 | + url = normalizeArxivUrl(arg); |
| 108 | + } |
| 109 | + urls.push(url); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return { urls: urls.length > 0 ? urls : [DEFAULT_PDF], stdio }; |
| 114 | +} |
| 115 | + |
| 116 | +async function main() { |
| 117 | + const { urls, stdio } = parseArgs(); |
| 118 | + |
| 119 | + console.error(`[pdf-server] Initializing with ${urls.length} PDF(s)...`); |
| 120 | + await initializePdfIndex(urls); |
| 121 | + console.error(`[pdf-server] Ready`); |
| 122 | + |
| 123 | + if (stdio) { |
| 124 | + await createServer().connect(new StdioServerTransport()); |
| 125 | + } else { |
| 126 | + const port = parseInt(process.env.PORT ?? "3120", 10); |
| 127 | + await startServer(createServer, { port, name: "PDF Server" }); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +main().catch((e) => { |
| 132 | + console.error(e); |
| 133 | + process.exit(1); |
| 134 | +}); |
0 commit comments