Decorator and YAML Bindings¶
Type: Implementation guide. Normative spec: PROTOCOL_SPEC §5.11 Function-based Module Definition / §5.12 External Schema Binding.
Overview¶
Besides implementing the Module interface directly, apcore offers two ways to turn existing code into modules:
- Function wrapping (§5.11) uses a callable as a module. Python infers schemas from type hints, TypeScript can infer schemas at build time, and Rust derives schemas from the input and output types.
- Binding files (§5.12) — declare modules in a YAML file that maps a
module_idto atargetcallable, with no change to the target's code. Every SDK ships aBindingLoader.
Both paths produce FunctionModule instances that run through the full execution pipeline (ACL, approval, middleware, validation).
Function wrapping¶
# myapp/handlers.py
from apcore import APCore, Context
from apcore.decorator import module
# Decorator with arguments: registers into the given registry.
client = APCore()
@client.module(id="text.upper", description="Convert text to uppercase", tags=["text"])
def to_upper(text: str) -> dict:
return {"result": text.upper()}
# Bare decorator: auto-generates the ID from __module__ + __qualname__
# ("myapp.handlers.greet"), takes the description from the docstring, and
# attaches the module as greet.apcore_module (it does not register it).
@module
def greet(name: str, context: Context) -> dict:
"""Greet a user by name."""
return {"message": f"Hello, {name}!", "caller": context.caller_id}
client.register(greet.apcore_module.module_id, greet.apcore_module)
# Function-call form: returns the FunctionModule itself.
def shout(text: str) -> str:
return text.upper() + "!"
shout_module = module(shout, id="text.shout")
client.register("text.shout", shout_module)
print(client.call("text.upper", {"text": "hi"})) # {'result': 'HI'}
import { Type } from "@sinclair/typebox";
import { APCore } from "apcore-js";
const client = new APCore();
// Supply TypeBox schemas when calling this API directly from JavaScript.
const upper = client.module({
id: "text.upper",
description: "Convert text to uppercase",
tags: ["text"],
inputSchema: Type.Object({ text: Type.String() }),
outputSchema: Type.Object({ result: Type.String() }),
execute: (inputs) => ({ result: String(inputs.text).toUpperCase() }),
});
await upper.registration;
console.log(await client.call("text.upper", { text: "hi" })); // { result: 'HI' }
use std::collections::HashMap;
use apcore::{APCore, FunctionModule, ModuleAnnotations, ModuleError};
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), ModuleError> {
let client = APCore::new();
let upper = FunctionModule::with_description(
ModuleAnnotations::default(),
json!({ "type": "object", "properties": { "text": { "type": "string" } }, "required": ["text"] }),
json!({ "type": "object", "properties": { "result": { "type": "string" } } }),
"Convert text to uppercase",
None,
vec!["text".to_string()],
"1.0.0",
HashMap::new(),
Vec::new(),
|inputs, _ctx| {
Box::pin(async move {
let text = inputs["text"].as_str().unwrap_or("").to_uppercase();
Ok(json!({ "result": text }))
})
},
);
client.register("text.upper", Box::new(upper))?;
let out = client.call("text.upper", json!({ "text": "hi" }), None, None).await?;
println!("{out}"); // {"result":"HI"}
Ok(())
}
Python: @module and schema inference¶
apcore.decorator.module() works as a bare decorator (@module), a decorator with arguments (@module(id="x", ...)), and a function call (module(func, id="x")). The decorator forms return the original function with the module attached as func.apcore_module; the call form (any call that passes the function together with id= or registry=) returns the FunctionModule. Passing registry= registers the module immediately.
Two registering shortcuts take arguments only (always call them, even with no arguments): client.module(...) registers on that APCore client, and the package-level apcore.module(...) registers on a default process-wide client (or on registry= if given).
- Input schema — a Pydantic model built from the parameters' type hints.
self/cls,*argsand anyContext-typed parameter (detected by type, not name) are skipped; theContextis injected at call time.**kwargsmakes the model accept extra fields. A parameter with no type hint raisesFUNC_MISSING_TYPE_HINT. - Input values — the Executor uses the native values from validation for supplied parameters with an inferred schema. For example, a JSON object becomes its declared Pydantic model and a UUID string becomes a
UUIDinstance. Explicit JSON Schema bindings and class-based module inputs keep their JSON-shaped values. - Output schema — from the return annotation:
dictgives a permissive model, aBaseModelsubclass is used as is,Nonegives an empty permissive model, any other type gives a model with a singleresultfield. A missing return annotation raisesFUNC_MISSING_RETURN_TYPE. - Result normalisation —
None→{},dictunchanged,BaseModel→model_dump(), anything else →{"result": value}. - Description — the
descriptionargument, else the first docstring line, else"Module <name>". - Auto ID — from
__module__and__qualname__, lowercased, non-alphanumerics replaced with_, digit-leading segments prefixed with_(§5.11.6). - Async — an
async deffunction produces a module whoseexecuteis a coroutine function.
TypeScript schema inference¶
Both client.module(options) and the package-level module(options) return a FunctionModule synchronously. When the factory registers the module, await its registration promise to observe completion and asynchronous registration errors. A standalone module's registration promise is already resolved.
Use the apcore-js/compiler transformer with a TypeScript Program to infer schemas before the compiler erases types. Put the callable in the execute property of an inline options object passed to the named module export. The compiler takes the input schema from the callable's first parameter and the output schema from its awaited return type. An explicit schema in the options takes precedence.
import { APCore, module } from "apcore-js";
function toUpper(input: { text: string }): { result: string } {
return { result: input.text.toUpperCase() };
}
const client = new APCore();
await client.register("text.upper", module({ id: "text.upper", execute: toUpper }));
console.log(await client.call("text.upper", { text: "hi" }));
Add the transformer where your build emits the TypeScript program.
import ts from "typescript";
import { inferModuleSchemas } from "apcore-js/compiler";
const program = ts.createProgram(["app.mts"], {
target: ts.ScriptTarget.ES2022,
module: ts.ModuleKind.NodeNext,
strict: true,
});
const diagnostics = ts.getPreEmitDiagnostics(program);
if (diagnostics.length) {
throw new Error(ts.flattenDiagnosticMessageText(diagnostics[0].messageText, "\n"));
}
const result = program.emit(undefined, undefined, undefined, false, {
before: [inferModuleSchemas(program)],
});
if (result.emitSkipped) throw new Error("TypeScript emit failed");
Rust schema inference¶
APCore::typed_module derives the schemas using schemars::JsonSchema. The input type implements Deserialize and the output type implements Serialize. Use APCore::module to supply explicit JSON Schemas.
use apcore::{APCore, ModuleError, TypedModuleOptions};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::json;
#[derive(Deserialize, JsonSchema)]
struct Input {
text: String,
}
#[derive(Serialize, JsonSchema)]
struct Output {
result: String,
}
#[tokio::main]
async fn main() -> Result<(), ModuleError> {
let mut client = APCore::new();
client.typed_module(
"text.upper",
"Convert text to uppercase",
TypedModuleOptions::default(),
|input: Input, _| Box::pin(async move {
Ok(Output { result: input.text.to_uppercase() })
}),
)?;
let result = client.call("text.upper", json!({"text": "hi"}), None, None).await?;
println!("{result}");
Ok(())
}
Binding files¶
A binding file is YAML with a bindings list (schema: schemas/binding.schema.json):
# bindings/email.binding.yaml
bindings:
- module_id: "email.send"
target: "myapp.services.email:send_email"
description: "Send an email"
input_schema:
type: object
properties:
to: { type: string, description: "Recipient address" }
subject: { type: string, description: "Subject line" }
required: [to, subject]
output_schema:
type: object
properties:
message_id: { type: string }
annotations:
destructive: false
idempotent: false
tags: [email]
- module_id: "email.send_template"
target: "myapp.services.email:EmailService.send_template"
description: "Send an email from a template"
auto_schema: true
- module_id: "email.lookup"
target: "myapp.services.email:lookup"
schema_ref: "../schemas/email.lookup.schema.yaml"
| Field | Required | Meaning |
|---|---|---|
module_id |
yes | Canonical module ID (^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)*$, at most 192 chars) |
target |
yes | The callable, module.path:callable or module.path:ClassName.method (the class is instantiated with no arguments) |
description |
recommended | Plain-text description |
input_schema / output_schema |
one schema mode | Inline JSON Schema |
auto_schema |
one schema mode | true / "permissive" or "strict" — infer schemas from the target |
schema_ref |
one schema mode | Relative path from the declaring binding file to a schema in an allowed directory |
annotations, tags, version, metadata, documentation, display |
no | As for any module; display is the surface overlay of §5.13 |
The file may also carry a top-level spec_version (default "1.0").
Schema modes¶
Exactly one mode applies per entry; combining them raises BINDING_SCHEMA_MODE_CONFLICT.
- Inline —
input_schemaandoutput_schema. schema_ref— load both from an external schema file. The relative path may resolve inside the binding file's directory or the configuredschema.root. A standalone loader also recognizes the conventionalschemas/directory beside the binding directory. The resolved file must be inside one of those directories after following symbolic links.auto_schema: true(or"permissive") — infer from the target. Python reads the function's type hints; TypeScript reads the target module's exportedinputSchema/outputSchema(or<name>InputSchema/<name>OutputSchema); Rust takes the schemas carried by atyped_handler(derived withschemars). If nothing can be inferred,BINDING_SCHEMA_INFERENCE_FAILED.auto_schema: "strict"— as 3, and the inferred schema must be OpenAI/Anthropic strict-compatible, otherwiseBINDING_STRICT_SCHEMA_INCOMPATIBLE.- No schema key — treated as implicit
auto_schema: true. When inference finds nothing, Python raisesBINDING_SCHEMA_INFERENCE_FAILED, TypeScript and Rust fall back to a permissive object schema.
auto_schema: false with no other mode leaves no schema and raises BINDING_SCHEMA_INFERENCE_FAILED.
Target resolution¶
| SDK | How target is resolved |
|---|---|
| Python | importlib import of the module path, then attribute lookup. BindingLoader(trusted_package_prefixes={...}) restricts which module paths may be imported. |
| TypeScript | Dynamic import() of the module specifier (a package name or path; .. segments and file: URLs are rejected), then export lookup. new BindingLoader({ trustedPackagePrefixes: [...] }) restricts specifiers. |
| Rust | No dynamic loading: target is a key into a handler map you pass to register_into_with_handlers / register_into_with_typed_handlers. |
Loading binding files¶
Loading is always an explicit call; no SDK scans a bindings directory at start-up (§5.12.6).
| SDK | Single file | Directory |
|---|---|---|
| Python | BindingLoader().load_bindings(file_path, registry, config=None) |
load_binding_dir(dir_path=None, registry=None, pattern=None, *, config=None) |
| TypeScript | await loader.loadBindings(filePath, registry, config?) |
await loader.loadBindingDir(dirPath, registry, pattern?, config?) |
| Rust | loader.load_from_yaml(path) (or load_from_file for JSON), then register_into_with_handlers / register_into_with_typed_handlers |
loader.load_binding_dir(dir, pattern) / load_binding_dir_with_config(dir, pattern, config) |
The directory and pattern resolve as explicit argument > config > default:
| Setting | Config key | Default |
|---|---|---|
| directory | bindings.dir (env APCORE_BINDINGS_DIR) |
./bindings |
| filename pattern | bindings.pattern (env APCORE_BINDINGS_PATTERN) |
*.binding.yaml |
The pattern is matched against each file name (not recursively) with Algorithm A25 (* and ? only). A missing directory raises BINDING_FILE_INVALID; a pattern that matches nothing yields no modules. Passing a Config also applies the validation.binding.* limits (§9.1.2), which are unconstrained by default. Loading stops at the first error.
import { BindingLoader, Config, Registry } from "apcore-js/full";
const config = Config.load("apcore.yaml"); // may set bindings.dir / bindings.pattern
const registry = new Registry();
const modules = await new BindingLoader().loadBindingDir(undefined, registry, undefined, config);
console.log(modules.map((m) => m.moduleId));
use std::collections::HashMap;
use std::path::Path;
use apcore::{typed_handler, BindingLoader, ModuleError, Registry};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, JsonSchema)]
struct UpperInput {
text: String,
}
#[derive(Serialize, JsonSchema)]
struct UpperOutput {
result: String,
}
fn main() -> Result<(), ModuleError> {
// bindings/text.binding.yaml:
// bindings:
// - module_id: "text.upper"
// target: "text:to_upper"
// auto_schema: true
let mut loader = BindingLoader::new();
loader.load_from_yaml(Path::new("bindings/text.binding.yaml"))?;
// `target` strings are keys into this map; typed_handler derives the
// input/output schemas that auto_schema uses.
let mut handlers = HashMap::new();
handlers.insert(
"text:to_upper".to_string(),
typed_handler(|input: UpperInput| Ok(UpperOutput { result: input.text.to_uppercase() })),
);
let registry = Registry::new();
let count = loader.register_into_with_typed_handlers(®istry, handlers)?;
println!("registered {count} module(s)");
Ok(())
}
Errors¶
| Code | Python / TypeScript class | Cause |
|---|---|---|
FUNC_MISSING_TYPE_HINT |
FuncMissingTypeHintError |
A wrapped function's parameter has no type hint |
FUNC_MISSING_RETURN_TYPE |
FuncMissingReturnTypeError |
A wrapped function has no return annotation |
BINDING_FILE_INVALID |
BindingFileInvalidError |
Binding file or directory missing, empty, unparseable, or structurally invalid |
BINDING_INVALID_TARGET |
BindingInvalidTargetError |
target lacks :, or is outside the trusted prefixes |
BINDING_MODULE_NOT_FOUND |
BindingModuleNotFoundError |
The module path cannot be imported (Rust: no handler for the target) |
BINDING_CALLABLE_NOT_FOUND |
BindingCallableNotFoundError |
The callable is not in the module |
BINDING_NOT_CALLABLE |
BindingNotCallableError |
The resolved attribute is not callable |
BINDING_SCHEMA_INFERENCE_FAILED |
BindingSchemaInferenceFailedError |
No schema mode yields a schema |
BINDING_SCHEMA_MODE_CONFLICT |
BindingSchemaModeConflictError |
More than one schema mode on one entry |
BINDING_STRICT_SCHEMA_INCOMPATIBLE |
BindingStrictSchemaIncompatibleError |
auto_schema: "strict" and the schema cannot be made strict |
Rust returns these as ModuleError with the corresponding ErrorCode. A module_id that is already registered fails with the registry's DUPLICATE_MODULE_ID.
Python SDK reference
Not a protocol requirement — the relevant apcore-python source files.
| File | Purpose |
|---|---|
src/apcore/decorator.py |
module(), FunctionModule, type-hint schema inference, auto-ID generation |
src/apcore/bindings.py |
BindingLoader: YAML parsing, target resolution, schema modes, directory loading |
Contract: module (Python)¶
Inputs¶
func_or_none(callable/None, positional-only) — the function to wrap;Nonein the@module(...)formid(str, optional) — module ID; auto-generated from__module__/__qualname__when absentdescription,documentation(str, optional) — description falls back to the first docstring line, then"Module <name>"annotations(dict, optional),tags(list[str], optional),version(str, default"1.0.0"),metadata(dict, optional),display(dict, optional),examples(list, optional)registry(Registry, optional) — register the resultingFunctionModuleimmediately
Errors¶
FuncMissingTypeHintError(FUNC_MISSING_TYPE_HINT) — a parameter lacks a type hintFuncMissingReturnTypeError(FUNC_MISSING_RETURN_TYPE) — the function lacks a return annotation- Registry errors (e.g.
DUPLICATE_MODULE_ID) whenregistryis given
Returns¶
@module/@module(...): the original function, with.apcore_moduleattachedmodule(func, id=...)ormodule(func, registry=...): theFunctionModule
Properties¶
- async: false (the wrapper's
executeis async when the function is) - thread_safe: true
- pure: false when
registryis given
Contract: BindingLoader.load_bindings¶
Inputs¶
file_path(required) — a binding YAML fileregistry(required) — the registry to register intoconfig(optional) — supplies thevalidation.binding.*limits
Errors¶
BINDING_FILE_INVALID,BINDING_INVALID_TARGET,BINDING_MODULE_NOT_FOUND,BINDING_CALLABLE_NOT_FOUND,BINDING_NOT_CALLABLE,BINDING_SCHEMA_INFERENCE_FAILED,BINDING_SCHEMA_MODE_CONFLICT,BINDING_STRICT_SCHEMA_INCOMPATIBLE— see Errors- Registry errors such as
DUPLICATE_MODULE_ID
Returns¶
- The list of
FunctionModules registered (Python, TypeScript). Rust splits loading (load_from_yaml) from registration (register_into_with_*, which returns the count). - A binding's
displayoverlay is available undermetadata["display"]on the registered descriptor.
Properties¶
- async: false (TypeScript: returns a promise, because targets are loaded with dynamic
import()) - thread_safe: false (mutates the registry)
- pure: false (reads files, imports code, registers modules)
- idempotent: false (a second load re-registers and fails with
DUPLICATE_MODULE_ID) - Python and TypeScript prepare all declarations in a file before registering any of them. A registry error during registration can still leave earlier entries registered.
Contract: BindingLoader.load_binding_dir¶
Inputs¶
dir_path(optional) — directory to scan; falls back tobindings.dir, then./bindingsregistry(required)pattern(optional) — file-name pattern (A25); falls back tobindings.pattern, then*.binding.yamlconfig(optional) — source forbindings.dir,bindings.patternandvalidation.binding.*
Errors¶
BINDING_FILE_INVALID— the resolved directory does not exist; any error from loading a matched file (fail-fast)
Returns¶
- All modules from all matched files (Rust: the count), in file-name order; empty when nothing matches
Properties¶
- async: false (TypeScript: returns a promise)
- thread_safe: false
- pure: false
- idempotent: false
Testing strategy¶
- Function wrapping — schema inference for primitives, defaults,
Optional, unions, containers,Literal,Annotatedconstraints, nested models; Context detection by type;*args/**kwargs; missing-hint errors; result normalisation; sync vs. asyncexecute; the threemodule()forms; auto-ID sanitisation. - Binding files — parsing and structural errors; target resolution for functions and
Class.method; each schema mode and the mode-conflict error;schema_refrelative resolution;auto_schema: "strict"rejection; directory loading withbindings.dir/bindings.patternprecedence; missing-directory error; fail-fast; an end-to-end call through the executor.