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
| Name | Required | Description | Default |
|---|---|---|---|
| svg | No | Inline SVG markup to convert | |
| svgPath | No | Path to an SVG file to read | |
| outputPath | No | Optional output path for generated VectorDrawable XML | |
| floatPrecision | No | Decimal precision when serializing coordinates | |
| fillBlack | No | Force fill color black when missing | |
| xmlTag | No | Include XML declaration | |
| tint | No | Android tint color (e.g. #FF000000) | |
| cache | No | Reuse cached result for identical inputs within this process |
Implementation Reference
- src/tools/svgTool.js:94-145 (handler)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 }; }
- src/tools/svgTool.js:17-41 (schema)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' });
- src/tools/svgTool.js:86-146 (registration)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);
- src/tools/svgTool.js:5-5 (helper)Import of the underlying SVG to VectorDrawable converter library used in the handler.const svg2vectordrawable = require('../../vendor/svg2vectordrawable');