| Crates.io | pforge-codegen |
| lib.rs | pforge-codegen |
| version | 0.1.4 |
| created_at | 2025-10-02 14:50:22.320674+00 |
| updated_at | 2025-12-06 12:09:01.878724+00 |
| description | Zero-boilerplate MCP server framework with EXTREME TDD methodology |
| homepage | https://github.com/paiml/pforge |
| repository | https://github.com/paiml/pforge |
| max_upload_size | |
| id | 1864487 |
| size | 44,652 |
Code generation engine for pforge - converts YAML configuration into optimized Rust code.
cargo add pforge-codegen
use pforge_codegen::generate_server;
use pforge_config::ForgeConfig;
let config: ForgeConfig = serde_yaml::from_str(yaml_config)?;
// Generate Rust code
let generated_code = generate_server(&config)?;
// Write to file
std::fs::write("src/generated.rs", generated_code)?;
For this YAML configuration:
forge:
name: calculator
version: 0.1.0
tools:
- type: native
name: add
description: "Add two numbers"
handler:
path: handlers::add_handler
params:
a: { type: number, required: true }
b: { type: number, required: true }
The codegen generates:
// Handler registration
pub fn register_handlers(registry: &mut HandlerRegistry) -> Result<()> {
registry.register("add", Arc::new(handlers::add_handler()))?;
Ok(())
}
// JSON Schema for parameters
pub fn add_schema() -> schemars::schema::RootSchema {
schemars::schema_for!(AddParams)
}
#[derive(Serialize, Deserialize, JsonSchema)]
pub struct AddParams {
pub a: f64,
pub b: f64,
}
// Server initialization
pub async fn start_server() -> Result<()> {
let mut registry = HandlerRegistry::new();
register_handlers(&mut registry)?;
// Start MCP server with stdio transport
pmcp::start_stdio_server(registry).await
}
pforge-configGenerates optimized registry initialization:
let mut registry = HandlerRegistry::new();
registry.register("tool1", Arc::new(Tool1Handler::default()))?;
registry.register("tool2", Arc::new(Tool2Handler::default()))?;
Creates type-safe parameter structs:
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct ToolParams {
pub input: String,
#[serde(default)]
pub optional: Option<i64>,
}
Automatic JSON Schema for MCP protocol:
pub fn tool_schema() -> schemars::schema::RootSchema {
schemars::schema_for!(ToolParams)
}
Generates transport-specific initialization:
// For stdio transport
pmcp::start_stdio_server(registry).await?;
// For SSE transport
pmcp::start_sse_server("0.0.0.0:3000", registry).await?;
// For WebSocket transport
pmcp::start_ws_server("0.0.0.0:3001", registry).await?;
The code generator is used by pforge-cli during the build process:
# Automatically runs codegen
pforge build
# Or explicitly
pforge codegen
Provide custom code generation templates:
use pforge_codegen::{CodegenConfig, generate_with_config};
let config = CodegenConfig {
template_dir: Some("templates/".into()),
output_format: OutputFormat::Formatted,
optimize: true,
};
let code = generate_with_config(&forge_config, &config)?;
Generate handlers with inline logic:
tools:
- type: native
name: echo
description: "Echo input"
handler:
inline: |
Ok(json!({ "output": params["input"] }))
params:
input: { type: string, required: true }
Generates:
#[async_trait::async_trait]
impl Handler for EchoHandler {
async fn handle(&self, params: Value) -> Result<Value> {
Ok(json!({ "output": params["input"] }))
}
}
All generated code follows Rust best practices:
rustfmt for consistent styleclippy with no warningsThe code generator is designed for fast build times:
MIT