get_document_info
Extract structured document details by analyzing file paths, enabling efficient access to metadata and content insights for Word documents.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"filePath": {
"type": "string"
}
},
"required": [
"filePath"
],
"type": "object"
}
Implementation Reference
- src/services/DocumentService.ts:128-150 (handler)The core handler function implementing the logic for the 'get_document_info' tool. It fetches file statistics, extracts raw text using mammoth, calculates word count and estimated page count, and constructs the DocumentInfo object.async getDocumentInfo(filePath: string): Promise<APIResponse<DocumentInfo>> { try { const stats = await fs.stat(filePath); const buffer = await fs.readFile(filePath); const result = await mammoth.extractRawText({ buffer }); const info: DocumentInfo = { title: path.basename(filePath), author: 'Unknown', subject: '', keywords: [], pageCount: Math.ceil(result.value.length / 3000), // 粗略估计 wordCount: result.value.split(/\s+/).length, created: stats.birthtime, modified: stats.mtime, }; return { success: true, data: info }; } catch (error) { const err = error as Error; return { success: false, error: `获取文档信息失败: ${err.message}` }; } }
- src/types/index.ts:66-75 (schema)TypeScript interface defining the output structure (DocumentInfo) returned by the get_document_info tool.export interface DocumentInfo { title: string; author: string; subject: string; keywords: string[]; pageCount: number; wordCount: number; created: Date; modified: Date; }
- src/mcp-server.ts:178-215 (registration)Registration of the 'get_document_info' tool in the MCP server using server.tool(), including Zod input schema and a wrapper handler that formats the response.server.tool( "get_document_info", { filePath: z.string(), }, async (params) => { const result = await docService.getDocumentInfo(params.filePath); if (result.success) { const info = result.data!; return { content: [ { type: "text", text: `文档信息: 标题: ${info.title} 作者: ${info.author} 主题: ${info.subject} 关键词: ${info.keywords.join(", ")} 页数: ${info.pageCount} 字数: ${info.wordCount} 创建时间: ${info.created.toLocaleString()} 修改时间: ${info.modified.toLocaleString()}`, }, ], }; } else { return { content: [ { type: "text", text: result.error!, }, ], isError: true, }; } } );
- src/server.ts:96-107 (schema)JSON Schema for the input parameters of the 'get_document_info' tool, defined in the HTTP server's tools list.{ name: 'get_document_info', description: '获取文档信息', parameters: { properties: { filePath: { type: 'string', description: '文档路径' }, }, required: ['filePath'], type: 'object', }, }, ];
- src/server.ts:167-169 (registration)Dispatch/registration in the HTTP server's POST /tools/call switch statement that routes 'get_document_info' calls to the DocumentService handler.case 'get_document_info': result = await docService.getDocumentInfo(parameters.filePath); break;