使用说明
╔══════════════════╦═══════════════╦══════════════╤▶══╗ ·*· ·*· _..--""""--.._
║░░░░░░░░░░░░░░░░░░║ ANY LANGUAGE ║▓▓▓▓▓▓▓▓▓▓▓▓▓▓├▶ ╠═))·*·*·*·*·((══.' '-.
╚══════════════════╩═══════════════╩══════════════╧▶══╝ ·*· ·*· .' Go '.
│ / Rust ╲ │ ╱ Java \
│ ██████╗ ██████╗ ██████╗ | ╲ │ ╱ |
╱╲ ██╔══██╗██╔══██╗██╔════╝ | ╲ │ ╱ |
╱ ╲ ██████╔╝██████╔╝██║ ███╗ | _____╲___│___╱_____ |
╱ ╲ ██╔══██╗██╔═══╝ ██║ ██║ | C# ────────(⚡)──────── Python |
██║ ██║██║ ╚██████╔╝ | ‾‾‾‾‾╱‾‾‾│‾‾‾╲‾‾‾‾‾ |
╚═╝ ╚═╝╚═╝ ╚═════╝ \ ╱ │ ╲ /
'. ╱ │ ╲ .'
Rosetta Project Generator '-. TypeScript .-'
''--..______..--''
rpg-罗塞塔项目生成器
一种标记驱动的多语言代码生成工具,作为MCP(模型上下文协议)服务器公开。用自然语言markdown编写规范,AI代理将它们翻译成任何支持语言的惯用代码。
特性
- 多语言支持:使用Go、Rust、Java、C#、Python和TypeScript生成代码
- 特定语言约定:每个语言适配器都强制执行惯用模式、命名约定和项目结构
- 灵活的规格格式:将规范编写为详细的伪代码或高级解释
- MCP集成:适用于Claude Code、Cursor和其他MCP兼容工具
- 奇偶校验:自动比较不同语言的实现并生成修复指令
- 可配置输出:控制生成项目的保存位置
安装
先决条件
- 转到1.21或更高版本
- MCP兼容的AI工具(Claude Code、Cursor等)
从源代码构建
# Clone the repository
git clone https://github.com/kon1790/rpg.git
cd rpg
# Build
make build
# or
go build -o bin/rpg ./cmd/rpg
# Verify
./bin/rpg --help
配置
Claude代码设置
将rpg添加到项目的 .mcp.json:
{
"mcpServers": {
"rpg": {
"command": "/absolute/path/to/rpg"
}
}
}
或者使用自定义输出目录:
{
"mcpServers": {
"rpg": {
"command": "/absolute/path/to/rpg",
"args": ["--output", "./generated"]
}
}
}
CLI选项
rpg [options]
Options:
-o, --output <dir> Base output directory for generated projects (default: ./output)
Projects are saved to: <output>/<project-name>/<language>/
MCP工具
RPG公开了13个MCP工具,分为三类:
核心产生
| 工具 | 说明 |
|---|---|
list_languages |
列出所有支持的语言及其惯例和习语 |
parse_spec |
阅读markdown规范文件内容 |
get_generation_context |
获取规范+语言约定+代码生成提示模板 |
get_project_structure |
获取目标语言项目的推荐文件结构 |
generate_source_from_spec |
自动奇偶校验的自主代码生成 |
导入与分析
| 工具 | 说明 |
|---|---|
import_spec_from_source |
分析本地源代码以生成AI驱动的规范 |
import_spec_from_github |
克隆并分析GitHub存储库以生成规范 |
deep_analyze_source |
基于AST的语义分析(类型、函数、调用图) |
list_project_languages |
检测项目中的所有编程语言 |
get_files_for_language |
获取AI驱动分析的原始文件内容 |
对等与细化
| 工具 | 说明 |
|---|---|
ensure_parity |
使用修复指令比较不同语言的实现 |
semantic_parity_analysis |
基于AST分析的深层语义比较 |
iterative_refinement_loop |
自动细化,直到达到奇偶校验阈值 |
工具使用示例
从GitHub导入并移植到新语言:
Use rpg import_spec_from_github on owner/repo to analyze the codebase
Generate a spec from the analysis, then port to TypeScript and Go
自主代码生成:
Use rpg generate_source_from_spec to create a Rust implementation from my-api.spec.md
The tool will automatically validate parity and iterate until complete
对复杂代码库的深入分析:
Use rpg list_project_languages on ./my-project to see what languages are used
Use rpg deep_analyze_source for semantic analysis of the Go code
Use rpg get_files_for_language for SQL and Protobuf files
生产质量的迭代优化:
Use rpg iterative_refinement_loop to port my Go project to TypeScript
Keep refining until 95% parity is achieved
基本生成工作流程:
Use rpg get_generation_context for examples/my-api.spec.md in typescript
Generate the slugify spec in go, rust, and python
Use rpg ensure_parity to compare implementations
从现有代码导入规范
是否有现有代码?让RPG将其逆向工程为多语言生成规范。
导入工作流
Your Code RPG Analysis AI Generation Multi-Language
│ │ │ │
├── src/* ────►│ │ │
├── tests/* ────►│ import_spec_from │ Creates natural │ Go
├── config ────►│ _source/_github ──►│ language spec ───►│ Rust
└── README ────►│ │ │ TypeScript
└── Analysis Prompt ──┘ └─ Python...
快速开始
来自GitHub存储库:
# Import directly from GitHub (supports owner/repo shorthand)
Use rpg import_spec_from_github on owner/repo
# With specific branch
Use rpg import_spec_from_github on owner/repo@main
# Full URL also works
Use rpg import_spec_from_github on https://github.com/owner/repo
从本地目录:
# Import from local path
Use rpg import_spec_from_source on ./legacy-api
导入后:
# AI generates spec (review and refine as needed)
# Then generate in new languages
Use rpg get_generation_context for ./specs/my-project.spec.md in go
Use rpg get_generation_context for ./specs/my-project.spec.md in rust
# Verify parity
Use rpg ensure_parity to compare implementations
分析什么
| 文件类型 | 它揭示了什么 |
|---|---|
| 源文件 | 函数、类型、类、业务逻辑 |
| 测试文件 | 预期行为、边缘情况、使用示例 |
| 配置文件 | 依赖关系、环境变量 |
| 文档 | README,注释提供上下文 |
| API规范 | OpenAPI/GraphQL架构(如果存在) |
import_spec_from_github工具
| 参数 | 必填 | 说明 |
|---|---|---|
repository |
是 | GitHub URL或简写(owner/repo, owner/repo@branch) |
ref |
否 | 分支、标记或提交SHA(覆盖URL中的ref) |
token |
否 | GitHub PAT用于私有仓库(或使用 GITHUB_TOKEN ) 。 |
name |
否 | 规范的可选名称 |
shallow |
否 | 使用浅克隆(默认值:true) |
import_spec_from_source工具
| 参数 | 必填 | 说明 |
|---|---|---|
inputPath |
是 | 源代码目录路径 |
name |
否 | 规范的可选名称 |
退货:AI驱动的规范生成分析提示
备注:这是人工智能辅助的,不是自动的。检查生成的规范的准确性,并根据代码未捕获的上下文进行增强。
从头开始编写规范
RPG规格如下 自然语言标记描述你想要什么——人工智能会解释你的意图。
无格式规则
AI会适应你的风格。所有这些工作:
最小值:
# url-shortener
A service that shortens URLs and redirects them.
Store mappings in memory.
结构化的:
# url-shortener
## Functions
### shorten(url: string): string
Takes a long URL and returns a 6-character code.
### resolve(code: string): string
Returns the original URL for a code, or throws if not found.
对话:
# url-shortener
I need a URL shortening service like bit.ly. When someone gives us a long URL,
we generate a short code. Later they can use that code to get redirected.
Keep it simple - just store everything in memory for now.
快速开始
# 1. Write your spec (any style)
cat > my-api.spec.md << 'EOF'
# my-api
A REST API for managing tasks. Users can create, list, and complete tasks.
Use SQLite for storage.
EOF
# 2. Generate in any language
Use rpg get_generation_context for my-api.spec.md in typescript
常见模式(全部可选)
| 第节 | 目的 |
|---|---|
## Types |
定义数据结构 |
## Functions |
描述行为、输入、输出 |
## API Endpoints |
REST路由或GraphQL操作 |
## Configuration |
环境变量和默认值 |
## Tests |
给定/预期情景 |
关键:描述意图,而不是实施
| ❌ 过于具体 | ✅ 专注意图 |
|---|---|
| “使用HashMap<String,User>” | “按ID存储用户以进行快速查找” |
| “使用for循环迭代并筛选” | “筛选符合条件的用户” |
| “返回ArrayList” | “以列表形式返回匹配的用户” |
AI为每种目标语言选择惯用的实现。
便携式类型
使用这些泛型类型——AI将它们映射到特定语言的类型:
| 类型 | 映射到 |
|---|---|
string, text |
字符串类型 |
int, integer |
整数类型 |
float, decimal |
浮点型 |
bool, boolean |
布尔值 |
timestamp, datetime |
日期/时间类型 |
uuid |
UUID/gid |
list of X |
X的数组/切片 |
optional X |
可为空X |
支持的语言
| 语言 | 版本 | 主要约定 |
|---|---|---|
| 去 | 1.21+ | PascalCase 出口, (result, error) 返回, go.mod |
| 锈 | 2021 | snake_case, Result<T, E>,所有权, Cargo.toml |
| Java | 17+ | PascalCase 类别、记录, Optional<T>Maven/Gradle |
| C | 12+ | PascalCase,主要建设者, async/await,NuGet |
| python | 3.11+ | snake_case,类型提示,数据类, pyproject.toml |
| TypeScript | 5.0+ | camelCase,严格模式,歧视性工会,npm |
完成工作流程
1.创建规范文件
# Create spec
cat > examples/my-api.spec.md << 'EOF'
# my-api
A simple REST API for managing items.
## Target Languages
- go
- typescript
## Data Model
### Item
- id: UUID
- name: Text
- created_at: Timestamp
## API Design
GET/items-列出所有项目 POST/items-创建项目
EOF
2.生成代码
在克劳德代码中:
Use rpg to get the generation context for examples/my-api.spec.md in go,
then generate the complete implementation.
3.以其他语言生成
Now generate the same spec in typescript.
4.检查奇偶校验
Use rpg ensure_parity to compare the go and typescript implementations
and fix any missing features.
奇偶校验
这 ensure_parity 该工具比较不同语言的实现:
输入:
{
"specPath": "examples/my-app.spec.md",
"projects": [
{"language": "csharp", "path": "output/my-app/csharp"},
{"language": "java", "path": "output/my-app/java"}
]
}
输出包括:
parityScore-0.0到1.0(1.0=完全奇偶校验)referenceLanguage-第一个项目是参考featureMatrix-所有检测到的功能都具有每种语言的实现状态gaps-缺少带有参考代码片段的功能fixInstructions-固定间隙的详细标记说明
检测到的特征:
- URL验证
- 标签规范化
- 异步操作
- 元数据提取(og:标题,og:描述,favicon)
- 配置(端口、数据库、超时)
- 错误处理和日志
- 搜索功能
输出目录结构
生成的项目遵循以下结构:
output/
├── slugify/
│ ├── go/
│ │ ├── go.mod
│ │ ├── slugify.go
│ │ └── slugify_test.go
│ ├── python/
│ │ ├── pyproject.toml
│ │ └── slugify.py
│ └── typescript/
│ ├── package.json
│ └── src/slugify.ts
└── bookmark-manager/
├── csharp/
│ ├── BookmarkManager.csproj
│ ├── Program.cs
│ └── ...
└── java/
├── pom.xml
├── src/main/java/...
└── ...
项目结构
rpg/
├── cmd/rpg/ # CLI entry point
├── internal/
│ ├── server/ # MCP server implementation
│ │ ├── server.go # Server setup and registration
│ │ └── handlers.go # Tool handlers
│ ├── parser/ # Spec file parser
│ └── languages/ # Language adapters
│ ├── go.go
│ ├── rust.go
│ ├── java.go
│ ├── csharp.go
│ ├── python.go
│ └── typescript.go
├── pkg/spec/ # Public types
├── examples/ # Example spec files
├── output/ # Generated projects (default)
├── Makefile
└── .mcp.json # MCP configuration
故障排除
MCP服务器没有响应
- 重新生成二进制文件:
make build - 重新启动Claude Code以重新加载MCP服务器
- 检查路径
.mcp.json是绝对的
缺少语言
检查支持的语言:
Use rpg list_languages
许可证
麻省理工学院
