Skip to main content

Protocol

Struct Protocol 

Source
pub struct Protocol { /* private fields */ }
Expand description

Protocol state machine for handling JSON-RPC communication.

Implementations§

Source§

impl Protocol

Source

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);
Source

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);
Source

pub fn options(&self) -> &ProtocolOptions

Get protocol options.

Source

pub fn transport_id(&self) -> &TransportId

Get the transport ID for this protocol instance.

Source

pub fn register_request(&mut self, id: RequestId) -> Receiver<JSONRPCResponse>

Register a pending request.

Source

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.

Source

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);
Source

pub fn cancel_request(&mut self, id: &RequestId)

Cancel a pending request.

Trait Implementations§

Source§

impl Debug for Protocol

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more