Class InMemorySyncResourceRepository

java.lang.Object
org.springframework.ai.mcp.modern.InMemorySyncResourceRepository
All Implemented Interfaces:
io.modelcontextprotocol.modern.server.feature.McpSyncResourceRepository

public final class InMemorySyncResourceRepository extends Object implements io.modelcontextprotocol.modern.server.feature.McpSyncResourceRepository
A McpSyncResourceRepository that keeps SyncResourceRegistrations and SyncResourceTemplateRegistrations in sorted, concurrent maps. A resources/read URI is served by the resource registered under it, or else by the first template matching it in URI-template order. Resources and templates can be added and removed at runtime; each change is broadcast as notifications/resources/list_changed when a change broadcaster is configured, and resourceUpdated(String) notifies subscribers of a changed resource. Listings are paged with an opaque key cursor when a page size is set. Completion is served from the completionHandler of each template, and is advertised unless the repository is built with completionSupported(false).
Since:
2.1.0
Author:
Christian Tzolov
See Also:
  • Method Details

    • builder

      public static InMemorySyncResourceRepository.Builder builder()
    • add

      public void add(SyncResourceRegistration registration)
      Adds a resource and notifies listening clients.
      Throws:
      IllegalArgumentException - if a resource with the same URI exists
    • add

      public void add(SyncResourceTemplateRegistration registration)
      Adds a resource template and notifies listening clients.
      Throws:
      IllegalArgumentException - if a template with the same URI template exists, or the URI template is invalid
    • remove

      public boolean remove(String uri)
      Removes the resource or resource template with this URI and notifies listening clients.
      Returns:
      whether anything was removed
    • resourceUpdated

      public void resourceUpdated(String uri)
      Notifies subscribers that the content of uri changed. Reaches clients only when the repository was built with subscribeSupported.
    • list

      public io.modelcontextprotocol.modern.server.feature.ResourcesPage list(io.modelcontextprotocol.modern.server.McpRequestContext ctx, @Nullable String cursor)
      Specified by:
      list in interface io.modelcontextprotocol.modern.server.feature.McpSyncResourceRepository
    • listTemplates

      public io.modelcontextprotocol.modern.server.feature.ResourceTemplatesPage listTemplates(io.modelcontextprotocol.modern.server.McpRequestContext ctx, @Nullable String cursor)
      Specified by:
      listTemplates in interface io.modelcontextprotocol.modern.server.feature.McpSyncResourceRepository
    • find

      public @Nullable io.modelcontextprotocol.modern.server.feature.McpSyncResource find(io.modelcontextprotocol.modern.server.McpRequestContext ctx, String uri)
      Specified by:
      find in interface io.modelcontextprotocol.modern.server.feature.McpSyncResourceRepository
    • findTemplate

      public @Nullable io.modelcontextprotocol.modern.server.feature.McpSyncResourceTemplate findTemplate(io.modelcontextprotocol.modern.server.McpRequestContext ctx, String uriTemplate)
      The template registered under exactly this URI template, which carries the handler that completes its variables.
      Specified by:
      findTemplate in interface io.modelcontextprotocol.modern.server.feature.McpSyncResourceRepository
    • supportsSubscribe

      public boolean supportsSubscribe()
      Specified by:
      supportsSubscribe in interface io.modelcontextprotocol.modern.server.feature.McpSyncResourceRepository
    • supportsCompletion

      public boolean supportsCompletion()
      Specified by:
      supportsCompletion in interface io.modelcontextprotocol.modern.server.feature.McpSyncResourceRepository