Files
hz3-agent/mcp_server.py

35 lines
1.1 KiB
Python
Raw Permalink Normal View History

"""简单的 MCP 服务器 - 提供时间查询和文本处理工具"""
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("hz3-tools")
@mcp.tool()
def get_current_time(timezone: str = "Asia/Shanghai") -> str:
"""获取当前时间"""
from datetime import datetime
import pytz
try:
tz = pytz.timezone(timezone)
now = datetime.now(tz)
return f"当前时间({timezone}: {now.strftime('%Y-%m-%d %H:%M:%S %Z')}"
except Exception as e:
return f"时区错误: {e}"
@mcp.tool()
def count_chars(text: str) -> str:
"""统计文本的字符数、词数等"""
char_count = len(text)
word_count = len(text.split())
line_count = text.count('\n') + 1
chinese_chars = sum(1 for c in text if '\u4e00' <= c <= '\u9fff')
return f"字符数: {char_count}, 词数: {word_count}, 行数: {line_count}, 中文字符: {chinese_chars}"
@mcp.tool()
def generate_uuid() -> str:
"""生成一个随机UUID"""
import uuid
return str(uuid.uuid4())
if __name__ == "__main__":
mcp.run(transport="stdio")