parse_srt
Extract structured data from SRT subtitle files to process timing, text content, and subtitle sequences for translation workflows.
Instructions
Parse SRT file content and return structured data
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | SRT file content to parse |
Implementation Reference
- src/mcp/server.ts:422-439 (handler)The main handler function for the 'parse_srt' MCP tool. It extracts SRT content from input, calls parseSRTFile utility, validates success, and returns the structured SRT file data as JSON text content.private async handleParseSRT(args: any) { const { content } = args; const parseResult = parseSRTFile(content); if (!parseResult.success || !parseResult.file) { const errorDetails = parseResult.errors?.map(e => `${e.type}: ${e.message}`).join(', ') || 'Unknown parsing error'; throw new Error(`Failed to parse SRT file: ${errorDetails}`); } return { content: [ { type: 'text', text: JSON.stringify(parseResult.file, null, 2), }, ], }; }
- src/mcp/server.ts:95-106 (registration)Registration of the 'parse_srt' tool in the MCP server's ListToolsRequestSchema handler, including name, description, and input schema.name: 'parse_srt', description: 'Parse SRT file content and return structured data', inputSchema: { type: 'object', properties: { content: { type: 'string', description: 'SRT file content to parse', }, }, required: ['content'], },
- src/mcp/server.ts:97-106 (schema)Input schema definition for the 'parse_srt' tool requiring a 'content' string parameter.inputSchema: { type: 'object', properties: { content: { type: 'string', description: 'SRT file content to parse', }, }, required: ['content'], },
- src/parsers/srt-parser.ts:12-66 (helper)The core helper function 'parseSRTFile' that implements SRT parsing logic, called by the MCP tool handler. Handles block parsing, validation, error collection, and returns SRTProcessingResult.export function parseSRTFile(content: string): SRTProcessingResult { const errors: SRTValidationError[] = []; const warnings: string[] = []; const subtitles: SRTSubtitle[] = []; try { const blocks = content.trim().split(/\n\s*\n/); let lineNumber = 1; for (const block of blocks) { if (!block.trim()) continue; const lines = block.split('\n'); const subtitle = parseSubtitleBlock(lines, lineNumber); if (subtitle) { subtitles.push(subtitle); } else { errors.push({ line: lineNumber, message: 'Failed to parse subtitle block', type: 'format' }); } lineNumber += lines.length + 1; // +1 for empty line separator } // Validate timing sequences validateTimingSequences(subtitles, errors); // Validate style tags validateAllStyleTags(subtitles, errors, warnings); return { success: errors.length === 0, file: { subtitles, metadata: extractMetadata(content) }, errors: errors.length > 0 ? errors : undefined, warnings: warnings.length > 0 ? warnings : undefined }; } catch (error) { return { success: false, errors: [{ line: 1, message: `Parse error: ${error instanceof Error ? error.message : 'Unknown error'}`, type: 'format' }] }; } }