协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
rust-agentai【开发与工程】Build AI agents in Rust using the agentai crate. Use this skill whenever the user wants to create a Rust AI agent, implement a tool-using agent in Rust, use structured output from an LLM in Rust, integrate MCP servers into a Rust agent, or work with the agentai or genai crates. Trigger on phrases like "Rust agent", "build an agent in Rust", "agentai …
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Build AI agents in Rust using the agentai crate. It wraps the genai library for multi-provider LLM access and adds structured output, custom toolboxes, MCP server integration, and built-in web tools.
Warning: This library is under heavy development. The interface may change between versions.
Add the crate to your project:
cargo add agentai
cargo add tokio --features full
cargo add anyhow
cargo add serde --features derive
cargo add schemars
For logging (used in all examples):
cargo add log simplelog
Set environment variables:
AGENTAI_BASE_URL=https://openrouter.ai/api/v1 # or any OpenAI-compatible endpoint
AGENTAI_API_KEY=your_key_here
AGENTAI_MODEL=openai/gpt-4.1-mini # optional, fallback default
Load 📋 Agent Patterns before implementing any agent.
Decide what kind of agent you need based on the output and tool requirements:
| Type | Output | Tools | Reference |
|---|---|---|---|
| Simple Q&A | String | None | examples/simple.rs |
| Structured output | Custom struct | None | examples/struct_output.rs |
| Custom tools | String or struct | #[toolbox] impl | examples/tools_custom.rs |
| MCP integration | String or struct | McpToolBox | examples/tools_mcp.rs |
| Web search/fetch | String or struct | ToolBoxSet | examples/tools_web.rs |
Load 🔧 Toolbox Patterns if your agent uses tools.
Every agent follows the same pattern:
let mut agent = Agent::new_with_url(&base_url, &api_key, SYSTEM);
let answer: YourOutputType = agent.run(&model, question, toolbox_option).await?;
The return type YourOutputType drives the behaviour:
String → plain text response, no JSON schema enforcedDeserialize + JsonSchema → structured JSON output via LLM response formatAlways include a _thinking field in structured output structs — it gives the LLM a scratchpad and improves answer quality:
#[derive(Deserialize, JsonSchema, Debug)]
struct Answer {
/// In this field provide your thinking steps
#[serde(rename = "_thinking")]
thinking: String,
/// In this field provide the final answer
answer: String,
}
None as toolboxSome(&your_toolbox)ToolBoxSetMcpToolBox::new(cmd, args, env)WebFetchToolBox and/or WebSearchToolBox (requires BRAVE_API_KEY for search)Before shipping, verify:
_thinking fieldAGENTAI_BASE_URL, AGENTAI_API_KEY, AGENTAI_MODEL)anyhow::Result consistentlycargo build passes without warningsagent.run() calls within the same Agent instance (history is maintained), but not across program restartsString uses an internal escape hack — prefer structured output for complex use cases_thinking#[toolbox] + #[tool] macros