Skip to main content

MCP Server for Alibaba Cloud SLS (Log Service) - Pure Python

Project description

Aliyun SLS MCP Server (Pure Python)

阿里云 SLS (日志服务) MCP Server - 纯 Python 实现

特点

  • 纯 Python: 无需 Node.js/npm
  • 零安装: 通过 uvxpipx 直接运行
  • 轻量级: 仅依赖 mcpaliyun-log-python-sdk
  • FastMCP: 使用官方 Python SDK 的高级 API

安装与使用

方式一: uvx (推荐)

# 直接运行 (自动安装依赖)
export SLS_ENDPOINT=cn-hangzhou.log.aliyuncs.com
export SLS_ACCESS_KEY_ID=your-access-key-id
export SLS_ACCESS_KEY_SECRET=your-access-key-secret
export SLS_PROJECT=your-project

uvx aliyun-sls-mcp-py

方式二: pipx

pipx run aliyun-sls-mcp-py

方式三: pip 安装

pip install aliyun-sls-mcp-py

# 运行
SLS_ENDPOINT=xxx SLS_ACCESS_KEY_ID=xxx SLS_ACCESS_KEY_SECRET=xxx SLS_PROJECT=xxx \
    aliyun-sls-mcp-py

方式四: Claude Desktop 配置

编辑 ~/Library/Application Support/Claude/claude_desktop_config.json:

{
  "mcpServers": {
    "aliyun-sls": {
      "command": "uvx",
      "args": ["aliyun-sls-mcp-py"],
      "env": {
        "SLS_ENDPOINT": "cn-hangzhou.log.aliyuncs.com",
        "SLS_ACCESS_KEY_ID": "your-access-key-id",
        "SLS_ACCESS_KEY_SECRET": "your-access-key-secret",
        "SLS_PROJECT": "your-project",
        "SLS_LOGSTORE": "your-default-logstore"
      }
    }
  }
}

方式五: Cursor IDE 配置

编辑 ~/.cursor/mcp.json:

{
  "mcpServers": {
    "aliyun-sls": {
      "command": "uvx",
      "args": ["aliyun-sls-mcp-py"],
      "env": {
        "SLS_ENDPOINT": "cn-hangzhou.log.aliyuncs.com",
        "SLS_ACCESS_KEY_ID": "your-access-key-id",
        "SLS_ACCESS_KEY_SECRET": "your-access-key-secret",
        "SLS_PROJECT": "your-project"
      }
    }
  }
}

环境变量

变量名 必填 说明
SLS_ENDPOINT SLS 服务端点,如 cn-hangzhou.log.aliyuncs.com
SLS_ACCESS_KEY_ID 阿里云 AccessKey ID
SLS_ACCESS_KEY_SECRET 阿里云 AccessKey Secret
SLS_PROJECT SLS 项目名称
SLS_LOGSTORE 默认日志库名称

提供的工具

1. list_logstores

列出当前项目下所有的日志库

无需参数

2. query_logs

查询 SLS 日志(支持分页)

参数 类型 必填 说明
query string SLS 查询语句
logstore string 日志库名称
from_time string 开始时间 (默认 -1h)
to_time string 结束时间 (默认 now)
limit int 每页条数 (默认 100, 最大 500)
offset int 偏移量 (默认 0)

3. search_keyword

搜索包含关键字的日志

参数 类型 必填 说明
keyword string 搜索关键字
logstore string 日志库名称
from_time string 开始时间 (默认 -24h)
to_time string 结束时间 (默认 now)
limit int 每页条数 (默认 20)
offset int 偏移量 (默认 0)

4. analyze_errors

分析错误日志

参数 类型 必填 说明
logstore string 日志库名称
from_time string 开始时间 (默认 -24h)
to_time string 结束时间 (默认 now)
top_n int 返回前N个高频错误 (默认 10)

提供的资源

  • sls://config - 当前 SLS 配置信息
  • sls://help - SLS 查询语法帮助
  • sls://logstores - 所有日志库列表

提供的提示词

  • analyze_errors_prompt - 错误日志分析提示词
  • search_logs_prompt - 日志搜索提示词

时间格式支持

支持多种时间格式:

格式 示例 说明
相对时间 -1h, -30m, -7d 1小时前、30分钟前、7天前
当前时间 now 当前时间戳
ISO 格式 2024-01-01T00:00:00 ISO 8601 格式
Unix 时间戳 1704067200 秒或毫秒时间戳

开发

# 克隆项目
git clone https://github.com/dongmaowei/aliyun-sls-mcp-py.git
cd aliyun-sls-mcp-py

# 使用 uv 创建虚拟环境
uv venv
source .venv/bin/activate  # Linux/macOS
# .venv\Scripts\activate   # Windows

# 安装依赖
uv sync

# 开发模式运行
uv run mcp dev src/aliyun_sls_mcp_py/server.py

# 直接运行
uv run python -m aliyun_sls_mcp_py.server

测试

# 运行测试
uv run pytest

# 测试 MCP 服务器
uv run mcp dev src/aliyun_sls_mcp_py/server.py

发布到 PyPI

# 安装构建工具
uv pip install build twine

# 构建
python -m build

# 上传到 PyPI
twine upload dist/*

# 或使用 uv
uv publish

三种方案对比

特性 Python + npm 混合 纯 Node.js 纯 Python
依赖 Python + uv + npm Node.js Python + uv
启动 npx -y @scope/pkg npx -y @scope/pkg uvx pkg
包大小 较大
开发效率
SDK 成熟度 FastMCP 底层 API FastMCP
适用场景 npm 生态用户 前端团队 Python 生态用户

License

MIT

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

aliyun_sls_mcp_py-0.1.2.tar.gz (87.5 kB view details)

Uploaded Source

Built Distribution

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

aliyun_sls_mcp_py-0.1.2-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file aliyun_sls_mcp_py-0.1.2.tar.gz.

File metadata

  • Download URL: aliyun_sls_mcp_py-0.1.2.tar.gz
  • Upload date:
  • Size: 87.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for aliyun_sls_mcp_py-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e2806208dbccd9e3c9a9f4721bf8c6dd8c3a1d73c7715a875912088f0cfb2c11
MD5 f3ecaf51be73a9b45b9a4e142d0c3b1b
BLAKE2b-256 e7cdf9d688f8498e5819ceb9864cc3a440aa9bca58a83e83e81b1d793b434ec1

See more details on using hashes here.

File details

Details for the file aliyun_sls_mcp_py-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for aliyun_sls_mcp_py-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 90d8b27fbec36c9247adb4ce73d0e4e69836b86edbf9744c5e0541643c821cda
MD5 5544e1646b3444fd618a7c63f409d53c
BLAKE2b-256 87969f626e3868f9789683f48c125b53167527f03a8d8d7c2586793ce7132eb1

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