Class DefaultResourceRegistry

java.lang.Object
dev.tachyonmcp.core.server.features.resources.DefaultResourceRegistry
All Implemented Interfaces:
Resources

@InternalApi public class DefaultResourceRegistry extends Object implements Resources
Registry for resources, templates, and subscriptions.
  • Constructor Details

    • DefaultResourceRegistry

      public DefaultResourceRegistry(ServerEngine server, ResourcesConfig config)
      Creates a resource registry bound to the given server (for broadcasting subscription notifications).
  • Method Details

    • onChange

      public void onChange(Runnable callback)
    • register

      public DefaultResourceRegistry register(ResourceDescriptor descriptor, ResourceFn fn)
      Registers a resource descriptor and its handler.

      If resource support is disabled, the descriptor is not registered. Registering a resource with an existing name replaces the previous resource and updates URI mappings accordingly. A URI is unique across resources: registering a URI already owned by a different name is rejected.

      Specified by:
      register in interface Resources
      Parameters:
      descriptor - the resource descriptor to register
      fn - the resource function
      Returns:
      this registry
      Throws:
      IllegalArgumentException - if the URI is already registered under a different name
    • registerAsync

      public DefaultResourceRegistry registerAsync(ResourceDescriptor descriptor, AsyncResourceFn fn)
      Description copied from interface: Resources
      Registers a resource descriptor with an asynchronous function.
      Specified by:
      registerAsync in interface Resources
      Parameters:
      descriptor - the resource descriptor
      fn - the asynchronous resource function
      Returns:
      this resource registry for method chaining
    • unregister

      public boolean unregister(String name)
      Removes the resource registered under the specified name.
      Specified by:
      unregister in interface Resources
      Parameters:
      name - the resource name
      Returns:
      true if a resource was removed, false if no resource was registered under the name
    • unregisterByUri

      public boolean unregisterByUri(String uri)
      Description copied from interface: Resources
      Removes the registered resource with the specified uri.
      Specified by:
      unregisterByUri in interface Resources
      Parameters:
      uri - resource URI
      Returns:
      true if resource was unregistered
    • find

      public Optional<ResourceDescriptor> find(String name)
      Finds a registered resource by name.
      Specified by:
      find in interface Resources
      Parameters:
      name - the resource name
      Returns:
      the resource descriptor, or an empty optional if no resource has the specified name
    • findByUri

      public Optional<ResourceDescriptor> findByUri(String uri)
      Description copied from interface: Resources
      Finds a registered resource descriptor by URI.
      Specified by:
      findByUri in interface Resources
      Parameters:
      uri - the resource URI
      Returns:
      the matching descriptor, or an empty Optional if no resource is registered with that uri
    • descriptors

      public List<ResourceDescriptor> descriptors()
      Lists registered resource descriptors in ascending name order.
      Specified by:
      descriptors in interface Resources
      Returns:
      the registered resource descriptors
    • list

      public PaginatedResult<ResourceDescriptor> list(int limit, @Nullable String cursor)
      Lists registered resources in name order using cursor-based pagination.
      Parameters:
      limit - the maximum number of resources to include; the configured page size is used when this value is not positive
      cursor - the cursor identifying the starting position, or null to start from the beginning
      Returns:
      the paginated resources and the cursor for the next page
    • list

      public PaginatedResult<ResourceDescriptor> list(int limit, @Nullable String cursor, Predicate<ResourceDescriptor> filter)
    • registerTemplate

      public Resources registerTemplate(ResourceTemplateDescriptor descriptor, ResourceFn fn)
      Registers a resource template with its handler.

      If resource support is disabled, the template is not registered. An exception is thrown when a template with the same name already exists.

      Specified by:
      registerTemplate in interface Resources
      Parameters:
      descriptor - the resource template descriptor
      fn - the resource function
      Returns:
      this registry
      Throws:
      IllegalArgumentException - if a template with the same name is already registered
    • registerTemplateAsync

      public Resources registerTemplateAsync(ResourceTemplateDescriptor descriptor, AsyncResourceFn fn)
      Description copied from interface: Resources
      Registers a resource template with an asynchronous function.
      Specified by:
      registerTemplateAsync in interface Resources
      Parameters:
      descriptor - the resource template descriptor to register
      fn - the asynchronous function for the resource template
      Returns:
      this resource registry
    • unregisterTemplate

      public boolean unregisterTemplate(String name)
      Removes a registered resource template by name.
      Specified by:
      unregisterTemplate in interface Resources
      Parameters:
      name - the name of the template to remove
      Returns:
      true if a template was removed, false otherwise
    • findTemplate

      public Optional<ResourceTemplateDescriptor> findTemplate(String name)
      Finds a registered resource template by name.
      Specified by:
      findTemplate in interface Resources
      Parameters:
      name - the template name
      Returns:
      the matching resource template descriptor, or an empty optional if no template is registered with that name
    • templateDescriptors

      public List<ResourceTemplateDescriptor> templateDescriptors()
      Lists registered resource template descriptors in name order.
      Specified by:
      templateDescriptors in interface Resources
      Returns:
      the registered resource template descriptors sorted by name
    • isEmpty

      public boolean isEmpty()
      Determines whether the registry contains no resources.
      Returns:
      true if the registry contains no resources, false otherwise
    • isSubscribed

      public boolean isSubscribed(String uri, String sessionId)
      Returns whether the given session is subscribed to the resource URI.
    • notifyResourceUpdated

      public void notifyResourceUpdated(String uri)
      Notifies all subscribed sessions that a resource has been updated.
      Specified by:
      notifyResourceUpdated in interface Resources
      Parameters:
      uri - the URI of the resource that changed