get_transcript
Extract YouTube video transcripts by providing a URL or video ID with optional language support. Ideal for accessing and analyzing video content in specific languages.
Instructions
Extract transcript from a YouTube video URL or ID
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| lang | Yes | Language code for transcript (e.g., 'ko', 'en') | en |
| url | Yes | YouTube video URL or ID |
Implementation Reference
- src/index.ts:170-221 (handler)Main execution handler for the 'get_transcript' tool within the MCP call tool request handler. Handles argument validation, video ID extraction, transcript retrieval, error handling, and formats the MCP-compliant tool result.case "get_transcript": { const { url: input, lang = "en" } = args; if (!input || typeof input !== 'string') { throw new McpError( ErrorCode.InvalidParams, 'URL parameter is required and must be a string' ); } if (lang && typeof lang !== 'string') { throw new McpError( ErrorCode.InvalidParams, 'Language code must be a string' ); } try { const videoId = this.extractor.extractYoutubeId(input); console.error(`Processing transcript for video: ${videoId}`); const transcript = await this.extractor.getTranscript(videoId, lang); console.error(`Successfully extracted transcript (${transcript.length} chars)`); return { toolResult: { content: [{ type: "text", text: transcript, metadata: { videoId, language: lang, timestamp: new Date().toISOString(), charCount: transcript.length } }], isError: false } }; } catch (error) { console.error('Transcript extraction failed:', error); if (error instanceof McpError) { throw error; } throw new McpError( ErrorCode.InternalError, `Failed to process transcript: ${(error as Error).message}` ); } }
- src/index.ts:18-36 (schema)Tool schema definition for 'get_transcript', including name, description, and input schema specifying 'url' and 'lang' parameters.{ name: "get_transcript", description: "Extract transcript from a YouTube video URL or ID", inputSchema: { type: "object", properties: { url: { type: "string", description: "YouTube video URL or ID" }, lang: { type: "string", description: "Language code for transcript (e.g., 'ko', 'en')", default: "en" } }, required: ["url", "lang"] } }
- src/index.ts:153-163 (registration)Registers the MCP request handlers for listing tools (exposes get_transcript schema) and calling tools (dispatches to handleToolCall based on name).private setupHandlers(): void { // List available tools this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: TOOLS })); // Handle tool calls this.server.setRequestHandler(CallToolRequestSchema, async (request) => this.handleToolCall(request.params.name, request.params.arguments ?? {}) ); }
- src/index.ts:92-107 (helper)Core helper function in YouTubeTranscriptExtractor that fetches subtitles using 'youtube-captions-scraper' library and formats them into a single string.async getTranscript(videoId: string, lang: string): Promise<string> { try { const transcript = await getSubtitles({ videoID: videoId, lang: lang, }); return this.formatTranscript(transcript); } catch (error) { console.error('Failed to fetch transcript:', error); throw new McpError( ErrorCode.InternalError, `Failed to retrieve transcript: ${(error as Error).message}` ); } }
- src/index.ts:49-87 (helper)Helper function to parse YouTube video ID from various URL formats or direct ID input, with validation.extractYoutubeId(input: string): string { if (!input) { throw new McpError( ErrorCode.InvalidParams, 'YouTube URL or ID is required' ); } // Handle URL formats try { const url = new URL(input); if (url.hostname === 'youtu.be') { return url.pathname.slice(1); } else if (url.hostname.includes('youtube.com')) { const videoId = url.searchParams.get('v'); if (!videoId) { throw new McpError( ErrorCode.InvalidParams, `Invalid YouTube URL: ${input}` ); } return videoId; } } catch (error) { // Not a URL, check if it's a direct video ID if (!/^[a-zA-Z0-9_-]{11}$/.test(input)) { throw new McpError( ErrorCode.InvalidParams, `Invalid YouTube video ID: ${input}` ); } return input; } throw new McpError( ErrorCode.InvalidParams, `Could not extract video ID from: ${input}` ); }