MCPcopy Create free account
hub / github.com/adcontextprotocol/adcp / testPythonSnippet

Function testPythonSnippet

tests/snippet-validation.test.cjs:415–486  ·  view source on GitHub ↗

* Test a Python snippet

(snippet)

Source from the content-addressed store, hash-verified

413 * Test a Python snippet
414 */
415async function testPythonSnippet(snippet) {
416 const tempFile = path.join(__dirname, `temp-snippet-${Date.now()}.py`);
417
418 try {
419 // Write snippet to temporary file
420 fs.writeFileSync(tempFile, snippet.code);
421
422 // Use virtualenv Python directly (no activation needed - much faster!)
423 const venvPython = path.join(__dirname, '..', '.venv', 'bin', 'python');
424 const pythonCmd = fs.existsSync(venvPython) ? venvPython : 'python3';
425
426 // Execute from project root
427 const { stdout, stderr } = await execFileAsync(pythonCmd, [tempFile], {
428 timeout: 60000, // 60 second timeout (API calls can take time)
429 cwd: path.join(__dirname, '..') // Run from project root
430 });
431
432 return {
433 success: true,
434 output: stdout,
435 error: stderr
436 };
437 } catch (error) {
438 // WORKAROUND: Python MCP SDK has async cleanup bug (exit code 1)
439 // See PYTHON_MCP_ASYNC_BUG.md for details
440 // Waiting for upstream fix in mcp package (currently 1.21.0)
441 const hasAsyncCleanupBug = error.stderr && (
442 error.stderr.includes('an error occurred during closing of asynchronous generator') ||
443 error.stderr.includes('streamablehttp_client') ||
444 error.stderr.includes('async_generator object')
445 );
446
447 const hasOutput = error.stdout && error.stdout.trim().length > 0;
448
449 // If we have output, treat as success (test logic ran)
450 if (hasOutput) {
451 const warning = hasAsyncCleanupBug
452 ? 'Python MCP async cleanup bug - ignoring (see PYTHON_MCP_ASYNC_BUG.md)'
453 : 'Test produced output but exited with non-zero code';
454
455 return {
456 success: true,
457 output: error.stdout,
458 error: error.stderr,
459 warning
460 };
461 }
462
463 // If it's ONLY the async cleanup bug with no output, still pass
464 if (hasAsyncCleanupBug) {
465 return {
466 success: true,
467 output: '',
468 error: error.stderr,
469 warning: 'Python MCP async cleanup bug - no output (see PYTHON_MCP_ASYNC_BUG.md)'
470 };
471 }
472

Callers 1

validateSnippetFunction · 0.85

Calls 2

nowMethod · 0.80
trimMethod · 0.65

Tested by

no test coverage detected