Class BasicMcpSubscriptionBroker
- All Implemented Interfaces:
McpSubscriptionBroker,McpSubscriptions
McpSubscriptionBroker: a ConcurrentHashMap-backed registry doing filtered fan-out.
Publish methods (resourceUpdated(java.lang.String), toolsListChanged(), promptsListChanged(),
resourcesListChanged()) build one McpChangeEvent and iterate the live registry, offering the
event only to subscriptions whose McpSubscriptionFilter.matches(McpChangeEvent) returns
true — this never broadcasts unconditionally the way
org.apache.juneau.rest.server.sse.SseBroadcaster#publish does.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintThe number of currently registered (not yet closed) subscriptions.voidcloseAll()Closes every currently registered subscription.voidAnnounces that the prompt list changed.register(String subscriptionId, McpSubscriptionFilter honoredFilter) Registers a new subscription and returns its queue handle.registerIfUnder(int max, String subscriptionId, McpSubscriptionFilter honoredFilter) Atomically admission-checks againstmaxand registers, closing the TOCTOU window a separateMcpSubscriptionBroker.activeCount()check plusMcpSubscriptionBroker.register(java.lang.String, org.apache.juneau.rest.server.mcp.McpSubscriptionFilter)call would leave open under concurrent callers (in particular against a JVM-wide shared broker instance, where unrelated callers race each other).voidAnnounces that the resource list changed.voidresourceUpdated(String uri) Announces that a specific subscribed resource's content changed.voidAnnounces that the tool list changed.voidunregister(String subscriptionId) Deregisters and closes the subscription with the given id, if present.
-
Constructor Details
-
BasicMcpSubscriptionBroker
Creates a new broker.- Parameters:
queueSize- The per-subscription drop-oldest queue bound. Must be> 0.- Throws:
IllegalArgumentException- IfqueueSizeis not> 0.
-
-
Method Details
-
register
Description copied from interface:McpSubscriptionBrokerRegisters a new subscription and returns its queue handle. Uncapped: never rejects.- Specified by:
registerin interfaceMcpSubscriptionBroker- Parameters:
subscriptionId- The subscription identifier (the listen request's JSON-RPC id). Must not benullor empty.honoredFilter- The capability-gated filter this subscription honors. Must not benull.- Returns:
- The new subscription handle. Never
null.
-
registerIfUnder
public Optional<McpSubscription> registerIfUnder(int max, String subscriptionId, McpSubscriptionFilter honoredFilter) Description copied from interface:McpSubscriptionBrokerAtomically admission-checks againstmaxand registers, closing the TOCTOU window a separateMcpSubscriptionBroker.activeCount()check plusMcpSubscriptionBroker.register(java.lang.String, org.apache.juneau.rest.server.mcp.McpSubscriptionFilter)call would leave open under concurrent callers (in particular against a JVM-wide shared broker instance, where unrelated callers race each other).Re-registering an id that is already present is always allowed and never counted against
max, matchingMcpSubscriptionBroker.register(java.lang.String, org.apache.juneau.rest.server.mcp.McpSubscriptionFilter)'s replace-on-same-id semantics (the previous subscription with that id is closed) — a replacement does not growMcpSubscriptionBroker.activeCount(), so it cannot be what pushes the registry over the cap.- Specified by:
registerIfUnderin interfaceMcpSubscriptionBroker- Parameters:
max- The maximum number of concurrently active subscriptions to admit under (exclusive: a request is admitted only whileMcpSubscriptionBroker.activeCount()is strictly less than this, except for the same-id replacement case above).subscriptionId- The subscription identifier (the listen request's JSON-RPC id). Must not benullor empty.honoredFilter- The capability-gated filter this subscription honors. Must not benull.- Returns:
- The new subscription handle, or
Optional.empty()if the cap was reached and this was not a same-id replacement.
-
unregister
Description copied from interface:McpSubscriptionBrokerDeregisters and closes the subscription with the given id, if present.- Specified by:
unregisterin interfaceMcpSubscriptionBroker- Parameters:
subscriptionId- The subscription identifier. A missing id is a no-op.
-
activeCount
Description copied from interface:McpSubscriptionBrokerThe number of currently registered (not yet closed) subscriptions.- Specified by:
activeCountin interfaceMcpSubscriptionBroker- Returns:
- The active count.
-
closeAll
Description copied from interface:McpSubscriptionBrokerCloses every currently registered subscription.Called when the server begins shutting down, so open
subscriptions/listenstreams complete promptly instead of holding the graceful drain open until its stop timeout. The default implementation does nothing, so custom brokers that predate this method keep working (their streams then end on the normal heartbeat/idle-timeout paths).- Specified by:
closeAllin interfaceMcpSubscriptionBroker
-
resourceUpdated
Description copied from interface:McpSubscriptionsAnnounces that a specific subscribed resource's content changed.- Specified by:
resourceUpdatedin interfaceMcpSubscriptions- Parameters:
uri- The exact resource URI that changed. Must not benull.
-
toolsListChanged
Description copied from interface:McpSubscriptionsAnnounces that the tool list changed.- Specified by:
toolsListChangedin interfaceMcpSubscriptions
-
promptsListChanged
Description copied from interface:McpSubscriptionsAnnounces that the prompt list changed.- Specified by:
promptsListChangedin interfaceMcpSubscriptions
-
resourcesListChanged
Description copied from interface:McpSubscriptionsAnnounces that the resource list changed.- Specified by:
resourcesListChangedin interfaceMcpSubscriptions
-