thulp_core/
lib.rs

1//! # thulp-core
2//!
3//! Core types, traits, and error definitions for the thulp execution context platform.
4//!
5//! This crate provides the foundational abstractions for defining, validating, and executing
6//! tools in AI agent environments. It includes type-safe parameter definitions, tool metadata,
7//! and extensible traits for implementing custom tool providers and transports.
8//!
9//! ## Core Types
10//!
11//! - [`ToolDefinition`]: Describes an available tool with its parameters and metadata
12//! - [`ToolCall`]: Represents a request to execute a specific tool with arguments
13//! - [`ToolResult`]: The result of a tool execution (success or failure)
14//! - [`Parameter`]: Defines a tool parameter with type information and validation rules
15//! - [`ParameterType`]: Strongly-typed parameter types (String, Integer, Number, Boolean, Array, Object)
16//!
17//! ## MCP Types
18//!
19//! - [`Resource`]: MCP resource definition with URI, name, and metadata
20//! - [`ResourceContents`]: Content of a read resource (text or blob)
21//! - [`Prompt`]: MCP prompt definition with arguments
22//! - [`PromptMessage`]: Message in a rendered prompt
23//!
24//! ## Traits
25//!
26//! - [`Tool`]: Trait for implementing executable tools
27//! - [`Transport`]: Trait for implementing tool transport layers (e.g., MCP, HTTP, gRPC)
28//!
29//! ## Features
30//!
31//! - **Type Safety**: Compile-time and runtime validation of tool parameters
32//! - **Builder Pattern**: Ergonomic API for constructing tool definitions
33//! - **JSON Serialization**: Full serde support for all types
34//! - **MCP Integration**: Parse MCP JSON Schema to Thulp parameter definitions
35//! - **Async Support**: Built on tokio for efficient async execution
36//!
37//! ## Quick Start
38//!
39//! ### Defining a Tool
40//!
41//! ```rust
42//! use thulp_core::{ToolDefinition, Parameter, ParameterType};
43//!
44//! let tool = ToolDefinition::builder("read_file")
45//!     .description("Read contents of a file")
46//!     .parameter(
47//!         Parameter::builder("path")
48//!             .param_type(ParameterType::String)
49//!             .required(true)
50//!             .description("Path to the file to read")
51//!             .build()
52//!     )
53//!     .parameter(
54//!         Parameter::builder("encoding")
55//!             .param_type(ParameterType::String)
56//!             .default(serde_json::Value::String("utf-8".to_string()))
57//!             .description("File encoding")
58//!             .build()
59//!     )
60//!     .build();
61//!
62//! assert_eq!(tool.name, "read_file");
63//! assert_eq!(tool.parameters.len(), 2);
64//! ```
65//!
66//! ### Validating Tool Arguments
67//!
68//! ```rust
69//! use thulp_core::{ToolDefinition, Parameter, ParameterType};
70//! use serde_json::json;
71//!
72//! let tool = ToolDefinition::builder("add")
73//!     .description("Add two numbers")
74//!     .parameter(
75//!         Parameter::builder("a")
76//!             .param_type(ParameterType::Number)
77//!             .required(true)
78//!             .build()
79//!     )
80//!     .parameter(
81//!         Parameter::builder("b")
82//!             .param_type(ParameterType::Number)
83//!             .required(true)
84//!             .build()
85//!     )
86//!     .build();
87//!
88//! // Valid arguments
89//! let args = json!({"a": 5.0, "b": 3.0});
90//! assert!(tool.validate_args(&args).is_ok());
91//!
92//! // Invalid - missing required parameter
93//! let args = json!({"a": 5.0});
94//! assert!(tool.validate_args(&args).is_err());
95//!
96//! // Invalid - wrong type
97//! let args = json!({"a": "not a number", "b": 3.0});
98//! assert!(tool.validate_args(&args).is_err());
99//! ```
100//!
101//! ### Creating Tool Calls
102//!
103//! ```rust
104//! use thulp_core::ToolCall;
105//! use serde_json::json;
106//!
107//! let call = ToolCall::builder("search")
108//!     .arg("query", json!("rust programming"))
109//!     .arg("max_results", json!(10))
110//!     .build();
111//!
112//! assert_eq!(call.tool, "search");
113//! ```
114//!
115//! ### Parsing MCP JSON Schema
116//!
117//! ```rust
118//! use thulp_core::ToolDefinition;
119//! use serde_json::json;
120//!
121//! let schema = json!({
122//!     "type": "object",
123//!     "properties": {
124//!         "name": {
125//!             "type": "string",
126//!             "description": "User name"
127//!         },
128//!         "age": {
129//!             "type": "integer",
130//!             "description": "User age"
131//!         }
132//!     },
133//!     "required": ["name"]
134//! });
135//!
136//! let params = ToolDefinition::parse_mcp_input_schema(&schema).unwrap();
137//! assert_eq!(params.len(), 2);
138//! assert!(params.iter().find(|p| p.name == "name").unwrap().required);
139//! assert!(!params.iter().find(|p| p.name == "age").unwrap().required);
140//! ```
141//!
142//! ### Working with MCP Resources
143//!
144//! ```rust
145//! use thulp_core::{Resource, ResourceContents};
146//!
147//! let resource = Resource::builder("file:///docs/readme.md", "readme.md")
148//!     .title("Project README")
149//!     .mime_type("text/markdown")
150//!     .description("Main project documentation")
151//!     .build();
152//!
153//! let contents = ResourceContents::text("file:///docs/readme.md", "# Project\n...");
154//! ```
155//!
156//! ### Working with MCP Prompts
157//!
158//! ```rust
159//! use thulp_core::{Prompt, PromptArgument, PromptMessage, GetPromptResult};
160//!
161//! let prompt = Prompt::builder("code_review")
162//!     .title("Code Review")
163//!     .description("Review code for best practices")
164//!     .argument(PromptArgument::required("code", "Code to review"))
165//!     .build();
166//!
167//! let result = GetPromptResult::new(vec![
168//!     PromptMessage::user_text("Please review this code"),
169//! ]);
170//! ```
171//!
172//! ## Error Handling
173//!
174//! All fallible operations return [`Result<T, Error>`](Result), where [`Error`] provides
175//! detailed error information:
176//!
177//! ```rust
178//! use thulp_core::{ToolDefinition, Error};
179//! use serde_json::json;
180//!
181//! let tool = ToolDefinition::builder("test")
182//!     .parameter(
183//!         thulp_core::Parameter::builder("required_param")
184//!             .param_type(thulp_core::ParameterType::String)
185//!             .required(true)
186//!             .build()
187//!     )
188//!     .build();
189//!
190//! match tool.validate_args(&json!({})) {
191//!     Ok(_) => println!("Valid!"),
192//!     Err(Error::MissingParameter(name)) => {
193//!         eprintln!("Missing required parameter: {}", name);
194//!     }
195//!     Err(e) => eprintln!("Other error: {}", e),
196//! }
197//! ```
198
199mod error;
200mod mcp;
201mod parameter;
202mod tool;
203mod traits;
204
205pub use error::{Error, Result};
206pub use mcp::{
207    EmbeddedResource, GetPromptResult, Prompt, PromptArgument, PromptBuilder, PromptContent,
208    PromptListResult, PromptMessage, Resource, ResourceAnnotations, ResourceBuilder,
209    ResourceContents, ResourceListResult, ResourceTemplate, ResourceTemplateListResult,
210};
211pub use parameter::{Parameter, ParameterBuilder, ParameterType};
212pub use tool::{ToolCall, ToolCallBuilder, ToolDefinition, ToolDefinitionBuilder, ToolResult};
213pub use traits::{Tool, Transport};