docsmith-mcp 0.0.1-beta.1 → 0.0.1-beta.2
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/dist/index.cjs +703 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +24 -2
- package/.github/workflows/test.yml +0 -35
- package/dist/python/excel_handler.py +0 -97
- package/dist/python/pdf_handler.py +0 -81
- package/dist/python/text_handler.py +0 -331
- package/dist/python/word_handler.py +0 -98
- package/examples/sample_data.csv +0 -6
- package/examples/sample_data.json +0 -9
- package/examples/sample_document.pdf +0 -80
- package/examples/sample_report.docx +0 -0
- package/examples/sample_sales_data.xlsx +0 -0
- package/examples/sample_text.txt +0 -10
- package/src/code-runner.ts +0 -136
- package/src/index.ts +0 -496
- package/src/utils.ts +0 -45
- package/tests/document-processing.test.ts +0 -230
- package/tsconfig.json +0 -20
- package/tsdown.config.ts +0 -21
- package/vitest.config.ts +0 -15
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["__filename","__dirname","filePath: string","scriptPath: string","options: RunPythonFileOptions","runPyOptions: RunPyOptions","filePath: string","fileType: string","packages: Record<string, Record<string, string>>","Server","ListToolsRequestSchema","CallToolRequestSchema","scriptName: string","scriptArgs: string[]","StdioServerTransport"],"sources":["../src/code-runner.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":["/**\n * Code runner client - uses @mcpc-tech/code-runner-mcp npm package\n */\nimport { runPy, type RunPyOptions } from \"@mcpc-tech/code-runner-mcp\";\nimport { readFileSync } from \"fs\";\nimport { fileURLToPath } from \"url\";\nimport { dirname, join, resolve } from \"path\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);\n\n/**\n * Options for running Python script files\n */\nexport interface RunPythonFileOptions {\n /** Command line arguments to pass to the script */\n args?: string[];\n /** Package name mappings (import_name -> pypi_name) */\n packages?: Record<string, string>;\n /** Base directory for the script (default: \"python\") */\n baseDir?: string;\n /** User file paths that need to be accessible (for file system mounting) */\n filePaths?: string[];\n}\n\n/**\n * Convert absolute file path to Pyodide virtual path\n * Determines the mount root and converts the path accordingly\n *\n * @param filePath - Absolute path to the file\n * @returns Object with mountRoot (host path) and virtualPath (Pyodide path)\n */\nfunction getFileSystemMapping(\n filePath: string,\n): { mountRoot: string; virtualPath: string } {\n const absolutePath = resolve(filePath);\n\n // Mount the parent directory of the file\n // This allows Python to access the file and its siblings\n const mountRoot = dirname(absolutePath);\n const virtualPath = absolutePath;\n\n return { mountRoot, virtualPath };\n}\n\n/**\n * Run a Python script file using code-runner-mcp\n *\n * @param scriptPath - Path to the Python script (relative to baseDir)\n * @param options - Execution options\n * @returns The execution result\n */\nexport async function runPythonFile(\n scriptPath: string,\n options: RunPythonFileOptions = {},\n): Promise<any> {\n const {\n args = [],\n packages = {},\n baseDir = \"python\",\n filePaths = [],\n } = options;\n\n // Read the Python script\n // Python files are in the python/ directory at project root\n // From dist/index.js, go up one level to reach python/\n const fullPath = join(__dirname, \"..\", baseDir, scriptPath);\n const scriptContent = readFileSync(fullPath, \"utf-8\");\n\n // Build wrapper code that sets sys.argv and executes the script\n const wrapperCode = `\nimport sys\nimport json\n\n# Set command line arguments\nsys.argv = ['${scriptPath}'] + ${JSON.stringify(args)}\n\n# Execute the script\n${scriptContent}\n`;\n\n // Determine mount root from the first file path\n // Default: parent directory of dist/ (project root when running from dist/index.js)\n let mountRoot = join(__dirname, \"..\");\n if (filePaths.length > 0) {\n const mapping = getFileSystemMapping(filePaths[0]);\n mountRoot = mapping.mountRoot;\n }\n\n // Execute via runPy with options\n // Mount point is the same as the mount root (Pyodide will see host paths directly)\n const runPyOptions: RunPyOptions = {\n packages,\n nodeFSMountPoint: mountRoot,\n nodeFSRoot: mountRoot,\n };\n const stream = await runPy(wrapperCode, runPyOptions);\n\n // Read the stream output\n const reader = stream.getReader();\n const decoder = new TextDecoder();\n let stdout = \"\";\n let stderr = \"\";\n let error = \"\";\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n\n const chunk = decoder.decode(value, { stream: true });\n if (chunk.startsWith(\"[stderr] \")) {\n stderr += chunk.slice(9);\n } else if (chunk.startsWith(\"[err]\")) {\n error += chunk;\n } else {\n stdout += chunk;\n }\n }\n } catch (streamError) {\n // Stream error means Python execution failed\n return { error: String(streamError) };\n }\n\n // Check for errors\n if (error) {\n return { error: error.replace(/\\[err\\]\\[py\\]\\s*/g, \"\").trim() };\n }\n\n // Parse the JSON output from the script (last line)\n const lines = stdout.trim().split(\"\\n\");\n const lastLine = lines[lines.length - 1];\n\n try {\n return JSON.parse(lastLine);\n } catch {\n return { stdout, stderr };\n }\n}\n","/**\n * Utility functions for document processing\n */\n\n/**\n * Detect file type from file extension\n */\nexport function detectFileType(\n filePath: string,\n): \"excel\" | \"word\" | \"pdf\" | \"text\" | null {\n const ext = filePath.toLowerCase().split(\".\").pop();\n if (ext === \"xlsx\" || ext === \"xls\") return \"excel\";\n if (ext === \"docx\") return \"word\";\n if (ext === \"pdf\") return \"pdf\";\n if (\n ext === \"txt\" || ext === \"csv\" || ext === \"md\" || ext === \"json\" ||\n ext === \"yaml\" || ext === \"yml\"\n ) return \"text\";\n return null;\n}\n\n/**\n * Get required packages for each file type\n */\nexport function getPackages(fileType: string): Record<string, string> {\n const packages: Record<string, Record<string, string>> = {\n excel: { openpyxl: \"openpyxl\" },\n word: { docx: \"python-docx\" }, // Map docx import to python-docx package\n pdf: { PyPDF2: \"PyPDF2\" },\n text: {}, // No external packages needed for text files\n };\n return packages[fileType] || {};\n}\n\n/**\n * Get environment configuration\n */\nexport function getConfig() {\n return {\n rawFullRead: process.env.DOC_RAW_FULL_READ === \"true\",\n pageSize: parseInt(process.env.DOC_PAGE_SIZE || \"100\", 10),\n maxFileSize: parseInt(process.env.DOC_MAX_FILE_SIZE || \"50\", 10) * 1024 *\n 1024,\n };\n}\n","#!/usr/bin/env node\n\nimport { Server } from \"@modelcontextprotocol/sdk/server/index.js\";\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\";\nimport {\n CallToolRequestSchema,\n ListToolsRequestSchema,\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport { z } from \"zod\";\nimport { runPythonFile } from \"./code-runner.js\";\nimport { detectFileType, getConfig, getPackages } from \"./utils.js\";\n\n// Tool schemas\nconst ReadDocumentSchema = z.object({\n file_path: z.string().describe(\"Absolute path to the document file\"),\n mode: z.enum([\"raw\", \"paginated\"]).optional().describe(\n \"Read mode: 'raw' for full content, 'paginated' for chunked reading\",\n ),\n page: z.number().optional().describe(\n \"Page number for paginated mode (1-based)\",\n ),\n page_size: z.number().optional().describe(\n \"Items per page for paginated mode\",\n ),\n sheet_name: z.string().optional().describe(\"Sheet name for Excel files\"),\n});\n\nconst WriteDocumentSchema = z.object({\n file_path: z.string().describe(\"Absolute path to save the document\"),\n format: z.enum([\"excel\", \"word\", \"text\"]).describe(\"Document format\"),\n data: z.any().describe(\"Document data structure\"),\n});\n\nconst GetDocumentInfoSchema = z.object({\n file_path: z.string().describe(\"Absolute path to the document file\"),\n});\n\n// Server setup\nconst server = new Server(\n {\n name: \"docsmith-mcp\",\n version: \"0.1.0\",\n },\n {\n capabilities: {\n tools: {},\n },\n },\n);\n\n// Output schemas (reusable)\nconst BaseOutputSchema = {\n type: \"object\",\n properties: {\n success: { type: \"boolean\", description: \"Operation success status\" },\n error: { type: \"string\", description: \"Error message if failed\" },\n },\n} as const;\n\nconst PaginationSchema = {\n current_page: { type: \"number\", description: \"Current page number\" },\n page_size: { type: \"number\", description: \"Items per page\" },\n total_pages: { type: \"number\", description: \"Total number of pages\" },\n page: { type: \"number\", description: \"Current page number (alternative)\" },\n has_more: { type: \"boolean\", description: \"Whether more pages exist\" },\n} as const;\n\nconst ExcelReadOutputSchema = {\n type: \"object\",\n properties: {\n sheet_name: { type: \"string\", description: \"Active sheet name\" },\n sheets: {\n type: \"array\",\n items: { type: \"string\" },\n description: \"All sheet names\",\n },\n total_rows: { type: \"number\", description: \"Total rows in sheet\" },\n total_cols: { type: \"number\", description: \"Total columns in sheet\" },\n data: {\n type: \"array\",\n items: { type: \"array\", items: {} },\n description: \"Sheet data as array of rows\",\n },\n ...PaginationSchema,\n },\n} as const;\n\nconst WordReadOutputSchema = {\n type: \"object\",\n properties: {\n paragraphs: {\n type: \"array\",\n items: { type: \"string\" },\n description: \"Document paragraphs\",\n },\n tables: {\n type: \"array\",\n items: {\n type: \"array\",\n items: { type: \"array\", items: { type: \"string\" } },\n },\n description: \"Tables data\",\n },\n total_paragraphs: { type: \"number\", description: \"Total paragraph count\" },\n total_tables: { type: \"number\", description: \"Total table count\" },\n ...PaginationSchema,\n },\n} as const;\n\nconst PDFReadOutputSchema = {\n type: \"object\",\n properties: {\n total_pages: { type: \"number\", description: \"Total pages in PDF\" },\n content: {\n type: \"array\",\n items: {\n type: \"object\",\n properties: {\n page_number: { type: \"number\" },\n text: { type: \"string\" },\n },\n },\n description: \"Page content array\",\n },\n current_page_group: { type: \"number\" },\n page_size: { type: \"number\" },\n },\n} as const;\n\nconst TextReadOutputSchema = {\n type: \"object\",\n properties: {\n ...BaseOutputSchema.properties,\n content: { type: \"string\", description: \"Text content\" },\n total_lines: { type: \"number\", description: \"Total line count\" },\n encoding: { type: \"string\", description: \"File encoding\" },\n ...PaginationSchema,\n },\n} as const;\n\nconst CSVReadOutputSchema = {\n type: \"object\",\n properties: {\n ...BaseOutputSchema.properties,\n headers: {\n type: \"array\",\n items: { type: \"string\" },\n description: \"CSV headers\",\n },\n data: {\n type: \"array\",\n items: { type: \"object\" },\n description: \"Structured data as array of objects\",\n },\n total_rows: { type: \"number\", description: \"Total data rows\" },\n encoding: { type: \"string\", description: \"File encoding\" },\n ...PaginationSchema,\n },\n} as const;\n\nconst JSONReadOutputSchema = {\n type: \"object\",\n properties: {\n ...BaseOutputSchema.properties,\n data: { type: \"object\", description: \"Parsed JSON data\" },\n encoding: { type: \"string\", description: \"File encoding\" },\n },\n} as const;\n\nconst WriteOutputSchema = {\n type: \"object\",\n properties: {\n success: { type: \"boolean\", description: \"Write operation success\" },\n file_path: { type: \"string\", description: \"Written file path\" },\n message: { type: \"string\", description: \"Success message\" },\n error: { type: \"string\", description: \"Error message if failed\" },\n },\n} as const;\n\nconst ExcelInfoOutputSchema = {\n type: \"object\",\n properties: {\n sheets: {\n type: \"array\",\n items: {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n rows: { type: \"number\" },\n cols: { type: \"number\" },\n },\n },\n description: \"Sheet information\",\n },\n file_size: { type: \"number\", description: \"File size in bytes\" },\n },\n} as const;\n\nconst WordInfoOutputSchema = {\n type: \"object\",\n properties: {\n paragraphs: { type: \"number\", description: \"Paragraph count\" },\n tables: { type: \"number\", description: \"Table count\" },\n file_size: { type: \"number\", description: \"File size in bytes\" },\n },\n} as const;\n\nconst PDFInfoOutputSchema = {\n type: \"object\",\n properties: {\n pages: { type: \"number\", description: \"Page count\" },\n file_size: { type: \"number\", description: \"File size in bytes\" },\n total_words: { type: \"number\", description: \"Total word count\" },\n },\n} as const;\n\nconst TextInfoOutputSchema = {\n type: \"object\",\n properties: {\n ...BaseOutputSchema.properties,\n file_size: { type: \"number\", description: \"File size in bytes\" },\n line_count: { type: \"number\", description: \"Line count\" },\n encoding: { type: \"string\", description: \"File encoding\" },\n file_type: { type: \"string\", description: \"File extension\" },\n // CSV specific\n headers: { type: \"array\", items: { type: \"string\" } },\n total_rows: { type: \"number\" },\n total_cols: { type: \"number\" },\n // JSON specific\n item_count: { type: \"number\" },\n key_count: { type: \"number\" },\n },\n} as const;\n\n// List available tools\nserver.setRequestHandler(ListToolsRequestSchema, async () => {\n return {\n tools: [\n {\n name: \"read_document\",\n description:\n \"Read document content (Excel, Word, PDF, TXT, CSV, Markdown, JSON, YAML). Supports raw full read or paginated mode.\",\n inputSchema: {\n type: \"object\",\n properties: {\n file_path: {\n type: \"string\",\n description: \"Absolute path to the document file\",\n },\n mode: {\n type: \"string\",\n enum: [\"raw\", \"paginated\"],\n description: \"Read mode\",\n },\n page: {\n type: \"number\",\n description: \"Page number for paginated mode\",\n },\n page_size: { type: \"number\", description: \"Items per page\" },\n sheet_name: {\n type: \"string\",\n description: \"Sheet name for Excel files\",\n },\n },\n required: [\"file_path\"],\n },\n outputSchema: {\n type: \"object\",\n description:\n \"Returns different structures based on file type: Excel (sheet data), Word (paragraphs/tables), PDF (page content), Text (plain text), CSV (structured rows), JSON (parsed object)\",\n oneOf: [\n ExcelReadOutputSchema,\n WordReadOutputSchema,\n PDFReadOutputSchema,\n TextReadOutputSchema,\n CSVReadOutputSchema,\n JSONReadOutputSchema,\n ],\n },\n },\n {\n name: \"write_document\",\n description: \"Write document content (Excel, Word, Text)\",\n inputSchema: {\n type: \"object\",\n properties: {\n file_path: {\n type: \"string\",\n description: \"Absolute path to save the document\",\n },\n format: {\n type: \"string\",\n enum: [\"excel\", \"word\", \"text\"],\n description: \"Document format\",\n },\n data: {\n description:\n \"Document data structure. Excel: array of rows [[cell1, cell2], ...]. Word: {paragraphs: string[], tables?: [[[cell]]]}. Text/CSV/JSON: string or object\",\n },\n },\n required: [\"file_path\", \"format\", \"data\"],\n },\n outputSchema: WriteOutputSchema,\n },\n {\n name: \"get_document_info\",\n description:\n \"Get document metadata (page count, sheet count, file size, etc.)\",\n inputSchema: {\n type: \"object\",\n properties: {\n file_path: {\n type: \"string\",\n description: \"Absolute path to the document file\",\n },\n },\n required: [\"file_path\"],\n },\n outputSchema: {\n type: \"object\",\n description: \"Returns metadata based on file type\",\n oneOf: [\n ExcelInfoOutputSchema,\n WordInfoOutputSchema,\n PDFInfoOutputSchema,\n TextInfoOutputSchema,\n ],\n },\n },\n ],\n };\n});\n\n// Handle tool calls\nserver.setRequestHandler(CallToolRequestSchema, async (request) => {\n const { name, arguments: args } = request.params;\n\n try {\n if (name === \"read_document\") {\n const params = ReadDocumentSchema.parse(args);\n const fileType = detectFileType(params.file_path);\n\n if (!fileType) {\n throw new Error(`Unsupported file type: ${params.file_path}`);\n }\n\n // Determine read mode\n const config = getConfig();\n const mode = params.mode || (config.rawFullRead ? \"raw\" : \"paginated\");\n const page = mode === \"paginated\" ? (params.page || 1) : undefined;\n const pageSize = params.page_size || config.pageSize;\n\n let scriptName: string;\n let scriptArgs: string[];\n\n if (fileType === \"excel\") {\n scriptName = \"excel_handler.py\";\n scriptArgs = [\"read\", params.file_path];\n if (params.sheet_name) scriptArgs.push(params.sheet_name);\n if (page) {\n scriptArgs.push(String(page));\n scriptArgs.push(String(pageSize));\n }\n } else if (fileType === \"word\") {\n scriptName = \"word_handler.py\";\n scriptArgs = [\"read\", params.file_path];\n if (page) {\n scriptArgs.push(String(page));\n scriptArgs.push(String(pageSize));\n }\n } else if (fileType === \"pdf\") {\n scriptName = \"pdf_handler.py\";\n scriptArgs = [\"read\", params.file_path];\n if (page) {\n scriptArgs.push(String(page));\n scriptArgs.push(String(Math.min(pageSize, 10))); // PDF pages are larger\n }\n } else {\n // text files\n scriptName = \"text_handler.py\";\n scriptArgs = [\"read\", params.file_path];\n if (page) {\n scriptArgs.push(String(page));\n scriptArgs.push(String(pageSize));\n }\n }\n\n const result = await runPythonFile(scriptName, {\n args: scriptArgs,\n packages: getPackages(fileType),\n filePaths: [params.file_path],\n });\n return {\n content: [{\n type: \"text\",\n text: JSON.stringify(result, null, 2),\n }],\n structuredContent: result,\n };\n }\n\n if (name === \"write_document\") {\n const params = WriteDocumentSchema.parse(args);\n\n let scriptName: string;\n let scriptArgs: string[];\n\n if (params.format === \"excel\") {\n scriptName = \"excel_handler.py\";\n scriptArgs = [\"write\", params.file_path, JSON.stringify(params.data)];\n } else if (params.format === \"word\") {\n scriptName = \"word_handler.py\";\n const paragraphs = params.data.paragraphs || [];\n const tables = params.data.tables || null;\n scriptArgs = [\"write\", params.file_path, JSON.stringify(paragraphs)];\n if (tables) scriptArgs.push(JSON.stringify(tables));\n } else if (params.format === \"text\") {\n scriptName = \"text_handler.py\";\n const content = typeof params.data === \"string\"\n ? params.data\n : JSON.stringify(params.data);\n scriptArgs = [\"write\", params.file_path, content];\n } else {\n throw new Error(`Unsupported write format: ${params.format}`);\n }\n\n const result = await runPythonFile(scriptName, {\n args: scriptArgs,\n packages: getPackages(params.format),\n filePaths: [params.file_path],\n });\n return {\n content: [{\n type: \"text\",\n text: JSON.stringify(result, null, 2),\n }],\n structuredContent: result,\n };\n }\n\n if (name === \"get_document_info\") {\n const params = GetDocumentInfoSchema.parse(args);\n const fileType = detectFileType(params.file_path);\n\n if (!fileType) {\n throw new Error(`Unsupported file type: ${params.file_path}`);\n }\n\n let scriptName: string;\n let scriptArgs = [\"info\", params.file_path];\n\n if (fileType === \"excel\") {\n scriptName = \"excel_handler.py\";\n } else if (fileType === \"word\") {\n scriptName = \"word_handler.py\";\n } else if (fileType === \"pdf\") {\n scriptName = \"pdf_handler.py\";\n } else {\n scriptName = \"text_handler.py\";\n }\n\n const result = await runPythonFile(scriptName, {\n args: scriptArgs,\n packages: getPackages(fileType),\n filePaths: [params.file_path],\n });\n return {\n content: [{\n type: \"text\",\n text: JSON.stringify(result, null, 2),\n }],\n structuredContent: result,\n };\n }\n\n throw new Error(`Unknown tool: ${name}`);\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n return {\n content: [{ type: \"text\", text: `Error: ${errorMessage}` }],\n isError: true,\n };\n }\n});\n\n// Start server\nasync function main() {\n const transport = new StdioServerTransport();\n await server.connect(transport);\n console.error(\"Docsmith MCP server running on stdio\");\n}\n\nmain().catch((error) => {\n console.error(\"Server error:\", error);\n process.exit(1);\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,MAAMA,eAAa,qEAA8B;AACjD,MAAMC,cAAY,kBAAQD,aAAW;;;;;;;;AAuBrC,SAAS,qBACPE,UAC4C;CAC5C,MAAM,eAAe,kBAAQ,SAAS;CAItC,MAAM,YAAY,kBAAQ,aAAa;CACvC,MAAM,cAAc;AAEpB,QAAO;EAAE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ACR,MAAM;KACN,MAAM,KAAK,UAAU,QAAQ,MAAM,EAAE;IACtC,CAAC;IACF,mBAAmB;GACpB;EACF;AAED,QAAM,IAAI,OAAO,gBAAgB,KAAK;CACvC,SAAQ,OAAO;EACd,MAAM,eAAe,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM;AAC3E,SAAO;GACL,SAAS,CAAC;IAAE,MAAM;IAAQ,OAAO,SAAS,aAAa;GAAG,CAAC;GAC3D,SAAS;EACV;CACF;AACF,EAAC;AAGF,eAAe,OAAO;CACpB,MAAM,YAAY,IAAIE;AACtB,OAAM,OAAO,QAAQ,UAAU;AAC/B,SAAQ,MAAM,uCAAuC;AACtD;AAED,MAAM,CAAC,MAAM,CAAC,UAAU;AACtB,SAAQ,MAAM,iBAAiB,MAAM;AACrC,SAAQ,KAAK,EAAE;AAChB,EAAC"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["filePath: string","scriptPath: string","options: RunPythonFileOptions","runPyOptions: RunPyOptions","filePath: string","fileType: string","packages: Record<string, Record<string, string>>","scriptName: string","scriptArgs: string[]"],"sources":["../src/code-runner.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":["/**\n * Code runner client - uses @mcpc-tech/code-runner-mcp npm package\n */\nimport { runPy, type RunPyOptions } from \"@mcpc-tech/code-runner-mcp\";\nimport { readFileSync } from \"fs\";\nimport { fileURLToPath } from \"url\";\nimport { dirname, join, resolve } from \"path\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);\n\n/**\n * Options for running Python script files\n */\nexport interface RunPythonFileOptions {\n /** Command line arguments to pass to the script */\n args?: string[];\n /** Package name mappings (import_name -> pypi_name) */\n packages?: Record<string, string>;\n /** Base directory for the script (default: \"python\") */\n baseDir?: string;\n /** User file paths that need to be accessible (for file system mounting) */\n filePaths?: string[];\n}\n\n/**\n * Convert absolute file path to Pyodide virtual path\n * Determines the mount root and converts the path accordingly\n *\n * @param filePath - Absolute path to the file\n * @returns Object with mountRoot (host path) and virtualPath (Pyodide path)\n */\nfunction getFileSystemMapping(\n filePath: string,\n): { mountRoot: string; virtualPath: string } {\n const absolutePath = resolve(filePath);\n\n // Mount the parent directory of the file\n // This allows Python to access the file and its siblings\n const mountRoot = dirname(absolutePath);\n const virtualPath = absolutePath;\n\n return { mountRoot, virtualPath };\n}\n\n/**\n * Run a Python script file using code-runner-mcp\n *\n * @param scriptPath - Path to the Python script (relative to baseDir)\n * @param options - Execution options\n * @returns The execution result\n */\nexport async function runPythonFile(\n scriptPath: string,\n options: RunPythonFileOptions = {},\n): Promise<any> {\n const {\n args = [],\n packages = {},\n baseDir = \"python\",\n filePaths = [],\n } = options;\n\n // Read the Python script\n const fullPath = join(__dirname, \"..\", baseDir, scriptPath);\n const scriptContent = readFileSync(fullPath, \"utf-8\");\n\n // Build wrapper code that sets sys.argv and executes the script\n const wrapperCode = `\nimport sys\nimport json\n\n# Set command line arguments\nsys.argv = ['${scriptPath}'] + ${JSON.stringify(args)}\n\n# Execute the script\n${scriptContent}\n`;\n\n // Determine mount root from the first file path\n let mountRoot = join(__dirname, \"..\"); // Default: project root\n if (filePaths.length > 0) {\n const mapping = getFileSystemMapping(filePaths[0]);\n mountRoot = mapping.mountRoot;\n }\n\n // Execute via runPy with options\n // Mount point is the same as the mount root (Pyodide will see host paths directly)\n const runPyOptions: RunPyOptions = {\n packages,\n nodeFSMountPoint: mountRoot,\n nodeFSRoot: mountRoot,\n };\n const stream = await runPy(wrapperCode, runPyOptions);\n\n // Read the stream output\n const reader = stream.getReader();\n const decoder = new TextDecoder();\n let stdout = \"\";\n let stderr = \"\";\n let error = \"\";\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n\n const chunk = decoder.decode(value, { stream: true });\n if (chunk.startsWith(\"[stderr] \")) {\n stderr += chunk.slice(9);\n } else if (chunk.startsWith(\"[err]\")) {\n error += chunk;\n } else {\n stdout += chunk;\n }\n }\n } catch (streamError) {\n // Stream error means Python execution failed\n return { error: String(streamError) };\n }\n\n // Check for errors\n if (error) {\n return { error: error.replace(/\\[err\\]\\[py\\]\\s*/g, \"\").trim() };\n }\n\n // Parse the JSON output from the script (last line)\n const lines = stdout.trim().split(\"\\n\");\n const lastLine = lines[lines.length - 1];\n\n try {\n return JSON.parse(lastLine);\n } catch {\n return { stdout, stderr };\n }\n}\n","/**\n * Utility functions for document processing\n */\n\n/**\n * Detect file type from file extension\n */\nexport function detectFileType(\n filePath: string,\n): \"excel\" | \"word\" | \"pdf\" | \"text\" | null {\n const ext = filePath.toLowerCase().split(\".\").pop();\n if (ext === \"xlsx\" || ext === \"xls\") return \"excel\";\n if (ext === \"docx\") return \"word\";\n if (ext === \"pdf\") return \"pdf\";\n if (\n ext === \"txt\" || ext === \"csv\" || ext === \"md\" || ext === \"json\" ||\n ext === \"yaml\" || ext === \"yml\"\n ) return \"text\";\n return null;\n}\n\n/**\n * Get required packages for each file type\n */\nexport function getPackages(fileType: string): Record<string, string> {\n const packages: Record<string, Record<string, string>> = {\n excel: { openpyxl: \"openpyxl\" },\n word: { docx: \"python-docx\" }, // Map docx import to python-docx package\n pdf: { PyPDF2: \"PyPDF2\" },\n text: {}, // No external packages needed for text files\n };\n return packages[fileType] || {};\n}\n\n/**\n * Get environment configuration\n */\nexport function getConfig() {\n return {\n rawFullRead: process.env.DOC_RAW_FULL_READ === \"true\",\n pageSize: parseInt(process.env.DOC_PAGE_SIZE || \"100\", 10),\n maxFileSize: parseInt(process.env.DOC_MAX_FILE_SIZE || \"50\", 10) * 1024 *\n 1024,\n };\n}\n","#!/usr/bin/env node\n\nimport { Server } from \"@modelcontextprotocol/sdk/server/index.js\";\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\";\nimport {\n CallToolRequestSchema,\n ListToolsRequestSchema,\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport { z } from \"zod\";\nimport { runPythonFile } from \"./code-runner.js\";\nimport { detectFileType, getConfig, getPackages } from \"./utils.js\";\n\n// Tool schemas\nconst ReadDocumentSchema = z.object({\n file_path: z.string().describe(\"Absolute path to the document file\"),\n mode: z.enum([\"raw\", \"paginated\"]).optional().describe(\n \"Read mode: 'raw' for full content, 'paginated' for chunked reading\",\n ),\n page: z.number().optional().describe(\n \"Page number for paginated mode (1-based)\",\n ),\n page_size: z.number().optional().describe(\n \"Items per page for paginated mode\",\n ),\n sheet_name: z.string().optional().describe(\"Sheet name for Excel files\"),\n});\n\nconst WriteDocumentSchema = z.object({\n file_path: z.string().describe(\"Absolute path to save the document\"),\n format: z.enum([\"excel\", \"word\", \"text\"]).describe(\"Document format\"),\n data: z.any().describe(\"Document data structure\"),\n});\n\nconst GetDocumentInfoSchema = z.object({\n file_path: z.string().describe(\"Absolute path to the document file\"),\n});\n\n// Server setup\nconst server = new Server(\n {\n name: \"docsmith-mcp\",\n version: \"0.1.0\",\n },\n {\n capabilities: {\n tools: {},\n },\n },\n);\n\n// Output schemas (reusable)\nconst BaseOutputSchema = {\n type: \"object\",\n properties: {\n success: { type: \"boolean\", description: \"Operation success status\" },\n error: { type: \"string\", description: \"Error message if failed\" },\n },\n} as const;\n\nconst PaginationSchema = {\n current_page: { type: \"number\", description: \"Current page number\" },\n page_size: { type: \"number\", description: \"Items per page\" },\n total_pages: { type: \"number\", description: \"Total number of pages\" },\n page: { type: \"number\", description: \"Current page number (alternative)\" },\n has_more: { type: \"boolean\", description: \"Whether more pages exist\" },\n} as const;\n\nconst ExcelReadOutputSchema = {\n type: \"object\",\n properties: {\n sheet_name: { type: \"string\", description: \"Active sheet name\" },\n sheets: {\n type: \"array\",\n items: { type: \"string\" },\n description: \"All sheet names\",\n },\n total_rows: { type: \"number\", description: \"Total rows in sheet\" },\n total_cols: { type: \"number\", description: \"Total columns in sheet\" },\n data: {\n type: \"array\",\n items: { type: \"array\", items: {} },\n description: \"Sheet data as array of rows\",\n },\n ...PaginationSchema,\n },\n} as const;\n\nconst WordReadOutputSchema = {\n type: \"object\",\n properties: {\n paragraphs: {\n type: \"array\",\n items: { type: \"string\" },\n description: \"Document paragraphs\",\n },\n tables: {\n type: \"array\",\n items: {\n type: \"array\",\n items: { type: \"array\", items: { type: \"string\" } },\n },\n description: \"Tables data\",\n },\n total_paragraphs: { type: \"number\", description: \"Total paragraph count\" },\n total_tables: { type: \"number\", description: \"Total table count\" },\n ...PaginationSchema,\n },\n} as const;\n\nconst PDFReadOutputSchema = {\n type: \"object\",\n properties: {\n total_pages: { type: \"number\", description: \"Total pages in PDF\" },\n content: {\n type: \"array\",\n items: {\n type: \"object\",\n properties: {\n page_number: { type: \"number\" },\n text: { type: \"string\" },\n },\n },\n description: \"Page content array\",\n },\n current_page_group: { type: \"number\" },\n page_size: { type: \"number\" },\n },\n} as const;\n\nconst TextReadOutputSchema = {\n type: \"object\",\n properties: {\n ...BaseOutputSchema.properties,\n content: { type: \"string\", description: \"Text content\" },\n total_lines: { type: \"number\", description: \"Total line count\" },\n encoding: { type: \"string\", description: \"File encoding\" },\n ...PaginationSchema,\n },\n} as const;\n\nconst CSVReadOutputSchema = {\n type: \"object\",\n properties: {\n ...BaseOutputSchema.properties,\n headers: {\n type: \"array\",\n items: { type: \"string\" },\n description: \"CSV headers\",\n },\n data: {\n type: \"array\",\n items: { type: \"object\" },\n description: \"Structured data as array of objects\",\n },\n total_rows: { type: \"number\", description: \"Total data rows\" },\n encoding: { type: \"string\", description: \"File encoding\" },\n ...PaginationSchema,\n },\n} as const;\n\nconst JSONReadOutputSchema = {\n type: \"object\",\n properties: {\n ...BaseOutputSchema.properties,\n data: { type: \"object\", description: \"Parsed JSON data\" },\n encoding: { type: \"string\", description: \"File encoding\" },\n },\n} as const;\n\nconst WriteOutputSchema = {\n type: \"object\",\n properties: {\n success: { type: \"boolean\", description: \"Write operation success\" },\n file_path: { type: \"string\", description: \"Written file path\" },\n message: { type: \"string\", description: \"Success message\" },\n error: { type: \"string\", description: \"Error message if failed\" },\n },\n} as const;\n\nconst ExcelInfoOutputSchema = {\n type: \"object\",\n properties: {\n sheets: {\n type: \"array\",\n items: {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n rows: { type: \"number\" },\n cols: { type: \"number\" },\n },\n },\n description: \"Sheet information\",\n },\n file_size: { type: \"number\", description: \"File size in bytes\" },\n },\n} as const;\n\nconst WordInfoOutputSchema = {\n type: \"object\",\n properties: {\n paragraphs: { type: \"number\", description: \"Paragraph count\" },\n tables: { type: \"number\", description: \"Table count\" },\n file_size: { type: \"number\", description: \"File size in bytes\" },\n },\n} as const;\n\nconst PDFInfoOutputSchema = {\n type: \"object\",\n properties: {\n pages: { type: \"number\", description: \"Page count\" },\n file_size: { type: \"number\", description: \"File size in bytes\" },\n total_words: { type: \"number\", description: \"Total word count\" },\n },\n} as const;\n\nconst TextInfoOutputSchema = {\n type: \"object\",\n properties: {\n ...BaseOutputSchema.properties,\n file_size: { type: \"number\", description: \"File size in bytes\" },\n line_count: { type: \"number\", description: \"Line count\" },\n encoding: { type: \"string\", description: \"File encoding\" },\n file_type: { type: \"string\", description: \"File extension\" },\n // CSV specific\n headers: { type: \"array\", items: { type: \"string\" } },\n total_rows: { type: \"number\" },\n total_cols: { type: \"number\" },\n // JSON specific\n item_count: { type: \"number\" },\n key_count: { type: \"number\" },\n },\n} as const;\n\n// List available tools\nserver.setRequestHandler(ListToolsRequestSchema, async () => {\n return {\n tools: [\n {\n name: \"read_document\",\n description:\n \"Read document content (Excel, Word, PDF, TXT, CSV, Markdown, JSON, YAML). Supports raw full read or paginated mode.\",\n inputSchema: {\n type: \"object\",\n properties: {\n file_path: {\n type: \"string\",\n description: \"Absolute path to the document file\",\n },\n mode: {\n type: \"string\",\n enum: [\"raw\", \"paginated\"],\n description: \"Read mode\",\n },\n page: {\n type: \"number\",\n description: \"Page number for paginated mode\",\n },\n page_size: { type: \"number\", description: \"Items per page\" },\n sheet_name: {\n type: \"string\",\n description: \"Sheet name for Excel files\",\n },\n },\n required: [\"file_path\"],\n },\n outputSchema: {\n type: \"object\",\n description:\n \"Returns different structures based on file type: Excel (sheet data), Word (paragraphs/tables), PDF (page content), Text (plain text), CSV (structured rows), JSON (parsed object)\",\n oneOf: [\n ExcelReadOutputSchema,\n WordReadOutputSchema,\n PDFReadOutputSchema,\n TextReadOutputSchema,\n CSVReadOutputSchema,\n JSONReadOutputSchema,\n ],\n },\n },\n {\n name: \"write_document\",\n description: \"Write document content (Excel, Word, Text)\",\n inputSchema: {\n type: \"object\",\n properties: {\n file_path: {\n type: \"string\",\n description: \"Absolute path to save the document\",\n },\n format: {\n type: \"string\",\n enum: [\"excel\", \"word\", \"text\"],\n description: \"Document format\",\n },\n data: {\n description:\n \"Document data structure. Excel: array of rows [[cell1, cell2], ...]. Word: {paragraphs: string[], tables?: [[[cell]]]}. Text/CSV/JSON: string or object\",\n },\n },\n required: [\"file_path\", \"format\", \"data\"],\n },\n outputSchema: WriteOutputSchema,\n },\n {\n name: \"get_document_info\",\n description:\n \"Get document metadata (page count, sheet count, file size, etc.)\",\n inputSchema: {\n type: \"object\",\n properties: {\n file_path: {\n type: \"string\",\n description: \"Absolute path to the document file\",\n },\n },\n required: [\"file_path\"],\n },\n outputSchema: {\n type: \"object\",\n description: \"Returns metadata based on file type\",\n oneOf: [\n ExcelInfoOutputSchema,\n WordInfoOutputSchema,\n PDFInfoOutputSchema,\n TextInfoOutputSchema,\n ],\n },\n },\n ],\n };\n});\n\n// Handle tool calls\nserver.setRequestHandler(CallToolRequestSchema, async (request) => {\n const { name, arguments: args } = request.params;\n\n try {\n if (name === \"read_document\") {\n const params = ReadDocumentSchema.parse(args);\n const fileType = detectFileType(params.file_path);\n\n if (!fileType) {\n throw new Error(`Unsupported file type: ${params.file_path}`);\n }\n\n // Determine read mode\n const config = getConfig();\n const mode = params.mode || (config.rawFullRead ? \"raw\" : \"paginated\");\n const page = mode === \"paginated\" ? (params.page || 1) : undefined;\n const pageSize = params.page_size || config.pageSize;\n\n let scriptName: string;\n let scriptArgs: string[];\n\n if (fileType === \"excel\") {\n scriptName = \"excel_handler.py\";\n scriptArgs = [\"read\", params.file_path];\n if (params.sheet_name) scriptArgs.push(params.sheet_name);\n if (page) {\n scriptArgs.push(String(page));\n scriptArgs.push(String(pageSize));\n }\n } else if (fileType === \"word\") {\n scriptName = \"word_handler.py\";\n scriptArgs = [\"read\", params.file_path];\n if (page) {\n scriptArgs.push(String(page));\n scriptArgs.push(String(pageSize));\n }\n } else if (fileType === \"pdf\") {\n scriptName = \"pdf_handler.py\";\n scriptArgs = [\"read\", params.file_path];\n if (page) {\n scriptArgs.push(String(page));\n scriptArgs.push(String(Math.min(pageSize, 10))); // PDF pages are larger\n }\n } else {\n // text files\n scriptName = \"text_handler.py\";\n scriptArgs = [\"read\", params.file_path];\n if (page) {\n scriptArgs.push(String(page));\n scriptArgs.push(String(pageSize));\n }\n }\n\n const result = await runPythonFile(scriptName, {\n args: scriptArgs,\n packages: getPackages(fileType),\n filePaths: [params.file_path],\n });\n return {\n content: [{\n type: \"text\",\n text: JSON.stringify(result, null, 2),\n }],\n structuredContent: result,\n };\n }\n\n if (name === \"write_document\") {\n const params = WriteDocumentSchema.parse(args);\n\n let scriptName: string;\n let scriptArgs: string[];\n\n if (params.format === \"excel\") {\n scriptName = \"excel_handler.py\";\n scriptArgs = [\"write\", params.file_path, JSON.stringify(params.data)];\n } else if (params.format === \"word\") {\n scriptName = \"word_handler.py\";\n const paragraphs = params.data.paragraphs || [];\n const tables = params.data.tables || null;\n scriptArgs = [\"write\", params.file_path, JSON.stringify(paragraphs)];\n if (tables) scriptArgs.push(JSON.stringify(tables));\n } else if (params.format === \"text\") {\n scriptName = \"text_handler.py\";\n const content = typeof params.data === \"string\"\n ? params.data\n : JSON.stringify(params.data);\n scriptArgs = [\"write\", params.file_path, content];\n } else {\n throw new Error(`Unsupported write format: ${params.format}`);\n }\n\n const result = await runPythonFile(scriptName, {\n args: scriptArgs,\n packages: getPackages(params.format),\n filePaths: [params.file_path],\n });\n return {\n content: [{\n type: \"text\",\n text: JSON.stringify(result, null, 2),\n }],\n structuredContent: result,\n };\n }\n\n if (name === \"get_document_info\") {\n const params = GetDocumentInfoSchema.parse(args);\n const fileType = detectFileType(params.file_path);\n\n if (!fileType) {\n throw new Error(`Unsupported file type: ${params.file_path}`);\n }\n\n let scriptName: string;\n let scriptArgs = [\"info\", params.file_path];\n\n if (fileType === \"excel\") {\n scriptName = \"excel_handler.py\";\n } else if (fileType === \"word\") {\n scriptName = \"word_handler.py\";\n } else if (fileType === \"pdf\") {\n scriptName = \"pdf_handler.py\";\n } else {\n scriptName = \"text_handler.py\";\n }\n\n const result = await runPythonFile(scriptName, {\n args: scriptArgs,\n packages: getPackages(fileType),\n filePaths: [params.file_path],\n });\n return {\n content: [{\n type: \"text\",\n text: JSON.stringify(result, null, 2),\n }],\n structuredContent: result,\n };\n }\n\n throw new Error(`Unknown tool: ${name}`);\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n return {\n content: [{ type: \"text\", text: `Error: ${errorMessage}` }],\n isError: true,\n };\n }\n});\n\n// Start server\nasync function main() {\n const transport = new StdioServerTransport();\n await server.connect(transport);\n console.error(\"Docsmith MCP server running on stdio\");\n}\n\nmain().catch((error) => {\n console.error(\"Server error:\", error);\n process.exit(1);\n});\n"],"mappings":";;;;;;;;;;;AAQA,MAAM,aAAa,cAAc,OAAO,KAAK,IAAI;AACjD,MAAM,YAAY,QAAQ,WAAW;;;;;;;;AAuBrC,SAAS,qBACPA,UAC4C;CAC5C,MAAM,eAAe,QAAQ,SAAS;CAItC,MAAM,YAAY,QAAQ,aAAa;CACvC,MAAM,cAAc;AAEpB,QAAO;EAAE;EAAW;CAAa;AAClC;;;;;;;;AASD,eAAsB,cACpBC,YACAC,UAAgC,CAAE,GACpB;CACd,MAAM,EACJ,OAAO,CAAE,GACT,WAAW,CAAE,GACb,UAAU,UACV,YAAY,CAAE,GACf,GAAG;CAGJ,MA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AAM,UAAU,OAAO,MAAM;AAC3E,SAAO;GACL,SAAS,CAAC;IAAE,MAAM;IAAQ,OAAO,SAAS,aAAa;GAAG,CAAC;GAC3D,SAAS;EACV;CACF;AACF,EAAC;AAGF,eAAe,OAAO;CACpB,MAAM,YAAY,IAAI;AACtB,OAAM,OAAO,QAAQ,UAAU;AAC/B,SAAQ,MAAM,uCAAuC;AACtD;AAED,MAAM,CAAC,MAAM,CAAC,UAAU;AACtB,SAAQ,MAAM,iBAAiB,MAAM;AACrC,SAAQ,KAAK,EAAE;AAChB,EAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["filePath: string","scriptPath: string","options: RunPythonFileOptions","runPyOptions: RunPyOptions","filePath: string","fileType: string","packages: Record<string, Record<string, string>>","scriptName: string","scriptArgs: string[]"],"sources":["../src/code-runner.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":["/**\n * Code runner client - uses @mcpc-tech/code-runner-mcp npm package\n */\nimport { runPy, type RunPyOptions } from \"@mcpc-tech/code-runner-mcp\";\nimport { readFileSync } from \"fs\";\nimport { fileURLToPath } from \"url\";\nimport { dirname, join, resolve } from \"path\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);\n\n/**\n * Options for running Python script files\n */\nexport interface RunPythonFileOptions {\n /** Command line arguments to pass to the script */\n args?: string[];\n /** Package name mappings (import_name -> pypi_name) */\n packages?: Record<string, string>;\n /** Base directory for the script (default: \"python\") */\n baseDir?: string;\n /** User file paths that need to be accessible (for file system mounting) */\n filePaths?: string[];\n}\n\n/**\n * Convert absolute file path to Pyodide virtual path\n * Determines the mount root and converts the path accordingly\n *\n * @param filePath - Absolute path to the file\n * @returns Object with mountRoot (host path) and virtualPath (Pyodide path)\n */\nfunction getFileSystemMapping(\n filePath: string,\n): { mountRoot: string; virtualPath: string } {\n const absolutePath = resolve(filePath);\n\n // Mount the parent directory of the file\n // This allows Python to access the file and its siblings\n const mountRoot = dirname(absolutePath);\n const virtualPath = absolutePath;\n\n return { mountRoot, virtualPath };\n}\n\n/**\n * Run a Python script file using code-runner-mcp\n *\n * @param scriptPath - Path to the Python script (relative to baseDir)\n * @param options - Execution options\n * @returns The execution result\n */\nexport async function runPythonFile(\n scriptPath: string,\n options: RunPythonFileOptions = {},\n): Promise<any> {\n const {\n args = [],\n packages = {},\n baseDir = \"python\",\n filePaths = [],\n } = options;\n\n // Read the Python script\n // Python files are in the python/ directory at project root\n // From dist/index.js, go up one level to reach python/\n const fullPath = join(__dirname, \"..\", baseDir, scriptPath);\n const scriptContent = readFileSync(fullPath, \"utf-8\");\n\n // Build wrapper code that sets sys.argv and executes the script\n const wrapperCode = `\nimport sys\nimport json\n\n# Set command line arguments\nsys.argv = ['${scriptPath}'] + ${JSON.stringify(args)}\n\n# Execute the script\n${scriptContent}\n`;\n\n // Determine mount root from the first file path\n // Default: parent directory of dist/ (project root when running from dist/index.js)\n let mountRoot = join(__dirname, \"..\");\n if (filePaths.length > 0) {\n const mapping = getFileSystemMapping(filePaths[0]);\n mountRoot = mapping.mountRoot;\n }\n\n // Execute via runPy with options\n // Mount point is the same as the mount root (Pyodide will see host paths directly)\n const runPyOptions: RunPyOptions = {\n packages,\n nodeFSMountPoint: mountRoot,\n nodeFSRoot: mountRoot,\n };\n const stream = await runPy(wrapperCode, runPyOptions);\n\n // Read the stream output\n const reader = stream.getReader();\n const decoder = new TextDecoder();\n let stdout = \"\";\n let stderr = \"\";\n let error = \"\";\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n\n const chunk = decoder.decode(value, { stream: true });\n if (chunk.startsWith(\"[stderr] \")) {\n stderr += chunk.slice(9);\n } else if (chunk.startsWith(\"[err]\")) {\n error += chunk;\n } else {\n stdout += chunk;\n }\n }\n } catch (streamError) {\n // Stream error means Python execution failed\n return { error: String(streamError) };\n }\n\n // Check for errors\n if (error) {\n return { error: error.replace(/\\[err\\]\\[py\\]\\s*/g, \"\").trim() };\n }\n\n // Parse the JSON output from the script (last line)\n const lines = stdout.trim().split(\"\\n\");\n const lastLine = lines[lines.length - 1];\n\n try {\n return JSON.parse(lastLine);\n } catch {\n return { stdout, stderr };\n }\n}\n","/**\n * Utility functions for document processing\n */\n\n/**\n * Detect file type from file extension\n */\nexport function detectFileType(\n filePath: string,\n): \"excel\" | \"word\" | \"pdf\" | \"text\" | null {\n const ext = filePath.toLowerCase().split(\".\").pop();\n if (ext === \"xlsx\" || ext === \"xls\") return \"excel\";\n if (ext === \"docx\") return \"word\";\n if (ext === \"pdf\") return \"pdf\";\n if (\n ext === \"txt\" || ext === \"csv\" || ext === \"md\" || ext === \"json\" ||\n ext === \"yaml\" || ext === \"yml\"\n ) return \"text\";\n return null;\n}\n\n/**\n * Get required packages for each file type\n */\nexport function getPackages(fileType: string): Record<string, string> {\n const packages: Record<string, Record<string, string>> = {\n excel: { openpyxl: \"openpyxl\" },\n word: { docx: \"python-docx\" }, // Map docx import to python-docx package\n pdf: { PyPDF2: \"PyPDF2\" },\n text: {}, // No external packages needed for text files\n };\n return packages[fileType] || {};\n}\n\n/**\n * Get environment configuration\n */\nexport function getConfig() {\n return {\n rawFullRead: process.env.DOC_RAW_FULL_READ === \"true\",\n pageSize: parseInt(process.env.DOC_PAGE_SIZE || \"100\", 10),\n maxFileSize: parseInt(process.env.DOC_MAX_FILE_SIZE || \"50\", 10) * 1024 *\n 1024,\n };\n}\n","#!/usr/bin/env node\n\nimport { Server } from \"@modelcontextprotocol/sdk/server/index.js\";\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\";\nimport {\n CallToolRequestSchema,\n ListToolsRequestSchema,\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport { z } from \"zod\";\nimport { runPythonFile } from \"./code-runner.js\";\nimport { detectFileType, getConfig, getPackages } from \"./utils.js\";\n\n// Tool schemas\nconst ReadDocumentSchema = z.object({\n file_path: z.string().describe(\"Absolute path to the document file\"),\n mode: z.enum([\"raw\", \"paginated\"]).optional().describe(\n \"Read mode: 'raw' for full content, 'paginated' for chunked reading\",\n ),\n page: z.number().optional().describe(\n \"Page number for paginated mode (1-based)\",\n ),\n page_size: z.number().optional().describe(\n \"Items per page for paginated mode\",\n ),\n sheet_name: z.string().optional().describe(\"Sheet name for Excel files\"),\n});\n\nconst WriteDocumentSchema = z.object({\n file_path: z.string().describe(\"Absolute path to save the document\"),\n format: z.enum([\"excel\", \"word\", \"text\"]).describe(\"Document format\"),\n data: z.any().describe(\"Document data structure\"),\n});\n\nconst GetDocumentInfoSchema = z.object({\n file_path: z.string().describe(\"Absolute path to the document file\"),\n});\n\n// Server setup\nconst server = new Server(\n {\n name: \"docsmith-mcp\",\n version: \"0.1.0\",\n },\n {\n capabilities: {\n tools: {},\n },\n },\n);\n\n// Output schemas (reusable)\nconst BaseOutputSchema = {\n type: \"object\",\n properties: {\n success: { type: \"boolean\", description: \"Operation success status\" },\n error: { type: \"string\", description: \"Error message if failed\" },\n },\n} as const;\n\nconst PaginationSchema = {\n current_page: { type: \"number\", description: \"Current page number\" },\n page_size: { type: \"number\", description: \"Items per page\" },\n total_pages: { type: \"number\", description: \"Total number of pages\" },\n page: { type: \"number\", description: \"Current page number (alternative)\" },\n has_more: { type: \"boolean\", description: \"Whether more pages exist\" },\n} as const;\n\nconst ExcelReadOutputSchema = {\n type: \"object\",\n properties: {\n sheet_name: { type: \"string\", description: \"Active sheet name\" },\n sheets: {\n type: \"array\",\n items: { type: \"string\" },\n description: \"All sheet names\",\n },\n total_rows: { type: \"number\", description: \"Total rows in sheet\" },\n total_cols: { type: \"number\", description: \"Total columns in sheet\" },\n data: {\n type: \"array\",\n items: { type: \"array\", items: {} },\n description: \"Sheet data as array of rows\",\n },\n ...PaginationSchema,\n },\n} as const;\n\nconst WordReadOutputSchema = {\n type: \"object\",\n properties: {\n paragraphs: {\n type: \"array\",\n items: { type: \"string\" },\n description: \"Document paragraphs\",\n },\n tables: {\n type: \"array\",\n items: {\n type: \"array\",\n items: { type: \"array\", items: { type: \"string\" } },\n },\n description: \"Tables data\",\n },\n total_paragraphs: { type: \"number\", description: \"Total paragraph count\" },\n total_tables: { type: \"number\", description: \"Total table count\" },\n ...PaginationSchema,\n },\n} as const;\n\nconst PDFReadOutputSchema = {\n type: \"object\",\n properties: {\n total_pages: { type: \"number\", description: \"Total pages in PDF\" },\n content: {\n type: \"array\",\n items: {\n type: \"object\",\n properties: {\n page_number: { type: \"number\" },\n text: { type: \"string\" },\n },\n },\n description: \"Page content array\",\n },\n current_page_group: { type: \"number\" },\n page_size: { type: \"number\" },\n },\n} as const;\n\nconst TextReadOutputSchema = {\n type: \"object\",\n properties: {\n ...BaseOutputSchema.properties,\n content: { type: \"string\", description: \"Text content\" },\n total_lines: { type: \"number\", description: \"Total line count\" },\n encoding: { type: \"string\", description: \"File encoding\" },\n ...PaginationSchema,\n },\n} as const;\n\nconst CSVReadOutputSchema = {\n type: \"object\",\n properties: {\n ...BaseOutputSchema.properties,\n headers: {\n type: \"array\",\n items: { type: \"string\" },\n description: \"CSV headers\",\n },\n data: {\n type: \"array\",\n items: { type: \"object\" },\n description: \"Structured data as array of objects\",\n },\n total_rows: { type: \"number\", description: \"Total data rows\" },\n encoding: { type: \"string\", description: \"File encoding\" },\n ...PaginationSchema,\n },\n} as const;\n\nconst JSONReadOutputSchema = {\n type: \"object\",\n properties: {\n ...BaseOutputSchema.properties,\n data: { type: \"object\", description: \"Parsed JSON data\" },\n encoding: { type: \"string\", description: \"File encoding\" },\n },\n} as const;\n\nconst WriteOutputSchema = {\n type: \"object\",\n properties: {\n success: { type: \"boolean\", description: \"Write operation success\" },\n file_path: { type: \"string\", description: \"Written file path\" },\n message: { type: \"string\", description: \"Success message\" },\n error: { type: \"string\", description: \"Error message if failed\" },\n },\n} as const;\n\nconst ExcelInfoOutputSchema = {\n type: \"object\",\n properties: {\n sheets: {\n type: \"array\",\n items: {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n rows: { type: \"number\" },\n cols: { type: \"number\" },\n },\n },\n description: \"Sheet information\",\n },\n file_size: { type: \"number\", description: \"File size in bytes\" },\n },\n} as const;\n\nconst WordInfoOutputSchema = {\n type: \"object\",\n properties: {\n paragraphs: { type: \"number\", description: \"Paragraph count\" },\n tables: { type: \"number\", description: \"Table count\" },\n file_size: { type: \"number\", description: \"File size in bytes\" },\n },\n} as const;\n\nconst PDFInfoOutputSchema = {\n type: \"object\",\n properties: {\n pages: { type: \"number\", description: \"Page count\" },\n file_size: { type: \"number\", description: \"File size in bytes\" },\n total_words: { type: \"number\", description: \"Total word count\" },\n },\n} as const;\n\nconst TextInfoOutputSchema = {\n type: \"object\",\n properties: {\n ...BaseOutputSchema.properties,\n file_size: { type: \"number\", description: \"File size in bytes\" },\n line_count: { type: \"number\", description: \"Line count\" },\n encoding: { type: \"string\", description: \"File encoding\" },\n file_type: { type: \"string\", description: \"File extension\" },\n // CSV specific\n headers: { type: \"array\", items: { type: \"string\" } },\n total_rows: { type: \"number\" },\n total_cols: { type: \"number\" },\n // JSON specific\n item_count: { type: \"number\" },\n key_count: { type: \"number\" },\n },\n} as const;\n\n// List available tools\nserver.setRequestHandler(ListToolsRequestSchema, async () => {\n return {\n tools: [\n {\n name: \"read_document\",\n description:\n \"Read document content (Excel, Word, PDF, TXT, CSV, Markdown, JSON, YAML). Supports raw full read or paginated mode.\",\n inputSchema: {\n type: \"object\",\n properties: {\n file_path: {\n type: \"string\",\n description: \"Absolute path to the document file\",\n },\n mode: {\n type: \"string\",\n enum: [\"raw\", \"paginated\"],\n description: \"Read mode\",\n },\n page: {\n type: \"number\",\n description: \"Page number for paginated mode\",\n },\n page_size: { type: \"number\", description: \"Items per page\" },\n sheet_name: {\n type: \"string\",\n description: \"Sheet name for Excel files\",\n },\n },\n required: [\"file_path\"],\n },\n outputSchema: {\n type: \"object\",\n description:\n \"Returns different structures based on file type: Excel (sheet data), Word (paragraphs/tables), PDF (page content), Text (plain text), CSV (structured rows), JSON (parsed object)\",\n oneOf: [\n ExcelReadOutputSchema,\n WordReadOutputSchema,\n PDFReadOutputSchema,\n TextReadOutputSchema,\n CSVReadOutputSchema,\n JSONReadOutputSchema,\n ],\n },\n },\n {\n name: \"write_document\",\n description: \"Write document content (Excel, Word, Text)\",\n inputSchema: {\n type: \"object\",\n properties: {\n file_path: {\n type: \"string\",\n description: \"Absolute path to save the document\",\n },\n format: {\n type: \"string\",\n enum: [\"excel\", \"word\", \"text\"],\n description: \"Document format\",\n },\n data: {\n description:\n \"Document data structure. Excel: array of rows [[cell1, cell2], ...]. Word: {paragraphs: string[], tables?: [[[cell]]]}. Text/CSV/JSON: string or object\",\n },\n },\n required: [\"file_path\", \"format\", \"data\"],\n },\n outputSchema: WriteOutputSchema,\n },\n {\n name: \"get_document_info\",\n description:\n \"Get document metadata (page count, sheet count, file size, etc.)\",\n inputSchema: {\n type: \"object\",\n properties: {\n file_path: {\n type: \"string\",\n description: \"Absolute path to the document file\",\n },\n },\n required: [\"file_path\"],\n },\n outputSchema: {\n type: \"object\",\n description: \"Returns metadata based on file type\",\n oneOf: [\n ExcelInfoOutputSchema,\n WordInfoOutputSchema,\n PDFInfoOutputSchema,\n TextInfoOutputSchema,\n ],\n },\n },\n ],\n };\n});\n\n// Handle tool calls\nserver.setRequestHandler(CallToolRequestSchema, async (request) => {\n const { name, arguments: args } = request.params;\n\n try {\n if (name === \"read_document\") {\n const params = ReadDocumentSchema.parse(args);\n const fileType = detectFileType(params.file_path);\n\n if (!fileType) {\n throw new Error(`Unsupported file type: ${params.file_path}`);\n }\n\n // Determine read mode\n const config = getConfig();\n const mode = params.mode || (config.rawFullRead ? \"raw\" : \"paginated\");\n const page = mode === \"paginated\" ? (params.page || 1) : undefined;\n const pageSize = params.page_size || config.pageSize;\n\n let scriptName: string;\n let scriptArgs: string[];\n\n if (fileType === \"excel\") {\n scriptName = \"excel_handler.py\";\n scriptArgs = [\"read\", params.file_path];\n if (params.sheet_name) scriptArgs.push(params.sheet_name);\n if (page) {\n scriptArgs.push(String(page));\n scriptArgs.push(String(pageSize));\n }\n } else if (fileType === \"word\") {\n scriptName = \"word_handler.py\";\n scriptArgs = [\"read\", params.file_path];\n if (page) {\n scriptArgs.push(String(page));\n scriptArgs.push(String(pageSize));\n }\n } else if (fileType === \"pdf\") {\n scriptName = \"pdf_handler.py\";\n scriptArgs = [\"read\", params.file_path];\n if (page) {\n scriptArgs.push(String(page));\n scriptArgs.push(String(Math.min(pageSize, 10))); // PDF pages are larger\n }\n } else {\n // text files\n scriptName = \"text_handler.py\";\n scriptArgs = [\"read\", params.file_path];\n if (page) {\n scriptArgs.push(String(page));\n scriptArgs.push(String(pageSize));\n }\n }\n\n const result = await runPythonFile(scriptName, {\n args: scriptArgs,\n packages: getPackages(fileType),\n filePaths: [params.file_path],\n });\n return {\n content: [{\n type: \"text\",\n text: JSON.stringify(result, null, 2),\n }],\n structuredContent: result,\n };\n }\n\n if (name === \"write_document\") {\n const params = WriteDocumentSchema.parse(args);\n\n let scriptName: string;\n let scriptArgs: string[];\n\n if (params.format === \"excel\") {\n scriptName = \"excel_handler.py\";\n scriptArgs = [\"write\", params.file_path, JSON.stringify(params.data)];\n } else if (params.format === \"word\") {\n scriptName = \"word_handler.py\";\n const paragraphs = params.data.paragraphs || [];\n const tables = params.data.tables || null;\n scriptArgs = [\"write\", params.file_path, JSON.stringify(paragraphs)];\n if (tables) scriptArgs.push(JSON.stringify(tables));\n } else if (params.format === \"text\") {\n scriptName = \"text_handler.py\";\n const content = typeof params.data === \"string\"\n ? params.data\n : JSON.stringify(params.data);\n scriptArgs = [\"write\", params.file_path, content];\n } else {\n throw new Error(`Unsupported write format: ${params.format}`);\n }\n\n const result = await runPythonFile(scriptName, {\n args: scriptArgs,\n packages: getPackages(params.format),\n filePaths: [params.file_path],\n });\n return {\n content: [{\n type: \"text\",\n text: JSON.stringify(result, null, 2),\n }],\n structuredContent: result,\n };\n }\n\n if (name === \"get_document_info\") {\n const params = GetDocumentInfoSchema.parse(args);\n const fileType = detectFileType(params.file_path);\n\n if (!fileType) {\n throw new Error(`Unsupported file type: ${params.file_path}`);\n }\n\n let scriptName: string;\n let scriptArgs = [\"info\", params.file_path];\n\n if (fileType === \"excel\") {\n scriptName = \"excel_handler.py\";\n } else if (fileType === \"word\") {\n scriptName = \"word_handler.py\";\n } else if (fileType === \"pdf\") {\n scriptName = \"pdf_handler.py\";\n } else {\n scriptName = \"text_handler.py\";\n }\n\n const result = await runPythonFile(scriptName, {\n args: scriptArgs,\n packages: getPackages(fileType),\n filePaths: [params.file_path],\n });\n return {\n content: [{\n type: \"text\",\n text: JSON.stringify(result, null, 2),\n }],\n structuredContent: result,\n };\n }\n\n throw new Error(`Unknown tool: ${name}`);\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n return {\n content: [{ type: \"text\", text: `Error: ${errorMessage}` }],\n isError: true,\n };\n }\n});\n\n// Start server\nasync function main() {\n const transport = new StdioServerTransport();\n await server.connect(transport);\n console.error(\"Docsmith MCP server running on stdio\");\n}\n\nmain().catch((error) => {\n console.error(\"Server error:\", error);\n process.exit(1);\n});\n"],"mappings":";;;;;;;;;;;AAQA,MAAM,aAAa,cAAc,OAAO,KAAK,IAAI;AACjD,MAAM,YAAY,QAAQ,WAAW;;;;;;;;AAuBrC,SAAS,qBACPA,UAC4C;CAC5C,MAAM,eAAe,QAAQ,SAAS;CAItC,MAAM,YAAY,QAAQ,aAAa;CACvC,MAAM,cAAc;AAEpB,QAAO;EAAE;EAAW;CAAa;AAClC;;;;;;;;AASD,eAAsB,cACpBC,YACAC,UAAgC,CAAE,GACpB;CACd,MAAM,EACJ,OAAO,CAAE,GACT,WAAW,CAAE,GACb,UAAU,UACV,YAAY,CAAE,GACf,GAAG;CAKJ,MAAM,WAAW,KAAK,WAAW,MAAM,SAAS,WAAW;CAC3D,MAAM,gBAAgB,aAAa,UAAU,QAAQ;CAGrD,MAAM,eAAe;;;;;eAKR,WAAW,OAAO,KAAK,UAAU,KAAK,CAAC;;;EAGpD,cAAc;;CAKd,IAAI,YAAY,KAAK,WAAW,KAAK;AACrC,KAAI,U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AC/B,SAAQ,MAAM,uCAAuC;AACtD;AAED,MAAM,CAAC,MAAM,CAAC,UAAU;AACtB,SAAQ,MAAM,iBAAiB,MAAM;AACrC,SAAQ,KAAK,EAAE;AAChB,EAAC"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "docsmith-mcp",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.2",
|
|
4
4
|
"description": "Python-powered document processing MCP for Excel, Word, PDF",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "dist/index.js",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"bin": {
|
|
22
|
+
"docsmith-mcp": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"python",
|
|
27
|
+
"scripts"
|
|
28
|
+
],
|
|
7
29
|
"scripts": {
|
|
8
30
|
"build": "tsdown",
|
|
9
31
|
"dev": "tsdown --watch",
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
name: Test
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches: [main, master]
|
|
6
|
-
pull_request:
|
|
7
|
-
branches: [main, master]
|
|
8
|
-
|
|
9
|
-
jobs:
|
|
10
|
-
test:
|
|
11
|
-
runs-on: ubuntu-latest
|
|
12
|
-
|
|
13
|
-
steps:
|
|
14
|
-
- name: Checkout code
|
|
15
|
-
uses: actions/checkout@v4
|
|
16
|
-
|
|
17
|
-
- name: Setup pnpm
|
|
18
|
-
uses: pnpm/action-setup@v2
|
|
19
|
-
with:
|
|
20
|
-
version: 9
|
|
21
|
-
|
|
22
|
-
- name: Setup Node.js
|
|
23
|
-
uses: actions/setup-node@v4
|
|
24
|
-
with:
|
|
25
|
-
node-version: 24
|
|
26
|
-
cache: "pnpm"
|
|
27
|
-
|
|
28
|
-
- name: Install dependencies
|
|
29
|
-
run: pnpm install --frozen-lockfile
|
|
30
|
-
|
|
31
|
-
- name: Run tests
|
|
32
|
-
run: pnpm test:run
|
|
33
|
-
|
|
34
|
-
- name: Build
|
|
35
|
-
run: pnpm build
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Excel document handler - read/write Excel files
|
|
3
|
-
"""
|
|
4
|
-
import json
|
|
5
|
-
import sys
|
|
6
|
-
from pathlib import Path
|
|
7
|
-
|
|
8
|
-
def read_excel(file_path: str, sheet_name: str = None, page: int = None, page_size: int = 100):
|
|
9
|
-
"""Read Excel file with optional pagination"""
|
|
10
|
-
import openpyxl
|
|
11
|
-
|
|
12
|
-
wb = openpyxl.load_workbook(file_path, data_only=True)
|
|
13
|
-
|
|
14
|
-
if sheet_name is None:
|
|
15
|
-
sheet_name = wb.sheetnames[0]
|
|
16
|
-
|
|
17
|
-
ws = wb[sheet_name]
|
|
18
|
-
|
|
19
|
-
# Get all data
|
|
20
|
-
data = []
|
|
21
|
-
for row in ws.iter_rows(values_only=True):
|
|
22
|
-
data.append(row)
|
|
23
|
-
|
|
24
|
-
# Handle pagination
|
|
25
|
-
if page is not None:
|
|
26
|
-
start = (page - 1) * page_size
|
|
27
|
-
end = start + page_size
|
|
28
|
-
data = data[start:end]
|
|
29
|
-
total_pages = (len(data) + page_size - 1) // page_size if data else 1
|
|
30
|
-
else:
|
|
31
|
-
total_pages = 1
|
|
32
|
-
|
|
33
|
-
return {
|
|
34
|
-
"sheet_name": sheet_name,
|
|
35
|
-
"sheets": wb.sheetnames,
|
|
36
|
-
"total_rows": ws.max_row,
|
|
37
|
-
"total_cols": ws.max_column,
|
|
38
|
-
"current_page": page,
|
|
39
|
-
"page_size": page_size if page else None,
|
|
40
|
-
"total_pages": total_pages,
|
|
41
|
-
"data": data
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
def get_excel_info(file_path: str):
|
|
45
|
-
"""Get Excel file metadata"""
|
|
46
|
-
import openpyxl
|
|
47
|
-
|
|
48
|
-
wb = openpyxl.load_workbook(file_path, data_only=True)
|
|
49
|
-
info = {
|
|
50
|
-
"sheets": [],
|
|
51
|
-
"file_size": Path(file_path).stat().st_size
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
for sheet_name in wb.sheetnames:
|
|
55
|
-
ws = wb[sheet_name]
|
|
56
|
-
info["sheets"].append({
|
|
57
|
-
"name": sheet_name,
|
|
58
|
-
"rows": ws.max_row,
|
|
59
|
-
"cols": ws.max_column
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
return info
|
|
63
|
-
|
|
64
|
-
def write_excel(file_path: str, data: list, sheet_name: str = "Sheet1"):
|
|
65
|
-
"""Write data to Excel file"""
|
|
66
|
-
import openpyxl
|
|
67
|
-
|
|
68
|
-
wb = openpyxl.Workbook()
|
|
69
|
-
ws = wb.active
|
|
70
|
-
ws.title = sheet_name
|
|
71
|
-
|
|
72
|
-
for row in data:
|
|
73
|
-
ws.append(row)
|
|
74
|
-
|
|
75
|
-
wb.save(file_path)
|
|
76
|
-
return {"success": True, "file_path": file_path}
|
|
77
|
-
|
|
78
|
-
if __name__ == "__main__":
|
|
79
|
-
command = sys.argv[1]
|
|
80
|
-
file_path = sys.argv[2]
|
|
81
|
-
|
|
82
|
-
if command == "read":
|
|
83
|
-
sheet = sys.argv[3] if len(sys.argv) > 3 else None
|
|
84
|
-
page = int(sys.argv[4]) if len(sys.argv) > 4 else None
|
|
85
|
-
page_size = int(sys.argv[5]) if len(sys.argv) > 5 else 100
|
|
86
|
-
result = read_excel(file_path, sheet, page, page_size)
|
|
87
|
-
elif command == "info":
|
|
88
|
-
result = get_excel_info(file_path)
|
|
89
|
-
elif command == "write":
|
|
90
|
-
# Data passed as JSON string
|
|
91
|
-
data = json.loads(sys.argv[3])
|
|
92
|
-
sheet = sys.argv[4] if len(sys.argv) > 4 else "Sheet1"
|
|
93
|
-
result = write_excel(file_path, data, sheet)
|
|
94
|
-
else:
|
|
95
|
-
result = {"error": f"Unknown command: {command}"}
|
|
96
|
-
|
|
97
|
-
print(json.dumps(result, default=str))
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
PDF document handler - read PDF files using PyPDF2
|
|
3
|
-
"""
|
|
4
|
-
import json
|
|
5
|
-
import sys
|
|
6
|
-
from pathlib import Path
|
|
7
|
-
|
|
8
|
-
def read_pdf(file_path: str, page: int = None, page_size: int = 100):
|
|
9
|
-
"""Read PDF with optional pagination by pages"""
|
|
10
|
-
from PyPDF2 import PdfReader
|
|
11
|
-
|
|
12
|
-
reader = PdfReader(file_path)
|
|
13
|
-
total_pages = len(reader.pages)
|
|
14
|
-
|
|
15
|
-
# Handle pagination
|
|
16
|
-
if page is not None:
|
|
17
|
-
start_page = (page - 1) * page_size
|
|
18
|
-
end_page = min(start_page + page_size, total_pages)
|
|
19
|
-
pages_to_read = range(start_page, end_page)
|
|
20
|
-
current_page = page
|
|
21
|
-
total_page_groups = (total_pages + page_size - 1) // page_size
|
|
22
|
-
else:
|
|
23
|
-
pages_to_read = range(total_pages)
|
|
24
|
-
current_page = None
|
|
25
|
-
total_page_groups = 1
|
|
26
|
-
|
|
27
|
-
content = []
|
|
28
|
-
for i in pages_to_read:
|
|
29
|
-
page_obj = reader.pages[i]
|
|
30
|
-
text = page_obj.extract_text()
|
|
31
|
-
content.append({
|
|
32
|
-
"page_number": i + 1,
|
|
33
|
-
"text": text or "",
|
|
34
|
-
"words": len(text.split()) if text else 0
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
"total_pages": total_pages,
|
|
39
|
-
"current_page_group": current_page,
|
|
40
|
-
"page_size": page_size if page else None,
|
|
41
|
-
"total_page_groups": total_page_groups,
|
|
42
|
-
"content": content
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
def get_pdf_info(file_path: str):
|
|
46
|
-
"""Get PDF metadata"""
|
|
47
|
-
from PyPDF2 import PdfReader
|
|
48
|
-
|
|
49
|
-
reader = PdfReader(file_path)
|
|
50
|
-
info = {
|
|
51
|
-
"pages": len(reader.pages),
|
|
52
|
-
"file_size": Path(file_path).stat().st_size
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
# Try to get PDF metadata
|
|
56
|
-
if reader.metadata:
|
|
57
|
-
info["metadata"] = {k: str(v) for k, v in reader.metadata.items()}
|
|
58
|
-
|
|
59
|
-
# Count total words
|
|
60
|
-
total_words = 0
|
|
61
|
-
for page in reader.pages:
|
|
62
|
-
text = page.extract_text() or ""
|
|
63
|
-
total_words += len(text.split())
|
|
64
|
-
info["total_words"] = total_words
|
|
65
|
-
|
|
66
|
-
return info
|
|
67
|
-
|
|
68
|
-
if __name__ == "__main__":
|
|
69
|
-
command = sys.argv[1]
|
|
70
|
-
file_path = sys.argv[2]
|
|
71
|
-
|
|
72
|
-
if command == "read":
|
|
73
|
-
page = int(sys.argv[3]) if len(sys.argv) > 3 else None
|
|
74
|
-
page_size = int(sys.argv[4]) if len(sys.argv) > 4 else 10
|
|
75
|
-
result = read_pdf(file_path, page, page_size)
|
|
76
|
-
elif command == "info":
|
|
77
|
-
result = get_pdf_info(file_path)
|
|
78
|
-
else:
|
|
79
|
-
result = {"error": f"Unknown command: {command}"}
|
|
80
|
-
|
|
81
|
-
print(json.dumps(result, default=str))
|