A powerful Go code generation tool for creating type-safe enums from YAML configuration files.
KD Gen is a CLI tool that generates Go enum types with useful helper methods from YAML configuration files. It helps you create type-safe enums with string representations, parsing functions, and more.
- Generate type-safe enums (int, string, uint64, …)
- JSON support (MarshalJSON, UnmarshalJSON)
- Database/sql integration (Scan, Value for DB storage)
- MCP Tools → expose enum generation as interactive tools:
| Tool | Category | Description |
|---|---|---|
list_methods |
Enum | List all supported enum generation methods with descriptions. |
show_config_structure |
Config | Show the YAML structure for a valid enum configuration file. |
example_config |
Config | Generate an example enum config file for a given type (e.g., string, int). |
validate_config |
Config | Validate an enum YAML config against schema and supported types. |
generate_enum |
Codegen | Generate Go enum code from a provided YAML configuration. |
explain_enum |
Docs | Explain what a given enum config means and how it maps to generated code. |
diff_config |
Utility | Compare two enum YAML config files and show the differences. |
format_config |
Utility | Format and normalize enum YAML config files. |
This project is developed using Go 1.25.0. It should work with other recent Go versions as well.
- github.com/dave/jennifer – Code generation for Go
- github.com/spf13/cobra – CLI command framework
- github.com/spf13/viper – Configuration management
- github.com/mitchellh/mapstructure – Decode generic map values to Go structs
- github.com/shopspring/decimal – Arbitrary-precision fixed-point decimal numbers
- github.com/mark3labs/mcp-go - A Go implementation of the Model Context Protocol (MCP), enabling seamless integration between LLM applications and external data sources and tools.
# Clone the repository
git clone https://github.com/khuong02/kd-gen.git
cd kd-gen
# Build the binary
go build -o ./build/kd-gen main.go
# Optional: Move to a directory in your PATH
mv ./build/kd-gen /usr/local/bin/# Build image
docker build -t kd-gen .
# Run
docker run --rm -v $(pwd):/app -w /app kd-gen \
enum gen --package core --output ./example/enum/enum_gen.go --config ./example/enum/enum.yamlgo install github.com/khuong02/kd-gen@latestYou can install KD Gen via Homebrew using a custom tap:
# Add the tap
brew tap khuong02/tap https://github.com/khuong02/homebrew-tap
# Install kd-gen
brew install kd-gen
## Usage
### Basic Usage
Create a YAML configuration file defining your enums:
```yaml
enums:
- name: Lang
type: string
methods:
- String|Parse|Normalize|JSON|SQL
values:
- name: LangEnglish
display: "English"
code: "en"
- name: LangVietnamese
display: "Vietnamese"
code: "vi"
- name: LogLevel
type: int
values:
- name: Debug
- name: Info
- name: Warn
- name: Error
- name: FatalThen run the tool to generate the Go code:
kd-gen enum gen --package core --output ./example/enum/enum_gen.go --config ./example/enum/enum.yaml# Create a config file
cat > enum.yaml << EOF
enums:
- name: Status
type: string
methods:
- String
values:
- name: StatusActive
display: "active"
code: 1
- name: StatusInactive
display: "inactive"
code: 0
EOF
# Generate the enum code
kd-gen enum gen --package core --output status_enum.go --config enum.yamlThe generated file will contain:
- Status type definition
- Constants for each enum value
String()method for string representationParsefunction to convert strings to enum values- Map of codes
- Slice of all values
For each enum defined in your YAML file, the tool generates:
- A type definition (
string,int, etc.) - Constants for each enum value
- A map of code values (if provided)
- A slice containing all enum values
- A
String()method for string representation - A
Parsefunction to convert strings to enum values - A
Normalize()method for string enums
enums:
- name: EnumName # Name of the enum type
methods:
- string
- parse
- normalize
- json
- sql
type: string|int # Type of the enum (string, int, etc.)
values:
- name: ValueName # Name of the enum constant
display: "Display" # String representation (optional)
code: value # Optional "Code" field → supports multiple types:This project can also be used as an MCP server so Claude Desktop can call its tools directly.
👉 See MCP_GUIDE.md for full setup instructions.
| Project | Purpose |
|---|---|
| mcp-go | Go implementation of the Model Context Protocol. |
| mcp-grafana | Example MCP server integrating with Grafana. |