Chainguard注册表API
一个生产就绪的REST API和FastMCP服务器,用于与Chainguard的容器注册表交互。基于FastAPI和FastMCP构建,为AI助手提供传统的HTTP端点和模型上下文协议集成。
🚀 特性
- API全面覆盖:从Chainguard注册表API实现所有与注册表相关的终结点
- 类型安全:使用Pydantic用Python编写,具有全面的类型定义
- 双接口:用于人工智能助手的REST API+FastMCP服务器
- 认证:Chainguard身份验证的安全API令牌处理(提取令牌,OIDC)
- 速率限制:内置速率限制,以遵守API配额并防止滥用
- 缓存:使用Redis进行智能响应缓存,以减少API调用
- 错误处理:使用有意义的错误消息进行正确的错误处理
- 日志记录:带相关ID的结构化JSON日志记录,用于调试和监控
- 测试:全面的单元、集成和性能测试
- 文档:带有交互式文档的OpenAPI/Swagger规范
- 容器原生:Docker和Kubernetes部署就绪
📦 安装
# Using pip
pip install chainguard-registry-api
# Using pipenv
pipenv install chainguard-registry-api
# From source
git clone
cd chainguard-registry-api
pip install -e .🔧 配置
可以使用环境变量配置服务器:
| 变量 | 描述 | 默认值 |
|---|---|---|
CHAINGUARD_REGISTRY_URL | Chaingguard注册表URL | https://cgr.dev |
CHAINGUARD_ORG | 默认组织 | chainguard |
CHAINGUARD_PULL_TOKEN | 您的Chaingguard拉令牌 | - |
API_PORT | API服务器端口 | 8000 |
RATE_LIMIT_REQUESTS | 每个窗口的API请求 | 100 |
RATE_LIMIT_WINDOW | 速率限制窗口(秒) | 3600 |
REDIS_URL | Redis缓存URL | redis://localhost:6379 |
LOG_LEVEL | 日志记录级别(调试、信息、警告、错误) | info |
🚀 用法
MCP服务器的工作原理
MCP服务器通过stdio(标准输入/输出)而不是HTTP进行通信。它们的设计用途是:
- AI助手 (如Claude)内置MCP支持
- MCP客户端 你建立自己
- MCP兼容应用程序 可以生成并与MCP服务器通信
启动服务器
# With environment variables
export CHAINGUARD_PULL_TOKEN=your_token_here
export CHAINGUARD_ORG=my-org
# Start REST API server
make dev
# Start MCP server
make mcp
# Or using Python directly
python app/main.py # REST API
python mcp/server.py # MCP server方法1:使用Claude桌面应用程序
- 将服务器添加到Claude Desktop配置中(
~/Library/Application Support/Claude/claude_desktop_config.json在 macOS 上:
{
"mcpServers": {
"chainguard": {
"command": "python",
"args": ["/path/to/chainguard-registry-api/mcp/server.py"],
"env": {
"CHAINGUARD_PULL_TOKEN": "your_token_here",
"CHAINGUARD_ORG": "your-org"
}
}
}
}- 重新启动克劳德桌面
- Chaingguard工具将在您的对话中提供
方法1a:使用Claude桌面应用程序(Docker)
如果你在Docker中运行MCP服务器,请按如下方式配置Claude Desktop:
{
"mcpServers": {
"chainguard": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e",
"CHAINGUARD_PULL_TOKEN=your_token_here",
"-e",
"CHAINGUARD_ORG=your-org",
"chainguard-registry-api:latest",
"python",
"mcp/server.py"
]
}
}
}方法1b:与VS代码或其他MCP兼容编辑器一起使用
对于带有MCP支持扩展的VS Code,请添加到您的设置中:
{
"mcp.servers": {
"chainguard": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e",
"CHAINGUARD_PULL_TOKEN=${env:CHAINGUARD_PULL_TOKEN}",
"-e",
"CHAINGUARD_ORG=${env:CHAINGUARD_ORG}",
"-e",
"LOG_LEVEL=info",
"chainguard-registry-api:latest",
"python",
"mcp/server.py"
],
"env": {
"DOCKER_HOST": "unix:///var/run/docker.sock"
}
}
}
}对于带有卷装载的开发设置:
{
"mcp.servers": {
"chainguard": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-v",
"${workspaceFolder}:/app:ro",
"-e",
"CHAINGUARD_PULL_TOKEN=${env:CHAINGUARD_PULL_TOKEN}",
"-e",
"LOG_LEVEL=debug",
"--name",
"chainguard-mcp-dev",
"chainguard-registry-api:latest",
"python",
"mcp/server.py"
]
}
}
}方法2:构建MCP客户端
创建一个生成服务器并与之通信的客户端应用程序:
import asyncio
from mcp.client import MCPTestClient
async def main():
# Create MCP client
client = MCPTestClient()
try:
await client.connect()
# List available repositories
repos = await client.call_tool(
"list_repositories",
organization="chainguard"
)
print("Repositories:", repos)
# Get Python repository images
images = await client.call_tool(
"get_repository_images",
repository="python",
organization="chainguard"
)
print("Python images:", images)
# Get manifest for specific image
manifest = await client.call_tool(
"get_image_manifest",
repository="python",
tag="latest",
organization="chainguard"
)
print("Manifest:", manifest)
# Search repositories
search_results = await client.call_tool(
"search_repositories",
query="python",
limit=5
)
print("Search results:", search_results)
finally:
await client.disconnect()
if __name__ == "__main__":
asyncio.run(main())方法3:使用MCP检查员进行直接测试
您可以直接使用MCP检查器工具测试服务器:
# Install FastMCP with CLI tools
pip install fastmcp[cli]
# Run the inspector with your server
CHAINGUARD_PULL_TOKEN=your_token_here mcp dev mcp/server.py这将打开一个web界面,您可以在其中:
- 查看所有可用的工具和资源
- 交互式测试工具调用
- 查看请求/响应日志
- 检查资源内容
方法4:与其他MCP兼容应用程序集成
许多应用程序正在添加MCP支持。查看应用程序的文档,了解如何配置MCP服务器。一般来说,您需要提供:
- 要运行的命令:
python - 服务器的路径:
/path/to/mcp/server.py - 环境变量:
CHAINGUARD_PULL_TOKEN,CHAINGUARD_ORG
🛠️ 可用工具
list_repositories
列出组织的所有存储库。
参数:
organization(字符串,可选):组织名称(默认:“chainguard”)
例子:
list_repositories(organization="chainguard")get_repository_images
获取特定存储库的可用容器图像标签。
参数:
repository(字符串,必填):存储库名称organization(字符串,可选):组织名称(默认:“chainguard”)
例子:
get_repository_images(repository="python", organization="chainguard")get_image_manifest
获取特定容器映像的详细清单信息。
参数:
repository(字符串,必填):存储库名称tag(字符串,可选):图像标签(默认:“latest”)organization(字符串,可选):组织名称(默认:“chainguard”)
例子:
get_image_manifest(repository="python", tag="latest", organization="chainguard")search_repositories
搜索与查询字符串匹配的存储库。
参数:
query(字符串,必填):搜索查询organization(字符串,可选):组织名称(默认:“chainguard”)limit(数字,可选):返回的最大结果数(默认值:10)
例子:
search_repositories(query="python", limit=5)get_popular_repositories
获取热门Chaingguard存储库的精选列表。
参数:
organization(字符串,可选):组织名称(默认:“chainguard”)limit(数字,可选):返回的最大结果数(默认值:10)
例子:
get_popular_repositories(limit=10)get_registry_stats
获取注册表统计信息和概述信息。
参数:
organization(字符串,可选):组织名称(默认:“chainguard”)
例子:
get_registry_stats(organization="chainguard")📡 可用资源
chainguard://config
服务器配置和可用工具/资源信息。
chainguard://health
注册表连接的健康检查状态和诊断。
chainguard://popular-images
带有描述的流行Chaingaguard图片的综合目录。
chainguard://examples
使用Chaingguard图像的使用示例和最佳实践。
📖 REST API文档
跑步后,请访问:
- Swagger用户界面: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- OpenAPI JSON: http://localhost:8000/openapi.json
关键REST端点
GET /health-健康检查GET /api/v1/repositories-列出存储库GET /api/v1/repositories/{repo}/images-获取图像标签GET /api/v1/repositories/{repo}/images/{tag}/manifest-获取清单
⚠️ 错误处理
服务器处理各种错误情况:
- 401未经授权:身份验证无效或缺失
- 403禁止:私有存储库的权限不足
- 404未找到:未找到存储库或映像
- 429价格有限:请求太多-延迟后重试
- 500+服务器错误:Chainguard API错误
所有错误都正确键入,并包含带有调试相关ID的描述性消息。
🏗️ 发展
设置
# Clone repository
git clone
cd chainguard-registry-api
# Setup development environment
./scripts/setup.sh
# Activate virtual environment
source venv/bin/activate
# Install development dependencies
pip install -r requirements-dev.txt在发展中奔跑
# Run REST API in development mode
make dev
# Run MCP server in development mode
make mcp
# Run with auto-reload
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000测试
# Run all tests
make test
# Run specific test categories
./scripts/run_tests.sh unit
./scripts/run_tests.sh integration
./scripts/run_tests.sh performance
# Run tests with coverage
pytest --cov=app --cov=mcp --cov-report=html
# Test registry connectivity
python scripts/test_registry.py
# Test specific repository
python scripts/test_registry.py python代码质量
# Type checking
mypy app/ mcp/
# Linting
flake8 app/ mcp/ tests/
# Formatting
black app/ mcp/ tests/
isort app/ mcp/ tests/
# Check formatting
black --check app/ mcp/ tests/🚀 部署
码头工人
# Build and run
make docker
# Or manually
docker build -t chainguard-api .
docker run -p 8000:8000 -e CHAINGUARD_ORG=chainguard chainguard-apiDocker Compose
# Deploy full stack (API + Redis + MCP)
docker-compose up -d
# View logs
docker-compose logs -fKubernetes
# Deploy to Kubernetes
kubectl apply -f k8s/
# Check deployment
kubectl get pods
kubectl get services生产部署脚本
# Deploy with automated script
./scripts/deploy.sh docker # Docker deployment
./scripts/deploy.sh compose # Docker Compose deployment
./scripts/deploy.sh kubernetes # Kubernetes deployment
./scripts/deploy.sh status # Check deployment status📊 监控
- 健康检查:
/health,/health/ready,/health/live - 指标:普罗米修斯兼容指标(可选)
- 日志记录:带相关ID的结构化JSON日志记录
- 速率限制:Redis后端的每IP请求限制
- 缓存:基于Redis的缓存,具有命中/未命中指标
🔒 安全最佳实践
- API代币存储:从不将pull令牌提交到版本控制
- 仅限HTTPS:在生产中始终使用HTTPS进行API通信
- 令牌权限:使用具有最低所需权限的令牌
- 速率限制:尊重Chaingguard的速率限制并实施客户端限制
- 错误消息:不要在错误响应中暴露敏感信息
- 输入验证:所有输入都经过验证和消毒
- 安全标头:配置了CORS、CSP和其他安全标头
🤝 贡献
- 分叉存储库
- 创建要素分支(
git checkout -b feature/amazing-feature) - 通过测试进行更改
- 运行测试套件(
make test) - 提交您的更改(
git commit -m 'Add amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
📄 许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
🆘 支持
对于问题和疑问:
- GitHub问题: chainguard开发/注册表api/问题
- 文档:检查
/docs目录 - 讨论:使用GitHub讨论提问
- 链罩支架: Chainguard文档
- MCP文件: 模型上下文协议
