mapbox-create-map-mcp 1.4.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -106,6 +106,47 @@ npx mapbox-create-map-mcp
106
106
  }
107
107
  ```
108
108
 
109
+ #### CreateIcon - 搜索图标
110
+
111
+ 从 iconfont 搜索图标并返回 `data:image/svg+xml;base64` 格式的图标数据,可直接用于 `<img>` 标签的 `src` 属性。
112
+
113
+ 输入参数:
114
+ - `keyword` (string, 必需): 搜索关键词,例如 "车辆"、"建筑"、"定位"
115
+ - `id` (number, 可选): 图标ID,传入后会优先查找该ID的图标,方便复用之前的图标
116
+ - `index` (number, 可选): 选择搜索结果中的第几个图标,从0开始,范围0-49,默认0
117
+ - `color` (string, 可选): 图标颜色,十六进制格式如 "#FF0000"。**不传则保持原始颜色**
118
+ - `size` (number, 可选): 图标大小,单位像素,默认48
119
+
120
+ 返回结果示例:
121
+ ```json
122
+ {
123
+ "success": true,
124
+ "data": "data:image/svg+xml;base64,PHN2ZyBjbGFzcz0i...",
125
+ "name": "建筑",
126
+ "id": 3841001
127
+ }
128
+ ```
129
+
130
+ 使用示例:
131
+ ```javascript
132
+ // 基本搜索
133
+ { keyword: "建筑" }
134
+
135
+ // 选择第3个结果
136
+ { keyword: "建筑", index: 2 }
137
+
138
+ // 使用之前的ID精确查找
139
+ { keyword: "建筑", id: 3841001 }
140
+
141
+ // 指定颜色和大小
142
+ { keyword: "建筑", color: "#FF5500", size: 64 }
143
+ ```
144
+
145
+ 返回的 `data` 可直接用于:
146
+ ```html
147
+ <img src="data:image/svg+xml;base64,PHN2Zy..." />
148
+ ```
149
+
109
150
  ## JavaScript 库使用
110
151
 
111
152
  ### 基本使用
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mapbox-create-map-mcp",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "A Mapbox-based MCP tool for creating geographic data visualizations",
5
5
  "main": "dist/index.js",
6
6
  "module": "src/index.js",
package/src/icon.js CHANGED
@@ -10,11 +10,21 @@ class CreateIcon {
10
10
  required: true,
11
11
  description: '搜索关键词,例如: "车辆", "地图", "定位" 等'
12
12
  },
13
+ id: {
14
+ type: 'number',
15
+ required: false,
16
+ description: '图标ID,传入后会优先查找该ID的图标'
17
+ },
18
+ index: {
19
+ type: 'number',
20
+ required: false,
21
+ default: 0,
22
+ description: '选择搜索结果中的第几个图标,从0开始,范围0-49'
23
+ },
13
24
  color: {
14
25
  type: 'string',
15
26
  required: false,
16
- default: '#1890FF',
17
- description: '图标颜色,十六进制格式,例如: "#FF0000"'
27
+ description: '图标颜色,十六进制格式,例如: "#FF0000"。不传则保持原始颜色'
18
28
  },
19
29
  size: {
20
30
  type: 'number',
@@ -29,7 +39,9 @@ class CreateIcon {
29
39
  * 搜索图标
30
40
  * @param {Object} config - 搜索配置
31
41
  * @param {string} config.keyword - 搜索关键词
32
- * @param {string} [config.color] - 图标颜色
42
+ * @param {number} [config.id] - 图标ID,优先查找
43
+ * @param {number} [config.index] - 选择第几个(0-49)
44
+ * @param {string} [config.color] - 图标颜色,不传保持原色
33
45
  * @param {number} [config.size] - 图标大小
34
46
  * @returns {Promise<Object>} 返回图标数据
35
47
  */
@@ -41,11 +53,16 @@ class CreateIcon {
41
53
  const https = require('https');
42
54
  const querystring = require('querystring');
43
55
 
56
+ // 确定 index,限制在0-49范围
57
+ let index = config.index !== undefined ? Math.max(0, Math.min(49, Math.floor(config.index))) : 0;
58
+ // pageSize 取 index + 1,确保能获取到指定位置的图标
59
+ const pageSize = index + 1;
60
+
44
61
  const postData = querystring.stringify({
45
62
  q: config.keyword,
46
63
  sortType: 'updated_at',
47
64
  page: 1,
48
- pageSize: 10,
65
+ pageSize: pageSize,
49
66
  fromCollection: -1,
50
67
  complex: 1,
51
68
  t: Date.now(),
@@ -75,7 +92,20 @@ class CreateIcon {
75
92
  try {
76
93
  const result = JSON.parse(data);
77
94
  if (result.code === 200 && result.data && result.data.icons && result.data.icons.length > 0) {
78
- const icon = result.data.icons[0];
95
+ const icons = result.data.icons;
96
+ let icon = null;
97
+
98
+ // 优先按 id 查找
99
+ if (config.id !== undefined) {
100
+ icon = icons.find(i => i.id === config.id);
101
+ }
102
+
103
+ // 没找到则按 index 选择,越界默认第一个
104
+ if (!icon) {
105
+ const safeIndex = index < icons.length ? index : 0;
106
+ icon = icons[safeIndex];
107
+ }
108
+
79
109
  let svgContent = icon.show_svg;
80
110
 
81
111
  if (!svgContent) {
@@ -83,14 +113,17 @@ class CreateIcon {
83
113
  return;
84
114
  }
85
115
 
86
- // 应用颜色和大小
87
- const color = config.color || '#1890FF';
116
+ // 应用大小
88
117
  const size = config.size || 48;
89
118
 
90
- // 替换fill颜色
91
- svgContent = svgContent.replace(/fill="[^"]*"/g, `fill="${color}"`);
92
- // 设置宽高
93
- svgContent = svgContent.replace(/style="[^"]*"/, `style="width: ${size}px; height: ${size}px; vertical-align: middle; fill: ${color}; overflow: hidden;"`);
119
+ // 只有传入 color 时才替换颜色,否则保持原始样式
120
+ if (config.color) {
121
+ svgContent = svgContent.replace(/fill="[^"]*"/g, `fill="${config.color}"`);
122
+ svgContent = svgContent.replace(/style="[^"]*"/, `style="width: ${size}px; height: ${size}px; vertical-align: middle; fill: ${config.color}; overflow: hidden;"`);
123
+ } else {
124
+ // 不修改颜色,只设置宽高
125
+ svgContent = svgContent.replace(/style="[^"]*"/, `style="width: ${size}px; height: ${size}px; vertical-align: middle; overflow: hidden;"`);
126
+ }
94
127
 
95
128
  // 转换为data URI (base64编码)
96
129
  const base64 = Buffer.from(svgContent).toString('base64');
package/src/server.js CHANGED
@@ -199,11 +199,21 @@ class CreateIcon {
199
199
  required: true,
200
200
  description: '搜索关键词,例如: "车辆", "地图", "定位" 等'
201
201
  },
202
+ id: {
203
+ type: 'number',
204
+ required: false,
205
+ description: '图标ID,传入后会优先查找该ID的图标'
206
+ },
207
+ index: {
208
+ type: 'number',
209
+ required: false,
210
+ default: 0,
211
+ description: '选择搜索结果中的第几个图标,从0开始,范围0-49'
212
+ },
202
213
  color: {
203
214
  type: 'string',
204
215
  required: false,
205
- default: '#1890FF',
206
- description: '图标颜色,十六进制格式,例如: "#FF0000"'
216
+ description: '图标颜色,十六进制格式,例如: "#FF0000"。不传则保持原始颜色'
207
217
  },
208
218
  size: {
209
219
  type: 'number',
@@ -222,11 +232,16 @@ class CreateIcon {
222
232
  const https = require('https');
223
233
  const querystring = require('querystring');
224
234
 
235
+ // 确定 index,限制在0-49范围
236
+ let index = config.index !== undefined ? Math.max(0, Math.min(49, Math.floor(config.index))) : 0;
237
+ // pageSize 取 index + 1,确保能获取到指定位置的图标
238
+ const pageSize = index + 1;
239
+
225
240
  const postData = querystring.stringify({
226
241
  q: config.keyword,
227
242
  sortType: 'updated_at',
228
243
  page: 1,
229
- pageSize: 10,
244
+ pageSize: pageSize,
230
245
  fromCollection: -1,
231
246
  complex: 1,
232
247
  t: Date.now(),
@@ -256,7 +271,20 @@ class CreateIcon {
256
271
  try {
257
272
  const result = JSON.parse(data);
258
273
  if (result.code === 200 && result.data && result.data.icons && result.data.icons.length > 0) {
259
- const icon = result.data.icons[0];
274
+ const icons = result.data.icons;
275
+ let icon = null;
276
+
277
+ // 优先按 id 查找
278
+ if (config.id !== undefined) {
279
+ icon = icons.find(i => i.id === config.id);
280
+ }
281
+
282
+ // 没找到则按 index 选择,越界默认第一个
283
+ if (!icon) {
284
+ const safeIndex = index < icons.length ? index : 0;
285
+ icon = icons[safeIndex];
286
+ }
287
+
260
288
  let svgContent = icon.show_svg;
261
289
 
262
290
  if (!svgContent) {
@@ -264,14 +292,17 @@ class CreateIcon {
264
292
  return;
265
293
  }
266
294
 
267
- // 应用颜色和大小
268
- const color = config.color || '#1890FF';
295
+ // 应用大小
269
296
  const size = config.size || 48;
270
297
 
271
- // 替换fill颜色
272
- svgContent = svgContent.replace(/fill="[^"]*"/g, `fill="${color}"`);
273
- // 设置宽高
274
- svgContent = svgContent.replace(/style="[^"]*"/, `style="width: ${size}px; height: ${size}px; vertical-align: middle; fill: ${color}; overflow: hidden;"`);
298
+ // 只有传入 color 时才替换颜色,否则保持原始样式
299
+ if (config.color) {
300
+ svgContent = svgContent.replace(/fill="[^"]*"/g, `fill="${config.color}"`);
301
+ svgContent = svgContent.replace(/style="[^"]*"/, `style="width: ${size}px; height: ${size}px; vertical-align: middle; fill: ${config.color}; overflow: hidden;"`);
302
+ } else {
303
+ // 不修改颜色,只设置宽高
304
+ svgContent = svgContent.replace(/style="[^"]*"/, `style="width: ${size}px; height: ${size}px; vertical-align: middle; overflow: hidden;"`);
305
+ }
275
306
 
276
307
  // 转换为data URI
277
308
  const base64 = Buffer.from(svgContent).toString('base64');