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