时间服务工具

一个提供多种时间相关功能的MCP服务器,支持本地Stdio和远程HTTP两种连接方式,适用于需要时间处理的LLM应用。

  • by Cam10001110101
  • JavaScript
  • 混合部署
  • 更新于
在 GitHub 查看 Cam10001110101/mcp-server-http-time

使用说明

MCP时间服务器

一个提供时间相关工具的模型上下文协议(MCP)服务器 双模式支持:

  • 标准I/O传输 对于本地MCP客户端(通过npm)
  • 可流式传输的HTTP传输 用于远程访问(通过Cloudflare Workers)

这台服务器允许大型语言模型(LLMs)通过多种连接方式访问各种日期/时间函数。

特点/特性

提供以下MCP工具:

  • current_time获取当前日期和时间,并以指定的格式和时区进行显示。
  • relative_time获取一个易于人类阅读的相对时间字符串(例如,“5分钟后”,“2小时前”)。
  • days_in_month获取特定月份的天数。
  • get_timestamp获取给定时间的Unix时间戳(毫秒)。
  • convert_time在不同的IANA时区之间转换时间。
  • get_week_year获取给定日期的周数和ISO周数。

项目结构

mcp-time/
├── src/
│   └── index.ts      # Cloudflare Worker entry point & MCP logic
├── package.json      # Project dependencies and scripts
├── tsconfig.json     # TypeScript configuration
└── wrangler.toml     # Cloudflare Worker configuration

安装

选项1:从npm安装(标准I/O模式)

全局安装该包或使用 npx 来运行:

# Global installation
npm install -g @cbuk100011/mcp-time

# Or use directly with npx
npx @cbuk100011/mcp-time

选项2:使用远程服务器(HTTP模式)

直接连接到已部署的 Cloudflare Worker:

示例:

https://mcp.time.mcpcentral.io

使用方法/用法

标准I/O传输(本地)

配置您的MCP客户端(例如,Claude Desktop)以使用stdio传输方式:

{
  "mcpServers": {
    "time-server": {
      "command": "npx",
      "args": ["@cbuk100011/mcp-time"]
    }
  }
}

或者使用全局安装:

{
  "mcpServers": {
    "time-server": {
      "command": "/path/to/node/bin/mcp-time"
    }
  }
}

可流式传输的HTTP传输(远程)

将您的MCP客户端配置为使用远程HTTP端点:

{
  "mcpServers": {
    "time-server": {
      "url": "https://mcp.time.mcpcentral.io",
      "transport": "streamable-http"
    }
  }
}

发展

  1. 克隆仓库:

    git clone https://github.com/Cam10001110101/mcp-server-http-time.git
    cd mcp-server-http-time
    
  2. 安装依赖项:

    npm install
    
  3. 构建: 编译TypeScript代码:

    npm run build
    

    (这个编译通过 src/index.ts to dist/index.js)

  4. 本地测试:

    测试Studio模式:

    echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}' | node dist/index.js
    

    测试HTTP模式(通过Wrangler):

    npx wrangler dev
    

    这将启动服务器 http://localhost:8787然后,您可以使用curl进行测试,或者将您的MCP客户端指向这个本地端点。

部署

部署到 Cloudflare Workers(HTTP 模式)

  1. 配置Cloudflare:

    cp wrangler.toml.example wrangler.toml
    

    编辑 wrangler.toml 配置您的域名(可选)。

  2. 登录并部署:

    wrangler login
    npx wrangler deploy
    

发布到 npm(标准输入输出模式)

  1. 构建软件包:

    npm run build
    
  2. 发布:

    npm publish --access public
    

适用于可流式HTTP服务器的连接器

新主要服务提供商已采用模型上下文协议,并且现在直接支持可流式传输的HTTP服务器。Anthropic、OpenAI和微软均已采纳这一现代传输协议。

📋 协议注意事项Streamable HTTP 是已弃用的 HTTP+SSE 传输方式的现代替代方案。

Anthropic MCP 连接器

Anthropic的 MCP 连接器 允许您通过消息API直接使用Streamable HTTP服务器,而无需单独的MCP客户端。

MCP连接器非常适合此服务器,因为它使用的是可流式传输的HTTP架构。只需在您的API请求中包含此服务器即可:

curl https://api.anthropic.com/v1/messages \
  -H "Content-Type: application/json" \
  -H "X-API-Key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "anthropic-beta: mcp-client-2025-04-04" \
  -d '{
    "model": "claude-sonnet-4-20250514",
    "max_tokens": 1000,
    "messages": [{
      "role": "user", 
      "content": "What time is it in Tokyo?"
    }],
    "mcp_servers": [{
      "type": "url",
      "url": "https://your.worker.url.workers.dev",
      "name": "http-time-server"
    }]
  }'

Anthropic MCP连接器的优势:

  • 无需客户端设置 通过API直接连接
  • 原生可流式传输的HTTP支持 - 专为此类服务器设计

OpenAI智能体软件开发工具包(SDK)

OpenAI 还通过其(技术/服务)支持 Streamable HTTP 服务器 代理SDK 使用 MCPServerStreamableHttp 班级:

from agents.mcp.server import MCPServerStreamableHttp

# Connect to this Streamable HTTP server
server = MCPServerStreamableHttp({
    "url": "https://your.worker.url.workers.dev",
    "headers": {"Authorization": "Bearer your-token"},  # if needed
})

# Use the server in your OpenAI agent
await server.connect()
tools = await server.list_tools()
result = await server.call_tool("current_time", {"timezone": "Asia/Tokyo"})

Microsoft Copilot Studio(可译为“微软Copilot工作室”或根据具体语境简化为“微软Copilot平台”)

Microsoft Copilot Studio 现在支持可流式传输的 HTTP 服务器 MCP集成现已全面推出您可以将此服务器连接到 Copilot Studio 的方法是:

  1. 构建自定义连接器 将您的MCP服务器与Copilot Studio连接起来
  2. 在Copilot Studio中添加工具 通过选择“添加工具”并搜索您的MCP服务器
  3. 直接使用服务器 在您的代理中启用生成式编排功能

更多MCP客户端即将推出

随着越来越多的MCP客户端开始支持Streamable HTTP,请保持关注。以下是一些资源,它们维护着MCP客户端及其功能的列表:

测试与验证

MCP 检查器(HTTP 模式)

官方 MCP 检查器 该工具提供了一个基于网页的界面来测试您的服务器。

  1. 启动你的服务器:

    # Local HTTP server
    npx wrangler dev
    
    # Or use deployed URL: https://mcp.time.mcpcentral.io
    
  2. 打开MCP检查器:

    • 访问:https://github.com/modelcontextprotocol/inspector
    • 或者跑: npx @modelcontextprotocol/inspector
  3. 配置连接:

    • 交通: 可流式传输的HTTP
    • 网址: http://localhost:8787 或者 https://mcp.time.mcpcentral.io
    • 点击 连接

命令行测试(标准I/O模式)

直接测试stdio传输:

# Test initialization
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}' | npx @cbuk100011/mcp-time

# Test tool call
echo '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"current_time","arguments":{"timezone":"America/New_York"}}}' | npx @cbuk100011/mcp-time

可用于测试的工具

检查员将展示所有六个与时间相关的工具:

  • **current_time**使用不同的时区进行测试(例如,“America/New_York”,“Europe/London”)
  • **relative_time**使用各种时间字符串进行测试(例如,“2024-12-25T00:00:00Z”)
  • **days_in_month**使用不同的月份和年份进行测试
  • **get_timestamp**将日期时间字符串转换为Unix时间戳
  • **convert_time**在不同的时区之间进行转换
  • **get_week_year**获取特定日期的周数

示例测试用例

在检查器中尝试这些测试用例:

// current_time
{"timezone": "Asia/Tokyo", "format": "iso"}

// relative_time  
{"time": "2024-12-25T00:00:00Z"}

// days_in_month
{"month": 2, "year": 2024}

// get_timestamp
{"time": "2024-06-15T12:00:00Z"}

// convert_time
{"time": "2024-06-15T12:00:00", "from": "UTC", "to": "America/Los_Angeles"}

// get_week_year
{"date": "2024-06-15"}

验证核对清单

使用检查器进行验证:

  • ✅ 服务器连接成功
  • ✅ 列出了所有6个工具
  • ✅ 工具模式已正确定义
  • ✅ 工具执行无误
  • ✅ 结果格式正确
  • ✅ 错误处理机制适用于无效输入

MCP Inspector 提供了在将服务器与AI客户端集成之前进行测试的最全面方法。


身份验证与安全考量

⚠️(警告符号,无具体文字含义,通常表示警告或注意) 重要提示:此示例服务器未实施任何身份验证或安全措施。

对于生产环境的MCP服务器,您应按照概述中所述实施适当的认证措施 官方MCP文档