extract_data
Extract embedded data (e.g., i18n translations) from source code into a structured JSON file. Replace source file with a migration notice for tracking. Customize behavior via environment variables.
Instructions
Extract data content (e.g. i18n translations) from source code to a JSON file. IMPORTANT: When encountering files with data such as i18n content embedded in code, use this tool directly instead of reading the file content first. This tool will programmatically extract all translations into a structured JSON file, preserving nested objects, arrays, template variables, and formatting. This helps keep translations as configuration and prevents filling up the AI context window with translation content. By default, the source file will be replaced with "MIGRATED TO " and a warning message after successful extraction, making it easy to track where the data was moved to. This behaviour can be disabled by setting the DISABLE_SOURCE_REPLACEMENT environment variable to 'true'. The warning message can be customized by setting the WARNING_MESSAGE environment variable.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sourcePath | Yes | Path to the source file containing data inside code | |
| targetPath | Yes | Path where the resulting JSON file should be written |
Implementation Reference
- src/index.ts:111-145 (handler)Main handler logic for the 'extract_data' tool within the CallToolRequestSchema request handler. Reads the source file, extracts data using extractDataContent, writes to target JSON file, optionally replaces source with migration message, and returns success content.if (request.params.name === 'extract_data') { const { targetPath } = request.params.arguments as { targetPath: string }; const dataContent = await this.extractDataContent(sourceCode); // Create target directory if it doesn't exist await fs.mkdir(path.dirname(targetPath), { recursive: true }); // Write extracted content to JSON file await fs.writeFile( targetPath, JSON.stringify(dataContent, null, 2), 'utf-8' ); // Replace source file content with migration message if not disabled if (!DISABLE_SOURCE_REPLACEMENT) { const absoluteTargetPath = path.resolve(targetPath); await fs.writeFile( sourcePath, `MIGRATED TO ${absoluteTargetPath}${WARNING_MESSAGE}`, 'utf-8' ); } return { content: [ { type: 'text', text: `Successfully extracted ${Object.keys(dataContent).length} data entries to ${path.resolve(targetPath)}${ !DISABLE_SOURCE_REPLACEMENT ? `. Source file replaced with "MIGRATED TO ${path.resolve(targetPath)}"` : '' }`, }, ], }; } else if (request.params.name === 'extract_svg') {
- src/index.ts:297-353 (helper)Core helper method that parses the source code into an AST using Babel parser and traverses it to extract string values from object expressions (especially default exports), building a flat key-value record of data entries.private async extractDataContent(sourceCode: string): Promise<Record<string, string | string[] | Array<Record<string, string | string[]>>>> { const ast = parser.parse(sourceCode, { sourceType: 'module', plugins: ['typescript', 'jsx'], }); const result: Record<string, any> = {}; const processValue = (value: t.Node, currentPath: string[]): void => { if (t.isStringLiteral(value) || t.isTemplateLiteral(value)) { const extractedValue = this.extractStringValue(value); if (extractedValue !== null && extractedValue.trim() !== '') { result[this.buildKey(currentPath)] = extractedValue; } } else if (t.isArrayExpression(value)) { value.elements.forEach((element, index) => { if (!element) return; if (t.isStringLiteral(element) || t.isTemplateLiteral(element)) { const extractedValue = this.extractStringValue(element); if (extractedValue !== null && extractedValue.trim() !== '') { result[`${this.buildKey(currentPath)}.${index}`] = extractedValue; } } else if (t.isObjectExpression(element)) { processObject(element, [...currentPath, index.toString()]); } }); } else if (t.isObjectExpression(value)) { processObject(value, currentPath); } }; const processObject = (obj: t.ObjectExpression, parentPath: string[] = []): void => { obj.properties.forEach(prop => { if (!t.isObjectProperty(prop)) return; const key = t.isIdentifier(prop.key) ? prop.key.name : t.isStringLiteral(prop.key) ? prop.key.value : null; if (!key) return; const currentPath = [...parentPath, key]; processValue(prop.value, currentPath); }); }; traverse(ast, { ExportDefaultDeclaration(path: NodePath<t.ExportDefaultDeclaration>) { const declaration = path.node.declaration; if (t.isObjectExpression(declaration)) { processObject(declaration); } } }); return result; }
- src/index.ts:65-78 (schema)Input schema definition for the 'extract_data' tool, specifying sourcePath and targetPath as required string parameters.inputSchema: { type: 'object', properties: { sourcePath: { type: 'string', description: 'Path to the source file containing data inside code', }, targetPath: { type: 'string', description: 'Path where the resulting JSON file should be written', }, }, required: ['sourcePath', 'targetPath'], },
- src/index.ts:62-79 (registration)Registration of the 'extract_data' tool in the listTools response, including name, detailed description, and input schema.{ name: 'extract_data', description: 'Extract data content (e.g. i18n translations) from source code to a JSON file. IMPORTANT: When encountering files with data such as i18n content embedded in code, use this tool directly instead of reading the file content first. This tool will programmatically extract all translations into a structured JSON file, preserving nested objects, arrays, template variables, and formatting. This helps keep translations as configuration and prevents filling up the AI context window with translation content. By default, the source file will be replaced with "MIGRATED TO <target absolute path>" and a warning message after successful extraction, making it easy to track where the data was moved to. This behaviour can be disabled by setting the DISABLE_SOURCE_REPLACEMENT environment variable to \'true\'. The warning message can be customized by setting the WARNING_MESSAGE environment variable.', inputSchema: { type: 'object', properties: { sourcePath: { type: 'string', description: 'Path to the source file containing data inside code', }, targetPath: { type: 'string', description: 'Path where the resulting JSON file should be written', }, }, required: ['sourcePath', 'targetPath'], }, },