get_video_info
Retrieve detailed metadata such as title and description from a YouTube video by passing its URL or video ID.
Instructions
Get metadata from a YouTube video.
Args: url: YouTube URL or video ID
Returns: Dict containing video metadata
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/youtube_mcp/video_info.py:10-60 (handler)The core handler function for the 'get_video_info' tool. It extracts a video ID from a YouTube URL using extract_video_id helper, fetches metadata via yt-dlp, and returns a dict with id, title, description, duration, channel, upload_date, view_count, and available subtitle languages.
def get_video_info(url: str) -> dict[str, Any]: """Get metadata from a YouTube video. Args: url: YouTube URL or video ID Returns: Dict containing video metadata """ video_id = extract_video_id(url) video_url = f"https://www.youtube.com/watch?v={video_id}" ydl_opts = { "quiet": True, "no_warnings": True, "skip_download": True, "writesubtitles": True, "writeautomaticsub": True, } try: with yt_dlp.YoutubeDL(ydl_opts) as ydl: info = ydl.extract_info(video_url, download=False) if info is None: return { "id": video_id, "error": "Failed to extract video info", } # Collect available subtitle languages subtitles = info.get("subtitles", {}) automatic_captions = info.get("automatic_captions", {}) available_languages = sorted(set(subtitles.keys()) | set(automatic_captions.keys())) return { "id": video_id, "title": info.get("title"), "description": info.get("description"), "duration": info.get("duration"), "channel": info.get("channel") or info.get("uploader"), "upload_date": _format_date(info.get("upload_date")), "view_count": info.get("view_count"), "available_languages": available_languages, } except Exception as e: return { "id": video_id, "error": str(e), } - src/youtube_mcp/server.py:11-11 (registration)Registration of the get_video_info function as an MCP tool using FastMCP's decorator pattern (mcp.tool()(get_video_info)).
mcp.tool()(get_video_info) - src/youtube_mcp/video_info.py:63-67 (helper)Helper function used within get_video_info to format the upload_date from YYYYMMDD to YYYY-MM-DD format.
def _format_date(date_str: str | None) -> str | None: """Format YYYYMMDD to YYYY-MM-DD.""" if not date_str or len(date_str) != 8: return date_str return f"{date_str[:4]}-{date_str[4:6]}-{date_str[6:8]}" - src/youtube_mcp/url.py:7-66 (helper)Helper utility used by get_video_info to extract a YouTube video ID from various URL formats (standard, shortlinks, embed, shorts, live, etc.) or a bare video ID.
def extract_video_id(url: str) -> str: """Extract video ID from various forms of YouTube URLs. Handles standard, shortened, embed, shorts, live URLs, as well as regional domains and mobile versions. Args: url: YouTube URL or video ID Returns: The extracted video ID Raises: ValueError: If the video ID cannot be extracted """ if not url: raise ValueError("URL is required") # Handle case where input is just the 11-character video ID if re.match(r"^[a-zA-Z0-9_-]{11}$", url): return url # Prepend scheme if not present if not re.match(r"https?://", url): url = "https://" + url parsed = urlparse(url) hostname = parsed.hostname if not hostname: raise ValueError(f"Could not parse URL: {url}") # youtu.be shortlinks if hostname == "youtu.be": video_id = parsed.path.lstrip("/").split("/")[0].split("?")[0] if video_id: return video_id raise ValueError(f"Could not extract video ID from URL: {url}") # youtube.com variants youtube_pattern = r"^(?:www\.|m\.|music\.)?youtube\.com$" if not re.match(youtube_pattern, hostname): raise ValueError(f"Not a YouTube URL: {url}") # /watch?v=VIDEO_ID if parsed.path == "/watch": query_params = parse_qs(parsed.query) video_ids = query_params.get("v") if video_ids: return video_ids[0] raise ValueError(f"Could not extract video ID from URL: {url}") # /embed/VIDEO_ID, /shorts/VIDEO_ID, /live/VIDEO_ID, /v/VIDEO_ID path_parts = parsed.path.split("/") if len(path_parts) >= 3 and path_parts[1] in ("embed", "shorts", "live", "v"): video_id = path_parts[2].split("?")[0] if video_id: return video_id raise ValueError(f"Could not extract video ID from URL: {url}")