Session

Struct Session 

Source
pub struct Session {
    pub id: String,
    pub client_info: Option<Implementation>,
    pub capabilities: Option<ClientCapabilities>,
    pub protocol_version: Option<String>,
    /* private fields */
}
Expand description

Session represents a single MCP client connection

Fields§

§id: String

Unique session ID

§client_info: Option<Implementation>

Client information (set during initialization)

§capabilities: Option<ClientCapabilities>

Client capabilities (set during initialization)

§protocol_version: Option<String>

Negotiated protocol version (set during initialization)

Implementations§

Source§

impl Session

Source

pub fn new() -> Self

Create new session with random UUID

Source

pub fn with_id(id: impl Into<String>) -> Self

Create session with specific ID

Source

pub fn initialize( &mut self, client_info: Implementation, capabilities: ClientCapabilities, protocol_version: String, )

Initialize session with client info and capabilities Transitions: Created -> Ready

Source

pub fn set_notification_channel( &mut self, tx: UnboundedSender<JsonRpcNotification>, )

Set notification channel for this session

Source

pub fn is_initialized(&self) -> bool

Check if session is initialized (Ready or Degraded)

Source

pub fn protocol_version(&self) -> Option<&str>

Get the negotiated protocol version

Source

pub fn record_error(&mut self)

Record an error, potentially transitioning to Degraded state Transitions: Ready -> Degraded (after threshold errors)

Source

pub fn record_success(&mut self)

Record success, potentially recovering from Degraded state Transitions: Degraded -> Ready (resets error count)

Source

pub fn close(&mut self)

Close the session Transitions: Any -> Closed

Source

pub fn lifecycle(&self) -> SessionLifecycle

Get current lifecycle state

Source

pub fn error_count(&self) -> u32

Get current error count

Source

pub fn get_state(&self, key: &str) -> Option<Value>

Get state value

Source

pub fn set_state(&self, key: impl Into<String>, value: Value)

Set state value

Source

pub fn remove_state(&self, key: &str) -> Option<Value>

Remove state value

Source

pub fn clear_state(&self)

Clear all state

Source

pub fn state_keys(&self) -> Vec<String>

Get all state keys

Source

pub fn add_tool(&self, tool: Arc<dyn Tool>)

Add an extra tool to this session

Source

pub fn override_tool(&self, name: impl Into<String>, tool: Arc<dyn Tool>)

Override a tool (same name, different implementation)

Source

pub fn hide_tool(&self, name: impl Into<String>, notify: bool)

Hide a tool from this session

If notify is true, sends a notifications/tools/list_changed notification.

Source

pub fn unhide_tool(&self, name: &str, notify: bool)

Unhide a tool

If notify is true, sends a notifications/tools/list_changed notification.

Source

pub fn alias_tool(&self, alias: impl Into<String>, target: impl Into<String>)

Add a tool alias (alias -> target)

Source

pub fn remove_tool_alias(&self, alias: &str)

Remove a tool alias

Source

pub fn is_tool_hidden(&self, name: &str) -> bool

Check if a tool is hidden

Source

pub fn resolve_tool_alias<'a>(&self, name: &'a str) -> Cow<'a, str>

Resolve a tool alias to its target name

Source

pub fn get_tool_override(&self, name: &str) -> Option<Arc<dyn Tool>>

Get a tool override for this session

Source

pub fn get_tool_extra(&self, name: &str) -> Option<Arc<dyn Tool>>

Get an extra tool added to this session

Source

pub fn tool_overrides(&self) -> &Arc<DashMap<String, Arc<dyn Tool>>>

Get all tool overrides

Source

pub fn tool_extras(&self) -> &Arc<DashMap<String, Arc<dyn Tool>>>

Get all extra tools

Source

pub fn remove_tool(&self, name: &str) -> Option<Arc<dyn Tool>>

Remove an extra tool from this session

Source

pub fn remove_tool_override(&self, name: &str) -> Option<Arc<dyn Tool>>

Remove a tool override

Source

pub fn add_resource(&self, resource: Arc<dyn Resource>)

Add an extra resource to this session

Source

pub fn override_resource( &self, uri: impl Into<String>, resource: Arc<dyn Resource>, )

Override a resource (same URI, different implementation)

Source

pub fn hide_resource(&self, uri: impl Into<String>)

Hide a resource from this session

Source

pub fn unhide_resource(&self, uri: &str)

Unhide a resource

Source

pub fn is_resource_hidden(&self, uri: &str) -> bool

Check if a resource is hidden

Source

pub fn get_resource_override(&self, uri: &str) -> Option<Arc<dyn Resource>>

Get a resource override for this session

Source

pub fn get_resource_extra(&self, uri: &str) -> Option<Arc<dyn Resource>>

Get an extra resource added to this session

Source

pub fn resource_overrides(&self) -> &Arc<DashMap<String, Arc<dyn Resource>>>

Get all resource overrides

Source

pub fn resource_extras(&self) -> &Arc<DashMap<String, Arc<dyn Resource>>>

Get all extra resources

Source

pub fn remove_resource(&self, uri: &str) -> Option<Arc<dyn Resource>>

Remove an extra resource from this session

Source

pub fn remove_resource_override(&self, uri: &str) -> Option<Arc<dyn Resource>>

Remove a resource override

Source

pub fn add_prompt(&self, prompt: Arc<dyn Prompt>)

Add an extra prompt to this session

Source

pub fn override_prompt(&self, name: impl Into<String>, prompt: Arc<dyn Prompt>)

Override a prompt (same name, different implementation)

Source

pub fn hide_prompt(&self, name: impl Into<String>)

Hide a prompt from this session

Source

pub fn unhide_prompt(&self, name: &str)

Unhide a prompt

Source

pub fn is_prompt_hidden(&self, name: &str) -> bool

Check if a prompt is hidden

Source

pub fn get_prompt_override(&self, name: &str) -> Option<Arc<dyn Prompt>>

Get a prompt override for this session

Source

pub fn get_prompt_extra(&self, name: &str) -> Option<Arc<dyn Prompt>>

Get an extra prompt added to this session

Source

pub fn prompt_overrides(&self) -> &Arc<DashMap<String, Arc<dyn Prompt>>>

Get all prompt overrides

Source

pub fn prompt_extras(&self) -> &Arc<DashMap<String, Arc<dyn Prompt>>>

Get all extra prompts

Source

pub fn remove_prompt(&self, name: &str) -> Option<Arc<dyn Prompt>>

Remove an extra prompt from this session

Source

pub fn remove_prompt_override(&self, name: &str) -> Option<Arc<dyn Prompt>>

Remove a prompt override

Source

pub fn batch<F>(&self, f: F)
where F: FnOnce(&mut SessionBatch<'_>),

Perform batch operations without sending notifications until complete

This allows you to make multiple changes and send a single notification.

§Example
session.batch(|batch| {
    batch.add_tool(Arc::new(Tool1));
    batch.add_tool(Arc::new(Tool2));
    batch.remove_resource("old://resource");
    batch.hide_prompt("deprecated");
});
// Single notification sent after closure completes
Source

pub fn apply_profile(&self, profile: &SessionProfile)

Apply a session profile

This adds/overrides/hides tools, resources, and prompts according to the profile

Source

pub fn clear_customizations(&self)

Clear all session customizations

Trait Implementations§

Source§

impl Clone for Session

Source§

fn clone(&self) -> Session

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Session

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Session

Source§

fn default() -> Self

Returns the “default value” for a type. 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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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