Skip to main content

基于 Model Context Protocol (MCP) 的时间服务器,支持通过 StreamableHTTP 传输协议提供当前时间查询服务

Project description

MCP Time Server

一个基于 Model Context Protocol (MCP) 的时间服务器,支持通过 StreamableHTTP 传输协议提供当前时间查询服务。

功能特性

  • 🕒 支持多时区时间查询
  • 🌐 使用 MCP StreamableHTTP 协议
  • 🎯 同时支持有状态和无状态模式
  • 📦 提供 JSON-RPC 接口
  • 🔧 支持多种运行模式(Studio、SSE、StreamableHTTP)

安装

pip install mcp-time-server

快速开始

启动服务器

使用 StreamableHTTP 模式启动服务器(推荐生产环境使用):

python -m mcp_time_server streamable-http

或者使用命令行脚本:

mcp-time-server streamable-http

支持的运行模式

  1. Studio 模式(默认):使用 stdio 传输,适用于本地进程间通信

    python -m mcp_time_server studio
    
  2. SSE 模式:使用 Server-Sent Events 传输

    python -m mcp_time_server sse
    
  3. StreamableHTTP 模式:推荐的生产环境传输方式

    python -m mcp_time_server streamable-http
    

API 文档

get_current_time

获取指定时区的当前时间。

参数

  • timezone (可选): 时区字符串,例如 "Asia/Shanghai"、"America/New_York"

返回值

格式化的当前时间字符串,格式为:YYYY-MM-DD HH:MM:SS.ffffff TZ

示例请求

{
  "jsonrpc": "2.0",
  "id": "1",
  "method": "tools/call",
  "params": {
    "name": "get_current_time",
    "arguments": {
      "timezone": "Asia/Shanghai"
    }
  }
}

示例响应

{
  "jsonrpc": "2.0",
  "id": "1",
  "result": {
    "content": [
      {
        "type": "text",
        "text": "2025-12-31 14:35:58.589092 CST"
      }
    ],
    "structuredContent": {
      "result": "2025-12-31 14:35:58.589092 CST"
    },
    "isError": false
  }
}

客户端示例

Python 客户端

import requests
import json

BASE_URL = "http://localhost:8000/mcp"

# 发送 JSON-RPC 请求
def send_json_rpc(method, params):
    request = {
        "jsonrpc": "2.0",
        "id": "1",
        "method": method,
        "params": params
    }
    
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json"
    }
    
    response = requests.post(BASE_URL, json=request, headers=headers)
    return response.json()

# 获取当前时间
result = send_json_rpc("tools/call", {
    "name": "get_current_time",
    "arguments": {
        "timezone": "Asia/Shanghai"
    }
})

print(result)

配置说明

服务器支持通过配置文件进行自定义设置:

# mcp_server_config.py
MODES = {
    "streamable-http": {
        "transport": "streamable-http",
        "description": "StreamableHttp模式 - 推荐的生产环境传输方式",
        "stateless_http": True,
        "json_response": True,
        "host": "0.0.0.0",
        "port": 8000
    }
}

技术栈

  • Python 3.7+
  • FastAPI
  • Starlette
  • MCP Protocol
  • pytz

开发

安装开发依赖

pip install -e .

运行测试

python -m pytest

许可证

MIT License

贡献

欢迎提交 Issue 和 Pull Request!

关于 MCP

Model Context Protocol (MCP) 是一个用于构建服务器的协议,允许你以安全、标准化的方式向 LLM 应用程序公开数据和功能。

了解更多:Model Context Protocol

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcp_time_server_studio-0.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcp_time_server_studio-0.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file mcp_time_server_studio-0.1.0.tar.gz.

File metadata

  • Download URL: mcp_time_server_studio-0.1.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for mcp_time_server_studio-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e42f581c8b1b6b978a0eff6f2f4bb1076ba3ef3145644638cafa889bb5c7f4f0
MD5 fd3b38be5771442bc7717983528b6724
BLAKE2b-256 8ddb8abd64ee5f719f06ca4b87123a23780515760e15d6a74e4ca6e2c9c910a7

See more details on using hashes here.

File details

Details for the file mcp_time_server_studio-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mcp_time_server_studio-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 81087893dcf44284ab76efb68838790ea6a30eac3b6035f07e5419f722296699
MD5 99c5b005cc90ab015886171c622cfda4
BLAKE2b-256 82c11b64b9febc8ec531dcd2a669d79f2fa56e55ae1071a602cd7e6311d00739

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page