Skip to main content

tool

Attribute Macro tool 

Source
#[tool]
Expand description

Maps the function to a tool

§Parameters

  • title - Tool title.
  • descr - Tool description.
  • input_schema - Schema for the tool input.
  • output_schema - Schema for the tool output.
  • annotations - Arbitrary metadata.
  • roles & permissions - Define which users can run the tool when using Streamable HTTP transport with OAuth.
  • middleware - Middleware list to apply to the tool.
  • task_support - Specifies task augmentation support for this tool.
  • no_schema - Explicitly disables input schema generation if it’s not set in input_schema.

§Simple Example

use neva::prelude::*;

#[tool(descr = "Hello world tool")]
async fn say_hello() -> &'static str {
    "Hello, world!"
}

§Full Example

use neva::prelude::*;

#[derive(serde::Deserialize)]
struct Payload {
    say: String,
    name: String,
}

#[json_schema(ser)]
struct Results {
    message: String,
}

#[tool(
    title = "JSON Hello",
    descr = "Say from JSON",
    roles = ["user"],
    permissions = ["read"],
    annotations = r#"{
        "destructiveHint": false,
        "idempotentHint": true,
        "openWorldHint": false,
        "readOnlyHint": false
    }"#,
    input_schema = r#"{
        "properties": {
            "arg": {
                "type": "object",
                "description": "A message in JSON format",
                "properties": {
                    "say": { "type": "string", "description": "A message to say" },
                    "name": { "type": "string", "description": "A name to whom say Hello" }
                },
                "required": ["say", "name"]
            }
        },
        "required": ["arg"]
    }"#,
    output_schema = r#"{
        "properties": {
            "message": { "type": "string", "description": "A message to say" }
        },
        "required": ["message"]
    }"#
)]
async fn say_json(arg: Json<Payload>) -> Json<Results> {
    let result = Results { message: format!("{}, {}!", arg.say, arg.name) };
    result.into()
}