magi-openai 0.0.9

OpenAI compatible API SDK for Magi AI agents
Documentation
use rmcp::model::Tool as RmcpTool;

use crate::responses::request::{FunctionTool, Tool};

impl From<RmcpTool> for Tool {
    fn from(rmcp_tool: RmcpTool) -> Self {
        Tool::Function(FunctionTool {
            name: rmcp_tool.name.to_string(),
            description: rmcp_tool.description,
            parameters: Some(serde_json::to_value(&*rmcp_tool.input_schema).unwrap_or_default()),
            strict: None,
        })
    }
}

impl From<Tool> for RmcpTool {
    fn from(tool: Tool) -> Self {
        match tool {
            Tool::Function(function) => RmcpTool {
                name: function.name.clone().into(),
                description: function.description.clone(),
                input_schema: match function.parameters {
                    Some(params) => serde_json::from_value(params).unwrap_or_default(),
                    None => Default::default(),
                },
                annotations: None,
                icons: None,
                meta: None,
                output_schema: None,
                title: None,
            },
            // For non-function tools, create a placeholder tool
            _ => RmcpTool {
                name: "unsupported_tool".into(),
                description: None,
                input_schema: Default::default(),
                annotations: None,
                icons: None,
                meta: None,
                output_schema: None,
                title: None,
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::responses::request::FunctionTool;
    use serde_json::json;
    use std::borrow::Cow;

    #[test]
    fn converts_rmcp_tool_to_openai_tool() {
        let rmcp_tool = RmcpTool {
            name: "get_weather".into(),
            description: Some(Cow::from("Get the current weather")),
            input_schema: serde_json::from_value(json!({
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state"
                    }
                },
                "required": ["location"]
            }))
            .unwrap(),
            annotations: None,
            icons: None,
            output_schema: None,
            title: None,
            meta: None,
        };

        let openai_tool: Tool = rmcp_tool.into();
        match openai_tool {
            Tool::Function(function) => {
                assert_eq!(function.name, "get_weather");
                assert_eq!(
                    function.description,
                    Some(Cow::from("Get the current weather"))
                );
            }
            _ => panic!("Expected Function variant"),
        }
    }

    #[test]
    fn converts_openai_tool_to_rmcp_tool() {
        let openai_tool = Tool::Function(FunctionTool {
            name: "get_weather".into(),
            description: Some(Cow::from("Get the current weather")),
            parameters: Some(json!({
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state"
                    }
                },
                "required": ["location"]
            })),
            strict: None,
        });

        let rmcp_tool: RmcpTool = openai_tool.into();
        assert_eq!(rmcp_tool.name, "get_weather");
        assert_eq!(
            rmcp_tool.description,
            Some(Cow::from("Get the current weather"))
        );
    }
}