megacode-agent-sdk 0.31.0

Drive a MEGA harness agent session from a Rust program.
Documentation

megacode-agent-sdk

Drive a MEGA harness agent session from a Rust program: start a session, give the model tools your program runs, answer its approval requests, and read what it does. One session is one harness process. Use it to build an agent that is not a coding agent.

cargo add megacode-agent-sdk

The example below also uses tokio (with macros and rt), serde (with derive) and serde_json.

The crate needs a harness binary of the same major and minor version. It uses the binary option, then the MEGACODE_AGENT_SDK_BINARY environment variable, then a verified copy in its cache, and otherwise downloads the matching release and checks it against digests compiled into the crate.

use megacode_agent_sdk::{Client, Options, Tool, ToolContext, ToolOutput};
use serde::Deserialize;
use serde_json::json;

#[derive(Deserialize)]
struct Search { to: String }

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), megacode_agent_sdk::Error> {
    let search = Tool::new(
        "search_trains",
        "Find trains to a destination.",
        json!({"type": "object", "properties": {"to": {"type": "string"}}, "required": ["to"]}),
        |args: Search, _ctx: ToolContext| async move {
            Ok::<_, String>(ToolOutput::text(format!("to {}: 9:10 for 42 pounds", args.to)))
        },
    );
    let options = Options::plain("You are Pip, a personal assistant. Be brief.").tool(search);

    let client = Client::start(options).await?;
    let mut turn = client.send("find me a train to Leeds").await?;
    while let Some(event) = turn.next().await {
        println!("{:?}", event?);
    }
    client.close().await;
    Ok(())
}

In a source checkout, a complete errand with an approval and a scripted model, which needs no API key, is examples/pip.rs:

cargo run -p megacode-agent-sdk --example pip -- examples/pip.mock.toml /path/to/harness

The second argument names the harness binary; without it the example reads MEGACODE_AGENT_SDK_BINARY, and only then downloads.

The options, tools, approvals, testing with a mock file, versions, the download and its trust rules are in the agent SDK guide (docs/sdk.md in the source).

Licensed under either of MIT or Apache-2.0, at your option.