neuron-tool
Tool registry and middleware pipeline for the neuron ecosystem. Provides
ToolRegistry for registering, looking up, and executing tools through a
composable middleware chain. The middleware pattern is identical to axum's
from_fn -- each middleware receives a Next that it calls to continue the
chain or skips to short-circuit.
Key Types
ToolRegistry-- stores type-erasedToolDyntrait objects, dispatches calls through middlewareToolMiddleware-- trait for middleware that wraps tool execution (validate, log, permission check)ToolCall-- a tool call in flight:id,name, andinputJSONNext-- the remaining middleware chain plus the underlying tool; consumed on calltool_middleware_fn()-- creates middleware from a closure (like axum'sfrom_fn)
Usage
use ;
use ToolContext;
// Create a registry and register tools
let mut registry = new;
registry.register;
// Add global middleware (applies to all tool executions)
registry.add_middleware;
// Add per-tool middleware (applies only to "my_tool")
registry.add_tool_middleware;
// Execute a tool by name -- runs through global, then per-tool middleware
let input = json!;
let output = registry.execute.await?;
// Get all tool definitions for passing to a CompletionRequest
let definitions = registry.definitions;
Tools are registered as strongly-typed Tool impls and automatically erased
to ToolDyn for storage. Pre-erased tools can also be registered via
register_dyn() for tools that arrive as Arc<dyn ToolDyn> (e.g., from MCP).
The companion crate neuron-tool-macros provides the #[neuron_tool]
attribute macro to generate Tool implementations from annotated async
functions. It is re-exported as neuron_tool::neuron_tool when the macros
feature is enabled.
Part of neuron
This crate is part of neuron, a composable building-blocks library for AI agents in Rust.
License
Licensed under either of Apache License, Version 2.0 or MIT License at your option.