内容
# 锚点脚手架 MCP
[](https://github.com/mukulcodezz/anchor-scaffold-mcp/actions/workflows/ci.yml)
[](https://www.npmjs.com/package/anchor-scaffold-mcp)
[](LICENSE)
用于 Solana Anchor 程序的 AI 代码生成器。生成可投入生产的 TypeScript 客户端、Rust 账户结构体、测试和完整的程序。
**从这里开始:** [快速入门指南](QUICKSTART.md)
## 功能
```bash
# TypeScript 客户端(类型化的指令调用者、PDA 帮助器、账户获取器)
anchor-scaffold gen-ts-client --idl target/idl/my_program.json
# Rust 账户结构体(派生(Accounts),所有约束,空间计算)
anchor-scaffold gen-rust-accounts --idl target/idl/my_program.json --instruction initialize
# 测试套件(Mocha/Chai,指令测试,状态验证)
anchor-scaffold gen-tests --idl target/idl/my_program.json
# 完整程序(lib.rs,所有指令,错误枚举,状态账户)
anchor-scaffold gen-program --description "A multi-sig wallet" --program-name multisig
```
## 安装
```bash
npm install
npm run build
npm start # MCP 服务器模式
```
或
```bash
npm install -g anchor-scaffold-mcp
anchor-scaffold --help
```
## 配置
```bash
export SCAFFOLD_API_KEY=sk-ant-xxxxx
export SCAFFOLD_PROVIDER=claude # 或 openai
```
或创建 `~/.anchor-scaffold/config.json`:
```json
{
"provider": "claude",
"apiKey": "sk-ant-...",
"model": "claude-sonnet-4-6"
}
```
## 在 Claude 代码中
添加到 `.claude/settings.json`:
```json
{
"mcpServers": {
"anchor-scaffold": {
"command": "npx",
"args": ["anchor-scaffold-mcp"]
}
}
}
```
然后询问 Claude:
> 为我的程序在 target/idl/my_program.json 处生成 TypeScript 客户端
## 工作原理
1. 解析 Anchor IDL(JSON 格式)
2. 将 IDL + 提示发送到 AI(Claude 或 OpenAI)
3. AI 根据 IDL 结构生成代码
4. 返回可直接使用的代码
生成的代码:
- **TypeScript**:从 `@coral-xyz/anchor` 导入,完全类型化
- **Rust**:`anchor build` 兼容,所有约束正确
- **测试**:`anchor test` 兼容,指令验证
- **程序**:完整,编译 lib.rs
## 验证
- TypeScript 输出:无 TS 错误,导入有效
- Rust 输出:`anchor build` 成功
- 测试:`anchor test` 通过
- 程序:`anchor build` 成功
## 限制
- IDL 必须是有效的 Anchor 格式
- 生成的代码是起始/脚手架(可能需要自定义)
- 复杂的验证逻辑需要手动添加
- 自定义业务逻辑由开发人员编写
## 支持
- Anchor 0.30+
- Solana 1.17+
- Node.js 18+
## 成本
使用 Claude 进行提示缓存:每次生成约 0.01-0.05 美元(重复 IDL 便宜 90%)。
## 部署
参见 [DEPLOY.md](DEPLOY.md) 了解:
- npm 发布
- VS Code 扩展
- Docker/云部署
- Web 服务包装
## 许可证
MIT