Kubernetes CRD代码生成工具

MCP Toolgen是一款为Kubernetes自定义资源定义(CRD)生成Go工具集的代码生成工具,专为扩展extendable-kubernetes-mcp-server而设计,支持CRUD操作、Kubernetes客户端集成和JSON模式验证。

  • by friedrichwilken
  • Go
  • 本地部署
  • 更新于
在 GitHub 查看 friedrichwilken/mcp-toolgen

使用说明

MCP 工具

一个代码生成工具,为Kubernetes自定义资源定义(CRD)创建Go工具集,专门用于扩展 可扩展kubernetes mcp服务器 使用自定义资源操作。

概述

MCP Toolgen分析CRD YAML文件并生成完整的Go包,包括:

  • MCP(模型上下文协议)工具集注册
  • 自定义资源的CRUD操作
  • Kubernetes客户端集成
  • JSON模式验证
  • 全面的错误处理

特性

  • CRD分析:使用OpenAPI v3模式支持解析和分析CRD YAML文件
  • 代码生成:基于模板的Go代码生成遵循既定模式
  • MCP集成:生成的工具集与MCP服务器无缝集成
  • MCP资源支持:用于LLM访问定义的可选CRD资源生成
  • 多集群支持:生成的代码支持多集群操作
  • 类型安全:从CRD模式生成完整的Go类型
  • 灵活的CRUD:根据需要生成特定操作(创建、读取、更新、删除)
  • 向后兼容:默认设置适用于标准ek8sms(不需要资源支持)

安装

# Clone the repository
git clone https://github.com/friedrichwilken/mcp-toolgen.git
cd mcp-toolgen

# Build the binary
make build

# Install to GOPATH/bin
make install

用法

基本用法

# Generate toolset from a single CRD
mcp-toolgen --crd ./crds/function-crd.yaml \
            --output ./pkg/functions \
            --package functions \
            --module-path github.com/myorg/myproject

# Generate toolsets from a directory of CRDs
mcp-toolgen --crd-dir ./crds \
            --output-base ./pkg \
            --module-path github.com/myorg/myproject

# Generate with MCP resource support (requires ek8sms with resource support)
mcp-toolgen --crd ./crds/function-crd.yaml \
            --output ./pkg/functions \
            --package functions \
            --module-path github.com/myorg/myproject \
            --generate-crd-resource

# Generate with specific CRUD operations only
mcp-toolgen --crd ./crds/function-crd.yaml \
            --output ./pkg/functions \
            --package functions \
            --module-path github.com/myorg/myproject \
            --crud cr  # Only create and read operations

# Generate with custom templates
mcp-toolgen --crd ./crds/function-crd.yaml \
            --templates ./custom-templates \
            --output ./pkg/functions \
            --package functions \
            --module-path github.com/myorg/myproject

命令行标志

标志 说明 必填 默认
--crd 单个CRD YAML文件的路径 是(或 --crd-dir) -
--crd-dir 包含多个CRD YAML文件的目录 是(或 --crd) -
--output 生成代码的输出目录(单个CRD) 是(带 --crd) -
--output-base 多CRD生成的基本目录 是(带 --crd-dir) -
--package Go包名称 否 CRD复数名称
--module-path Go模块路径(例如,github.com/myorg/myproject) 是 -
--crud 要生成的CRUD操作(c=创建,r=读取,u=更新,d=删除) 否 crud (全部)
--generate-crd-resource 为CRD定义生成MCP资源 否 false
--templates 自定义模板目录 否 嵌入模板
--overwrite 覆盖现有文件 否 false
--dry-run 不创建文件预览生成 否 false
--verbose 启用详细日志记录 否 false

与可扩展的kubernetes mcp服务器集成

  1. 生成工具集 在您的ek8sms项目中:

    mcp-toolgen --crd ./crds/function-crd.yaml \
                --output ./pkg/functions \
                --package functions \
                --module-path github.com/friedrichwilken/extendable-kubernetes-mcp-server
    
    # Optional: Generate with MCP resource support
    mcp-toolgen --crd ./crds/function-crd.yaml \
                --output ./pkg/functions \
                --package functions \
                --module-path github.com/friedrichwilken/extendable-kubernetes-mcp-server \
                --generate-crd-resource
    
  2. 生成的包结构:

    pkg/functions/
    ├── toolset.go      # MCP toolset registration (+ resource support if enabled)
    ├── types.go        # Go types from CRD schema
    ├── client.go       # Kubernetes client wrapper
    ├── handlers.go     # MCP tool handlers
    ├── schema.go       # JSON schemas for validation
    └── doc.go          # Package documentation
    
  3. 自动注册:生成的工具集通过以下方式自动注册到MCP服务器 init() 功能。

  4. MCP资源支持 (可选):当 --generate-crd-resource 已启用:

    • 生成的工具集实现 ResourceProvider 接口
    • CRD YAML被嵌入并作为MCP资源公开
    • 允许LLM直接访问CRD定义
    • 需要启用资源支持的ek8sms

建筑

项目结构

mcp-toolgen/
├── cmd/mcp-toolgen/         # CLI entry point
├── pkg/
│   ├── analyzer/             # CRD parsing and analysis
│   ├── generator/            # Code generation engine
│   │   └── templates/        # Go code templates
│   └── config/               # Configuration management
├── test/
│   ├── fixtures/             # Test CRD files
│   └── integration/          # Integration tests
└── Makefile                  # Build automation

代码生成流程

  1. 解析CRD:提取元数据、架构和资源信息
  2. 分析架构:将OpenAPI v3模式转换为Go类型定义
  3. 生成代码:应用模板创建完整的Go包
  4. 验证输出:确保生成的代码遵循模式并编译

发展

先决条件

  • 转到1.25+
  • 制造

自动安装的工具 (需要时自动安装):

  • goimport:组织和修复导入
  • gofumpt:现代Go格式化程序(严格,替换 interface{} 随着 any)
  • 戈朗茨皮棉:具有自动修复功能的综合过梁
  • gosec:安全漏洞扫描程序
  • 高斯检验:依赖性漏洞检查器

开发设置

# Set up development environment
make dev-setup

# Run tests
make test

# AUTO-FIX ALL ISSUES (RECOMMENDED - runs all auto-fixers)
make auto-fix       # Runs: goimports → gofumpt → golangci-lint --fix

# Or run code quality checks individually
make fmt-imports    # Fix and organize imports
make fmt-modern     # Modern Go formatting (interface{} -> any)
make lint           # Check code (read-only)
make lint-fix       # Fix linting issues automatically
make security-check # Run gosec and govulncheck

# Complete workflow
make code-quality   # Run all quality checks with auto-fixes
make check          # Check without making changes (for CI)
make pre-commit-check # Quality + tests before committing

测试

# Run all tests
make test

# Run unit tests only
make test-unit

# Run integration tests only
make test-integration

# Generate coverage report
make test-coverage

贡献

  1. 复刻仓库
  2. 创建要素分支
  3. 进行更改
  4. 添加新功能的测试
  5. 运行测试套件
  6. 提交拉取请求

许可证

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

相关项目