renderGeometricImage
Generate precise geometric images by compiling Asymptote code into SVG or PNG formats, with customizable rendering quality for optimal visual output.
Instructions
Renders an image from Asymptote code.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| asyCode | Yes | A string containing complete and valid Asymptote code to be compiled. The server executes this code directly. Ensure necessary `import` statements (e.g., `import graph;`) and settings (e.g., `unitsize(1cm);`) are included within this code block if needed. | |
| outputParams | No | Optional parameters to control the output image. |
Implementation Reference
- src/server.ts:131-236 (handler)The primary handler function `handleRenderGeometricImage` that processes Asymptote code: writes to temp file, spawns `asy` process, captures base64 image output with optional logs, handles errors.private async handleRenderGeometricImage(args: AsyToolArguments): Promise<CallToolResult> { const { asyCode, outputParams } = args; const format = outputParams?.format || 'svg'; const renderLevel = outputParams?.renderLevel || 4; if (asyCode.trim() === '') { throw new McpError(ErrorCode.InvalidParams, 'asyCode parameter cannot be empty.'); } const tempDir = os.tmpdir(); const uniqueId = uuidv4(); const baseOutputName = uniqueId; const asyFilePath = path.join(tempDir, `${baseOutputName}.asy`); const outputPathForAsyO = path.join(tempDir, baseOutputName); const actualOutputFilePath = path.join(tempDir, `${baseOutputName}.${format}`); let logs = ''; try { await fs.writeFile(asyFilePath, asyCode, 'utf-8'); const asyCmdArgs: string[] = ['-f', format]; if (format === 'png') { asyCmdArgs.push(`-render=${renderLevel}`); } asyCmdArgs.push('-o', outputPathForAsyO, asyFilePath); const asyProcess = spawn('asy', asyCmdArgs, { cwd: tempDir }); let processErrorStr = ''; asyProcess.stdout.on('data', (data) => { logs += `[ASY STDOUT]: ${data.toString()}`; }); asyProcess.stderr.on('data', (data) => { logs += `[ASY STDERR]: ${data.toString()}`; }); const exitCode = await new Promise<number | null>((resolve, reject) => { asyProcess.on('error', (err) => { processErrorStr = `Failed to start Asymptote process: ${err.message}`; logs += `[PROCESS ERROR]: ${processErrorStr}\n`; reject(new Error(processErrorStr)); }); asyProcess.on('exit', (code) => resolve(code)); }); if (processErrorStr) { const message = `Failed to start Asymptote process: ${processErrorStr}. Logs: ${logs.trim()}`; throw new McpError(ErrorCode.InternalError, message, { originalError: processErrorStr, logs: logs.trim() }); } if (exitCode !== 0) { logs += `[ASY EXIT CODE]: ${exitCode}\n`; const message = `Asymptote process exited with code ${exitCode}. Output file not found. Logs: ${logs.trim()}`; try { await fs.access(actualOutputFilePath); } catch (e) { throw new McpError(ErrorCode.InternalError, message, { logs: logs.trim() }); } // If fs.access succeeded but we still consider it an error due to exitCode, // this part might need refinement, but usually non-zero exit means failure. // For now, if file exists, we proceed, but the log indicates an issue. } try { await fs.access(actualOutputFilePath); } catch (e) { const message = `Asymptote process completed (exit code ${exitCode}), but output file ${baseOutputName}.${format} was not created. Logs: ${logs.trim()}`; throw new McpError(ErrorCode.InternalError, message, { logs: logs.trim() }); } const imageBuffer = await fs.readFile(actualOutputFilePath); const base64Data = imageBuffer.toString('base64'); const imageContent: ImageContent = { type: 'image', mimeType: `image/${format === 'svg' ? 'svg+xml' : format}`, data: base64Data, }; const contentParts: (ImageContent | TextContent)[] = [imageContent]; if (logs && logs.trim() !== '') { const logContent: TextContent = { type: 'text', text: `Asymptote Logs:\n${logs.trim()}` }; contentParts.push(logContent); } return { content: contentParts, }; } catch (error: any) { const currentLogs = logs.trim(); let finalMessage = `Server error: ${error.message}`; if (currentLogs) { finalMessage += `. Logs: ${currentLogs}`; } if (error instanceof McpError) { const errorData = error.data || {}; // Ensure message includes current logs if not already part of error.message const combinedMessage = error.message.includes(currentLogs) ? error.message : `${error.message}. Current server logs: ${currentLogs}`; throw new McpError(error.code, combinedMessage, { ...errorData, serverLogs: currentLogs }); } throw new McpError(ErrorCode.InternalError, finalMessage, { serverLogs: currentLogs }); } finally { fs.unlink(asyFilePath).catch(e => console.error(`Failed to delete temp file ${asyFilePath}:`, e)); fs.unlink(actualOutputFilePath).catch(e => { /* ignore if file wasn't created */ }); } }
- src/server.ts:39-66 (schema)Zod-like input schema for renderGeometricImage tool defining required `asyCode` and optional `outputParams` with format and renderLevel.const renderGeometricImageInputSchema = { type: 'object' as const, properties: { asyCode: { type: 'string', description: 'A string containing complete and valid Asymptote code to be compiled. The server executes this code directly. Ensure necessary `import` statements (e.g., `import graph;`) and settings (e.g., `unitsize(1cm);`) are included within this code block if needed.' }, outputParams: { type: 'object' as const, description: 'Optional parameters to control the output image.', properties: { format: { type: 'string', enum: ['svg', 'png'], description: 'The desired output image format. "svg" (default) produces scalable vector graphics, ideal for high-quality diagrams and plots. "png" produces raster graphics, which have broader compatibility with clients that may not support SVG directly (e.g., some versions of Cherry Studio). If targeting such clients, explicitly specify "png".' }, renderLevel: { type: 'number', description: 'For PNG output only. Specifies the rendering quality (supersampling level for antialiasing). Higher values (e.g., 4 or 8) produce smoother images but take longer to render and result in larger files. Asymptote\'s own default is 2. This server defaults to 4 if "png" format is chosen and renderLevel is not specified. Ignored for SVG output.' } }, required: [] as string[], // outputParams itself is optional additionalProperties: false, } }, required: ['asyCode'] as string[], additionalProperties: false, };
- src/server.ts:104-110 (registration)Tool registration in ListToolsRequestSchema handler, specifying name, description, and inputSchema.const tools: Tool[] = [ { name: 'renderGeometricImage', description: 'Renders an image from Asymptote code.', inputSchema: renderGeometricImageInputSchema, }, ];