# WIT Tool Authoring

Source: https://runvero.dev/docs/wit-contracts/
Documentation index: https://runvero.dev/llms.txt

Every RunVero tool is a WASI 0.2 component that implements the contract in [`wit/agent-tool.wit`](https://github.com/runvero-labs/runvero/blob/main/wit/agent-tool.wit).

## The WIT contract

```wit
package runvero:tooling@0.1.0;

world tool-executor {
    export execute: func(input: string) -> result<string, string>;
}

world http-tool-executor {
    import wasi:cli/environment@0.2.12;
    import wasi:http/outgoing-handler@0.2.12;
    import wasi:http/types@0.2.12;
    export execute: func(input: string) -> result<string, string>;
}
```

- A tool exports one function, `execute`. It takes a string and returns either an output string or an error string.
- `input` is the tool's arguments as a JSON object. The host checks them against the `input_schema` in the tool's manifest before the tool runs.
- Return `Ok` with your output (usually JSON) or `Err` with a message the caller can act on.
- Tools that call HTTP APIs target `http-tool-executor`. Their requests only reach hosts that both the manifest and the operator allow. Secrets arrive as `rvsec_...` placeholders in the environment, and the host swaps in the real value only on requests to the bound host.

The host sets the limits, not the tool: every call runs with a fuel (instruction) budget, a memory cap and a timeout.

## Build a tool in Rust

Rust is the supported authoring language. You need the `runvero` CLI ([build it from source](https://runvero.dev/docs/quickstart#2-build-the-cli)) and Rust with the `wasm32-wasip2` target:

```bash
rustup target add wasm32-wasip2
```

### 1. Scaffold the project

```bash
runvero init my-tool --lang rust
cd my-tool
```

This creates a Cargo project with the WIT files, a `runvero.tool.json` manifest, sample input and test fixtures.

### 2. Implement `execute`

`src/lib.rs` generates bindings from the bundled WIT and implements the `Guest` trait:

```rust
use serde::{Deserialize, Serialize};

wit_bindgen::generate!({
    path: "wit",
    world: "tool-executor",
});

#[derive(Deserialize)]
struct InputArgs {
    message: String,
    #[serde(default)]
    uppercase: bool,
}

#[derive(Serialize)]
struct OutputPayload {
    processed: String,
    word_count: usize,
}

struct Component;

impl Guest for Component {
    fn execute(input: String) -> Result<String, String> {
        let args: InputArgs =
            serde_json::from_str(&input).map_err(|err| format!("invalid JSON input: {err}"))?;

        if args.message.trim().is_empty() {
            return Err("message must not be blank".to_string());
        }

        let processed = if args.uppercase {
            args.message.to_uppercase()
        } else {
            args.message.clone()
        };

        serde_json::to_string(&OutputPayload {
            word_count: args.message.split_whitespace().count(),
            processed,
        })
        .map_err(|err| format!("failed to serialize output: {err}"))
    }
}

export!(Component);
```

### 3. Describe the tool in its manifest

`runvero.tool.json` tells the host and MCP clients what the tool does, what input it accepts and what it may use:

```json
{
  "schema_version": "runvero.tool/v1",
  "name": "my_tool",
  "version": "0.1.0",
  "description": "Sandboxed WASI 0.2 RunVero tool (my_tool).",
  "component": "my_tool.wasm",
  "input_schema": {
    "type": "object",
    "properties": {
      "message": { "type": "string", "minLength": 1, "maxLength": 4096 },
      "uppercase": { "type": "boolean", "default": false }
    },
    "required": ["message"],
    "additionalProperties": false
  },
  "resources": { "fuel": 5000000, "memory_mb": 16, "timeout_ms": 2000 },
  "capabilities": { "allowed_hosts": [], "secret_bindings": [] }
}
```

`resources` is the budget the tool asks for, and `runvero test` never runs above it. Empty `allowed_hosts` means no network access. The full format is in the [tool manifest spec](https://github.com/runvero-labs/runvero/blob/main/docs/tool-manifest-spec.md).

### 4. Build, test and run

```bash
runvero build
runvero test
runvero run . --args '{"message": "hello runvero", "uppercase": true}'
```

`runvero build` compiles to `wasm32-wasip2` and checks that the component exports `execute` on `runvero:tooling/tool-executor@0.1.0` and imports only supported WASI 0.2 interfaces. `runvero test` runs the manifest examples and `tests/fixtures.json` inside the sandbox:

```text
  [PASS] default-greeting (1.06 MiB, 6,550 fuel, 0.14 ms)
  [PASS] uppercase-transform (1.06 MiB, 7,567 fuel, 0.10 ms)
  [PASS] reject-blank-message (0 B, 0 fuel, 0.00 ms)
  [PASS] reject-missing-required-field (0 B, 0 fuel, 0.00 ms)

Test result: PASSED (4 passed, 0 failed)
```

## Python and TypeScript

**Preview.** Python (`runvero-guest`) and TypeScript (`@runvero/guest`) authoring work inside a checkout of the `runvero` repository, but the packages are not published yet. See the [WIT development guide](https://github.com/runvero-labs/runvero/blob/main/docs/wit-development-guide.md) for the in-repository steps.
