import subprocess
import json
import sys
request = {
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "get_transcript",
"arguments": {
"url": "https://www.youtube.com/watch?v=1OSEzdOpmWQ"
}
}
}
json_req = json.dumps(request) + "\n"
cmd = ["docker", "run", "--rm", "-i", "mcp-browser-youtube:latest"]
try:
process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Send request
process.stdin.write(json_req)
process.stdin.flush()
# Read response line by line
output_json = ""
while True:
line = process.stdout.readline()
if not line:
break
# print("STDOUT:", line) # Debugging
if "jsonrpc" in line and "result" in line:
output_json = line
break
process.terminate()
if output_json:
data = json.loads(output_json)
# The tool returns a JSON string inside the 'text' field
content_str = data['result']['content'][0]['text']
content_data = json.loads(content_str)
# Handle get_transcript response (plain text)
if 'transcript' in content_data and isinstance(content_data['transcript'], str):
full_text = content_data['transcript']
# Clean up newlines and extra spaces
full_text = full_text.replace('\n', ' ').replace(' ', ' ')
with open("transcript_full.txt", "w", encoding="utf-8") as f:
f.write(full_text)
print("Transcript saved to transcript_full.txt")
# Handle get_timed_transcript response (list of objects)
elif 'transcript' in content_data and isinstance(content_data['transcript'], list):
transcript_list = content_data['transcript']
full_text = " ".join([item['text'] for item in transcript_list])
full_text = full_text.replace('\n', ' ').replace(' ', ' ')
with open("transcript_full.txt", "w", encoding="utf-8") as f:
f.write(full_text)
print("Transcript saved to transcript_full.txt")
else:
print("No valid JSON response found.")
except Exception as e:
print("Error:", e)