Commit d1922d5
Support aggregate event IDs for multi-toolkit MCP proxy (#1791)
* feat(binding-mcp): aggregate event ids across routes in mcp.proxy lifecycle
In multi-route mode each upstream MCP server mints SSE event ids in its
own namespace, so forwarding them verbatim collides on the agent's
merged stream and breaks Last-Event-ID recovery on reconnect.
The proxy now derives a unique short prefix per route from CRC32C of
its `when[].toolkit` (URL-safe base64, shortest unique length), then on
each outbound FlushEx that carries an id (resumable, listChanged,
progress, elicitComplete) rewrites the id to a canonical sorted
`<prefix>=<id>;<prefix>=<id>` aggregate of the latest known per-route
ids. On inbound McpResumeChallengeEx the aggregate is decoded back to
per-route ids and dispatched to each route's lifecycle client, opening
clients lazily when needed; unknown prefixes are skipped without
rejecting the resume.
Single-route configs and the hydrater self-loop bypass aggregation
entirely. McpBindingConfig validates that every route declares a
toolkit when more than one route is configured.
* test(binding-mcp): k3po IT coverage for aggregate-id resume demux
Adds engine-driven McpProxyIT scenarios that exercise the demux path on
McpLifecycleServer.onServerChallenge end-to-end:
- lifecycle.events.resume.aggregate.event.id — client injects a resume
challenge with the aggregate id "2=200;S=100"; the proxy decodes it,
opens both upstream lifecycle clients lazily, and the upstreams each
receive a resume challenge carrying their per-route id. When
bluesky subsequently emits toolsListChanged id=101, the agent reads
"2=200;S=101", proving the aggregate snapshot retained the inbound
per-route ids.
- lifecycle.events.resume.missing.prefix — client injects an aggregate
containing an unknown prefix X plus a known prefix S; the proxy
skips X (no route bound to that prefix) and only dispatches the
bluesky route. When bluesky emits id=101 the agent reads "S=101".
The challenge is injected via `read advise zilla:challenge` on the
script's connect side — the same active-emit primitive the elicit
scripts use on the accept side. This is the script analogue of
mcp.server translating an HTTP Last-Event-ID header into a
ChallengeFW on the lifecycle stream.
Also fixes McpLifecycleServer.dispatchAggregateResume to seed
lastEventIdsByPrefix with the per-route ids decoded from the inbound
aggregate, so the first post-resume FlushEx from any one route still
emits a complete aggregate covering every route present in the
original Last-Event-ID rather than just the route that just emitted.
* feat(binding-mcp): scope toolkit and cache to kind: proxy in schema
Tightens the mcp binding schema so `toolkit` is required on every
`when` item — and only permitted on `kind: proxy`. The `cache` option
is likewise restricted to `kind: proxy`. Adds invalid configs and
SchemaTest cases asserting that misconfigured server-kind bindings
(cache option, toolkit-bearing when) and proxy-kind routes missing
toolkit are rejected at config-parse time.
Drops the runtime IllegalArgumentException in McpBindingConfig that
checked multi-route toolkit presence; the schema now enforces this
earlier (config-parse, not engine startup), making the runtime check
dead code.
Splits the multi-route resume scenarios into prefixed and non-prefixed
variants per repo convention so both forms have peer-to-peer
ApplicationIT coverage and engine-driven McpProxyIT coverage:
- lifecycle.events.resume.aggregate.event.id{.prefixed}/
- lifecycle.events.resume.missing.prefix{.prefixed}/
The prefixed/client.rpt connects to the proxy's app surface and
injects the aggregate id; the non-prefixed/client.rpt simulates what
the proxy would do upstream — two direct connects, one per route,
each carrying the per-route id. The shared server.rpt observes the
per-route challenges via `write advised zilla:challenge`.
* perf(binding-mcp): zero-allocation aggregate event id encode
Replaces the StringBuilder-backed encode that allocated a StringBuilder
plus a String per outbound FlushEx with a buffer-based encode that
writes UTF-8 bytes directly into a factory-level MutableDirectBuffer
and returns the byte count.
McpLifecycleServer.mintAggregateEventId now wraps the encoded slice in
a reusable OctetsFW (null if no route has emitted yet); the
rewriteFlushExWithAggregateId helper passes
buffer/offset/length to the generated id(DirectBuffer, int, int)
builder overload on every applicable FlushEx variant
(resumable, toolsListChanged, promptsListChanged, resourcesListChanged,
progress, elicitComplete).
McpAggregateEventIdTest exercises the new buffer API (including encode
at a non-zero offset) and reads the encoded bytes back via
getStringWithoutLengthUtf8 for assertion.
* refactor(binding-mcp): trim verbose names in aggregate event id paths
Tightens names that encoded preconditions or context that is already
clear from scope:
- McpBindingConfig.sortedPrefixes -> prefixes
- McpBindingConfig.sortedRoutedIdsByPrefix -> routedIds
- McpBindingConfig.prefixByRoutedId dropped (was never read)
- McpAggregateEventId.encode parameters
prefixesSortedAscending / idsAlignedWithPrefixes -> prefixes / ids
- McpProxyLifecycleFactory:
flushCodecBuffer -> flushExBuffer
aggregateIdBuffer -> aggregateBuffer
aggregateIdRO -> aggregateRO
lastEventIdsByPrefix -> eventIds
pendingResumeId -> resumeId
recordRouteEventId(sourceRoutedId, perRouteId) -> recordEventId(routedId, id)
mintAggregateEventId() -> mintAggregate()
dispatchAggregateResume(...) -> dispatchResume(...)
extractEventId(...) -> eventIdOf(...) (matches the existing
capabilityOf / identifierOf getter convention)
rewriteFlushExWithAggregateId(...) -> rewriteFlushEx(...)
Pure rename — no behavioural change. All 188 tests + checkstyle + license
remain green.
* refactor(binding-mcp): rename aggregate-resume scenario dirs
Resolves the "prefix"-overloading in the previous scenario names:
- "prefix" in `missing.prefix` referred to the *toolkit* routing prefix
- ".prefixed" suffix is the convention marker for the engine-driven
variant (per tools.list.toolkit.multi.prefixed)
The two collided, so `resume.missing.prefix.prefixed` read as nonsense.
Renames:
- lifecycle.events.resume.aggregate.event.id -> lifecycle.events.resume.aggregate
- lifecycle.events.resume.aggregate.event.id.prefixed -> lifecycle.events.resume.aggregate.prefixed
- lifecycle.events.resume.missing.prefix -> lifecycle.events.resume.partial
- lifecycle.events.resume.missing.prefix.prefixed -> lifecycle.events.resume.partial.prefixed
`event.id` is redundant once `resume` is in the name; `partial`
describes the outcome (only the resolvable prefixes are dispatched)
without re-using the "prefix" word.
Test methods follow:
- shouldResumeLifecycleEventsWithAggregateEventId -> shouldResumeLifecycleEventsAggregate
- shouldResumeLifecycleEventsWithMissingPrefix -> shouldResumeLifecycleEventsPartial
* refactor(binding-mcp): apply PR #1791 review feedback
Naming and structure tweaks from the review:
- McpBindingConfig: replace parallel arrays (prefixes[], routedIds[])
with a single McpAggregateRoute[] (new record (prefix, routedId)).
- McpAggregateEventId.computePrefixes: drop the redundant single-toolkit
special case (the general loop produces the same 1-char prefix);
replace explicit Map builders with stream + toMap collectors.
- McpAggregateEventId.encode: signature now takes McpAggregateRoute[]
directly instead of a parallel String[] prefixes argument.
- McpLifecycleServer.recordEventId -> onDecodeEventId (event-handler
naming consistent with on*/do* convention).
- McpLifecycleServer.mintAggregate -> nextEventId.
- McpProxyLifecycleFactory.eventIdOf -> extractEventId.
- onClientFlush now just forwards to doServerFlush(... routedId); the
aggregation transform lives on doServerFlush where it can read its
own server state directly. Inside, drop the extension.sizeof() > 0
guard and use wrap (not tryWrap) — the FlushEx is non-null on this
path.
- McpLifecycleClient.doClientResume: drop the resumeId parameter; the
client's own resumeId field is read via a `this::injectResumeId`
method reference on the builder.
- McpLifecycleClient.sessionId: now private with a sessionId() accessor;
external callers in McpProxyItemFactory / McpProxyListFactory updated
(the McpLifecycleServer.sessionId field — a different class — stays
package-private).
- rewriteFlushEx locals renamed to (buffer, offset, length) to avoid
shadowing the factory-level aggregateBuffer field.
- forwardExtension -> newExtension.
SchemaTest: positive cases for each kind with all supported options
populated (server.options.yaml, proxy.options.yaml, client.options.yaml).
Pure refactor — all 18 unit + 171 IT tests still green.
* refactor(binding-mcp): apply second round PR #1791 feedback
- Long2ObjectHashMap<String> replaces String[] eventIds for cleaner
lookup and storage keyed by routedId
- aggregateBuffer sized to fixed 1024 bytes instead of writeBuffer
capacity
- doClientResume guards on replyOpened and clears resumeId on send
- doClientBegin pre-sets lifecycle sessionId from server, allowing
list factory to drop fallback to server.lifecycle.sessionId
- McpItem.doClientBegin accepts sessionId as parameter from server
call site instead of computing locally
- resumeClient extracted from dispatchResume lambda; onServerChallenge
inlines aggregate decode under nested KIND_RESUME guard
- onClientFlush records per-route event id; doServerFlush now only
rewrites the aggregate
- SchemaTest keeps a single positive options test per kind with all
supported properties, drops the redundant toolkit-multi test
* refactor(binding-mcp): apply third round PR #1791 feedback
- Pass identifier as a parameter to McpItem.doClientBegin from the
server call site instead of pulling from server.identifier inside
- Rename resumeClient to onDecodeAggregateEventId for symmetry with
onDecodeEventId
- Replace rewriteFlushEx with kind-specific inject methods dispatched
from a generic injectFlushEx; inline the builder chain at the call
site in doServerFlush
- Drop redundant shouldValidateServer; the with-options positive tests
cover the supported properties for each kind
* refactor(binding-mcp): drop WithOptions suffix from schema test names
* refactor(binding-mcp): drop dead prefix field and sessionId accessors
- Remove unused McpProxyItemFactory.McpServer.prefix field, its
constructor parameter, the call-site argument, and the local
variable from the resolve block
- Inline McpProxyItemFactory.McpServer.sessionId() to direct
lifecycle.sessionId field access at the two call sites
- Promote McpProxyLifecycleFactory.McpLifecycleClient.sessionId to
package-private and drop the sessionId() accessor; the sole
external caller now reads the field directly
* refactor(binding-mcp): store originId and routedId on proxy clients
McpLifecycleClient, McpProxyItemFactory.McpClient, and
McpProxyListFactory.McpListClient now hold originId and routedId as
final fields set once in the constructor, instead of recomputing
server.lifecycle.originId / server.routedId at every do* call site.
The resolvedId field is renamed to routedId for consistency with the
existing initialId/replyId/originId/routedId naming convention.
* fix(binding-mcp): restore proxy.toolkit.multi.yaml for IT tests
McpProxyIT references @configuration("proxy.toolkit.multi.yaml") for
six multi-toolkit and aggregate-resume tests. The yaml was removed
along with the redundant SchemaTest case, but the IT still depends on
it for the multi-route proxy configuration. Local tests masked the
gap because target/classes retained a stale copy; clean CI builds
hit NullPointerException loading the missing config.
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent a21ba2e commit d1922d5
26 files changed
Lines changed: 1659 additions & 66 deletions
File tree
- runtime/binding-mcp/src
- main/java/io/aklivity/zilla/runtime/binding/mcp/internal
- config
- stream
- test/java/io/aklivity/zilla/runtime/binding/mcp/internal
- config
- stream
- specs/binding-mcp.spec/src
- main/scripts/io/aklivity/zilla/specs/binding/mcp
- config
- schema
- streams/application
- lifecycle.events.resume.aggregate.prefixed
- lifecycle.events.resume.aggregate
- lifecycle.events.resume.partial.prefixed
- lifecycle.events.resume.partial
- lifecycle.notify.tools.list.changed.toolkit.multi.prefixed
- lifecycle.notify.tools.list.changed.toolkit.multi
- test/java/io/aklivity/zilla/specs/binding/mcp
- config
- streams/application
Lines changed: 153 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
Lines changed: 21 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
Lines changed: 23 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| 47 | + | |
| 48 | + | |
46 | 49 | | |
47 | 50 | | |
48 | 51 | | |
| |||
57 | 60 | | |
58 | 61 | | |
59 | 62 | | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
60 | 83 | | |
61 | 84 | | |
62 | 85 | | |
| |||
Lines changed: 13 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| |||
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
63 | 74 | | |
64 | 75 | | |
65 | 76 | | |
| |||
216 | 227 | | |
217 | 228 | | |
218 | 229 | | |
| 230 | + | |
219 | 231 | | |
220 | 232 | | |
221 | 233 | | |
| |||
225 | 237 | | |
226 | 238 | | |
227 | 239 | | |
| 240 | + | |
228 | 241 | | |
229 | 242 | | |
230 | 243 | | |
| |||
0 commit comments