use rmcp::model::JsonObject;
use serde_json::Value;
use std::sync::Arc;
use crate::core::ApiMetadata;
use crate::mcp::SdForgeTool;
#[derive(Clone)]
pub struct McpToolInstance {
pub(crate) tool: Arc<dyn SdForgeTool>,
pub(crate) metadata: ApiMetadata,
}
impl McpToolInstance {
pub fn new(tool: Arc<dyn SdForgeTool>, metadata: ApiMetadata) -> Self {
Self { tool, metadata }
}
pub fn tool(&self) -> &Arc<dyn SdForgeTool> {
&self.tool
}
pub fn metadata(&self) -> &ApiMetadata {
&self.metadata
}
}
pub(crate) fn value_to_json_object_arc(value: Value) -> Arc<JsonObject> {
match value {
Value::Object(map) => Arc::new(map),
_ => Arc::new(serde_json::Map::new()),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_value_to_json_object_arc_with_object() {
let map = serde_json::json!({
"type": "object",
"properties": {
"name": {"type": "string"}
}
});
let arc = value_to_json_object_arc(map);
assert_eq!(arc.len(), 2);
assert!(arc.contains_key("type"));
assert!(arc.contains_key("properties"));
}
#[test]
fn test_value_to_json_object_arc_with_empty_object() {
let arc = value_to_json_object_arc(serde_json::json!({}));
assert!(arc.is_empty());
assert_eq!(arc.len(), 0);
}
#[test]
fn test_value_to_json_object_arc_with_string_falls_back_to_empty() {
let arc = value_to_json_object_arc(serde_json::json!("not an object"));
assert!(arc.is_empty());
}
#[test]
fn test_value_to_json_object_arc_with_number_falls_back_to_empty() {
let arc = value_to_json_object_arc(serde_json::json!(42));
assert!(arc.is_empty());
}
#[test]
fn test_value_to_json_object_arc_with_array_falls_back_to_empty() {
let arc = value_to_json_object_arc(serde_json::json!([1, 2, 3]));
assert!(arc.is_empty());
}
#[test]
fn test_value_to_json_object_arc_with_null_falls_back_to_empty() {
let arc = value_to_json_object_arc(serde_json::Value::Null);
assert!(arc.is_empty());
}
#[test]
fn test_value_to_json_object_arc_with_bool_falls_back_to_empty() {
let arc = value_to_json_object_arc(serde_json::json!(true));
assert!(arc.is_empty());
}
#[test]
fn test_value_to_json_object_arc_nested_object() {
let nested = serde_json::json!({
"level1": {"level2": "value"}
});
let arc = value_to_json_object_arc(nested);
assert_eq!(arc.len(), 1);
let level1 = arc.get("level1").expect("level1 key should exist");
assert!(level1.is_object());
assert_eq!(level1["level2"], "value");
}
}