Skip to main content
Glama
Nam0101

android-mcp-toolkit

convert-svg-to-android-drawable

Convert SVG markup or files into Android VectorDrawable XML for use in Android development, with options to control precision, colors, and output format.

Instructions

Convert SVG markup or files into Android VectorDrawable XML quickly, optionally writing to disk.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
svgNoInline SVG markup to convert
svgPathNoPath to an SVG file to read
outputPathNoOptional output path for generated VectorDrawable XML
floatPrecisionNoDecimal precision when serializing coordinates
fillBlackNoForce fill color black when missing
xmlTagNoInclude XML declaration
tintNoAndroid tint color (e.g. #FF000000)
cacheNoReuse cached result for identical inputs within this process

Implementation Reference

  • Main handler function executing the tool: loads SVG, applies caching, converts using svg2vectordrawable library, handles output file writing, logs performance, and returns content.
    async (params, extra) => { const svgCode = await loadSvg(params); const options = { floatPrecision: params.floatPrecision, fillBlack: params.fillBlack, xmlTag: params.xmlTag, tint: params.tint }; const cacheKey = makeCacheKey(svgCode, options); const startTime = process.hrtime.bigint(); let xml = null; if (params.cache) { xml = getCached(cacheKey); } if (!xml) { xml = await svg2vectordrawable(svgCode, options); if (!xml || typeof xml !== 'string') { throw new Error('Conversion did not produce XML'); } setCache(cacheKey, xml); } const savedPath = await maybeWriteOutput(params.outputPath, xml); const elapsedMs = Number(process.hrtime.bigint() - startTime) / 1_000_000; if (extra && typeof extra.sessionId === 'string') { server .sendLoggingMessage( { level: 'info', data: `Converted SVG in ${elapsedMs.toFixed(2)}ms` + (savedPath ? ` (saved to ${savedPath})` : '') }, extra.sessionId ) .catch(() => { /* best-effort logging */ }); } const content = []; if (savedPath) { content.push({ type: 'text', text: `Saved VectorDrawable to ${savedPath}` }); } content.push({ type: 'text', text: xml }); return { content }; }
  • Zod input schema defining parameters like svg/svgPath, outputPath, floatPrecision, fillBlack, xmlTag, tint, and cache.
    const convertInputSchema = z .object({ svg: z.string().min(1).describe('Inline SVG markup to convert').optional(), svgPath: z.string().min(1).describe('Path to an SVG file to read').optional(), outputPath: z .string() .min(1) .describe('Optional output path for generated VectorDrawable XML') .optional(), floatPrecision: z .number() .int() .min(0) .max(6) .default(2) .describe('Decimal precision when serializing coordinates'), fillBlack: z.boolean().default(false).describe('Force fill color black when missing'), xmlTag: z.boolean().default(false).describe('Include XML declaration'), tint: z.string().min(1).optional().describe('Android tint color (e.g. #FF000000)'), cache: z .boolean() .default(true) .describe('Reuse cached result for identical inputs within this process') }) .refine(data => data.svg || data.svgPath, { message: 'Provide either svg or svgPath' });
  • Tool registration within registerSvgTool function, specifying name, title, description, inputSchema, and handler.
    server.registerTool( 'convert-svg-to-android-drawable', { title: 'SVG to VectorDrawable', description: 'Convert SVG markup or files into Android VectorDrawable XML quickly, optionally writing to disk.', inputSchema: convertInputSchema }, async (params, extra) => { const svgCode = await loadSvg(params); const options = { floatPrecision: params.floatPrecision, fillBlack: params.fillBlack, xmlTag: params.xmlTag, tint: params.tint }; const cacheKey = makeCacheKey(svgCode, options); const startTime = process.hrtime.bigint(); let xml = null; if (params.cache) { xml = getCached(cacheKey); } if (!xml) { xml = await svg2vectordrawable(svgCode, options); if (!xml || typeof xml !== 'string') { throw new Error('Conversion did not produce XML'); } setCache(cacheKey, xml); } const savedPath = await maybeWriteOutput(params.outputPath, xml); const elapsedMs = Number(process.hrtime.bigint() - startTime) / 1_000_000; if (extra && typeof extra.sessionId === 'string') { server .sendLoggingMessage( { level: 'info', data: `Converted SVG in ${elapsedMs.toFixed(2)}ms` + (savedPath ? ` (saved to ${savedPath})` : '') }, extra.sessionId ) .catch(() => { /* best-effort logging */ }); } const content = []; if (savedPath) { content.push({ type: 'text', text: `Saved VectorDrawable to ${savedPath}` }); } content.push({ type: 'text', text: xml }); return { content }; } );
  • src/index.js:27-27 (registration)
    Invocation of registerSvgTool on the MCP server instance to register the tool.
    registerSvgTool(server);
  • Import of the underlying SVG to VectorDrawable converter library used in the handler.
    const svg2vectordrawable = require('../../vendor/svg2vectordrawable');

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/Nam0101/android-mcp-toolkit'

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