{"record":{"id":"425946061c0d9b24","repo":"CherryHQ/cherry-studio","slug":"internalerror-425946","errorCode":"InternalError","errorMessage":"Python execution failed: ${errorMessage}","messagePattern":"Python execution failed: (.+?)","errorType":"exception","errorClass":"McpError","httpStatus":null,"severity":"error","filePath":"src/main/ai/mcp/servers/python.ts","lineNumber":115,"sourceCode":"        const timeout = Math.min(Math.max(parsed.data.timeout, MIN_TIMEOUT_MS), MAX_TIMEOUT_MS)\n\n        logger.debug('Executing Python code via Pyodide')\n\n        const result = await application.get('PythonService').executeScript(code, context, timeout)\n\n        return {\n          content: [\n            {\n              type: 'text',\n              text: result\n            }\n          ]\n        }\n      } catch (error) {\n        const errorMessage = error instanceof Error ? error.message : String(error)\n        logger.error(`Python execution error: ${errorMessage}`)\n\n        throw new McpError(ErrorCode.InternalError, `Python execution failed: ${errorMessage}`)\n      }\n    })\n  }\n}\n\nexport default PythonServer\n","sourceCodeStart":97,"sourceCodeEnd":122,"githubUrl":"https://github.com/CherryHQ/cherry-studio/blob/726446b54cd69ffe51a276638672f6d95ca0768c/src/main/ai/mcp/servers/python.ts#L97-L122","documentation":"Catch-all at the end of the python server's CallTool handler. Every exception inside the try-block — including a zod InvalidParams McpError (see 348) and any failure from `PythonService.executeScript` (Pyodide) — is logged and re-thrown as McpError InternalError. Because the catch does not re-throw McpError (unlike memory.ts), genuine validation failures and genuine execution failures are indistinguishable to the client.","triggerScenarios":"Pyodide raises a Python exception (`SyntaxError`, `NameError`, import failure, timeout); the sandboxed runtime crashes; a zod validation failure (re-wrapped here); a dependency listed via PEP 723 fails to install.","commonSituations":"Buggy submitted code; missing third-party package not declared via PEP 723 metadata; execution exceeds the clamped timeout; non-string `code` slipping past validation.","solutions":["Read the wrapped message — it preserves the underlying Python traceback/cause verbatim.","For dependency errors, declare packages via PEP 723 `# /// script` metadata at the top of `code`.","For timeouts, pass a larger `timeout` (clamped to MAX_TIMEOUT_MS = 10 min) or reduce work.","Server-side: add `if (error instanceof McpError) throw error` before wrapping so validation errors keep their real code."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Reduce runtime failures: declare third-party deps via PEP 723 and keep code under the timeout.\nfunction wrapPythonCode(userCode: string, deps: string[] = []): string {\n  const meta = deps.length\n    ? `# /// script\\n# dependencies = ${JSON.stringify(deps)}\\n# ///\\n`\n    : ''\n  return `${meta}${userCode}`\n}","typeGuard":"const isMcpInternalError = (e: unknown): boolean =>\n  e instanceof Error && (e as any).code === ErrorCode.InternalError","tryCatchPattern":"try {\n  await client.callTool({ name: 'python_execute', arguments: { code, timeout: 30000 } })\n} catch (e) {\n  if (e instanceof McpError && e.code === ErrorCode.InternalError) {\n    // e.message preserves the Python traceback / zod message verbatim — classify by content\n    if (/Timeout|timed out/i.test(e.message)) {/* shorten code or raise timeout (<= 600000) */}\n    else if (/ModuleNotFoundError|ImportError/.test(e.message)) {/* add PEP 723 dep */}\n    else {/* genuine Python exception — fix the submitted code */}\n  }\n  throw e\n}","preventionTips":["Declare every third-party import via PEP 723 script metadata.","Keep submitted code bounded; timeout is clamped to [1000ms, 600000ms].","Inspect the wrapped message — it carries the real Python cause.","Server-side: re-throw McpError before wrapping so validation vs runtime errors are distinguishable."],"tags":["mcp","internal-error","python-server","pyodide","catch-all"],"backgroundTag":null,"analyzedSha":"726446b54cd69ffe51a276638672f6d95ca0768c","analyzedAt":"2026-08-12T17:30:37.448Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}