关于 Time MCP Server
Time MCP Server 是一个基于 FastMCP 2.0 的时间工具服务,可返回当前时间及其组成信息。它适用于需要时区和格式选项的 LLM 客户端。
主要功能
- 01获取当前日期和时间
- 02按格式输出时间
- 03支持指定时区
- 04返回时间组成部分
适用场景
- 01查询指定时区的当前时间
- 02按所需格式获取日期
- 03获取星期及时间各字段
MCP 相关
服务通过 MCP 提供获取当前时间和时间组成部分的工具,支持 stdio 和 streamable HTTP 传输。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
时间 MCP 服务
一个为大模型提供时间相关工具的模型上下文协议(MCP)服务。使用 FastMCP 2.0 构建。
设置
本项目使用 uv 管理 Python 包,并使用 task 运行常用命令。
先决条件
安装
- 克隆仓库
- 运行设置任务以创建虚拟环境:
task setup - 以开发模式安装软件包:
task install
使用方法
运行服务
使用任务运行器运行 MCP 服务:
task run
默认情况下,服务将使用 stdio 传输启动,非常适合与 Claude Desktop 和其他本地 MCP 客户端连接。
命令行选项
你可以配置传输类型和端口:
# Run with stdio transport (default)
python -m time_mcp_server.main --transport stdio
# Run with HTTP transport on port 9000
python -m time_mcp_server.main --transport streamable-http --port 9000
或者使用任务运行器时,可以在 -- 后传递参数:
# Run with stdio transport (default)
task run
# Run with HTTP transport
task run -- --transport streamable-http --port 9000
可用工具
Time MCP Server 为大模型提供以下工具:
-
get_current_time - 获取当前日期和时间
- 参数:
date_format(可选):日期时间的格式字符串(例如,'%Y-%m-%d %H:%M:%S')timezone(可选):时区名称(例如,'Europe/Berlin'、'America/New_York')。默认为 'Europe/Berlin'
- 返回:包含 ISO 格式的当前时间、指定格式的时间以及所用时区的字典
- 参数:
-
get_time_components - 获取当前时间的组成部分
- 参数:
timezone(可选):时区名称(例如,'Europe/Berlin'、'America/New_York')。默认为 'Europe/Berlin'
- 返回:包含年、月、日、小时、分钟、秒、微秒、星期几和时区的字典
- 参数:
客户端使用示例
要通过大模型客户端使用此 MCP 服务,请连接到服务端点并调用工具:
import asyncio
from fastmcp import Client
async def main():
# Connect to the Time MCP Server
# Make sure to include the root path '/' in the URL
async with Client("http://localhost:8090/") as client:
# Get the current time
result = await client.call_tool("get_current_time", {"date_format": "%Y-%m-%d %H:%M:%S", "timezone": "America/New_York"})
# Parse the JSON response
import json
time_data = json.loads(result[0].text)
print(time_data)
# Output: {'iso_time': '2025-05-09T10:27:31.123456-04:00', 'formatted_time': '2025-05-09 10:27:31', 'timezone': 'America/New_York'}
# Get time components
result = await client.call_tool("get_time_components", {"timezone": "Europe/Berlin"})
components = json.loads(result[0].text)
print(components)
# Output: {'year': 2025, 'month': 5, 'day': 9, 'hour': 16, 'minute': 27, 'second': 31, 'microsecond': 123456, 'weekday': 4, 'timezone': 'Europe/Berlin'}
# Run the async client
asyncio.run(main())
连接到 Claude Desktop
由于 Time MCP Server 现在默认使用 stdio 传输,因此可以轻松连接到 Claude Desktop。设置方法如下:
- 打开 Claude Desktop
- 点击顶部栏中的“Claude”菜单(macOS 上),或 Windows 上相应的菜单
- 选择“Settings...”,然后前往“Developer” > “Edit Config”
- 将 Time MCP Server 添加到配置文件中:
{
"mcpServers": {
"current-time": {
"command": "/path/to/uv",
"args": [
"run", "--directory", "/path/to/time-mcp-server", "python", "-m", "time_mcp_server.main"
]
}
}
}
请务必将 /path/to/uv 替换为 uv 可执行文件的路径,并将 /path/to/time-mcp-server 替换为项目目录的实际路径。
- 保存配置文件并重启 Claude Desktop
- 你应该会在 Claude 输入框的右下角看到一个锤子图标
- 点击该图标即可查看 Time MCP Server 的工具
现在你可以让 Claude 使用时间工具,例如:
- “现在几点了?”
- “你能告诉我当前日期吗?格式为 YYYY-MM-DD。”
- “今天是星期几?”
- “纽约现在几点?”
- “东京时区现在几点?”
项目结构
项目采用以下结构组织:
src/time_mcp_server/
├── __init__.py # Package initialization
├── main.py # Command-line interface and entry point
├── server.py # MCP server configuration
└── tools.py # Time tool implementations
tests/
└── test_client.py # Tests for the time tools
开发
- 格式化代码:
task format - 检查并修复 lint 问题:
task check - 运行测试:
task test
测试
本项目包含演示如何将 Time MCP Server 用作客户端的测试。测试使用 FastMCP 的内存客户端直接连接到服务,无需网络连接。
# Example of using the FastMCP client to connect to the server
from fastmcp import Client
from time_mcp_server.server import mcp # Import the MCP server instance
async with Client(mcp) as client:
# List available tools
tools = await client.list_tools()
# Call the get_current_time tool
result = await client.call_tool("get_current_time", {"date_format": "%Y-%m-%d %H:%M:%S", "timezone": "Europe/Berlin"})
text_content = result[0]
# Parse the JSON response
import json
time_data = json.loads(text_content.text)
print(time_data)
运行测试:
task test
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。