Build a native connector
A native connector pairs a Rune-facing manifest with a shared library. The experimental v1 C ABI works with any language that can export the required C symbols. The example below uses Rust and runs in the native REST host on macOS or Linux.
Declare the Rune interface
[native] names the owner and its owner-qualified actions. [native.sections] maps each contributed section to a JSON configuration key. The bare import loads this declaration. In the example, @Echo/demo supplies settings.prefix and echo.message takes demo plus a context value named input.
version = 1
[package]
name = "native-echo"
version = "0.1.0"
description = "Echo a message from an approved native extension"
license = "MIT"
readme = "README.md"
[native]
owner = "echo"
actions = ["echo.message"]
[native.sections]
Echo = "settings"
[native.tool]
entrypoint = "tool.rune"
description = "Serve a local echo endpoint"
section = "Echo/local"
[native.tool.options]
prefix = "prefix"
import "pkg:echo"
@App
type = REST
port = 3000
@Echo/demo
prefix = "Hello"
@Route/GET/echo
run:
input = { message: "Vectrune" }
result = echo.message demo input
respond 200 result
Complete Rust example
Cargo builds this crate as a shared library. Buffer uses C layout. The create function allocates an Echo instance, invoke reads a borrowed JSON request, and free and destroy release allocations made by the library.
[package]
name = "vectrune-native-echo"
version = "0.1.0"
edition = "2021"
license = "MIT"
publish = false
[lib]
crate-type = ["cdylib"]
[dependencies]
serde_json = "1"
use serde_json::{json, Value};
use std::{
ffi::c_void,
panic::{catch_unwind, AssertUnwindSafe},
ptr, slice,
};
#[repr(C)]
pub struct Buffer {
data: *mut u8,
len: usize,
}
struct Echo {
prefix: String,
}
// The exported functions follow native/extensions/v1.h. Inputs are borrowed;
// instances and response buffers are allocated and released by this library.
#[no_mangle]
pub extern "C" fn vectrune_extension_abi_version() -> u32 {
1
}
#[no_mangle]
pub unsafe extern "C" fn vectrune_extension_create(
config: *const u8,
len: usize,
instance: *mut *mut c_void,
out: *mut Buffer,
) -> i32 {
*instance = ptr::null_mut();
let mut state = None;
let status = reply(out, || {
let config = parse(config, len)?;
let prefix = match config.get("settings") {
None if config.as_object().is_some_and(|object| object.is_empty()) => "",
Some(settings) => settings["prefix"]
.as_str()
.ok_or("prefix must be a string")?,
None => return Err("expected settings"),
};
state = Some(Echo {
prefix: prefix.to_owned(),
});
Ok(json!({}))
});
if let (0, Some(state)) = (status, state) {
*instance = Box::into_raw(Box::new(state)).cast();
}
status
}
#[no_mangle]
pub unsafe extern "C" fn vectrune_extension_invoke(
instance: *mut c_void,
request: *const u8,
len: usize,
out: *mut Buffer,
) -> i32 {
reply(out, || {
let state = &*instance.cast::<Echo>();
let request = parse(request, len)?;
if request["version"].as_u64() != Some(1) {
return Err("expected protocol version 1");
}
let input = &request["input"];
match request["action"].as_str() {
Some("extension.describe") if input.is_null() => Ok(json!({
"version": 1,
"owner": "echo",
"sections": ["Echo"],
"actions": ["echo.message"],
"execution": 1
})),
Some("extension.execute") if input["action"] == "echo.message" => {
let Some(message) = input["input"]["message"].as_str() else {
return Ok(json!({"outcome": "failed"}));
};
Ok(json!({
"outcome": "acknowledged",
"result": {"text": format!("{} {message}", state.prefix)}
}))
}
_ => Err("unsupported request"),
}
})
}
#[no_mangle]
pub unsafe extern "C" fn vectrune_extension_free(buffer: Buffer) {
let bytes = ptr::slice_from_raw_parts_mut(buffer.data, buffer.len);
drop(Box::from_raw(bytes));
}
#[no_mangle]
pub unsafe extern "C" fn vectrune_extension_destroy(instance: *mut c_void) {
drop(Box::from_raw(instance.cast::<Echo>()));
}
unsafe fn parse(data: *const u8, len: usize) -> Result<Value, &'static str> {
serde_json::from_slice(slice::from_raw_parts(data, len)).map_err(|_| "invalid JSON")
}
unsafe fn reply(out: *mut Buffer, operation: impl FnOnce() -> Result<Value, &'static str>) -> i32 {
let (status, body) = match catch_unwind(AssertUnwindSafe(operation)) {
Ok(Ok(body)) => (0, body),
Ok(Err(message)) => (1, json!({"error": message})),
Err(_) => (1, json!({"error": "extension panicked"})),
};
let bytes = body.to_string().into_bytes().into_boxed_slice();
let len = bytes.len();
*out = Buffer {
data: Box::into_raw(bytes).cast(),
len,
};
status
}
Follow the ABI lifecycle
- Export vectrune_extension_abi_version and return 1. The host calls vectrune_extension_create with an empty JSON object for discovery, then invokes extension.describe. The advertised owner, sections, actions, and optional sources must match vectrune.toml. Report execution version 1.
- The host calls vectrune_extension_create for each named section with its mapped JSON settings. Set a non-null opaque instance pointer on success and leave it null on failure. Input buffers are borrowed for the call.
- vectrune_extension_invoke receives a UTF-8 JSON envelope with version, action, and input. Ordinary Rune actions arrive inside extension.execute. Return a numeric ABI status and a UTF-8 JSON response buffer. The host limits each serialized input and response to 8 MiB.
- The host gives response buffers back to vectrune_extension_free and calls vectrune_extension_destroy at shutdown. Calls on one instance are serialized. Keep panics and exceptions inside the library and join any threads you start before destroy returns.
{
"describe": {
"request": {"version": 1, "action": "extension.describe", "input": null},
"response": {
"version": 1,
"owner": "echo",
"sections": ["Echo"],
"actions": ["echo.message"],
"execution": 1
}
},
"execute": {
"config": {"settings": {"prefix": "Hello"}},
"request": {
"version": 1,
"action": "extension.execute",
"input": {"action": "echo.message", "input": {"message": "Vectrune"}}
},
"response": {"outcome": "acknowledged", "result": {"text": "Hello Vectrune"}}
}
}
For extension.execute, return ABI status 0 with outcome acknowledged and a result, failed for a known rejection, or unknown when the effects are uncertain. The REST host reports known rejections as 503 and unknown outcomes as 502. A Rune action can handle either with catch HANDLER.
To produce events, include the contributed section names in [native].sources and in the descriptor. Each instance selects a receiver job. The host calls extension.poll for one event and extension.ack after the Rune receiver succeeds.
Build, approve, and run
Native code and its dynamic dependencies run with Vectrune’s process permissions. Approval pins the library’s SHA-256 in the selected scope. For non-interactive installs, pass --sha256 with a reviewed digest.
With Rust and Cargo installed, extract the example and build the shared library for the OS and architecture of your Vectrune binary. On Linux, use libvectrune_native_echo.so in the install command. The consumer folder contains app.rune.
unzip native-echo.zip
cd native-echo
cargo build
cd consumerInstall from consumer and approve the exact shared library when prompted. Repeat the install when the library changes.
vectrune deps install .. --library ../target/debug/libvectrune_native_echo.dylibvectrune app.runeFrom a second terminal, call the running app. The response contains Hello Vectrune.
curl http://127.0.0.1:3000/echo