Aliyun SLS MCP Server (Pure Python)
阿里云 SLS (日志服务) MCP Server - 纯 Python 实现
特点
- 纯 Python: 无需 Node.js/npm
- 零安装: 通过
uvx或pipx直接运行 - 轻量级: 仅依赖
mcp和aliyun-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
Metadata
Release files for aliyun-sls-mcp-py 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| aliyun_sls_mcp_py-0.1.3.tar.gz | 87.9 kB | Without provenance | Details for aliyun_sls_mcp_py-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| aliyun_sls_mcp_py-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for aliyun_sls_mcp_py-0.1.3-py3-none-any.whl |
Total release size: 97.9 kB
Release files / aliyun_sls_mcp_py-0.1.3.tar.gz
| Download URL | aliyun_sls_mcp_py-0.1.3.tar.gz |
|---|---|
| Size | 87.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2287063b6d3e0452347fd0879cdb6ce3c861946f5dae3d0141e728d9db9130e3
|
|
BLAKE2b-256 checksum How to use checksums |
c0742dd03c736ca81c8c3a71d5ecd9fb906eb5ccb5e577cffe9f2c9bb87427ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.13
|
Release files / aliyun_sls_mcp_py-0.1.3-py3-none-any.whl
| Download URL | aliyun_sls_mcp_py-0.1.3-py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4fd5f5ffb90d1c6de6292cc33221340e3176b797e280bad1ef2e44d800019189
|
|
BLAKE2b-256 checksum How to use checksums |
d186fd502c61cd563bebc903b9fa0477a7475a55a65d5b42add77e6af6c29be2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.13
|
Security for aliyun-sls-mcp-py 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.