import os
from mcp.server.fastmcp import FastMCP

# Read user-provided credentials from environment variables
# (configured by the user when enabling the server in Obot)
API_KEY = os.environ.get("MY_API_KEY")

mcp = FastMCP("my-simple-server")


@mcp.tool()
def add(a: int, b: int) -> int:
    """Add two numbers together."""
    return a + b


@mcp.tool()
def greet(name: str) -> str:
    """Return a friendly greeting for the given name."""
    return f"Hello, {name}! Nice to meet you."


@mcp.tool()
def word_count(text: str) -> dict:
    """Count the number of words and characters in a text."""
    words = text.split()
    return {
        "word_count": len(words),
        "char_count": len(text),
        "char_count_no_spaces": len(text.replace(" ", "")),
    }


@mcp.tool()
def check_auth() -> str:
    """Check whether the API key has been configured."""
    if not API_KEY:
        return "No API key configured. Please set MY_API_KEY in your Obot server settings."
    return f"API key is set (starts with: {API_KEY[:4]}...)"


def main():
    mcp.run()

if __name__ == "__main__":
    main()
