"""
Hello World MCP Server
A simple MCP server that demonstrates basic functionality with a hello world tool.
"""

import asyncio
from mcp.server import Server
from mcp.types import Tool, TextContent
from mcp.server.stdio import stdio_server

# Create the server instance
app = Server("hello-world-mcp-server")


@app.list_tools()
async def list_tools() -> list[Tool]:
    """List available tools."""
    return [
        Tool(
            name="say_hello",
            description="Says hello to a specified name. If no name is provided, says 'Hello, World!'",
            inputSchema={
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "The name to greet (optional)",
                    }
                },
            },
        )
    ]


@app.call_tool()
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
    """Handle tool calls."""
    if name == "say_hello":
        person_name = arguments.get("name", "World")
        greeting = f"Hello, {person_name}!"
        
        return [
            TextContent(
                type="text",
                text=greeting
            )
        ]
    else:
        raise ValueError(f"Unknown tool: {name}")


async def main():
    """Main entry point for the server."""
    async with stdio_server() as (read_stream, write_stream):
        await app.run(
            read_stream,
            write_stream,
            app.create_initialization_options()
        )


def run():
    """Entry point for the package."""
    asyncio.run(main())


if __name__ == "__main__":
    run()
