pub struct Protocol { /* private fields */ }Expand description
Protocol state machine for handling JSON-RPC communication.
Implementations§
Source§impl Protocol
impl Protocol
Sourcepub fn new(options: ProtocolOptions) -> Self
pub fn new(options: ProtocolOptions) -> Self
Create a new protocol instance.
§Examples
use pmcp::shared::protocol::{Protocol, ProtocolOptions};
// Create with default options
let protocol = Protocol::new(ProtocolOptions::default());
// Create with custom options
let options = ProtocolOptions {
enforce_strict_capabilities: true,
debounced_notification_methods: vec!["progress".to_string()],
};
let protocol = Protocol::new(options);Sourcepub fn with_transport_id(
options: ProtocolOptions,
transport_id: TransportId,
) -> Self
pub fn with_transport_id( options: ProtocolOptions, transport_id: TransportId, ) -> Self
Create a new protocol instance with a specific transport ID.
§Examples
use pmcp::shared::protocol::{Protocol, ProtocolOptions, TransportId};
// Create with a specific transport ID
let transport_id = TransportId::from_string("websocket-1".to_string());
let protocol = Protocol::with_transport_id(
ProtocolOptions::default(),
transport_id.clone()
);
// Verify the transport ID is set
assert_eq!(protocol.transport_id(), &transport_id);Sourcepub fn options(&self) -> &ProtocolOptions
pub fn options(&self) -> &ProtocolOptions
Get protocol options.
Sourcepub fn transport_id(&self) -> &TransportId
pub fn transport_id(&self) -> &TransportId
Get the transport ID for this protocol instance.
Sourcepub fn register_request(&mut self, id: RequestId) -> Receiver<JSONRPCResponse> ⓘ
pub fn register_request(&mut self, id: RequestId) -> Receiver<JSONRPCResponse> ⓘ
Register a pending request.
Sourcepub fn complete_request(
&mut self,
id: &RequestId,
response: JSONRPCResponse,
) -> Result<()>
pub fn complete_request( &mut self, id: &RequestId, response: JSONRPCResponse, ) -> Result<()>
Complete a pending request. Only completes if the request was initiated by this transport.
Sourcepub fn complete_request_for_transport(
&mut self,
id: &RequestId,
response: JSONRPCResponse,
transport_id: &TransportId,
) -> Result<bool>
pub fn complete_request_for_transport( &mut self, id: &RequestId, response: JSONRPCResponse, transport_id: &TransportId, ) -> Result<bool>
Complete a pending request with transport verification. Only completes if the transport ID matches.
§Examples
use pmcp::shared::protocol::{Protocol, ProtocolOptions, TransportId};
use pmcp::types::{RequestId, JSONRPCResponse};
let transport_id = TransportId::new();
let mut protocol = Protocol::with_transport_id(
ProtocolOptions::default(),
transport_id.clone()
);
// Register a request
let request_id = RequestId::from("req-123");
let _rx = protocol.register_request(request_id.clone());
// Complete with matching transport ID - succeeds
let response = JSONRPCResponse::success(
request_id.clone(),
serde_json::json!("result")
);
let completed = protocol.complete_request_for_transport(
&request_id,
response.clone(),
&transport_id
)?;
assert!(completed);
// Complete with wrong transport ID - fails
let wrong_transport = TransportId::new();
let completed = protocol.complete_request_for_transport(
&request_id,
response,
&wrong_transport
)?;
assert!(!completed);Sourcepub fn cancel_request(&mut self, id: &RequestId)
pub fn cancel_request(&mut self, id: &RequestId)
Cancel a pending request.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Protocol
impl !UnwindSafe for Protocol
impl Freeze for Protocol
impl Send for Protocol
impl Sync for Protocol
impl Unpin for Protocol
impl UnsafeUnpin for Protocol
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