use serde::{Deserialize, Serialize};
use serde_json::Value;
use super::capabilities::ServerCapabilities;
use super::error::ToolDefinition;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerInfo {
pub name: String,
pub version: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InitializeResponse {
#[serde(rename = "protocolVersion")]
pub protocol_version: String,
pub capabilities: ServerCapabilities,
#[serde(rename = "serverInfo")]
pub server_info: ServerInfo,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolsListResponse {
pub tools: Vec<ToolDefinition>,
#[serde(rename = "nextCursor", skip_serializing_if = "Option::is_none")]
pub next_cursor: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolsCallResponse {
pub content: Vec<ToolContent>,
#[serde(rename = "isError", skip_serializing_if = "Option::is_none")]
pub is_error: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolContent {
#[serde(rename = "type")]
pub content_type: String,
pub text: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcError {
pub code: i32,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}
impl JsonRpcError {
pub fn parse_error(msg: String) -> Self {
Self {
code: -32700,
message: msg,
data: None,
}
}
pub fn invalid_request(msg: String) -> Self {
Self {
code: -32600,
message: msg,
data: None,
}
}
pub fn method_not_found(method: &str) -> Self {
Self {
code: -32601,
message: format!("Method not found: {}", method),
data: None,
}
}
pub fn invalid_params(msg: String) -> Self {
Self {
code: -32602,
message: msg,
data: None,
}
}
pub fn internal_error(msg: String) -> Self {
Self {
code: -32603,
message: msg,
data: None,
}
}
pub fn tool_not_found(tool: &str) -> Self {
Self {
code: -32803,
message: format!("Tool not found: {}", tool),
data: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_json_rpc_error_codes() {
assert_eq!(JsonRpcError::parse_error("x".into()).code, -32700);
assert_eq!(JsonRpcError::invalid_request("x".into()).code, -32600);
assert_eq!(JsonRpcError::method_not_found("x").code, -32601);
assert_eq!(JsonRpcError::invalid_params("x".into()).code, -32602);
assert_eq!(JsonRpcError::internal_error("x".into()).code, -32603);
assert_eq!(JsonRpcError::tool_not_found("x").code, -32803);
}
#[test]
fn test_initialize_response_serialization() {
let resp = InitializeResponse {
protocol_version: "2024-11-05".into(),
capabilities: ServerCapabilities::default(),
server_info: ServerInfo {
name: "agentic-reality".into(),
version: "0.1.0".into(),
},
};
let json = serde_json::to_value(&resp);
assert!(json.is_ok());
}
#[test]
fn test_tools_list_response() {
let resp = ToolsListResponse {
tools: vec![],
next_cursor: None,
};
let json = serde_json::to_value(&resp);
assert!(json.is_ok());
}
#[test]
fn test_tools_call_response() {
let resp = ToolsCallResponse {
content: vec![ToolContent {
content_type: "text".into(),
text: "hello".into(),
}],
is_error: None,
};
let json = serde_json::to_value(&resp);
assert!(json.is_ok());
}
}