from mcp.server import Server
from mcp.types import Tool, TextContent
from pydantic import BaseModel
from notion_client import Client

class NotionCreatePageInput(BaseModel):
    notion_token: str
    parent_id: str
    title: str
    content: str

server = Server("mcp-notion")

@server.list_tools()
async def list_tools():
    return [
        Tool(
            name="notion_create_page",
            description="Notion에 새 페이지를 생성",
            inputSchema=NotionCreatePageInput.schema(),
        ),
    ]

@server.call_tool()
async def call_tool(name: str, arguments: dict):
    if name == "notion_create_page":
        notion = Client(auth=arguments["notion_token"])
        new_page = notion.pages.create(
            parent={"database_id": arguments["parent_id"]},
            properties={
                "title": [
                    {
                        "type": "text",
                        "text": {"content": arguments["title"]}
                    }
                ]
            },
            children=[
                {
                    "object": "block",
                    "type": "paragraph",
                    "paragraph": {
                        "rich_text": [
                            {
                                "type": "text",
                                "text": {"content": arguments["content"]}
                            }
                        ]
                    }
                }
            ]
        )
        return [TextContent(type="text", text=f"페이지 생성됨: {new_page['url']}")]
    raise ValueError(f"Unknown tool: {name}")

def main():
    import asyncio
    asyncio.run(server.run_stdio())

if __name__ == "__main__":
    main()
