"""Validators for YouTube MCP operations."""
import re
def validate_youtube_url(url: str) -> tuple[bool, str]:
"""
Validate that a URL is a valid YouTube URL.
Args:
url: URL to validate
Returns:
Tuple of (is_valid: bool, message: str)
"""
if not url or not isinstance(url, str):
return False, "URL must be a non-empty string"
# Accept standard watch URLs and deterministic multi-video queue URLs
patterns = [
r"^https?://(?:www\.)?youtube\.com/watch\?v=[a-zA-Z0-9_-]+", # single video
r"^https?://(?:www\.)?youtube\.com/watch_videos\?video_ids=[a-zA-Z0-9_-]+(?:,[a-zA-Z0-9_-]+)*$", # explicit queue
]
if any(re.match(p, url) for p in patterns):
return True, "URL is valid"
return False, "URL must be a valid YouTube video URL"
def validate_search_query(query: str) -> tuple[bool, str]:
"""
Validate search query.
Args:
query: Search query string
Returns:
Tuple of (is_valid: bool, message: str)
"""
if not query or not isinstance(query, str):
return False, "Query must be a non-empty string"
if len(query) > 300:
return False, "Query must be 300 characters or less"
return True, "Query is valid"
def validate_max_results(max_results: int) -> tuple[bool, str]:
"""
Validate max_results parameter.
Args:
max_results: Maximum number of results
Returns:
Tuple of (is_valid: bool, message: str)
"""
if not isinstance(max_results, int):
return False, "max_results must be an integer"
if max_results < 1 or max_results > 50:
return False, "max_results must be between 1 and 50"
return True, "max_results is valid"
def validate_api_key(api_key: str) -> tuple[bool, str]:
"""
Validate API key format.
Args:
api_key: Google API key
Returns:
Tuple of (is_valid: bool, message: str)
"""
if not api_key or not isinstance(api_key, str):
return False, "API key must be a non-empty string"
if len(api_key) < 20:
return False, "API key appears to be invalid (too short)"
return True, "API key is valid"