pub struct JSONRPCResponse<R = Value, E = JSONRPCError> {
pub jsonrpc: String,
pub id: RequestId,
pub payload: ResponsePayload<R, E>,
}Expand description
A JSON-RPC response.
§Examples
use pmcp::types::{JSONRPCResponse, RequestId, JSONRPCError};
use pmcp::types::jsonrpc::ResponsePayload;
use serde_json::json;
// Success response
let success: JSONRPCResponse<serde_json::Value, JSONRPCError> = JSONRPCResponse {
jsonrpc: "2.0".to_string(),
id: RequestId::from(1i64),
payload: ResponsePayload::Result(json!({
"tools": [
{"name": "calculator", "description": "Perform calculations"},
{"name": "weather", "description": "Get weather info"}
]
})),
};
// Error response
let error: JSONRPCResponse<serde_json::Value, JSONRPCError> = JSONRPCResponse {
jsonrpc: "2.0".to_string(),
id: RequestId::from("req-123".to_string()),
payload: ResponsePayload::Error(JSONRPCError {
code: -32602,
message: "Invalid parameters".to_string(),
data: Some(json!({"field": "name", "reason": "required"})),
}),
};
// Using constructors
let ping_response: JSONRPCResponse<serde_json::Value, JSONRPCError> = JSONRPCResponse::success(
RequestId::from(99i64),
json!({"status": "pong"})
);
let not_found: JSONRPCResponse<serde_json::Value, JSONRPCError> = JSONRPCResponse::error(
RequestId::from("missing".to_string()),
JSONRPCError {
code: -32601,
message: "Method not found".to_string(),
data: None,
}
);
// Check response types
assert!(success.is_success());
assert!(!success.is_error());
assert!(error.is_error());
assert!(!error.is_success());Fields§
§jsonrpc: StringMust be “2.0”
id: RequestIdRequest ID this response corresponds to
payload: ResponsePayload<R, E>Either result or error must be present
Implementations§
Source§impl<R, E> JSONRPCResponse<R, E>
impl<R, E> JSONRPCResponse<R, E>
Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Check if this is a successful response.
Trait Implementations§
Source§impl<R: Clone, E: Clone> Clone for JSONRPCResponse<R, E>
impl<R: Clone, E: Clone> Clone for JSONRPCResponse<R, E>
Source§fn clone(&self) -> JSONRPCResponse<R, E>
fn clone(&self) -> JSONRPCResponse<R, E>
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<'de, R, E> Deserialize<'de> for JSONRPCResponse<R, E>where
R: Deserialize<'de>,
E: Deserialize<'de>,
impl<'de, R, E> Deserialize<'de> for JSONRPCResponse<R, E>where
R: Deserialize<'de>,
E: Deserialize<'de>,
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
Auto Trait Implementations§
impl<R, E> Freeze for JSONRPCResponse<R, E>
impl<R, E> RefUnwindSafe for JSONRPCResponse<R, E>where
R: RefUnwindSafe,
E: RefUnwindSafe,
impl<R, E> Send for JSONRPCResponse<R, E>
impl<R, E> Sync for JSONRPCResponse<R, E>
impl<R, E> Unpin for JSONRPCResponse<R, E>
impl<R, E> UnsafeUnpin for JSONRPCResponse<R, E>where
R: UnsafeUnpin,
E: UnsafeUnpin,
impl<R, E> UnwindSafe for JSONRPCResponse<R, E>where
R: UnwindSafe,
E: UnwindSafe,
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