Kubernetes集群交互服务器

一个通过kubectl命令提供Kubernetes集群交互能力的MCP服务器,支持安全执行命令和获取集群信息。

  • by Joelayo
  • Go
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 Joelayo/kubectl-go-mcp-server

使用说明

kubectl go mcp服务器

一个模型上下文协议(MCP)服务器,通过kubectl命令提供Kubernetes集群交互功能。此服务器使MCP兼容的客户端(如带有Copilot的VS Code)能够安全地执行kubectl命令并检索Kubernetes集群信息。

特性

  • Kubernetes集成:通过MCP接口执行kubectl命令
  • 交互式命令保护:防止执行可能挂起的交互式命令
  • 资源修改检测:标识修改群集资源的命令
  • 鲁棒安全:多个验证层,防止命令注入和不安全操作
  • 可配置的Kubeconfig:支持自定义kubeconfig路径
  • 标准Go项目布局:遵循Go的可维护性最佳实践
  • Cobra CLI集成:带子命令的专业命令行界面

建筑

kubectl go-mcp服务器通过kubectl命令充当mcp客户端(如带有Copilot的VS Code)和Kubernetes集群之间的桥梁:

VS Code/Copilot → MCP Client → kubectl-go-mcp-server → kubectl → Kubernetes Cluster

关键组件

  • MCP服务器:处理JSON-RPC通信和工具注册
  • kubectl工具:安全验证和执行kubectl命令
  • 安全层:防止交互式命令和命令注入

有关详细的体系结构信息,请参见 docs/architecture.md.

pkg/
├── types/          # 🔧 Core interfaces and data structures
│   ├── Tool        # Interface for all MCP tools
│   ├── Schema      # JSON schema definitions
│   └── ExecResult  # Command execution results
│
├── kubectl/        # 🎯 kubectl-specific implementation
│   ├── KubectlTool # Main tool implementation
│   ├── Validation  # Command safety checks
│   └── Execution   # kubectl command runner
│
internal/
├── mcp/           # 🌐 MCP protocol implementation
│   ├── Server     # MCP server and protocol handling
│   ├── Tools      # Tool registry and management
│   └── Protocol   # JSON-RPC message handling
│
└── config/        # ⚙️ Configuration management
    ├── Config     # Application configuration
    └── Defaults   # Default settings

扩展点

该架构旨在实现可扩展性:

  1. 新工具:实施 Tool 添加新功能的界面
  2. 自定义验证:为特定用例添加验证层
  3. 协议扩展:扩展MCP处理以实现其他功能
  4. 输出格式化程序:添加自定义结果处理

性能注意事项

  • 并发安全:所有组件都是为并发访问而设计的
  • 资源管理:适当的清理和资源处置
  • 超时处理:所有操作的可配置超时
  • 内存效率:用于大输出的流式和缓冲I/O

安装

先决条件

  • 转到1.23或更高版本
  • kubectl已安装并配置
  • 访问Kubernetes集群

从源代码构建

# Clone the repository
git clone https://github.com/Joelayo/kubectl-go-mcp-server.git
cd kubectl-go-mcp-server

# Build the binary
make build

# Or install directly
make install

下载二进制文件

从下载最新版本 发布页面 为了你的平台。

用法

独立

# Run with default kubeconfig
./kubectl-go-mcp-server

# Run with custom kubeconfig
./kubectl-go-mcp-server --kubeconfig /path/to/kubeconfig

# Show version
./kubectl-go-mcp-server version

VS代码集成

要与VS Code和Copilot一起使用,请将此MCP服务器添加到您的VS Code设置中:

{
  "mcp": {
    "servers": {
      "kubectl-go-mcp-server": {
        "type": "stdio",
        "command": "/path/to/kubectl-go-mcp-server",
        "env": {}
      }
    }
  }
}

有关平台特定的安装详细信息,请参阅 示例目录.

可用工具

MCP服务器提供以下工具:

kubectl

执行kubectl命令,并进行全面的验证和安全检查。

参数:

  • command (必填):要执行的完整kubectl命令(包括'kubectl'前缀)
  • modifies_resource (可选):指示命令是否修改资源(“是”、“否”、“未知”)

例子:

{
  "name": "kubectl",
  "arguments": {
    "command": "kubectl get pods -o json",
    "modifies_resource": "no"
  }
}

安全特性:

  • 交互式命令检测:防止挂起交互式命令,如 kubectl exec -it, kubectl edit, kubectl port-forward
  • 资源修改跟踪:自动检测破坏性操作
  • 命令验证:确保只执行有效的kubectl命令

安全

此服务器实现了多个安全层,包括命令验证、注入预防和交互式命令阻止。有关详细的安全信息,请参阅:

发展

看 贡献.md 用于开发设置、工作流程和贡献指南。

开发人员快速入门

# Install dependencies and build
make deps && make build

# Run tests
make test

# Format and lint code  
make fmt && make lint

故障排除

常见问题

  • 服务器未响应:验证kubectl是否已安装,kubeconfig是否可访问
  • 交互命令错误:使用非交互式替代方案(见 安全概述)
  • 权限不足:检查kubectl权限和集群连接

有关详细的调试信息,服务器会记录所有工具调用、验证结果和错误。

支持

许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。