Skip to main content
Glama

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
NameRequiredDescriptionDefault
asyCodeYesA 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.
outputParamsNoOptional parameters to control the output image.

Implementation Reference

  • 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 */ });
      }
    }
  • 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}`
        );
      }
    });
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It discloses that the tool 'Renders an image' and implies execution of code, but it doesn't mention behavioral traits like error handling (e.g., what happens with invalid code), performance (e.g., rendering time), or output specifics (e.g., image dimensions). The description is minimal, leaving gaps in transparency for a code-execution tool.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence: 'Renders an image from Asymptote code.' It is front-loaded with the core purpose and has zero waste, making it highly concise and well-structured for its simplicity.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity (code execution with optional parameters), no annotations, and no output schema, the description is incomplete. It doesn't cover output details (e.g., what the image looks like, error responses) or behavioral aspects. However, the schema provides good parameter documentation, so it's minimally adequate but lacks context for a tool that executes external code.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all parameters thoroughly. The description adds no additional meaning beyond the schema's details on 'asyCode' and 'outputParams'. It doesn't explain parameter interactions or provide examples, so it meets the baseline for high schema coverage without compensating value.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Renders an image from Asymptote code.' It specifies the verb ('Renders') and resource ('image'), and while there are no sibling tools to distinguish from, the description is specific about the input type (Asymptote code). However, it doesn't mention what kind of image is produced (e.g., geometric diagrams, plots) beyond the Asymptote context, which could be more precise.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives, as there are no sibling tools mentioned. It lacks context about typical use cases (e.g., generating diagrams for documentation, creating plots) or prerequisites (e.g., needing valid Asymptote syntax). Without siblings, the score is based on the absence of any usage context or exclusions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Luorivergoddess/mcp-geo'

If you have feedback or need assistance with the MCP directory API, please join our Discord server