-
Notifications
You must be signed in to change notification settings - Fork 0
feat: eliminate redundant bunx usage with BUN_BE_BUN support #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| # Eliminating Redundant bunx Usage with BUN_BE_BUN | ||
|
|
||
| ## Problem | ||
|
|
||
| Currently, using this MCP controller with MCP servers requires redundant `bunx` calls: | ||
|
|
||
| ```bash | ||
| bunx mcp-controller bunx @some/mcp-server # Redundant bunx | ||
| ``` | ||
|
|
||
| This is clunky and unnecessary since many MCP servers are npm packages that work with `bunx`. | ||
|
|
||
| ## Solution | ||
|
|
||
| Use Bun's `BUN_BE_BUN=1` environment variable to make our compiled executable behave like `bun x` (bunx). | ||
|
|
||
| ## How It Works | ||
|
|
||
| When `BUN_BE_BUN=1` is set, a compiled Bun executable ignores its bundled code and acts like the `bun` CLI. Combined with the `x` command, this gives us `bunx` functionality. | ||
|
|
||
| **User runs:** | ||
| ```bash | ||
| bunx mcp-controller some-mcp-server | ||
| ``` | ||
|
|
||
| **We internally execute:** | ||
| ```bash | ||
| BUN_BE_BUN=1 ./mcp-controller x some-mcp-server | ||
| ``` | ||
|
|
||
| **Which behaves like:** | ||
| ```bash | ||
| bunx some-mcp-server | ||
| ``` | ||
|
|
||
| ## Implementation | ||
|
|
||
| Change `src/target-server.ts` line 11-17 from: | ||
|
|
||
| ```typescript | ||
| const [command, ...args] = config.targetCommand; | ||
| const process = Bun.spawn([command, ...args], { | ||
| stdin: 'pipe', | ||
| stdout: 'pipe', | ||
| stderr: 'inherit', | ||
| }); | ||
| ``` | ||
|
|
||
| To: | ||
|
|
||
| ```typescript | ||
| const process = Bun.spawn([process.execPath, "x", ...config.targetCommand], { | ||
| stdin: 'pipe', | ||
| stdout: 'pipe', | ||
| stderr: 'inherit', | ||
| env: { | ||
| ...process.env, | ||
| BUN_BE_BUN: "1" | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| ## Benefits | ||
|
|
||
| 1. **Clean UX**: `bunx mcp-controller server-name` instead of `bunx mcp-controller bunx server-name` | ||
| 2. **Universal**: Works for npm packages, Docker commands, local scripts - anything `bunx` supports | ||
| 3. **Fast**: Leverages Bun's global caching and 100x speed improvement over npx | ||
| 4. **Simple**: Single line change, leverages existing Bun functionality | ||
|
|
||
| ## Usage Examples | ||
|
|
||
| ```bash | ||
| # npm packages | ||
| bunx mcp-controller @some/mcp-server | ||
|
|
||
| # Docker | ||
| bunx mcp-controller docker run --rm some-image | ||
|
|
||
| # Local scripts | ||
| bunx mcp-controller ./local-server.ts | ||
|
|
||
| # Any CLI tool | ||
| bunx mcp-controller python server.py | ||
| ``` | ||
|
|
||
| All work exactly like `bunx` would, but proxied through our controller. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,93 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { test, expect, describe } from 'bun:test'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import path from 'path'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { JSONRPCResponseSchema, InitializeResultSchema } from '@modelcontextprotocol/sdk/types.js'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { createInitializeRequest } from './test-messages.js'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Complete response schema for initialize using MCP SDK types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const InitializeResponseSchema = JSONRPCResponseSchema.extend({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result: InitializeResultSchema | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const controllerExecutable = path.resolve('./mcp-controller'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('Bunx Integration Tests', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| test('should work with npm package @modelcontextprotocol/server-sequential-thinking', async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Test that our BUN_BE_BUN implementation works with real npm packages | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const proxyProcess = Bun.spawn([ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| controllerExecutable, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| '@modelcontextprotocol/server-sequential-thinking' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ], { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stdin: 'pipe', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stdout: 'pipe', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stderr: 'pipe', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Give the proxy time to start and install the npm package | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await new Promise(resolve => setTimeout(resolve, 3000)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+26
to
+28
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Give the proxy time to start and install the npm package | |
| await new Promise(resolve => setTimeout(resolve, 3000)); | |
| // Wait for the proxy process to be ready by reading the first line of stdout (with timeout) | |
| async function waitForProcessReady(stream, timeoutMs = 10000) { | |
| const reader = stream.getReader(); | |
| const decoder = new TextDecoder(); | |
| let ready = false; | |
| let value; | |
| const start = Date.now(); | |
| while (Date.now() - start < timeoutMs) { | |
| const { value: chunk, done } = await Promise.race([ | |
| reader.read(), | |
| new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout waiting for process readiness')), 1000)) | |
| ]); | |
| if (done) break; | |
| if (chunk) { | |
| value = chunk; | |
| ready = true; | |
| break; | |
| } | |
| } | |
| reader.releaseLock(); | |
| if (!ready) throw new Error('Process did not become ready in time'); | |
| return value; | |
| } | |
| // Wait for process to emit first stdout (as readiness signal) | |
| if (!proxyProcess.stdout || typeof proxyProcess.stdout === 'number') { | |
| throw new Error('Process stdout is not available'); | |
| } | |
| await waitForProcessReady(proxyProcess.stdout); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic incorrectly handles the case when args is empty. If command is 'bunx' or 'npx' but args is empty, targetCommand will be an empty array, leading to an invalid spawn call. Should check args.length before destructuring.