use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
pub const JSONRPC_VERSION: &str = "2.0";
pub const MCP_VERSION: &str = "2024-11-05";
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcRequest {
pub jsonrpc: String,
pub id: RequestId,
pub method: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub params: Option<Value>,
}
impl JsonRpcRequest {
pub fn new(method: impl Into<String>, params: Option<Value>) -> Self {
Self {
jsonrpc: JSONRPC_VERSION.to_string(),
id: RequestId::Number(rand::random()),
method: method.into(),
params,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcResponse {
pub jsonrpc: String,
pub id: RequestId,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<JsonRpcError>,
}
impl JsonRpcResponse {
pub fn success(id: RequestId, result: Value) -> Self {
Self {
jsonrpc: JSONRPC_VERSION.to_string(),
id,
result: Some(result),
error: None,
}
}
pub fn error(id: RequestId, error: JsonRpcError) -> Self {
Self {
jsonrpc: JSONRPC_VERSION.to_string(),
id,
result: None,
error: Some(error),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(untagged)]
pub enum RequestId {
Number(i64),
String(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>,
}
pub mod error_codes {
pub const PARSE_ERROR: i32 = -32700;
pub const INVALID_REQUEST: i32 = -32600;
pub const METHOD_NOT_FOUND: i32 = -32601;
pub const INVALID_PARAMS: i32 = -32602;
pub const INTERNAL_ERROR: i32 = -32603;
}
impl JsonRpcError {
pub fn parse_error() -> Self {
Self {
code: error_codes::PARSE_ERROR,
message: "Parse error".to_string(),
data: None,
}
}
pub fn invalid_request(msg: impl Into<String>) -> Self {
Self {
code: error_codes::INVALID_REQUEST,
message: msg.into(),
data: None,
}
}
pub fn method_not_found(method: &str) -> Self {
Self {
code: error_codes::METHOD_NOT_FOUND,
message: format!("Method not found: {}", method),
data: None,
}
}
pub fn invalid_params(msg: impl Into<String>) -> Self {
Self {
code: error_codes::INVALID_PARAMS,
message: msg.into(),
data: None,
}
}
pub fn internal_error(msg: impl Into<String>) -> Self {
Self {
code: error_codes::INTERNAL_ERROR,
message: msg.into(),
data: None,
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ServerCapabilities {
#[serde(skip_serializing_if = "Option::is_none")]
pub tools: Option<ToolsCapability>,
#[serde(skip_serializing_if = "Option::is_none")]
pub resources: Option<ResourcesCapability>,
#[serde(skip_serializing_if = "Option::is_none")]
pub prompts: Option<PromptsCapability>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ToolsCapability {
#[serde(default)]
pub list_changed: bool,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ResourcesCapability {
#[serde(default)]
pub subscribe: bool,
#[serde(default)]
pub list_changed: bool,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PromptsCapability {
#[serde(default)]
pub list_changed: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerInfo {
pub name: String,
pub version: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InitializeResult {
pub protocol_version: String,
pub capabilities: ServerCapabilities,
pub server_info: ServerInfo,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tool {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub input_schema: InputSchema,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InputSchema {
#[serde(rename = "type")]
pub schema_type: String,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub properties: HashMap<String, PropertySchema>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub required: Vec<String>,
}
impl InputSchema {
pub fn object() -> Self {
Self {
schema_type: "object".to_string(),
properties: HashMap::new(),
required: Vec::new(),
}
}
pub fn with_property(mut self, name: impl Into<String>, schema: PropertySchema) -> Self {
self.properties.insert(name.into(), schema);
self
}
pub fn with_required(mut self, name: impl Into<String>, schema: PropertySchema) -> Self {
let name = name.into();
self.required.push(name.clone());
self.properties.insert(name, schema);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PropertySchema {
#[serde(rename = "type")]
pub schema_type: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub default: Option<Value>,
#[serde(rename = "enum", skip_serializing_if = "Option::is_none")]
pub enum_values: Option<Vec<String>>,
}
impl PropertySchema {
pub fn string(description: impl Into<String>) -> Self {
Self {
schema_type: "string".to_string(),
description: Some(description.into()),
default: None,
enum_values: None,
}
}
pub fn number(description: impl Into<String>) -> Self {
Self {
schema_type: "number".to_string(),
description: Some(description.into()),
default: None,
enum_values: None,
}
}
pub fn boolean(description: impl Into<String>) -> Self {
Self {
schema_type: "boolean".to_string(),
description: Some(description.into()),
default: None,
enum_values: None,
}
}
pub fn array(description: impl Into<String>) -> Self {
Self {
schema_type: "array".to_string(),
description: Some(description.into()),
default: None,
enum_values: None,
}
}
pub fn with_default(mut self, value: Value) -> Self {
self.default = Some(value);
self
}
pub fn with_enum(mut self, values: Vec<&str>) -> Self {
self.enum_values = Some(values.into_iter().map(|s| s.to_string()).collect());
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CallToolRequest {
pub name: String,
#[serde(default)]
pub arguments: HashMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CallToolResult {
pub content: Vec<Content>,
#[serde(default)]
pub is_error: bool,
}
impl CallToolResult {
pub fn text(text: impl Into<String>) -> Self {
Self {
content: vec![Content::text(text)],
is_error: false,
}
}
pub fn error(message: impl Into<String>) -> Self {
Self {
content: vec![Content::text(message)],
is_error: true,
}
}
pub fn json(value: Value) -> Self {
Self {
content: vec![Content::text(
serde_json::to_string_pretty(&value).unwrap_or_default(),
)],
is_error: false,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Content {
#[serde(rename = "text")]
Text { text: String },
#[serde(rename = "image")]
Image { data: String, mime_type: String },
#[serde(rename = "resource")]
Resource { resource: ResourceContent },
}
impl Content {
pub fn text(text: impl Into<String>) -> Self {
Self::Text { text: text.into() }
}
pub fn image(data: String, mime_type: impl Into<String>) -> Self {
Self::Image {
data,
mime_type: mime_type.into(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceContent {
pub uri: String,
pub mime_type: Option<String>,
pub text: Option<String>,
pub blob: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Resource {
pub uri: String,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub mime_type: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Prompt {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub arguments: Vec<PromptArgument>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptArgument {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default)]
pub required: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Notification {
pub jsonrpc: String,
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<Value>,
}
impl Notification {
pub fn new(method: impl Into<String>, params: Option<Value>) -> Self {
Self {
jsonrpc: JSONRPC_VERSION.to_string(),
method: method.into(),
params,
}
}
pub fn tools_list_changed() -> Self {
Self::new("notifications/tools/list_changed", None)
}
pub fn resources_list_changed() -> Self {
Self::new("notifications/resources/list_changed", None)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_json_rpc_request() {
let req = JsonRpcRequest::new("test_method", None);
assert_eq!(req.jsonrpc, JSONRPC_VERSION);
assert_eq!(req.method, "test_method");
}
#[test]
fn test_input_schema() {
let schema = InputSchema::object()
.with_required("content", PropertySchema::string("The content"))
.with_property(
"domain",
PropertySchema::string("Domain").with_enum(vec!["Code", "Docs"]),
);
assert_eq!(schema.schema_type, "object");
assert!(schema.required.contains(&"content".to_string()));
assert!(schema.properties.contains_key("domain"));
}
#[test]
fn test_tool_result() {
let result = CallToolResult::text("Success");
assert!(!result.is_error);
assert_eq!(result.content.len(), 1);
}
}