一个用于从SVG文件生成和管理图标字体的Model Context Protocol (MCP)服务器。此工具提供了全面的字体生成能力,包括向后兼容性和高级字形提取功能。
git clone <repository-url>
cd mcp-svg-to-font
pnpm install
pnpm build
pnpm dev
npx @modelcontextprotocol/inspector npx tsx src/main.ts
pnpm build
将以下配置添加到您的MCP客户端(例如Claude Desktop):
{
"mcpServers": {
"svg-to-font": {
"command": "npx",
"args": ["tsx", "src/main.ts"]
}
}
}
对于生产环境(已编译版本):
{
"mcpServers": {
"svg-to-font": {
"command": "node",
"args": ["./dist/main.js"]
}
}
}
list-svgs目的:扫描并列出目录中的所有SVG文件
参数:
directory (字符串,必需):要扫描SVG文件的目录路径使用场景:
示例:
{
"directory": "./icons"
}
generate-font-from-svgs目的:从一组SVG文件创建全新的图标字体
参数:
directory (字符串,必需):包含SVG文件的目录fontName (字符串,可选):字体族名称(默认:"IconFont")outputDir (字符串,可选):输出目录(默认:"./fonts")formats (数组,可选):要生成的字体格式(默认:["woff2", "woff", "ttf"])cssPrefix (字符串,可选):CSS类前缀(默认:"icon")generateTypes (布尔值,可选):是否生成TypeScript类型(默认:true)使用场景:
生成的文件:
FontName.ttf,FontName.woff,FontName.woff2FontName.cssFontName.types.tsextend-existing-font目的:在保留所有现有图标及其Unicode值的情况下,向现有字体添加新图标
参数:
existingFontDir (字符串,必需):包含现有字体文件(.css和字体文件)的目录originalSvgDirectory (字符串,必需):包含用于创建现有字体的原始SVG文件的目录newSvgDirectory (字符串,必需):包含要添加的新SVG文件的目录fontName (字符串,可选):字体名称(未提供时自动检测)outputDir (字符串,可选):输出目录(默认:与现有字体相同)cssPrefix (字符串,可选):CSS类前缀(未提供时自动检测)generateTypes (布尔值,可选):是否生成TypeScript类型(默认:true)使用场景:
要求:
兼容性: ✅ 100% 向后兼容 - 现有项目无需更改继续工作
extend-font-advanced目的:通过直接从TTF文件中提取原始字形来向现有字体添加新图标,无需原始SVG文件
参数:
existingFontDir (字符串,必需):包含现有字体文件(.css和.ttf文件)的目录newSvgDirectory (字符串,必需):包含要添加的新SVG文件的目录fontName (字符串,可选):字体名称(从文件中自动检测)outputDir (字符串,可选):输出目录(默认:与现有字体相同)cssPrefix (字符串,可选):CSS类前缀(从CSS中自动检测)generateTypes (布尔值,可选项):是否生成TypeScript类型(默认:true)preserveMetrics (布尔值,可选项):是否保留原始字体度量(默认:true)使用场景:
高级特性:
要求:
.icon-name:before { content: "\\e001"; }技术流程:
兼容性: ✅ 100% 向后兼容 - 所有原始图标保持其Unicode值
生成字体后,您将得到:
fonts/
├── IconFont.css # 包含font-face和图标类的CSS
├── IconFont.ttf # TrueType字体
├── IconFont.woff # Web Open Font Format
├── IconFont.woff2 # Web Open Font Format 2.0
└── IconFont.types.ts # TypeScript类型定义
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="./fonts/IconFont.css" />
</head>
<body>
<i class="icon icon-home"></i>
<i class="icon icon-user"></i>
<i class="icon icon-settings"></i>
</body>
</html>
.icon {
font-family: 'IconFont';
font-size: 24px;
color: #333;
}
.icon-home:before {
content: '\e001';
}
import { IconName, getIconClass, ICON_NAMES } from './fonts/IconFont.types';
// 类型安全的图标使用
const iconName: IconName = 'home';
const className = getIconClass(iconName); // 返回 "icon-home"
// 获取所有可用图标
const allIcons = ICON_NAMES; // ['home', 'user', 'settings', ...]
import { IconName, getIconClass } from './fonts/IconFont.types';
interface IconProps {
name: IconName;
className?: string;
}
const Icon: React.FC<IconProps> = ({ name, className = '' }) => {
return <i className={`icon ${getIconClass(name)} ${className}`} />;
};
// 使用
<Icon name="home" className="text-blue-500" />;
list-svgs验证文件generate-font-from-svgs创建字体extend-existing-font与原始SVG文件一起extend-font-advanced合并它们extend-existing-font:需要原始SVG文件extend-font-advanced:需要TTF文件和包含Unicode映射的CSS您的CSS文件必须包含这种格式的Unicode映射:
.icon-home:before {
content: '\e001';
}
.icon-user:before {
3content: '\e002';
}
.icon-settings:before {
content: '\e003';
}
“未找到SVG文件”
.svg扩展名“检测到图标名称冲突”
“未找到TTF文件”
“无法解析现有字体”