parse_srt
Extract structured data from SRT subtitle files to enable translation while preserving timing and formatting information.
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:94-107 (registration)Registration of the parse_srt tool in the MCP server's listTools response, 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:422-439 (handler)Handler function for the parse_srt MCP tool that invokes parseSRTFile, handles errors, and returns the parsed SRT structure as JSON.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:97-106 (schema)Input schema definition for the parse_srt tool requiring SRT content string.inputSchema: { type: 'object', properties: { content: { type: 'string', description: 'SRT file content to parse', }, }, required: ['content'], },
- src/parsers/srt-parser.ts:12-66 (helper)Core implementation of SRT parsing: splits into blocks, parses each subtitle, validates timings and styles, handles errors and metadata extraction.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' }] }; } }