get_transcript
Retrieve a YouTube video transcript with customizable language, format, timestamps, and metadata.
Instructions
Fetch a YouTube video's transcript.
Args: url: YouTube video URL or video ID languages: Preferred languages in priority order (e.g. ["en", "de"]). Defaults to English. format: Output format — one of: text, json, pretty, webvtt, srt preserve_formatting: Keep HTML formatting tags in the transcript text include_timestamps: When True with format="text", prefix each line with [HH:MM:SS]. Ignored for json/srt/webvtt/pretty (those formats already include timestamps). include_metadata: When True (default), prepend a [METADATA] block (title, channel, published, duration, views, description) before the transcript. Pass False for transcript-only output.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | ||
| languages | No | ||
| format | No | text | |
| preserve_formatting | No | ||
| include_timestamps | No | ||
| include_metadata | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- main.py:260-302 (handler)The main handler function for the 'get_transcript' MCP tool. It extracts the video ID, fetches the transcript via YouTubeTranscriptApi, optionally adds timestamps, formats the output (text/json/pretty/webvtt/srt), and optionally prepends a metadata block.
@mcp.tool() def get_transcript( url: str, languages: list[str] | None = None, format: str = "text", preserve_formatting: bool = False, include_timestamps: bool = False, include_metadata: bool = True, ) -> str: """Fetch a YouTube video's transcript. Args: url: YouTube video URL or video ID languages: Preferred languages in priority order (e.g. ["en", "de"]). Defaults to English. format: Output format — one of: text, json, pretty, webvtt, srt preserve_formatting: Keep HTML formatting tags in the transcript text include_timestamps: When True with format="text", prefix each line with [HH:MM:SS]. Ignored for json/srt/webvtt/pretty (those formats already include timestamps). include_metadata: When True (default), prepend a [METADATA] block (title, channel, published, duration, views, description) before the transcript. Pass False for transcript-only output. """ try: video_id = extract_video_id(url) except ValueError as e: return f"Error: {e}" langs = languages or ["en"] try: transcript = api.fetch( video_id, languages=langs, preserve_formatting=preserve_formatting, ) if include_timestamps and format == "text": body = _format_transcript_with_timestamps(transcript) else: body = _format_transcript(transcript, format) except Exception as e: return _handle_transcript_error(e, video_id, langs) if not include_metadata: return body meta = _fetch_metadata(video_id) return f"{_format_metadata_block(meta)}\n\n[TRANSCRIPT]\n{body}" - main.py:260-260 (registration)The @mcp.tool() decorator registers 'get_transcript' as an MCP tool on the FastMCP server instance.
@mcp.tool() - main.py:40-48 (helper)Helper function extract_video_id() used to parse a YouTube URL or bare video ID from the user-provided URL argument.
def extract_video_id(url_or_id: str) -> str: """Extract a YouTube video ID from a URL or bare ID string.""" url_or_id = url_or_id.strip() if BARE_ID_REGEX.match(url_or_id): return url_or_id match = VIDEO_ID_REGEX.search(url_or_id) if match: return match.group(1) raise ValueError( - main.py:66-71 (helper)Helper _format_transcript() used to format the transcript using the selected formatter (text, json, pretty, webvtt, srt).
def _format_transcript(transcript, fmt: str) -> str: """Format a FetchedTranscript using the specified formatter.""" formatter = FORMATTERS.get(fmt) if formatter is None: return f"Error: Unknown format '{fmt}'. Choose from: {', '.join(FORMATTERS)}" return formatter.format_transcript(transcript) - main.py:115-119 (helper)Helper _format_transcript_with_timestamps() used to render transcript as text with [HH:MM:SS] prefixes when include_timestamps=True.
def _format_transcript_with_timestamps(transcript) -> str: """Render a FetchedTranscript as text with an [HH:MM:SS] prefix on each line.""" return "\n".join( f"{_format_timestamp(snippet.start)} {snippet.text}" for snippet in transcript )