mod context;
mod registry;
mod result;
mod schema;
pub use context::*;
pub use registry::*;
pub use result::*;
pub use schema::*;
use crate::error::ToolError;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::path::PathBuf;
use std::sync::Arc;
use tokio_util::sync::CancellationToken;
pub use crate::context::config::PathStyle;
use crate::interface::permission::{ModeStance, PermissionFacts};
pub use crate::permission::PermissionMode;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct PromptContext {
pub cwd: PathBuf,
pub model: String,
pub session_id: String,
pub is_interactive: bool,
pub all_tool_names: Vec<String>,
pub allowed_agent_types: Vec<String>,
pub path_style: PathStyle,
pub layout: crate::paths::Layout,
}
impl Default for PromptContext {
fn default() -> Self {
Self {
cwd: PathBuf::new(),
model: String::new(),
session_id: String::new(),
is_interactive: false,
all_tool_names: vec![],
allowed_agent_types: vec![],
path_style: PathStyle::default(),
layout: crate::paths::Layout::default(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum ToolOrigin {
#[default]
Builtin,
Mcp {
server: String,
},
Plugin {
name: String,
},
Host,
}
impl ToolOrigin {
pub fn mcp(server: impl Into<String>) -> Self {
Self::Mcp {
server: server.into(),
}
}
pub fn plugin(name: impl Into<String>) -> Self {
Self::Plugin { name: name.into() }
}
pub fn mcp_server(&self) -> Option<&str> {
match self {
Self::Mcp { server } => Some(server),
Self::Builtin | Self::Plugin { .. } | Self::Host => None,
}
}
pub fn is_mcp(&self) -> bool {
self.mcp_server().is_some()
}
pub fn wire(&self) -> String {
match self {
Self::Builtin => "builtin".to_string(),
Self::Mcp { server } => format!("mcp:{server}"),
Self::Plugin { name } => format!("plugin:{name}"),
Self::Host => "host".to_string(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct CallTraits {
pub read_only: bool,
pub concurrency_safe: bool,
pub destructive: bool,
}
impl CallTraits {
pub fn read_only(mut self, yes: bool) -> Self {
self.read_only = yes;
self
}
pub fn concurrency_safe(mut self, yes: bool) -> Self {
self.concurrency_safe = yes;
self
}
pub fn destructive(mut self, yes: bool) -> Self {
self.destructive = yes;
self
}
}
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
pub struct ToolSpec {
pub short_description: Option<String>,
pub origin: ToolOrigin,
pub traits: CallTraits,
pub callable_from_code: bool,
pub code_result: Option<&'static str>,
pub needs_dispatch: bool,
pub stance: ModeStance,
pub pages_itself: bool,
}
impl ToolSpec {
pub fn new() -> Self {
Self::default()
}
pub fn short_description(mut self, text: impl Into<String>) -> Self {
self.short_description = Some(text.into());
self
}
pub fn origin(mut self, origin: ToolOrigin) -> Self {
self.origin = origin;
self
}
pub fn traits(mut self, traits: CallTraits) -> Self {
self.traits = traits;
self
}
pub fn read_only(mut self, yes: bool) -> Self {
self.traits.read_only = yes;
self
}
pub fn concurrency_safe(mut self, yes: bool) -> Self {
self.traits.concurrency_safe = yes;
self
}
pub fn destructive(mut self, yes: bool) -> Self {
self.traits.destructive = yes;
self
}
pub fn callable_from_code(mut self, yes: bool) -> Self {
self.callable_from_code = yes;
self
}
pub fn code_result(mut self, shape: &'static str) -> Self {
self.code_result = Some(shape);
self
}
pub fn pages_itself(mut self) -> Self {
self.pages_itself = true;
self
}
pub fn needs_dispatch(mut self, yes: bool) -> Self {
self.needs_dispatch = yes;
self
}
pub fn stance(mut self, stance: ModeStance) -> Self {
self.stance = stance;
self
}
}
#[async_trait]
pub trait Tool: Send + Sync + 'static {
fn name(&self) -> &str;
fn description(&self) -> &str;
fn spec(&self) -> ToolSpec;
fn input_schema(&self, style: PathStyle) -> Value;
async fn guide(&self, _ctx: &PromptContext) -> Option<String> {
None
}
fn call_traits(&self, _input: &Value) -> CallTraits {
self.spec().traits
}
async fn permission_facts(&self, _: &Value, _: &ToolContext) -> PermissionFacts {
PermissionFacts::unknown()
}
async fn validate_input(&self, _: &Value, _: &ToolContext) -> ValidationResult {
ValidationResult::Ok
}
async fn call(
&self,
i: Value,
c: ToolContext,
p: ProgressSender,
) -> Result<ToolResult, ToolError>;
}
#[cfg(test)]
mod tests {
use super::*;
struct F;
#[async_trait]
impl Tool for F {
fn name(&self) -> &str {
"f"
}
fn description(&self) -> &str {
"f"
}
fn spec(&self) -> ToolSpec {
ToolSpec::new().read_only(true).concurrency_safe(true)
}
fn input_schema(&self, _: PathStyle) -> Value {
serde_json::json!({})
}
async fn call(
&self,
_: Value,
_: ToolContext,
_: ProgressSender,
) -> Result<ToolResult, ToolError> {
Ok(ToolResult::text("ok"))
}
}
#[test]
fn empty() {
assert!(InMemoryToolRegistry::new().list().is_empty());
}
#[test]
fn find() {
let r = InMemoryToolRegistry::new();
r.register(Arc::new(F));
assert!(r.get("f").is_some());
}
#[test]
fn a_tool_that_says_nothing_is_a_builtin() {
assert_eq!(F.spec().origin, ToolOrigin::Builtin);
assert_eq!(ToolOrigin::Builtin.wire(), "builtin");
assert_eq!(ToolOrigin::mcp("github").wire(), "mcp:github");
}
}
#[cfg(test)]
mod spec_tests {
use super::*;
async fn unreachable_call() -> Result<ToolResult, ToolError> {
Ok(ToolResult::text("ok"))
}
struct Plain;
#[async_trait]
impl Tool for Plain {
fn name(&self) -> &str {
"Plain"
}
fn description(&self) -> &str {
"says nothing about itself"
}
fn spec(&self) -> ToolSpec {
ToolSpec::new()
}
fn input_schema(&self, _: PathStyle) -> Value {
serde_json::json!({"type": "object"})
}
async fn call(
&self,
_: Value,
_: ToolContext,
_: ProgressSender,
) -> Result<ToolResult, ToolError> {
unreachable_call().await
}
}
struct Declared;
#[async_trait]
impl Tool for Declared {
fn name(&self) -> &str {
"Declared"
}
fn description(&self) -> &str {
"declares a spec"
}
fn input_schema(&self, _: PathStyle) -> Value {
serde_json::json!({"type": "object"})
}
fn spec(&self) -> ToolSpec {
ToolSpec::new()
.origin(ToolOrigin::mcp("x"))
.concurrency_safe(true)
.destructive(true)
.needs_dispatch(true)
.stance(ModeStance::LeavesPlan)
.short_description("short")
}
fn call_traits(&self, input: &Value) -> CallTraits {
self.spec().traits.read_only(input.get("peek").is_some())
}
async fn call(
&self,
_: Value,
_: ToolContext,
_: ProgressSender,
) -> Result<ToolResult, ToolError> {
unreachable_call().await
}
}
#[tokio::test]
async fn a_tool_that_declares_nothing_gets_the_defaults() {
assert_eq!(Plain.spec(), ToolSpec::new());
assert_eq!(Plain.call_traits(&Value::Null), CallTraits::default());
assert_eq!(Plain.spec().origin, ToolOrigin::Builtin);
assert_eq!(Plain.spec().stance, ModeStance::Normal);
assert!(Plain.guide(&PromptContext::default()).await.is_none());
}
#[test]
fn a_declared_spec_is_what_the_engine_reads() {
let spec = Declared.spec();
assert_eq!(spec.origin.mcp_server(), Some("x"));
assert!(spec.traits.concurrency_safe && spec.traits.destructive);
assert!(spec.needs_dispatch);
assert_eq!(spec.stance, ModeStance::LeavesPlan);
assert!(!spec.callable_from_code);
assert_eq!(spec.short_description.as_deref(), Some("short"));
}
#[test]
fn call_traits_that_read_the_input_win_over_the_spec() {
assert!(!Declared.spec().traits.read_only);
assert!(
Declared
.call_traits(&serde_json::json!({"peek": 1}))
.read_only
);
assert!(!Declared.call_traits(&serde_json::json!({})).read_only);
}
}