Skip to content

Commit bc740ac

Browse files
authored
Merge pull request #11 from jkawamoto/short-url
Handle YouTube short URLs in transcript retrieval
2 parents ce7a468 + ad3cea2 commit bc740ac

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/mcp_youtube_transcript/server.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ def get_transcript(
3939
) -> str:
4040
"""Retrieves the transcript of a YouTube video."""
4141
parsed_url = urlparse(url)
42-
query_params = parse_qs(parsed_url.query)
4342

44-
video_id = query_params.get("v", [None])[0]
45-
if video_id is None:
46-
raise ValueError(f"couldn't find a video ID from the provided URL: {url}.")
43+
if parsed_url.hostname == "youtu.be":
44+
video_id = parsed_url.path.lstrip("/")
45+
else:
46+
q = parse_qs(parsed_url.query).get("v")
47+
if q is None:
48+
raise ValueError(f"couldn't find a video ID from the provided URL: {url}.")
49+
video_id = q[0]
4750

4851
if lang == "en":
4952
languages = ["en"]

tests/test_mcp.py

+16
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,19 @@ async def test_get_transcript_invalid_url(mcp_client_session: ClientSession) ->
101101
async def test_get_transcript_not_found(mcp_client_session: ClientSession) -> None:
102102
res = await mcp_client_session.call_tool("get_transcript", arguments={"url": "https//www.youtube.com/watch?v=a"})
103103
assert res.isError
104+
105+
106+
@pytest.mark.skipif(os.getenv("CI") == "true", reason="Skipping this test on CI")
107+
@pytest.mark.anyio
108+
async def test_get_transcript_with_short_url(mcp_client_session: ClientSession) -> None:
109+
video_id = "LPZh9BOjkQs"
110+
111+
expect = "\n".join((item.text for item in YouTubeTranscriptApi().fetch(video_id)))
112+
113+
res = await mcp_client_session.call_tool(
114+
"get_transcript",
115+
arguments={"url": f"https://youtu.be/{video_id}"},
116+
)
117+
assert isinstance(res.content[0], TextContent)
118+
assert res.content[0].text == expect
119+
assert not res.isError

0 commit comments

Comments
 (0)