process_file
Process and extract content from files including PDFs, documents, images, and spreadsheets using the GLM-4.5V MCP Server. Supports optional extraction prompts for precise content retrieval.
Instructions
使用 GLM-4.5V 处理文件(上传并提取内容)。支持 PDF、DOCX、DOC、XLS、XLSX、PPT、PPTX、PNG、JPG、JPEG、CSV 等格式
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| extractPrompt | No | 可选的内容提取提示词,用于指导如何提取文件内容 | |
| filePath | Yes | 文件路径(本地文件路径) |
Implementation Reference
- src/index.ts:307-362 (handler)Core handler function that validates the file path, checks size and type, uploads the file to GLM API to get fileId, retrieves the extracted content, and returns a FileProcessingResult.async function processFile(filePath: string, extractPrompt?: string): Promise<FileProcessingResult> { const startTime = Date.now(); try { console.error(`[DEBUG] processFile called with path: ${filePath}`); // 检查文件是否存在 const resolvedPath = path.resolve(filePath); const stats = await fs.stat(resolvedPath); const fileSize = stats.size; const filename = path.basename(filePath); console.error(`[DEBUG] File size: ${fileSize} bytes, filename: ${filename}`); // 检查文件大小限制 const maxSize = isImageFile(filename) ? 5 * 1024 * 1024 : 50 * 1024 * 1024; // 图片5MB,其他50MB if (fileSize > maxSize) { throw new Error(`文件大小超过限制。图片文件最大5MB,其他文件最大50MB。当前文件大小:${(fileSize / 1024 / 1024).toFixed(2)}MB`); } // 检查文件格式 if (!isSupportedFileType(filename)) { throw new Error(`不支持的文件格式。支持的格式:PDF、DOCX、DOC、XLS、XLSX、PPT、PPTX、PNG、JPG、JPEG、CSV`); } // 1. 上传文件 console.error(`[DEBUG] Uploading file...`); const fileId = await uploadFileToGLM(resolvedPath, filename); console.error(`[DEBUG] File uploaded with ID: ${fileId}`); // 2. 获取文件内容 console.error(`[DEBUG] Getting file content...`); const content = await getFileContentFromGLM(fileId); console.error(`[DEBUG] Content extracted, length: ${content.length}`); const processingTime = Date.now() - startTime; return { ok: true, fileId, content, fileType: getFileType(filename), filename, metadata: { uploadTime: startTime, fileSize, processingTime } }; } catch (error) { return { ok: false, error: error instanceof Error ? error.message : "Unknown error" }; } }
- src/index.ts:113-140 (registration)MCP tool registration for 'process_file', defining input schema with Zod and a thin async handler wrapper that calls processFile and formats the response.mcpServer.registerTool("process_file", { description: "使用 GLM-4.5V 处理文件(上传并提取内容)。支持 PDF、DOCX、DOC、XLS、XLSX、PPT、PPTX、PNG、JPG、JPEG、CSV 等格式", inputSchema: { filePath: z.string().describe("文件路径(本地文件路径)"), extractPrompt: z.string().optional().describe("可选的内容提取提示词,用于指导如何提取文件内容"), }, }, async ({ filePath, extractPrompt }) => { try { const result = await processFile(filePath, extractPrompt); return { content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] }; } catch (error) { return { content: [{ type: "text" as const, text: JSON.stringify({ ok: false, error: error instanceof Error ? error.message : "Unknown error" }, null, 2) }], isError: true }; } });
- src/index.ts:36-48 (schema)TypeScript interface defining the structure of the FileProcessingResult returned by the processFile handler.interface FileProcessingResult { ok: boolean; fileId?: string; content?: string; fileType?: string; filename?: string; error?: string; metadata?: { uploadTime: number; fileSize: number; processingTime: number; }; }
- src/index.ts:485-523 (helper)Helper function to upload the file to GLM API using FormData and retrieve the file ID.async function uploadFileToGLM(filePath: string, filename: string): Promise<string> { const glmApiKey = process.env.GLM_API_KEY; if (!glmApiKey) { throw new Error("GLM_API_KEY environment variable is required"); } try { // 读取文件 const fileBuffer = await fs.readFile(filePath); // 创建 FormData const formData = new FormData(); const blob = new Blob([new Uint8Array(fileBuffer)]); formData.append('file', blob, filename); formData.append('purpose', 'file-extract'); const response = await fetch('https://open.bigmodel.cn/api/paas/v4/files', { method: 'POST', headers: { 'Authorization': `Bearer ${glmApiKey}` }, body: formData }); if (!response.ok) { const errorText = await response.text(); throw new Error(`文件上传失败: ${response.status} ${response.statusText} - ${errorText}`); } const result = await response.json(); if (!result.id) { throw new Error('上传响应中缺少文件ID'); } return result.id; } catch (error) { throw new Error(`文件上传失败: ${error instanceof Error ? error.message : 'Unknown error'}`); } }
- src/index.ts:525-557 (helper)Helper function to fetch the extracted content from the GLM API using the file ID.async function getFileContentFromGLM(fileId: string): Promise<string> { const glmApiKey = process.env.GLM_API_KEY; if (!glmApiKey) { throw new Error("GLM_API_KEY environment variable is required"); } try { const response = await fetch(`https://open.bigmodel.cn/api/paas/v4/files/${fileId}/content`, { method: 'GET', headers: { 'Authorization': `Bearer ${glmApiKey}` } }); if (!response.ok) { const errorText = await response.text(); throw new Error(`获取文件内容失败: ${response.status} ${response.statusText} - ${errorText}`); } // 根据响应类型处理内容 const contentType = response.headers.get('content-type') || ''; if (contentType.includes('application/json')) { const jsonResult = await response.json(); return JSON.stringify(jsonResult, null, 2); } else { // 对于其他类型,尝试作为文本读取 return await response.text(); } } catch (error) { throw new Error(`获取文件内容失败: ${error instanceof Error ? error.message : 'Unknown error'}`); } }