from mcp.server.fastmcp import FastMCP
import os
from mcp.types import TextContent
import httpx

# 创建MCP服务器实例
mcp = FastMCP(
    name="dataeyes-mcp-server",
    version="0.1.0",
    instructions="This is a MCP server for Dataeyes."
)


"""

获取环境变量中的 API 密钥, 用于调用数眼智能 API
环境变量名为: DATAEYES_API_KEY
获取方式请访问: https://shuyanai.com 

"""

api_key = os.getenv('DATAEYES_API_KEY')
api_url = "https://api.shuyanai.com"


@mcp.tool(description="读取网页内容并返回大模型友好的 Markdown 格式")
async def reader(url: str , timeout: int = 30) -> TextContent:
    """
    读取网页内容并返回大模型友好的 Markdown 格式

    Args:
        url (str): 要读取的网页链接.
        timeout (int): 页面加载超时时间，单位为秒（1-60）.

    Returns:
        types.TextContent: 返回包含网页内容的 Markdown 格式.

    """


    try:
        reader_url = f"{api_url}/v1/reader"

        # 参数设置
        params = {
            "url": url,
            "timeout": timeout,
        }

        # 请求头设置
        headers = {
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json",
        }

        async with httpx.AsyncClient() as client:
            response = await client.get(reader_url, params=params, headers=headers)
            response.raise_for_status()
            result = response.json()

        if result.get("status") != 0 :
            error_msg = result.get("message", "unknown error")
            raise Exception(f"API response error: {error_msg}")

        return TextContent(type="text", text=response.text)

    except httpx.HTTPError as e:
        raise Exception(f"HTTP request failed: {str(e)}") from e


if __name__ == "__main__":
    mcp.run()