better-icons MCP工具参考手册:11个工具参数、用法与返回示例全解
better-icons MCP工具参考手册:11个工具参数、用法与返回示例全解
better-icons 是一个面向 AI 编程助手的 图标搜索与获取工具,它同时支持 MCP 服务器和 CLI 两种用法,能跨 150+ 图标集合、检索 20 万+ 图标。本文作为 MCP 工具参考手册,完整拆解它提供的 11 个工具:每个工具的参数、默认值、用法示例和返回格式,帮助新手快速上手、少查文档。
一、它解决什么问题
在 AI 辅助写代码时,图标一直是痛点:模型往往不知道该用哪个图标、写不出正确的 SVG、风格不统一,还会把大段内联 SVG 直接贴进对话,白白消耗 token。
better-icons 的思路是:
- 🔍 搜索:一次搜索覆盖 150+ 图标集合(Lucide、Heroicons、Material Design 等)。
- 🧠 自动学习:记住你常用的图标集合,后续搜索自动优先展示。
- 📝 项目同步:图标直接写进你的
icons.tsx文件,而不是往聊天里贴 SVG(省 token)。
它的所有能力都围绕 src/server.ts 中注册的 11 个 MCP 工具展开,图标数据由 Iconify 接口提供(见 src/constants.ts)。
二、30 秒接入:MCP 服务器一键安装步骤
在 Agent 环境中执行下面命令即可交互式配置:
npx better-icons setup
该命令会自动为已检测到的工具写入 MCP 配置,支持 6 类 AI 编程工具。配置路径与检测逻辑见 src/agents.ts:
| 工具 | 全局配置文件路径 | 项目级配置文件 |
|---|---|---|
| Cursor | ~/.cursor/mcp.json |
.cursor/mcp.json |
| Claude Code | ~/.claude.json 或 ~/.claude/settings.json |
.mcp.json |
| OpenCode | ~/.config/opencode/opencode.json |
opencode.json |
| Windsurf | ~/.windsurf/mcp.json |
.windsurf/mcp.json |
| VS Code (Copilot) | ~/.vscode/mcp.json |
.vscode/mcp.json |
| Google Antigravity | ~/.gemini/antigravity/mcp_config.json |
mcp_config.json |
💡 只想配置当前项目?使用
npx better-icons setup -s project;想自动确认并走全局作用域,用npx better-icons setup -y。手动配置的说明可由npx better-icons config打印,逻辑位于 src/commands/config.ts 与 src/commands/setup.ts。
三、核心概念:Icon ID 与图标集合
所有工具都围绕一个关键格式 prefix:name 工作,其中 prefix 是集合前缀,name 是图标名:
lucide:home → Lucide 集合里的 home 图标
mdi:arrow-right → Material Design Icons 里的 arrow-right
heroicons:check → Heroicons 里的 check
| 前缀 | 集合名 | 风格 | 图标数 |
|---|---|---|---|
mdi |
Material Design Icons | 实心 | 7,000+ |
lucide |
Lucide Icons | 描边 | 1,500+ |
heroicons |
Heroicons | 双风格 | 300+ |
tabler |
Tabler Icons | 描边 | 5,000+ |
ph |
Phosphor Icons | 多风格 | 9,000+ |
simple-icons |
Simple Icons | 品牌 Logo | 3,000+ |
记不住前缀?直接用后面的
list_collections工具查。
四、11 个 MCP 工具:参数、用法与返回示例全解
以下每个工具都在 src/server.ts 中注册,参数类型由 Zod 严格校验,格式错误的
icon_id会直接返回错误提示。
1. search_icons — 跨集合搜索图标
最核心的入口工具,按关键词在所有集合中搜索。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
query |
string | 是 | 搜索关键词,如 arrow、home、user |
limit |
number | 否 | 最多返回数(1–999),默认 32 |
prefix |
string | 否 | 按集合过滤,如 mdi、lucide |
category |
string | 否 | 按分类过滤,如 General、Emoji |
用法示例
搜索 "arrow" 图标
在 lucide 集合里搜索 "home" 图标
返回示例
Found 128 icons (showing 32)
**Icons:**
- `lucide:arrow-right`
- `mdi:arrow-right-bold`
- `heroicons:arrow-right`
...
Use `get_icon` with any icon ID to get the SVG code.
_Results prioritized from your frequently used collections: lucide, mdi_
若你已有使用偏好,结果会自动把你常用集合的图标排到前面(见 src/memory.ts 的
sortByLearnedPreferences逻辑)。
2. get_icon — 获取单个图标 SVG
拿到具体图标的 SVG 代码,并附带 React/JSX 与 Iconify 的写法,是 search_icons 的下游。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
icon_id |
string | 是 | 格式 prefix:name,如 mdi:home |
color |
string | 否 | 颜色,如 #ff0000、currentColor |
size |
number | 否 | 尺寸(像素) |
用法示例
获取 mdi:home 的 SVG
获取 lucide:arrow-right,尺寸 24
返回示例
# Icon: mdi:home
**Dimensions:** 24x24
## SVG
<svg xmlns="..." width="24" height="24" viewBox="0 0 24 24">...</svg>
## React/JSX
<svg xmlns="..." className="...">...</svg>
## Iconify
import { Icon } from '@iconify/react';
<Icon icon="mdi:home" />
每次成功获取都会触发
trackUsage,用于自动学习你的偏好与记录历史。
3. list_collections — 列出可用图标集合
想知道有哪些前缀、各有多少图标时用。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
category |
string | 否 | 按分类过滤 |
search |
string | 否 | 按集合名搜索 |
返回示例(按图标总数排序,最多显示前 50 个)
# Icon Collections
Found 150 collections (showing top 50)
- **mdi** - Material Design Icons (7,000+ icons)
- **ph** - Phosphor Icons (9,000+ icons)
- **tabler** - Tabler Icons (5,000+ icons)
...
**Popular:** mdi, lucide, heroicons, tabler, ph, ri
4. recommend_icons — 按使用场景推荐图标
不知道用哪个图标时,用自然语言描述场景即可。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
use_case |
string | 是 | 场景描述,如 settings button、navigation menu |
style |
enum | 否 | solid / outline / any,默认 any |
limit |
number | 否 | 推荐数量(1–20),默认 10 |
返回示例
# Recommendations for "settings button"
- `mdi:cog`
- `lucide:settings`
- `heroicons:cog-6-tooth`
...
Use `get_icon` to get SVG code.
5. find_similar_icons — 查找同款 / 相似图标
想要"同一个图标在不同集合里的风格变体"时用,方便统一切换实心/描边。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
icon_id |
string | 是 | 目标图标,如 lucide:home |
limit |
number | 否 | 最多返回(1–50),默认 10 |
返回示例(精确同名优先,再列相似)
# Similar Icons for `lucide:home`
**Same icon in other collections:**
- `mdi:home`
- `heroicons:home`
**Related icons:**
- `lucide:home-2`
Use `get_icon` to retrieve any of these icons.
6. get_icons — 批量获取多个图标
比多次调用 get_icon 更高效:内部按 prefix 分组、合并请求(见 src/server.ts 的 byPrefix 逻辑)。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
icon_ids |
array | 是 | 图标 ID 数组,最多 20 个 |
color |
string | 否 | 所有图标的颜色 |
size |
number | 否 | 所有图标的尺寸 |
返回示例(按输入顺序对齐,失败项单独列出)
# 3 Icons Retrieved
## lucide:home
<svg ...>...</svg>
## lucide:settings
<svg ...>...</svg>
## lucide:user
<svg ...>...</svg>
**Failed to retrieve:**
- `mdi:xxx`: Not found
7. sync_icon — 一键写入项目图标文件(推荐用法)⭐
这是官方推荐的加图标方式:取到图标后自动写入你的 icons 文件,并返回 import 与用法,避免在聊天里贴 SVG。写入与组件生成逻辑见 src/project-sync.ts。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
icons_file |
string | 是 | 图标文件绝对路径,如 /path/src/components/icons.tsx |
framework |
enum | 是 | react / vue / svelte / solid / svg |
icon_id |
string | 是 | 格式 prefix:name |
component_name |
string | 否 | 自定义组件名,缺省自动生成 |
color |
string | 否 | 图标颜色 |
size |
number | 否 | 图标尺寸 |
组件名规则:图标名转为 PascalCase 并加 Icon 后缀,例如 lucide:arrow-right → ArrowRightIcon。
返回示例
# Icon Added
**Icon:** lucide:home
**Component:** HomeIcon
**File:** /path/src/components/icons.tsx
## Import
import { HomeIcon } from './components/icons';
## Usage
<HomeIcon />
若图标已存在,会返回
# Icon Already Exists并给出现有组件名,避免重复写入。
8. scan_project_icons — 扫描项目已有图标
查看某个 icons 文件里已经有哪些图标,防止重复添加。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
icons_file |
string | 是 | 图标文件绝对路径 |
返回示例
# Project Icons
**File:** /path/src/components/icons.tsx
**Total:** 3 icons
- `HomeIcon` ← lucide:home
- `SettingsIcon` ← mdi:cog
- `UserIcon` ← heroicons:user
Use these component names directly in your code, or use `sync_icon` to add more.
9. get_recent_icons — 查看最近使用的图标
快速复用你刚取过的图标,无需再搜索。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
limit |
number | 否 | 显示条数(1–50),默认 20 |
返回示例(带相对时间)
# Recent Icons
1. `lucide:home` - 2m ago
2. `mdi:cog` - 1h ago
3. `heroicons:check` - 3d ago
Use `get_icon` or `get_icons` to retrieve any of these again.
10. get_icon_preferences — 查看学习到的偏好
查看服务自动记住的集合使用统计(无参数)。
返回示例
# Your Icon Preferences
The server has learned these collection preferences based on your usage:
- **lucide**: 42 uses (last: 2026/10/10)
- **mdi**: 18 uses (last: 2026/10/12)
Search results and recommendations will prioritize icons from these collections.
尚未积累偏好时,会提示先调用
get_icon开始使用。
11. clear_icon_preferences — 重置偏好记忆
想换个图标风格、从零开始学习时用(无参数)。执行后服务会重新开始记录你的使用习惯。
返回示例
Icon preferences have been cleared. The server will start learning your preferences again from scratch.
五、自动学习机制:它如何记住你
以上 search_icons、recommend_icons、get_icon 等工具之所以"越用越懂你",靠的是一套本地记忆:
- 📍 所有偏好与历史保存在
~/.better-icons/preferences.json(路径定义见 src/memory.ts 的getPreferencesPath)。 - 📈 每次
trackUsage都会累加该集合的使用次数并更新lastUsed。 - 🕘 历史最多保留 50 条(
MAX_HISTORY_SIZE),最新在最前。 - 🔒 采用原子写入(先写临时文件再重命名),避免并发写坏数据。
这套机制让搜索结果与推荐天然贴合你团队的视觉风格,无需额外配置。
六、典型工作流小结
search_icons找到候选 → 2.get_icon看 SVG 效果 → 3.sync_icon直接落盘到项目 → 4.scan_project_icons核对不重复 → 5.get_recent_icons随时复用。
配合 CLI 使用也很方便:npx better-icons search arrow --limit 10、npx better-icons get lucide:home --size 24,分别对应 src/commands/search.ts 和 src/commands/get.ts。掌握上面这 11 个工具,基本能覆盖图标检索、获取、风格统一与项目落盘的全部场景。