from mcp.server import FastMCP
from .process import get_video_meta, get_subtitles, transcribe_audio

app = FastMCP(
    "mcp-server-biliscribe",
    "A MCP Server that extracts and formats Bilibili video content into structured text, optimized for LLM processing and analysis."
)

@app.tool()
async def bili_scribe(
    video_url: str, 
    use_audio: bool = True
) -> str:
    """
    Extracts and formats Bilibili video content into structured text, optimized for LLM processing and analysis.
    
    Args:
        video_url (str): The URL(can also be av|bv|BV|ep|ss) of the Bilibili video to process.
        use_audio (bool): Whether to use audio for transcription. If False, only use subtitles generated by Bilibili.
    
    Returns:
        str: The formatted text content of the video.
    """

    metadata = get_video_meta(video_url)
    if "[Error]" in metadata:
        return metadata
    if use_audio:
        body = transcribe_audio(video_url)
    else:
        body = get_subtitles(video_url)

    return f"{metadata}\n=== 内容转录 ===\n{body}"

def serve():
    app.run(transport="stdio")