convert_svg_to_react
Convert SVG strings or files into React components using SVGR, with settings for TypeScript, React Native, optimization, and formatting.
Instructions
Convert SVG content or file to React component using SVGR
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| svg | Yes | SVG content as string or file path to SVG file | |
| componentName | No | Name for the React component (default: MyComponent) | |
| options | No | SVGR configuration options |
Implementation Reference
- src/svgr.ts:3-32 (handler)The core handler function that converts SVG to React component using SVGR's transform function. Takes svg string, optional componentName, and SVGR options (Config). Manages plugin ordering (svgo, prettier) and calls @svgr/core transform.
export async function convertSvgToReact({ svg, componentName = "MyComponent", options = {}, }: { svg: string; componentName?: string; options: Config; }): Promise<string> { const plugins = ["@svgr/plugin-jsx"]; if (options.svgo !== false) { plugins.unshift("@svgr/plugin-svgo"); } if (options.prettier !== false) { plugins.push("@svgr/plugin-prettier"); } return await transform( svg, { plugins: plugins, ...options, }, { componentName, }, ); } - src/index.ts:14-116 (registration)Registration of the 'convert_svg_to_react' tool on the MCP server with its full input schema (zod) and the async handler callback that invokes convertSvgToReact and formats the response.
server.registerTool( "convert_svg_to_react", { description: "Convert SVG content or file to React component using SVGR", inputSchema: z.object({ svg: z .string() .describe("SVG content as string or file path to SVG file"), componentName: z .string() .optional() .describe("Name for the React component (default: MyComponent)"), options: z .object({ icon: z .union([z.boolean(), z.string(), z.number()]) .optional() .describe("Replace SVG width and height with a custom value"), native: z .boolean() .optional() .describe("Modify all SVG nodes for React Native"), typescript: z .boolean() .optional() .describe("Generate TypeScript component"), dimensions: z .boolean() .optional() .describe("Keep width and height attributes from the root SVG tag"), expandProps: z .union([z.enum(["start", "end"]), z.boolean()]) .optional() .describe("Forward properties to the SVG tag"), prettier: z .boolean() .optional() .describe("Format output with Prettier"), prettierConfig: z .record(z.string(), z.unknown()) .optional() .describe("Specify Prettier config"), svgo: z .boolean() .optional() .describe("Use SVGO to optimize SVG code"), ref: z.boolean().optional().describe("Add ref support to component"), memo: z .boolean() .optional() .describe("Wrap component with React.memo"), replaceAttrValues: z .record(z.string(), z.string()) .optional() .describe("Replace an attribute value by another"), svgProps: z .record(z.string(), z.string()) .optional() .describe("Add props to the root SVG tag"), titleProp: z.boolean().optional().describe("Add title prop support"), descProp: z .boolean() .optional() .describe("Add description prop support"), jsxRuntime: z .enum(["classic", "automatic", "classic-preact"]) .optional() .describe("Specify a JSX runtime to use"), exportType: z .enum(["named", "default"]) .optional() .describe("Specify export type (named or default)"), }) .default({}) .describe("SVGR configuration options"), }), }, async (args) => { try { const jsCode = await convertSvgToReact(args); return { content: [ { type: "text", text: jsCode, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error converting SVG: ${ error instanceof Error ? error.message : error }`, }, ], }; } }, ); - src/index.ts:18-89 (schema)Zod input schema for convert_svg_to_react defining parameters: svg (required string), componentName (optional), and options (object with icon, native, typescript, dimensions, expandProps, prettier, prettierConfig, svgo, ref, memo, replaceAttrValues, svgProps, titleProp, descProp, jsxRuntime, exportType).
inputSchema: z.object({ svg: z .string() .describe("SVG content as string or file path to SVG file"), componentName: z .string() .optional() .describe("Name for the React component (default: MyComponent)"), options: z .object({ icon: z .union([z.boolean(), z.string(), z.number()]) .optional() .describe("Replace SVG width and height with a custom value"), native: z .boolean() .optional() .describe("Modify all SVG nodes for React Native"), typescript: z .boolean() .optional() .describe("Generate TypeScript component"), dimensions: z .boolean() .optional() .describe("Keep width and height attributes from the root SVG tag"), expandProps: z .union([z.enum(["start", "end"]), z.boolean()]) .optional() .describe("Forward properties to the SVG tag"), prettier: z .boolean() .optional() .describe("Format output with Prettier"), prettierConfig: z .record(z.string(), z.unknown()) .optional() .describe("Specify Prettier config"), svgo: z .boolean() .optional() .describe("Use SVGO to optimize SVG code"), ref: z.boolean().optional().describe("Add ref support to component"), memo: z .boolean() .optional() .describe("Wrap component with React.memo"), replaceAttrValues: z .record(z.string(), z.string()) .optional() .describe("Replace an attribute value by another"), svgProps: z .record(z.string(), z.string()) .optional() .describe("Add props to the root SVG tag"), titleProp: z.boolean().optional().describe("Add title prop support"), descProp: z .boolean() .optional() .describe("Add description prop support"), jsxRuntime: z .enum(["classic", "automatic", "classic-preact"]) .optional() .describe("Specify a JSX runtime to use"), exportType: z .enum(["named", "default"]) .optional() .describe("Specify export type (named or default)"), }) .default({}) .describe("SVGR configuration options"), }), - src/svgr.ts:1-32 (helper)Imports the transform function from @svgr/core and the Config type, which are the core dependencies used by the handler.
import { type Config, transform } from "@svgr/core"; export async function convertSvgToReact({ svg, componentName = "MyComponent", options = {}, }: { svg: string; componentName?: string; options: Config; }): Promise<string> { const plugins = ["@svgr/plugin-jsx"]; if (options.svgo !== false) { plugins.unshift("@svgr/plugin-svgo"); } if (options.prettier !== false) { plugins.push("@svgr/plugin-prettier"); } return await transform( svg, { plugins: plugins, ...options, }, { componentName, }, ); }