pub struct JsonRpcRequest {
pub jsonrpc: String,
pub method: String,
pub params: Option<Value>,
pub id: RequestId,
}Expand description
JSON-RPC 2.0 Request Message
Represents a request to invoke a method on the remote peer. All fields are required except for params, which may be omitted if the method takes no parameters.
§JSON-RPC 2.0 Specification Compliance
jsonrpc: MUST be exactly “2.0”method: MUST be a String containing the name of the method to invokeparams: MAY be omitted. If present, MUST be Structured values (Object) or Ordered values (Array)id: MUST be a String, Number, or NULL value
§Examples
use airsprotocols_mcp::protocol::{JsonRpcRequest, JsonRpcMessageTrait, RequestId};
use serde_json::json;
// Request with parameters
let request = JsonRpcRequest::new(
"subtract",
Some(json!([42, 23])),
RequestId::new_number(1)
);
// Use trait methods for serialization
let json = request.to_json().unwrap();
let parsed = JsonRpcRequest::from_json(&json).unwrap();
assert_eq!(request, parsed);Fields§
§jsonrpc: StringProtocol version - always “2.0” for JSON-RPC 2.0 compliance
method: StringName of the method to invoke
params: Option<Value>Parameters for the method (null, object, or array)
id: RequestIdUnique identifier for this request
Implementations§
Source§impl JsonRpcRequest
impl JsonRpcRequest
Sourcepub fn new(
method: impl Into<String>,
params: Option<Value>,
id: RequestId,
) -> Self
pub fn new( method: impl Into<String>, params: Option<Value>, id: RequestId, ) -> Self
Create a new JSON-RPC 2.0 request
§Parameters
method: Name of the method to invokeparams: Optional parameters (will be serialized as JSON)id: Unique request identifier
§Examples
use airsprotocols_mcp::protocol::{JsonRpcRequest, RequestId};
use serde_json::json;
let request = JsonRpcRequest::new(
"calculate",
Some(json!({"operation": "add", "values": [1, 2, 3]})),
RequestId::new_string("calc-123")
);Sourcepub fn new_validated(
method: impl Into<String>,
params: Option<Value>,
id: RequestId,
) -> Result<Self, JsonRpcError>
pub fn new_validated( method: impl Into<String>, params: Option<Value>, id: RequestId, ) -> Result<Self, JsonRpcError>
Create and validate a new JSON-RPC 2.0 request
This function performs validation during construction to catch invalid requests early.
§Parameters
method: Name of the method to invoke (must be non-empty)params: Optional parameters (will be serialized as JSON)id: Unique request identifier
§Returns
Ok(JsonRpcRequest)- Valid request created successfullyErr(JsonRpcError)- Request violates JSON-RPC 2.0 specification
§Examples
use airsprotocols_mcp::protocol::{JsonRpcRequest, RequestId};
use serde_json::json;
let request = JsonRpcRequest::new_validated(
"calculate",
Some(json!({"operation": "add", "values": [1, 2, 3]})),
RequestId::new_string("calc-123")
)?;Trait Implementations§
Source§impl Clone for JsonRpcRequest
impl Clone for JsonRpcRequest
Source§fn clone(&self) -> JsonRpcRequest
fn clone(&self) -> JsonRpcRequest
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for JsonRpcRequest
impl Debug for JsonRpcRequest
Source§impl<'de> Deserialize<'de> for JsonRpcRequest
impl<'de> Deserialize<'de> for JsonRpcRequest
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl JsonRpcMessageTrait for JsonRpcRequest
impl JsonRpcMessageTrait for JsonRpcRequest
Source§impl PartialEq for JsonRpcRequest
impl PartialEq for JsonRpcRequest
Source§impl Serialize for JsonRpcRequest
impl Serialize for JsonRpcRequest
impl StructuralPartialEq for JsonRpcRequest
Auto Trait Implementations§
impl Freeze for JsonRpcRequest
impl RefUnwindSafe for JsonRpcRequest
impl Send for JsonRpcRequest
impl Sync for JsonRpcRequest
impl Unpin for JsonRpcRequest
impl UnwindSafe for JsonRpcRequest
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more