ExecutionContext

Struct ExecutionContext 

Source
pub struct ExecutionContext<'a> {
    pub params: Value,
    pub uri_params: HashMap<String, String>,
    pub session: &'a Session,
    pub logger: &'a McpLogger,
    pub environment: Option<&'a dyn Environment>,
    /* private fields */
}
Expand description

Execution context passed to tools, resources, and prompts during execution

Provides access to:

  • Input parameters (for tools) or arguments (for prompts)
  • Session information (roles, state, client info)
  • Environment state (git status, filesystem)

§Example

impl Tool for MyTool {
    async fn execute(&self, ctx: ExecutionContext<'_>) -> Result<Vec<Box<dyn Content>>, ToolError> {
        // Access session info
        if ctx.is_admin() {
            // Admin-only behavior
        }

        // Access parameters
        let name = ctx.params.get("name").and_then(|v| v.as_str());

        // Access environment
        if let Some(env) = ctx.environment {
            if env.has_git_repo() {
                // Git-specific behavior
            }
        }

        Ok(vec![])
    }
}

Fields§

§params: Value

Input parameters (from tool call or prompt arguments)

§uri_params: HashMap<String, String>

URI parameters (extracted from resource URI template)

§session: &'a Session

Reference to the current session

§logger: &'a McpLogger

Logger for sending log notifications to the client

§environment: Option<&'a dyn Environment>

Optional environment state for contextual checks

Implementations§

Source§

impl<'a> ExecutionContext<'a>

Source

pub fn new(params: Value, session: &'a Session, logger: &'a McpLogger) -> Self

Create a new execution context

Source

pub fn with_environment( params: Value, session: &'a Session, logger: &'a McpLogger, environment: &'a dyn Environment, ) -> Self

Create an execution context with environment

Source

pub fn for_resource( uri_params: HashMap<String, String>, session: &'a Session, logger: &'a McpLogger, ) -> Self

Create an execution context for resources (with URI params)

Source

pub fn for_resource_with_environment( uri_params: HashMap<String, String>, session: &'a Session, logger: &'a McpLogger, environment: &'a dyn Environment, ) -> Self

Create a resource execution context with environment

Source

pub fn with_client_requester(self, requester: ClientRequester) -> Self

Set the client requester for server→client requests

Source

pub fn client_requester(&self) -> Option<&ClientRequester>

Get the client requester for making server→client requests

Returns None if the server hasn’t set up bidirectional communication.

Source

pub fn get_uri_param(&self, name: &str) -> Option<&str>

Get a URI parameter by name

Source

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

Get a session state value

Source

pub fn has_role(&self, role: &str) -> bool

Check if session has a specific role

Source

pub fn is_admin(&self) -> bool

Check if session is admin

Source

pub fn is_vip(&self) -> bool

Check if session is VIP

Source

pub fn session_id(&self) -> &str

Get session ID

Source

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

Get client name if available

Source

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

Get client version if available

Source

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

Get negotiated protocol version if available

Source

pub fn client_capabilities(&self) -> Option<&ClientCapabilities>

Get client capabilities if available

Source

pub fn as_visibility_context(&self) -> VisibilityContext<'a>

Convert to VisibilityContext (for visibility checks)

Auto Trait Implementations§

§

impl<'a> Freeze for ExecutionContext<'a>

§

impl<'a> !RefUnwindSafe for ExecutionContext<'a>

§

impl<'a> Send for ExecutionContext<'a>

§

impl<'a> Sync for ExecutionContext<'a>

§

impl<'a> Unpin for ExecutionContext<'a>

§

impl<'a> !UnwindSafe for ExecutionContext<'a>

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> 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, 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