use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Deserialize)]
pub struct McpRequest {
pub jsonrpc: String,
pub id: Value,
pub method: String,
#[serde(default)]
pub params: Option<Value>,
}
#[derive(Debug, Serialize)]
pub struct McpResponse {
pub jsonrpc: String,
pub id: Value,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<McpError>,
}
impl McpResponse {
pub fn success(id: Value, result: Value) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id,
result: Some(result),
error: None,
}
}
pub fn error(id: Value, error: McpError) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id,
result: None,
error: Some(error),
}
}
}
#[derive(Debug, Serialize)]
pub struct McpError {
pub code: i32,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}
impl McpError {
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;
pub fn parse_error(msg: impl Into<String>) -> Self {
Self {
code: Self::PARSE_ERROR,
message: msg.into(),
data: None,
}
}
pub fn method_not_found(method: &str) -> Self {
Self {
code: Self::METHOD_NOT_FOUND,
message: format!("Method not found: {}", method),
data: None,
}
}
pub fn invalid_params(msg: impl Into<String>) -> Self {
Self {
code: Self::INVALID_PARAMS,
message: msg.into(),
data: None,
}
}
pub fn internal_error(msg: impl Into<String>) -> Self {
Self {
code: Self::INTERNAL_ERROR,
message: msg.into(),
data: None,
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct McpTool {
pub name: String,
pub description: String,
#[serde(rename = "inputSchema")]
pub input_schema: Value,
}
#[derive(Debug, Clone, Serialize)]
pub struct McpResource {
pub uri: String,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
pub mime_type: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct McpResourceContents {
pub uri: String,
#[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
pub mime_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub text: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub blob: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct McpToolContent {
#[serde(rename = "type")]
pub content_type: String,
pub text: String,
}
impl McpToolContent {
pub fn text(text: impl Into<String>) -> Self {
Self {
content_type: "text".to_string(),
text: text.into(),
}
}
}
#[derive(Debug, Clone, Serialize, Default)]
pub struct McpCapabilities {
#[serde(skip_serializing_if = "Option::is_none")]
pub tools: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub resources: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub prompts: Option<Value>,
}
#[derive(Debug, Clone, Serialize)]
pub struct McpServerInfo {
pub name: String,
pub version: String,
}