generate_video_chapters
Create structured video chapters with timestamps and descriptions for YouTube videos using AI analysis to organize content and improve navigation.
Instructions
Generate AI-powered video chapters with timestamps and descriptions
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| includeDescriptions | No | Include detailed chapter descriptions | |
| maxChapters | No | Maximum number of chapters to generate | |
| minChapterLength | No | Minimum chapter length in seconds | |
| videoId | Yes | YouTube video ID to generate chapters for |
Implementation Reference
- src/tools/chapter-generator.ts:23-82 (handler)Main handler function implementing the generate_video_chapters tool. Parses input args using schema, fetches video transcript, generates chapters using AI/LLM or fallback methods, enhances them, generates analysis metadata, caches result, and returns structured ChapterAnalysis.async execute(args: unknown): Promise<ChapterAnalysis> { const params = GenerateVideoChaptersSchema.parse(args); this.logger.info(`Generating chapters for video ${params.videoId}`); // Generate cache key const cacheKey = `chapters:${params.videoId}:${params.minChapterLength}:${params.maxChapters}`; // Check cache first const cached = await this.cache.get<ChapterAnalysis>(cacheKey); if (cached) { this.logger.info(`Returning cached chapters for: ${params.videoId}`); return cached; } try { // Step 1: Get video details with transcript const videoDetails = await this.youtubeClient.getVideoDetails({ videoId: params.videoId, includeTranscript: true, includeComments: false }); if (!videoDetails.transcript || videoDetails.transcript.length === 0) { throw new Error(`No transcript available for video ${params.videoId}`); } // Step 2: Process transcript for structure analysis const processedTranscript = this.transcriptProcessor.processTranscript(videoDetails.transcript); // Step 3: Identify chapter boundaries using AI const chapters = await this.generateChapters( videoDetails.transcript, processedTranscript, videoDetails.video, params ); // Step 4: Enhance chapters with additional analysis const enhancedChapters = await this.enhanceChapters(chapters, videoDetails.transcript); // Step 5: Generate navigation and metadata const analysis = this.generateChapterAnalysis( params.videoId, enhancedChapters, videoDetails.video ); // Cache the result await this.cache.set(cacheKey, analysis, 3600); // 1 hour cache this.logger.info(`Chapter generation completed for ${params.videoId}: ${chapters.length} chapters created`); return analysis; } catch (error) { this.logger.error(`Failed to generate chapters for ${params.videoId}:`, error); throw error; } }
- src/types.ts:262-267 (schema)Zod schema defining input parameters and validation for the generate_video_chapters tool, used for parsing args in the handler.export const GenerateVideoChaptersSchema = z.object({ videoId: z.string().describe('YouTube video ID'), minChapterLength: z.number().min(30).max(600).default(120).describe('Minimum chapter length in seconds'), maxChapters: z.number().min(3).max(20).default(10).describe('Maximum number of chapters'), includeDescriptions: z.boolean().default(true).describe('Include chapter descriptions'), });
- src/index.ts:475-506 (registration)Tool registration in the MCP server's listTools handler, defining the tool's name, description, and input schema for clients to discover.name: 'generate_video_chapters', description: 'Generate AI-powered video chapters with timestamps and descriptions', inputSchema: { type: 'object', properties: { videoId: { type: 'string', description: 'YouTube video ID to generate chapters for' }, maxChapters: { type: 'number', minimum: 3, maximum: 20, default: 10, description: 'Maximum number of chapters to generate' }, minChapterLength: { type: 'number', minimum: 30, maximum: 600, default: 60, description: 'Minimum chapter length in seconds' }, includeDescriptions: { type: 'boolean', default: true, description: 'Include detailed chapter descriptions' } }, required: ['videoId'] } },
- src/index.ts:592-594 (registration)Dispatch/registration in the CallToolRequestSchema switch statement, routing tool calls to the ChapterGenerator.execute handler.case 'generate_video_chapters': result = await this.chapterTool.execute(args); break;
- Core helper method that orchestrates chapter generation, deciding between LLM analysis for short videos or segmented analysis for long videos.private async generateChapters( transcript: YouTubeTranscript[], processedTranscript: any, videoMetadata: any, params: GenerateVideoChaptersParams ): Promise<VideoChapter[]> { // For very long videos, analyze in segments const maxAnalysisLength = 15000; // ~15k characters if (processedTranscript.fullText.length > maxAnalysisLength) { return this.generateChaptersForLongVideo(transcript, videoMetadata, params); } else { return this.generateChaptersWithLLM(transcript, processedTranscript, videoMetadata, params); } }