pub struct ResourceRegistry { /* private fields */ }Expand description
A registry of resources and resource templates for dynamic dispatch.
Models MCP’s resources/list, resources/read, and resources/templates/list
primitives. Tools provide actionable commands; resources provide readable
context (static documents, log files, configuration snapshots).
§Example
use llm_tool::{ResourceOutput, ResourceRegistry, RustResource, ToolError};
struct ConfigResource;
impl RustResource for ConfigResource {
const NAME: &'static str = "config";
const URI_TEMPLATE: &'static str = "file:///etc/app.conf";
const DESCRIPTION: &'static str = "Application configuration";
const MIME_TYPE: Option<&'static str> = Some("text/plain");
type Params = ();
async fn read(
&self,
uri: &str,
_params: Self::Params,
) -> Result<ResourceOutput, ToolError> {
Ok(ResourceOutput::text(uri, Some("text/plain"), "debug=true"))
}
}
let mut reg = ResourceRegistry::new();
reg.register(ConfigResource);
assert_eq!(reg.len(), 1);
assert!(reg.matches("file:///etc/app.conf"));Implementations§
Source§impl ResourceRegistry
impl ResourceRegistry
Sourcepub fn register<R: RustResource + 'static>(&mut self, resource: R) -> &mut Self
pub fn register<R: RustResource + 'static>(&mut self, resource: R) -> &mut Self
Register a RustResource.
Replaces any existing registration with the same RustResource::NAME.
Sourcepub fn with_resource<R: RustResource + 'static>(self, resource: R) -> Self
pub fn with_resource<R: RustResource + 'static>(self, resource: R) -> Self
Register a RustResource, consuming and returning Self for chaining.
Sourcepub fn remove(&mut self, name: &str) -> bool
pub fn remove(&mut self, name: &str) -> bool
Remove a resource by name, returning true if it was present.
Sourcepub fn definitions(&self) -> Vec<ResourceDefinition>
pub fn definitions(&self) -> Vec<ResourceDefinition>
Collect ResourceDefinitions for all registered resources.
Returns clones of the cached definitions computed at registration time.
Sourcepub fn contains(&self, name: &str) -> bool
pub fn contains(&self, name: &str) -> bool
Whether a resource with the given name is registered.
Note that resources are read by URI (see matches),
not by name; this checks the registered resource name for parity
with ToolRegistry::contains.
Sourcepub fn definition(&self, name: &str) -> Option<&ResourceDefinition>
pub fn definition(&self, name: &str) -> Option<&ResourceDefinition>
Borrow the cached ResourceDefinition for a registered resource by name.
Returns None if no resource named name is registered.
Sourcepub fn iter(&self) -> ResourceDefinitions<'_> ⓘ
pub fn iter(&self) -> ResourceDefinitions<'_> ⓘ
Iterate over (name, definition) pairs for every registered resource.
Yields clones of the cached definitions computed at registration time.
Sourcepub async fn read(&self, uri: &str) -> Result<ResourceOutput, ToolError>
pub async fn read(&self, uri: &str) -> Result<ResourceOutput, ToolError>
Read the first resource whose URI template matches uri.
§Errors
Returns ToolError::not_found if no registered resource’s template
matches uri (carrying error_kind = "not_registered" metadata), or a
read error if URI-variable deserialization or reading fails.
Trait Implementations§
Source§impl Debug for ResourceRegistry
impl Debug for ResourceRegistry
Source§impl Default for ResourceRegistry
impl Default for ResourceRegistry
Source§fn default() -> ResourceRegistry
fn default() -> ResourceRegistry
Source§impl<'a> IntoIterator for &'a ResourceRegistry
Iterate over (name, definition) pairs for every registered resource.
impl<'a> IntoIterator for &'a ResourceRegistry
Iterate over (name, definition) pairs for every registered resource.