renderGeometricImage
Generate precise geometric diagrams and plots by compiling Asymptote code into SVG or PNG images for visualization and documentation.
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 main handler function that implements the renderGeometricImage tool. It writes the provided Asymptote code to a temporary file, spawns the 'asy' process with appropriate arguments for the specified format (SVG or PNG) and render level, captures stdout/stderr logs, verifies the output image file exists, reads and base64-encodes it, and returns it as ImageContent along with optional log text.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)TypeScript object defining the input schema for the renderGeometricImage tool, including required 'asyCode' string and optional 'outputParams' with format (svg/png) 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:103-112 (registration)Registration of the renderGeometricImage tool in the ListTools request handler, defining its name, description, and input schema.this.server.setRequestHandler(ListToolsRequestSchema, async () => { const tools: Tool[] = [ { name: 'renderGeometricImage', description: 'Renders an image from Asymptote code.', inputSchema: renderGeometricImageInputSchema, }, ]; return { tools }; });
- src/server.ts:114-128 (registration)Dispatch logic in the CallTool request handler that routes calls to 'renderGeometricImage' to the handleRenderGeometricImage method.this.server.setRequestHandler(CallToolRequestSchema, async (request): Promise<CallToolResult> => { const args = request.params.arguments as AsyToolArguments | undefined; if (request.params.name === 'renderGeometricImage') { if (!args || typeof args.asyCode !== 'string') { throw new McpError(ErrorCode.InvalidParams, 'Missing or invalid asyCode in arguments for renderGeometricImage tool.'); } return this.handleRenderGeometricImage(args); } else { throw new McpError( ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}` ); } });