Repository navigation
feat: add a session-safe shared MCP tools bridge runtime - #16221
sanshengai wants to merge 1 commit into
Conversation
📝 WalkthroughWalkthroughAdds the Merge Risk: 🟡 Moderate · up to The bridge can leave application shutdown hanging when an upstream connection remains open, and its integration test does not currently prove that multiple clients share one downstream process. These bounded runtime and validation issues should be fixed before merging. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Warning |
There was a problem hiding this comment.
Actionable comments posted: 2
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 9fff26c3-37ef-4574-8f1f-7b37630dc7de
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (6)
package.jsonsrc/main/mcp-bridge/index.tssrc/main/mcp-bridge/shared-mcp-tools-bridge-support.tssrc/main/mcp-bridge/shared-mcp-tools-bridge.test.tssrc/main/mcp-bridge/shared-mcp-tools-bridge.tssrc/main/mcp-bridge/test-fixtures/tools-server.mjs
Included review availability: Your plan provides up to 10 included reviews per hour; 5 remain after this review.
| ) | ||
| const parsed = results.map(readResult) | ||
|
|
||
| expect(new Set(parsed.map(({ pid }) => pid))).toHaveLength(1) |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Confirm the installed Vitest toHaveLength implementation requires a numeric `.length`.
fd -t f 'jest-expect' node_modules/@vitest --exec rg -n -A 12 "'toHaveLength'" 2>/dev/null \
|| rg -n -A 12 "'toHaveLength'" -g '**/@vitest/expect/**' .Repository: stablyai/orca
Length of output: 151
🏁 Script executed:
#!/bin/bash
set -e
printf '%s\n' '--- test file ---'
cat -n src/main/mcp-bridge/shared-mcp-tools-bridge.test.ts | sed -n '65,100p'
printf '%s\n' '--- Vitest declarations and package metadata ---'
rg -n -S 'toHaveLength|vitest' package.json package-lock.json pnpm-lock.yaml yarn.lock src 2>/dev/null | head -120
printf '%s\n' '--- local dependency candidates ---'
find . -path '*/node_modules/@vitest/expect*' -o -path '*/node_modules/@vitest/utils*' 2>/dev/null | head -40Repository: stablyai/orca
Length of output: 16118
🏁 Script executed:
#!/bin/bash
set -e
printf '%s\n' '--- Vitest 4.1.5 matcher implementation ---'
rg -n -S -A 20 -B 8 'toHaveLength|length.*expected|expected.*length' \
node_modules/@vitest/expect node_modules/.pnpm/@[email protected] 2>/dev/null | head -160
printf '%s\n' '--- package exports and matcher files ---'
find node_modules/@vitest/expect -maxdepth 3 -type f -print 2>/dev/null | sort | head -100
printf '%s\n' '--- Set property behavior ---'
node - <<'JS'
const values = [new Set([1]), [1], { length: 1 }]
for (const value of values) {
console.log(value.constructor.name, {
length: value.length,
size: value.size,
hasOwnLength: Object.prototype.hasOwnProperty.call(value, 'length')
})
}
JSRepository: stablyai/orca
Length of output: 438
🏁 Script executed:
#!/bin/bash
set -e
printf '%s\n' '--- resolved Vitest package paths ---'
readlink -f node_modules/@vitest/expect 2>/dev/null || true
find -L node_modules/@vitest/expect -maxdepth 3 -type f -print 2>/dev/null | sort | head -120
printf '%s\n' '--- matcher references in installed Vitest packages ---'
rg -n -S 'toHaveLength' node_modules/.pnpm 2>/dev/null | head -80Repository: stablyai/orca
Length of output: 533
Assert Set cardinality with size.
Set exposes size, not length. Therefore, toHaveLength(1) does not assert the number of unique process IDs. Use expect(new Set(parsed.map(({ pid }) => pid)).size).toBe(1).
| async close(): Promise<void> { | ||
| if (this.closing) { | ||
| return | ||
| } | ||
| this.closing = true | ||
| this.cancelIdleShutdown() | ||
| const sessionServers = [...this.sessions.values()].map((session) => session.server) | ||
| this.sessions.clear() | ||
| await Promise.allSettled(sessionServers.map((server) => server.close())) | ||
| await this.closeDownstream() | ||
| const server = this.httpServer | ||
| this.httpServer = null | ||
| this.connection = null | ||
| if (server) { | ||
| await new Promise<void>((resolve) => server.close(() => resolve())) | ||
| } | ||
| } |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Drop live sockets during close().
server.close(cb) stops new connections only. The callback runs after every open socket ends. A connected upstream client can hold a socket, for example an open GET stream or a socket that never initialized a session. In that case the promise at Line 310 stays pending and application shutdown stalls.
Call closeAllConnections() after close() so teardown always completes.
🔧 Proposed fix
if (server) {
- await new Promise<void>((resolve) => server.close(() => resolve()))
+ const closed = new Promise<void>((resolve) => server.close(() => resolve()))
+ server.closeAllConnections()
+ await closed
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| async close(): Promise<void> { | |
| if (this.closing) { | |
| return | |
| } | |
| this.closing = true | |
| this.cancelIdleShutdown() | |
| const sessionServers = [...this.sessions.values()].map((session) => session.server) | |
| this.sessions.clear() | |
| await Promise.allSettled(sessionServers.map((server) => server.close())) | |
| await this.closeDownstream() | |
| const server = this.httpServer | |
| this.httpServer = null | |
| this.connection = null | |
| if (server) { | |
| await new Promise<void>((resolve) => server.close(() => resolve())) | |
| } | |
| } | |
| async close(): Promise<void> { | |
| if (this.closing) { | |
| return | |
| } | |
| this.closing = true | |
| this.cancelIdleShutdown() | |
| const sessionServers = [...this.sessions.values()].map((session) => session.server) | |
| this.sessions.clear() | |
| await Promise.allSettled(sessionServers.map((server) => server.close())) | |
| await this.closeDownstream() | |
| const server = this.httpServer | |
| this.httpServer = null | |
| this.connection = null | |
| if (server) { | |
| const closed = new Promise<void>((resolve) => server.close(() => resolve())) | |
| server.closeAllConnections() | |
| await closed | |
| } | |
| } |
|
Closing as stale. main has moved on substantially since this branch was opened on Aug 24, and I am no longer following it. Happy to reopen a rebased, single-change PR if maintainers still want the fix. |
Summary
Adds the runtime foundation for a session-safe, shared MCP bridge (part of #7362 and the resource-reduction work tracked in #16211).
The bridge exposes a bearer-protected Streamable HTTP endpoint on
127.0.0.1and multiplexes independent upstream MCP sessions onto one downstream stdio MCP client/process. This is intentionally runtime-first and tools-only; settings UI, automatic agent config injection, and non-tools capabilities remain follow-ups.Why
Sharing a raw stdio pipe is not protocol-safe: unrelated clients can reuse JSON-RPC request IDs and have independent cancellation/session lifecycles. The bridge instead terminates each upstream session with its own MCP
Server/HTTP transport, then forwards tool calls through one SDKClient, which owns downstream request IDs.Safety boundary
isolationKeyfor workspace/auth/version boundariesLifecycle
Tests
pnpm exec vitest run --config config/vitest.config.ts src/main/mcp-bridge/shared-mcp-tools-bridge.test.tspnpm run typecheckpnpm run audit:code-quality:nativepnpm run audit:code-quality:type-awarepnpm run check:max-lines-ratchetThe integration test connects five independent Streamable HTTP clients, invokes tools concurrently (their SDK request ID sequences collide naturally), verifies one shared child PID, disconnects one client without disturbing the others, verifies bounded last-client cleanup, rejects a bad token, and verifies crash recovery.