import asyncio
import sys
from pathlib import Path

from fastmcp import Client
from fastmcp.client.transports import StdioTransport
from langchain_core.tools import tool
from pydantic import BaseModel, ConfigDict

SERVER = Path(__file__).with_name("book_server.py")

class ChapterArguments(BaseModel):
    model_config = ConfigDict(strict=True, extra="forbid")
    title: str

@tool("book_chapter", args_schema=ChapterArguments)
async def book_chapter(title: str) -> str:
    """Read the public lesson count for an exact chapter title."""
    transport = StdioTransport(
        command=sys.executable,
        args=[str(SERVER)],
    )
    async with Client(transport, timeout=5, init_timeout=10) as client:
        result = await client.call_tool(
            "chapter_lessons", {"title": title}
        )
        return result.content[0].text

async def main():
    answer = await book_chapter.ainvoke(
        {"title": "Python and Roots"}
    )
    print(answer)

if __name__ == "__main__":
    asyncio.run(main())
