Skip to content

Commit b60603a

Browse files
danieliserclaude
andcommitted
fix: Bun runtime result capture with MCP integration
🐛 Bug Fix: - Fixed Bun runtime not returning results when MCP integration enabled - Issue: wrapCode checked for 'return' anywhere, found it in MCP proxy functions - Result: Assumed code had top-level return, didn't add one to last expression - Effect: __result was undefined, no result returned to user 🔧 Solution: - Changed hasReturnStatement check to hasTopLevelReturn - Only check last 3 lines for return statements (not entire code) - Avoids false positives from return statements inside MCP proxy functions ✅ Testing: - QuickJS: Already working (no regression) - Bun: Now returns 42, "Hello", {objects}, [arrays], booleans correctly - All expression types validated Root cause: Line 276 `trimmedCode.includes('return ')` matched MCP proxy code like: ```js async store_memory(args) => { return await __mcpCall(...); // <-- Matched here! } ``` 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent dab0c15 commit b60603a

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/runtime/bun-runtime.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,17 @@ __originalConsole.log('__RESULT__', JSON.stringify({
271271
const lines = trimmedCode.split('\n');
272272
const lastLine = lines[lines.length - 1].trim();
273273

274-
// Check if code already contains a return statement
274+
// Check if code already contains a TOP-LEVEL return statement
275275
// (avoid wrapping multi-line returns like: return { ... };)
276-
const hasReturnStatement = trimmedCode.includes('return ');
276+
// NOTE: Don't just check for 'return' anywhere - it might be inside a function!
277+
// Simple heuristic: check if last 3 lines contain a return statement
278+
const hasTopLevelReturn = lines.some((line, idx) => {
279+
if (idx < lines.length - 3) return false; // Only check last few lines
280+
return line.trim().startsWith('return ');
281+
});
277282

278-
// If code already has a return statement, don't modify it
279-
if (hasReturnStatement) {
283+
// If code already has a top-level return statement, don't modify it
284+
if (hasTopLevelReturn) {
280285
wrappedCode = trimmedCode;
281286
} else {
282287
// Check if last line is a bare expression that should become a return

0 commit comments

Comments
 (0)