BMB代码生成服务

Chatter是一个MCP服务器,为AI模型提供实时BMB语言规范、编译反馈和合约验证,帮助生成高质量的BMB代码。

  • by lang-bmb
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 lang-bmb/bmb-mcp

使用说明

喋喋不休

用于BMB编程语言的MCP服务器

Chatter通过提供对语言规范、编译反馈和合同验证的实时访问,使AI模型能够生成高质量的BMB代码。

实施状态(2026-05-09,周期2524-2556): Python脚手架 chatter/.74/74 pytest通过。

  • ✅ 工具(12): bmb_check, bmb_ir, bmb_run, bmb_verify, bmb_spec_lookup, bmb_lint, bmb_lint_explain, bmb_example, bmb_compile, bmb_test, bmb_from_rust, bmb_context_pack
  • ✅ 资源(4): bmb://spec/full, bmb://spec/quick-reference, bmb://spec/rust-diff, bmb://context/stdlib
  • ✅ 提示: bmb_implement, bmb_add_contracts, bmb_optimize

长期(M3+),根据规则6 BMB重写策略,实施将转移到BMB本身。Python层故意设计得很薄,以保持端口较小。


为什么聊天?

BMB是一种AI优先的编程语言,旨在由AI编写并由人类审查。但人工智能模型在训练数据中从未见过BMB:

LLM Training Data:
  Rust: ████████████████████████  ~2M repositories
  C:    ████████████████████████  ~3M repositories
  Go:   ████████████████          ~1M repositories
  BMB:  ▏                         ~0 repositories

没有Chatter,AI模型:

  • 混淆 T? 随着 Option<T>
  • 使用 & 对于按位AND(应该是 band)
  • 忘记明确 return 块体中
  • 生成不正确或缺失的合同

Chatter通过在运行时注入BMB知识来解决这个问题。


运作原理

┌─────────────┐     ┌─────────────────┐     ┌─────────────┐
│    LLM      │────▶│    Chatter      │────▶│    BMB      │
│  (Claude)   │◀────│   MCP Server    │◀────│  Toolchain  │
└─────────────┘     └─────────────────┘     └─────────────┘
                           │
                    ┌──────┴──────┐
                    ▼             ▼
              [Spec Database] [Examples]

Chatter没有将整个规范填充到每个提示符中(~15K个标记),而是提供 选择性、按需访问 这正是AI所需要的。


特性

工具

工具 状态 描述
bmb_check ✅ 未完全编译的类型检查代码
bmb_verify ✅ 使用Z3求解器验证合同
bmb_spec_lookup ✅ 按主题关键字搜索语言规范
bmb_lint ✅ 运行样式/约定linter,返回JSON警告
bmb_lint_explain ✅ 用AI友好的解释和修复建议来解决问题
bmb_example ✅ 按概念关键字从教程中获取示例代码
bmb_ir ✅ 为源代码段发出LLVM IR(调试/优化分析)
bmb_run ✅ 使用树行走解释器运行代码(不需要LLVM)
bmb_compile ✅ 编译为本机可执行文件(需要LLVM工具链)
bmb_test ✅ 针对BMB代码运行测试用例
bmb_from_rust ✅ 将Rust代码转换为BMB(启发式——尽最大努力)
bmb_context_pack ✅ 扫描项目目录并返回上下文包v1 JSON

资源

URI 状态 描述
bmb://spec/full ✅ 完整的语言规范(docs/specification.md)
bmb://spec/quick-reference ✅ 备忘单:语法、契约、陷阱、stdlib
bmb://spec/rust-diff ✅ BMB与Rust:哲学、语法、内存模型
bmb://context/stdlib ✅ stdlib/的上下文包--所有公共导出+合约
bmb://examples/{category} ⏳ 按类别分类的示例代码
bmb://stdlib/{module} ⏳ 每个模块的标准库文档

提示

提示 状态 描述
bmb_implement ✅ 使用可选合约实现函数
bmb_add_contracts ✅ 将预/后合同添加到现有代码中
bmb_optimize ✅ 优化代码的速度或大小

安装(Python——当前实现)

# From source (recommended during M2)
cd ecosystem/bmb-mcp
pip install -e .

# Run the server
bmb-chatter

需求

  • Python 3.10+
  • BMB编译器--设置 BMB_BINARY,添加 bmb 到 PATH,或运行 cargo build --release 在工作空间中
  • Z3求解器(用于 bmb_verify,一旦该工具着陆)

用法

使用克劳德桌面

添加 claude_desktop_config.json:

{
  "mcpServers": {
    "bmb": {
      "command": "npx",
      "args": ["@bmb/chatter"]
    }
  }
}

独立服务器

# Start server
chatter serve

# With custom BMB path
BMB_PATH=/usr/local/bin/bmb chatter serve

# With debug logging
chatter serve --debug

示例会话

用户:在BMB中实现二分查找

AI的内部工作流程:

1. bmb_spec_lookup(topic="contracts")
   → Learn pre/post/invariant syntax

2. bmb_example(category="algorithms", name="binary_search")
   → Reference implementation

3. [Generate code]

4. bmb_check(code)
   → Error: "Array access without bounds check"
   → Suggestion: "Add `pre idx < arr.len()` contract"

5. [Fix code]

6. bmb_verify(code)
   → ✓ All contracts verified

7. Return verified code to user

结果:

pure fn is_sorted(arr: &[i32]) -> bool {
    let mut i = 1;
    while i < arr.len()
      invariant i <= arr.len()
    {
        if arr[i - 1] > arr[i] {
            return false;
        }
        i = i + 1;
    }
    return true;
}

fn binary_search(arr: &[i32], target: i32) -> usize?
  pre is_sorted(arr)
  post ret.is_none() implies forall i: 0..arr.len(). arr[i] != target
  post ret.is_some() implies arr[ret.unwrap()] == target
{
    let mut lo: usize = 0;
    let mut hi: usize = arr.len();

    while lo < hi
      invariant lo <= hi and hi <= arr.len()
    {
        let mid = lo + (hi - lo) / 2;

        if arr[mid] == target {
            return Some(mid);
        } else if arr[mid] < target {
            lo = mid + 1;
        } else {
            hi = mid;
        }
    }

    return None;
}

AI友好错误消息

Chatter将编译器错误转化为可操作的建议:

标准编译器输出:

error[E0421]: array index out of bounds cannot be proven safe
  --> main.bmb:3:5

Chatter输出:

{
  "success": false,
  "errors": [{
    "code": "E0421",
    "message": "Array index out of bounds cannot be proven safe",
    "location": { "line": 3, "column": 5 },
    "suggestion": "Add `pre idx < arr.len()` before the function body",
    "explanation": "BMB requires compile-time proof of array bounds. Add a precondition or use checked access with `arr.get(idx)`."
  }]
}

聊天常见错误

错误 错误 纠正 检测
可为空语法 Option<T> T? bmb_check
Bitwise和 a & b a band b bmb_check
Bitwise或 a | b a bor b bmb_check
隐式返回 { x } { return x; } bmb_check
合同缺失 arr[idx] pre idx < arr.len() bmb_verify
溢流 a + b pre 或 +%/+|/+? bmb_verify

配置

# chatter.yaml
bmb:
  path: /usr/local/bin/bmb
  runtime: /usr/local/lib/bmb/runtime.c

verification:
  timeout_ms: 5000
  z3_path: /usr/local/bin/z3

examples:
  path: /usr/local/share/bmb/examples

logging:
  level: info
  file: /var/log/chatter.log

发展

# Install dependencies
npm install

# Run tests
npm test

# Run in development mode
npm run dev

# Build for production
npm run build

# Lint
npm run lint

命名

喋喋不休 继续BMB命名主题:

BMB = Bare-Metal-Banter
                 └──────┐
                        ▼
                    Chatter
                 (friendly conversation)

正如BMB允许AI和裸机之间的“玩笑”一样,Chatter允许AI和BMB工具链之间的对话。


相关项目

项目 描述
--BM BMB编译器和语言
哥顿 BMB包管理器
树保姆bmb 语法高亮显示
vscode bmb VS代码扩展

许可证

麻省理工学院


Banter for AI. Bare-metal for humans.
Chatter makes BMB speakable.