A YouTube Download MCP Server for Claude Code, Cursor and Codex
What this MCP server does
An MCP server is a small program that an agent starts in the background and asks for a list of tools. This one exposes two:
download_youtube_videotakes a URL plus optional resolution, H.264-only, audio-only and captions flags. It waits up towait_seconds(default 300) and returns a download link, or an error code, or a job ID if the download is still running.check_youtube_downloadtakes that job ID and returns the link once it's ready.
We tested it against the official MCP Python SDK 2.3, over the same stdio connection the clients use. It also runs on the older 1.x SDK.
1. Install and save the server
You need Python 3.10 or newer and a VideoDL API key from your dashboard (keys start with vdl_).
python3 -m pip install mcp requests
Save this as videodl_mcp.py somewhere permanent, and note its full path:
import os
import time
import requests
try:
from mcp.server.mcpserver import MCPServer
except ImportError:
from mcp.server.fastmcp import FastMCP as MCPServer
API = "https://api.videodl.app"
HEADERS = {"X-API-Key": os.environ["VIDEODL_API_KEY"]}
server = MCPServer("videodl")
def _status(worker_id: str) -> dict:
r = requests.get(f"{API}/api/download-status", params={"worker_id": worker_id},
headers=HEADERS, timeout=30)
job = r.json()
if r.status_code != 200:
job["status"] = "error"
return job
def _result(job: dict) -> dict:
if job.get("status") == "downloaded":
return {
"status": "downloaded",
"title": job.get("title"),
"download_url": job.get("download_url"),
"expires_in": job.get("expires_in"),
"resolution": job.get("resolution"),
"codecs": job.get("codecs"),
"size": job.get("size_human"),
"extras": job.get("extras"),
}
if job.get("status") == "error":
return {
"status": "error",
"error_code": job.get("error_code"),
"error_label": job.get("error_label"),
"error": job.get("error"),
}
return {"status": job.get("status"), "worker_id": job.get("worker_id"),
"progress": (job.get("progress") or {}).get("percent")}
@server.tool()
def download_youtube_video(url: str, resolution: str = "1080", h264_only: bool = False,
audio_only: bool = False, captions: bool = False,
wait_seconds: int = 300) -> dict:
"""Download a public YouTube video and return a download link.
url: a YouTube video URL or 11-character video ID.
resolution: maximum height, e.g. "720", "1080", "2160".
h264_only: prefer H.264 (AVC) video for maximum compatibility (capped at 1080p).
audio_only: return an MP3 of the audio instead of the video.
captions: also return English captions as SRT, if the plan includes captions.
wait_seconds: how long to wait before returning a job ID to check later.
"""
body = {"url": url}
if audio_only:
body.update({"audio_only": True, "audio_format": "mp3"})
else:
body["resolution"] = resolution
if h264_only:
body["video_codec"] = ["avc1", "any"]
if captions:
body["captions"] = True
r = requests.post(f"{API}/api/start-download", json=body, headers=HEADERS, timeout=30)
data = r.json()
if r.status_code != 202:
return {"status": "error", "http_status": r.status_code,
"error_code": data.get("error_code"), "error": data.get("error"),
"hint": data.get("hint"), "retry_after": data.get("retry_after")}
worker_id = data["worker_id"]
deadline = time.time() + wait_seconds
while time.time() < deadline:
job = _status(worker_id)
if job.get("status") in ("downloaded", "error"):
return _result(job)
time.sleep(job.get("retry_after", 3))
return {"status": "still_running", "worker_id": worker_id,
"next_step": "Call check_youtube_download with this worker_id."}
@server.tool()
def check_youtube_download(worker_id: str) -> dict:
"""Check a download started earlier and return its link when it's ready."""
return _result(_status(worker_id))
if __name__ == "__main__":
server.run()
A note if you're following older MCP tutorials: version 2 of the Python SDK renamed FastMCP to MCPServer. The import at the top handles both.
2. Connect it to your agent
Claude Code
claude mcp add --transport stdio videodl --env VIDEODL_API_KEY=vdl_your_key -- python3 /full/path/to/videodl_mcp.py
Run claude mcp list to check it's connected, or /mcp inside a session.
Cursor
Add it to ~/.cursor/mcp.json for all projects, or .cursor/mcp.json in one project:
{
"mcpServers": {
"videodl": {
"command": "python3",
"args": ["/full/path/to/videodl_mcp.py"],
"env": { "VIDEODL_API_KEY": "vdl_your_key" }
}
}
}
Codex
Add it to ~/.codex/config.toml:
[mcp_servers.videodl]
command = "python3"
args = ["/full/path/to/videodl_mcp.py"]
env = { VIDEODL_API_KEY = "vdl_your_key" }
If python3 on that machine doesn't have the packages, point command at the Python inside your virtual environment instead.
3. Ask for a video
The agent reads the tool descriptions, so plain requests work:
- "Download https://youtu.be/VIDEO_ID at 720p and give me the link."
- "Get the audio of this talk as MP3, then transcribe it."
- "Download these five videos in H.264 so they import into my editor."
The tool returns the link, title, size, resolution and codecs. Links to hosted files expire, usually after about an hour, so have the agent download the file or pass the link on straight away. If you connect cloud storage in your dashboard, files go to your S3, GCS, R2 or Google Drive instead.
Long videos and client timeouts
Some MCP clients stop waiting for a tool after a minute or two. If you see timeouts on long videos, lower the default wait_seconds to 60. The tool then returns still_running with a worker_id, and the agent calls check_youtube_download later. The job keeps running on our side either way.
Troubleshooting
| What you see | Cause | Fix |
|---|---|---|
Server fails to start with KeyError: VIDEODL_API_KEY | The key isn't in the server's environment | Set it in the --env flag or the env block, not just your shell |
error_code 41 | Invalid API key | Copy the key again from the dashboard. Reset it if it leaked |
error_code 55 | Too many requests for your plan | Wait retry_after seconds. Starter allows 1 request a minute, Growth 3 |
error_code 58 | Feature not on your plan, e.g. captions | Drop the flag, or upgrade |
error_code 3, 1 or 4 | Private, deleted or geo-blocked video | Nothing to retry. Pick another video |
More codes are in YouTube download API error codes.
Keep the key out of prompts
The key lives in the server's environment, so the model never sees it. Don't paste it into a chat or a prompt file, and reset it from the dashboard if you ever do.
Using OpenClaw, Hermes Agent, NanoClaw, Manus or Zentor instead? The same server, plus a SKILL.md version for cloud agents, is in YouTube downloads for OpenClaw, Hermes Agent and Manus.
FAQ
Is there an MCP server for downloading YouTube videos?
Yes. Open-source ones wrap yt-dlp and run it on your machine, which can hit YouTube's bot checks. The server in this guide calls the VideoDL API instead, so downloads run on our side and the agent gets back a link.
How do I add an MCP server to Claude Code?
Run claude mcp add with a name, any environment variables, and the command that starts the server, for example: claude mcp add --transport stdio videodl --env VIDEODL_API_KEY=your_key -- python3 /path/videodl_mcp.py. Check it with claude mcp list.
Where does Cursor store MCP server settings?
In ~/.cursor/mcp.json for all projects, or .cursor/mcp.json inside a project. Each server has a command, args and an env block for keys.
Why does FastMCP fail to import in the MCP Python SDK?
Version 2 of the SDK renamed FastMCP to MCPServer, imported from mcp.server.mcpserver. Either update the import or pin mcp below version 2.
What if a download takes longer than my agent waits?
The tool returns still_running with a worker_id instead of failing. The job keeps going, and the agent calls check_youtube_download with that ID to get the link.
Get an API key for your agent
Videos, audio, captions and metadata by API, from $12/month. Start with a free trial.
Keep reading
All articles →
YouTube download API for AI agents
AI agents running yt-dlp hit "Sign in to confirm you're not a bot" on cloud servers. Give your agent a download API instead: one call in, a file link out.
YouTube download API error codes
Every VideoDL API error code explained: private, deleted, geo-blocked, age-restricted, rate limits and quotas. Which to retry, which to skip, with tested code.
Download YouTube videos in H.264 only
YouTube serves AV1 and VP9 first, and they don't play or edit everywhere. How to force H.264 (AVC1) MP4 with yt-dlp or an API, and why it stops at 1080p.