pub trait ResourceHandler: Send + Sync {
// Required methods
fn definition(&self) -> Resource;
fn read(&self, ctx: &McpContext) -> McpResult<Vec<ResourceContent>>;
// Provided methods
fn template(&self) -> Option<ResourceTemplate> { ... }
fn icon(&self) -> Option<&Icon> { ... }
fn version(&self) -> Option<&str> { ... }
fn tags(&self) -> &[String] { ... }
fn timeout(&self) -> Option<Duration> { ... }
fn read_with_uri(
&self,
ctx: &McpContext,
_uri: &str,
_params: &HashMap<String, String>,
) -> McpResult<Vec<ResourceContent>> { ... }
fn read_async_with_uri<'a>(
&'a self,
ctx: &'a McpContext,
uri: &'a str,
params: &'a HashMap<String, String>,
) -> BoxFuture<'a, McpOutcome<Vec<ResourceContent>>> { ... }
fn read_async<'a>(
&'a self,
ctx: &'a McpContext,
) -> BoxFuture<'a, McpOutcome<Vec<ResourceContent>>> { ... }
}Expand description
Handler for a resource.
This trait is typically implemented via the #[resource] macro.
§Sync vs Async
By default, implement read() for synchronous execution. For async resources,
override read_async() instead. The router uses read_async_with_uri() so
implementations can access matched URI parameters when needed.
which defaults to running read() in an async block.
§Return Type
Async handlers return McpOutcome<Vec<ResourceContent>>, a 4-valued type.
Required Methods§
Sourcefn definition(&self) -> Resource
fn definition(&self) -> Resource
Returns the resource definition.
Sourcefn read(&self, ctx: &McpContext) -> McpResult<Vec<ResourceContent>>
fn read(&self, ctx: &McpContext) -> McpResult<Vec<ResourceContent>>
Reads the resource content synchronously.
This is the default implementation point. Override this for simple
synchronous resources. Returns McpResult which is converted to McpOutcome
by the async wrapper.
Provided Methods§
Sourcefn template(&self) -> Option<ResourceTemplate>
fn template(&self) -> Option<ResourceTemplate>
Returns the resource template definition, if this resource uses a URI template.
Sourcefn icon(&self) -> Option<&Icon>
fn icon(&self) -> Option<&Icon>
Returns the resource’s icon, if any.
Default implementation returns None. Override to provide an icon.
Note: Icons can also be set directly in definition().
Sourcefn version(&self) -> Option<&str>
fn version(&self) -> Option<&str>
Returns the resource’s version, if any.
Default implementation returns None. Override to provide a version.
Note: Version can also be set directly in definition().
Returns the resource’s tags for filtering and organization.
Default implementation returns an empty slice. Override to provide tags.
Note: Tags can also be set directly in definition().
Sourcefn timeout(&self) -> Option<Duration>
fn timeout(&self) -> Option<Duration>
Returns the resource’s custom timeout duration.
Default implementation returns None, meaning the server’s default
timeout applies. Override to specify a per-handler timeout.
Sourcefn read_with_uri(
&self,
ctx: &McpContext,
_uri: &str,
_params: &HashMap<String, String>,
) -> McpResult<Vec<ResourceContent>>
fn read_with_uri( &self, ctx: &McpContext, _uri: &str, _params: &HashMap<String, String>, ) -> McpResult<Vec<ResourceContent>>
Reads the resource content synchronously with the matched URI and parameters.
Default implementation ignores URI params and delegates to read().
Sourcefn read_async_with_uri<'a>(
&'a self,
ctx: &'a McpContext,
uri: &'a str,
params: &'a HashMap<String, String>,
) -> BoxFuture<'a, McpOutcome<Vec<ResourceContent>>>
fn read_async_with_uri<'a>( &'a self, ctx: &'a McpContext, uri: &'a str, params: &'a HashMap<String, String>, ) -> BoxFuture<'a, McpOutcome<Vec<ResourceContent>>>
Reads the resource content asynchronously with the matched URI and parameters.
Default implementation delegates to the sync read_with_uri() method.
Sourcefn read_async<'a>(
&'a self,
ctx: &'a McpContext,
) -> BoxFuture<'a, McpOutcome<Vec<ResourceContent>>>
fn read_async<'a>( &'a self, ctx: &'a McpContext, ) -> BoxFuture<'a, McpOutcome<Vec<ResourceContent>>>
Reads the resource content asynchronously.
Override this for resources that need true async execution (e.g., file I/O, database queries, remote fetches).
Returns McpOutcome to properly represent all four states.
The default implementation delegates to the sync read() method.