Skip to main content

objectiveai_sdk/mcp/
json_rpc.rs

1//! JSON-RPC 2.0 envelope types used by the MCP transport.
2
3use schemars::JsonSchema;
4
5/// JSON-RPC 2.0 inbound request.
6#[derive(Debug, serde::Deserialize, JsonSchema)]
7#[schemars(rename = "mcp.JsonRpcRequest")]
8pub struct JsonRpcRequest {
9    pub jsonrpc: String,
10    pub id: serde_json::Value,
11    pub method: String,
12    #[serde(default)]
13    pub params: Option<serde_json::Value>,
14}
15
16/// JSON-RPC 2.0 response envelope.
17#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
18#[serde(untagged)]
19#[schemars(rename = "mcp.JsonRpcResponse.{T}", bound = "T: JsonSchema")]
20pub enum JsonRpcResponse<T> {
21    Success {
22        jsonrpc: String,
23        id: serde_json::Value,
24        result: T,
25    },
26    Error {
27        jsonrpc: String,
28        id: serde_json::Value,
29        error: JsonRpcError,
30    },
31}
32
33/// JSON-RPC 2.0 error object.
34#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
35#[schemars(rename = "mcp.JsonRpcError")]
36pub struct JsonRpcError {
37    pub code: i64,
38    pub message: String,
39    #[serde(skip_serializing_if = "Option::is_none")]
40    #[schemars(extend("omitempty" = true))]
41    pub data: Option<serde_json::Value>,
42}
43
44/// JSON-RPC 2.0 notification (no `id` field).
45#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, JsonSchema)]
46#[schemars(rename = "mcp.JsonRpcNotification")]
47pub struct JsonRpcNotification {
48    pub jsonrpc: String,
49    pub method: String,
50    #[serde(default, skip_serializing_if = "Option::is_none")]
51    #[schemars(extend("omitempty" = true))]
52    pub params: Option<serde_json::Value>,
53}