Contracts

How do I write a capability contract?

A contract is a TOML file. It sits next to your Rust implementation and describes what the capability accepts, what it produces, and what conditions must hold on both sides. You register the contract and the compiled WASM binary together.

Minimal contract structure

[capability]
name = "calculate_price"
version = "1.0.0"
description = "Calculates the total price for a line item"

[input]
type = "object"
required = ["quantity", "unit_price"]

[input.properties.quantity]
type = "integer"
minimum = 1

[input.properties.unit_price]
type = "number"
minimum = 0.0

[output]
type = "object"
required = ["total"]

[output.properties.total]
type = "number"

[[preconditions]]
field = "quantity"
op = "gt"
value = 0

[[postconditions]]
field = "total"
op = "gte"
value = 0.0

What each section does

  • [capability] — name, version, and description used by the registry and MCP server
  • [input] — JSON Schema for the input object the caller must provide
  • [output] — JSON Schema for the output the WASM binary must return
  • [[preconditions]] — conditions the runtime checks before execution; each is a field, operator, and value
  • [[postconditions]] — conditions the runtime checks after execution; same structure

Registering the contract

traverse capability register \
  --contract pricing.toml \
  --wasm target/wasm32-wasi/release/pricing.wasm

The CLI validates the TOML schema before accepting the registration. If your contract references a field that does not exist in the input schema, it will reject it.

See what are preconditions and what are postconditions for more detail on writing conditions.