Rust Coder
AI agents could autonomously write and execute software programs in order to accomplish their own tasks and goals. The Rust language is a perfect fit for AI agents -- as the powerful Rust compiler can provide real-time and autonomous feedback to the agent to ensure the validity of the generated code. The Rust Coder project is an open source effort to provide such tools. It consists of API and MCP services that generate fully functional Rust projects from natural language descriptions. These services leverage LLMs to create complete Rust cargo projects, compile Rust source code, and automatically fix compiler errors.
✨ Features
- Generate Rust Projects 📦 - Transform text descriptions into complete Rust projects.
- Automatic Compilation & Fixing 🛠 - Detect and resolve errors during compilation.
- Vector Search 🔍 - Search for similar projects and errors.
- Docker Containerization 🐳 - Easy deployment with Docker.
- Asynchronous Processing ⏳ - Handle long-running operations efficiently.
- Multiple Service Interfaces 🔄 - REST API and MCP (Model-Compiler-Processor) interface.
📋 Prerequisites
Ensure you have the following installed:
- Docker & Docker Compose 🐳
Or, if you want to run the services directly on your own computer:
- Python 3.8+ 🐍
- Rust Compiler and cargo tools 🦀
📦 Install
git clone https://github.com/WasmEdge/Rust_coder
cd Rust_coder
🚀 Configure and run
Using Docker (Recommended)
Create the .env file and specify your own LLM API server. The default config assumes that you have a Gaia node like this running on localhost port 8080. The alternative configuration shown below uses a public Gaia node for coding assistance.
LLM_API_BASE=https://0x9fcf7888963793472bfcb8c14f4b6b47a7462f17.gaia.domains/v1
LLM_MODEL=gemma-3-27b-it-q4_0
LLM_EMBED_MODEL=nomic-embed
LLM_API_KEY=1234ABCD
LLM_EMBED_SIZE=768
Start the services.
docker-compose up -d
Stop the services.
docker-compose stop
Manual Setup
By default, you will need a Qdrant server running on localhost port 6333. You also need a local Gaia node. Set the following environment variables in your terminal to point to the Qdrant and Gaia instances, as well as your Rust compiler tools.
QDRANT_HOST=localhost
QDRANT_PORT=6333
LLM_API_BASE=http://localhost:8080/v1
LLM_MODEL=Qwen2.5-Coder-3B-Instruct
LLM_EMBED_MODEL=nomic-embed
LLM_API_KEY=your_api_key
LLM_EMBED_SIZE=768
CARGO_PATH=/path/to/cargo
RUST_COMPILER_PATH=/path/to/rustc
Start the services.
pip install -r requirements.txt
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
🔥 Usage
The API provides the following endpoints:
🎯 Generate a Project
Endpoint: POST /generate-sync
Example:
curl -X POST http://localhost:8000/generate-sync \
-H "Content-Type: application/json" \
-d '{"description": "A command-line calculator in Rust", "requirements": "Should support addition, subtraction, multiplication, and division"}'
📥 Request Body:
{
"description": "A command-line calculator in Rust",
"requirements": "Should support addition, subtraction, multiplication, and division"
}
📤 Response:
The combined_text field contains the flat text output of Rust project files that can be used as input for /compile and /compile-and-fix API calls.
{
"success": true,
"message":"Project generated successfully",
"combined_text":"[filename: Cargo.toml]\n[package]\nname = \"calculator\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html\n\n[dependencies]\nclap = { version = \"4.5\", features = [\"derive\"] }\n\n[filename: src/main.rs]\nuse std::io;\nuse clap::Parser;\n\n#[derive(Parser, Debug)]\n#[command(author, version, about, long_about = None)]\nstruct Args {\n /// The first number\n #[arg(required = true)]\n num1: f64,\n /// Operator (+, -, *, /)\n #[arg(required = true, value_parser = clap::value_parser!(f64))]\n operator: String,\n /// The second number\n #[arg(required = true)]\n num2: f64,\n}\n\nfn main() -> Result<(), Box<dyn std::error::Error>> {\n let args = Args::parse();\n\n match args.operator.as_str() {\n \"+\" => {\n println!(\"{}\", args.num1 + args.num2);\n }\n \"-\" => {\n println!(\"{}\", args.num1 - args.num2);\n }\n \"*\" => {\n println!(\"{}\", args.num1 * args.num2);\n }\n \"/\" => {\n if args.num2 == 0.0 {\n eprintln!(\"Error: Cannot divide by zero.\");\n std::process::exit(1);\n }\n println!(\"{}\", args.num1 / args.num2);\n }\n _ => {\n eprintln!(\"Error: Invalid operator. Use +, -, *, or /\");\n std::process::exit(1);\n }\n }\n\n Ok(())\n}\n\n[filename: README.md]\n# Calculator\n\nA simple command-line calculator written in Rust. Supports addition, subtraction, multiplication, and division.\n\n## Usage\n\nRun the program with two numbers and an operator as arguments:\n\n```bash\ncargo run <num1> <operator> <num2>\n```\n\nWhere `<operator>` is one of `+`, `-`, `*`, or `/`.\n\n**Example:**\n\n```bash\ncargo run 5 + 3\n# Output: 8\n\ncargo run 10 / 2\n# Output: 5\n\ncargo run 7 * 4\n# Output: 28\n```\n\n## Error Handling\n\nThe calculator handles division by zero and invalid operator inputs. Error messages are printed to standard error, and the program exits with a non-zero exit code in case of an error.\n\n\n# Build succeeded",
"files":{
"Cargo.toml":"[package]\nname = \"calculator\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html\n\n[dependencies]\nclap = { version = \"4.5\", features = [\"derive\"] }",
"src/main.rs":"use std::io;\nuse clap::Parser;\n\n#[derive(Parser, Debug)]\n#[command(author, version, about, long_about = None)]\nstruct Args {\n /// The first number\n #[arg(required = true)]\n num1: f64,\n /// Operator (+, -, *, /)\n #[arg(required = true, value_parser = clap::value_parser!(f64))]\n operator: String,\n /// The second number\n #[arg(required = true)]\n num2: f64,\n}\n\nfn main() -> Result<(), Box<dyn std::error::Error>> {\n let args = Args::parse();\n\n match args.operator.as_str() {\n \"+\" => {\n println!(\"{}\", args.num1 + args.num2);\n }\n \"-\" => {\n println!(\"{}\", args.num1 - args.num2);\n }\n \"*\" => {\n println!(\"{}\", args.num1 * args.num2);\n }\n \"/\" => {\n if args.num2 == 0.0 {\n eprintln!(\"Error: Cannot divide by zero.\");\n std::process::exit(1);\n }\n println!(\"{}\", args.num1 / args.num2);\n }\n _ => {\n eprintln!(\"Error: Invalid operator. Use +, -, *, or /\");\n std::process::exit(1);\n }\n }\n\n Ok(())\n}",
"README.md":"# Calculator\n\nA simple command-line calculator written in Rust. Supports addition, subtraction, multiplication, and division.\n\n## Usage\n\nRun the program with two numbers and an operator as arguments:\n\n```bash\ncargo run <num1> <operator> <num2>\n```\n\nWhere `<operator>` is one of `+`, `-`, `*`, or `/`.\n\n**Example:**\n\n```bash\ncargo run 5 + 3\n# Output: 8\n\ncargo run 10 / 2\n# Output: 5\n\ncargo run 7 * 4\n# Output: 28\n```\n\n## Error Handling\n\nThe calculator handles division by zero and invalid operator inputs. Error messages are printed to standard error, and the program exits with a non-zero exit code in case of an error."
},
"build_output":null,
"build_success":true
}
🛠 Compile a Project
Endpoint: POST /compile
Example:
curl -X POST http://localhost:8000/compile \
-H "Content-Type: application/json" \
-d '{
"code": "[filename: Cargo.toml]\n[package]\nname = \"hello_world\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\n\n[filename: src/main.rs]\nfn main() {\n println!(\"Hello, World!\");\n}"
}'
📥 Request Body:
{
"code": "[filename: Cargo.toml]\n[package]\nname = \"hello_world\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\n\n[filename: src/main.rs]\nfn main() {\n println!(\"Hello, World!\");\n}"
}
📤 Response:
{
"success": true,
"files": [
"Cargo.toml",
"src/main.rs"
],
"build_output": "Build successful",
"run_output": "Hello, World!\n"
}
🩹 Compile and fix errors
Endpoint: POST /compile-and-fix
Example:
curl -X POST http://localhost:8000/compile-and-fix \
-H "Content-Type: application/json" \
-d '{
"code": "[filename: Cargo.toml]\n[package]\nname = \"hello_world\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\n\n[filename: src/main.rs]\nfn main() {\n print \"Hello, World!\" \n}",
"description": "A simple hello world program",
"max_attempts": 3
}'
📥 Request Body:
{
"code": "[filename: Cargo.toml]\n[package]\nname = \"hello_world\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\n\n[filename: src/main.rs]\nfn main() {\n print \"Hello, World!\" \n}",
"description": "A simple hello world program",
"max_attempts": 3
}
📤 Response:
The combined_text field contains the flat text output of Rust project files that is in the same format as the input code field.
{
"success": true,
"message":"Code fixed and compiled successfully",
"attempts":[
{
"attempt":1,
"success":false,
"output":" Compiling hello_world v0.1.0 (/tmp/tmpbgeg4x_e)\nerror: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `\"Hello, World!\"`\n --> src/main.rs:2:11\n |\n2 | print \"Hello, World!\" \n | ^^^^^^^^^^^^^^^ expected one of 8 possible tokens\n\nerror: could not compile `hello_world` (bin \"hello_world\") due to 1 previous error\n"
},
{
"attempt":2,
"success":true,
"output":null
}
],
"combined_text":"[filename: Cargo.toml]\n[package]\nname = \"hello_world\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html\n\n[dependencies]\n\n[filename: src/main.rs]\nfn main() {\n println!(\"Hello, World!\");\n}\n\n[filename: README.md]\n# Hello World\n\nThis is a simple \"Hello, World!\" program in Rust. It prints the message \"Hello, World!\" to the console.\n\nTo run it:\n\n1. Make sure you have Rust installed ([https://www.rust-lang.org/](https://www.rust-lang.org/)).\n2. Save the code as `src/main.rs`.\n3. Run `cargo run` in the terminal from the project directory.",
"files":{
"Cargo.toml":"[package]\nname = \"hello_world\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html\n\n[dependencies]",
"src/main.rs":"fn main() {\n println!(\"Hello, World!\");\n}",
"README.md":"# Hello World\n\nThis is a simple \"Hello, World!\" program in Rust. It prints the message \"Hello, World!\" to the console.\n\nTo run it:\n\n1. Make sure you have Rust installed ([https://www.rust-lang.org/](https://www.rust-lang.org/)).\n2. Save the code as `src/main.rs`.\n3. Run `cargo run` in the terminal from the project directory."
},
"build_output":"Build successful",
"run_o