extract_text
Extract plain text from .docx files for simplified content access and processing, ensuring compatibility with text-based workflows.
Instructions
Extract plain text content from a DOCX file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | Path to the .docx file |
Implementation Reference
- src/index.ts:25-64 (handler)Handler function that extracts plain text from a DOCX file using mammoth.extractRawText, computes word count, and returns JSON-structured content or error response.async ({ file_path }) => { try { const absolutePath = path.resolve(file_path) if (!fs.existsSync(absolutePath)) { throw new Error(`File not found: ${absolutePath}`) } const result = await mammoth.extractRawText({ path: absolutePath }) return { content: [ { type: 'text', text: JSON.stringify( { text: result.value, messages: result.messages, word_count: result.value .split(/\s+/) .filter((word: string) => word.length > 0).length, }, null, 2 ), }, ], } } catch (error) { return { content: [ { type: 'text', text: `Error extracting text: ${(error as Error).message}`, }, ], isError: true, } } }
- src/index.ts:22-24 (schema)Zod input schema defining the 'file_path' parameter as a string describing the path to the DOCX file.{ file_path: z.string().describe('Path to the .docx file'), },
- src/index.ts:19-65 (registration)Registration of the 'extract_text' tool on the McpServer instance, including name, description, input schema, and handler function.server.tool( 'extract_text', 'Extract plain text content from a DOCX file', { file_path: z.string().describe('Path to the .docx file'), }, async ({ file_path }) => { try { const absolutePath = path.resolve(file_path) if (!fs.existsSync(absolutePath)) { throw new Error(`File not found: ${absolutePath}`) } const result = await mammoth.extractRawText({ path: absolutePath }) return { content: [ { type: 'text', text: JSON.stringify( { text: result.value, messages: result.messages, word_count: result.value .split(/\s+/) .filter((word: string) => word.length > 0).length, }, null, 2 ), }, ], } } catch (error) { return { content: [ { type: 'text', text: `Error extracting text: ${(error as Error).message}`, }, ], isError: true, } } } )