customs-ui-kit-mcp 1.0.0 → 1.0.2

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.
Files changed (2) hide show
  1. package/dist/index.js +105 -19
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -39,33 +39,75 @@ const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
39
39
  const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
40
40
  const fs = __importStar(require("fs"));
41
41
  const path = __importStar(require("path"));
42
+ const child_process_1 = require("child_process");
43
+ // 缓存版本检查结果,避免频繁请求 npm
44
+ let versionCheckCache = {
45
+ localVersion: null,
46
+ latestVersion: null,
47
+ lastChecked: 0,
48
+ };
42
49
  // 动态解析 UI package 路径
43
- // 当作为 npm 包全局安装时,尝试在 node_modules 中寻找 customs-ui-kit
44
- function getUiPackagePath() {
50
+ function getUiPackageInfo() {
51
+ let uiPath = path.resolve(__dirname, "../../ui/src/components");
52
+ let version = "unknown";
45
53
  // 1. 本地开发环境路径 (Monorepo)
46
- const localPath = path.resolve(__dirname, "../../ui/src/components");
47
- if (fs.existsSync(localPath)) {
48
- return localPath;
54
+ const localPkgPath = path.resolve(__dirname, "../../ui/package.json");
55
+ if (fs.existsSync(localPkgPath)) {
56
+ try {
57
+ const pkg = JSON.parse(fs.readFileSync(localPkgPath, "utf-8"));
58
+ version = pkg.version;
59
+ }
60
+ catch (e) { }
61
+ return { path: uiPath, version };
49
62
  }
50
- // 2. 作为 npm 包安装后的路径 (寻找兄弟依赖 customs-ui-kit)
63
+ // 2. 作为 npm 包安装后,在其他业务项目中使用
51
64
  try {
52
- const uiPkgPath = require.resolve("customs-ui-kit/package.json");
53
- // 假设发布时将 src 也打包进去了,或者直接读取 dist/types
54
- // 为了简单起见,我们假设团队成员也会安装 customs-ui-kit 到他们的项目中
55
- // 我们需要读取的是组件的元数据。
56
- // 这里我们先尝试找项目根目录下的 node_modules/customs-ui-kit/src/components
57
- const resolvedPath = path.join(path.dirname(uiPkgPath), "src/components");
65
+ // 关键修复 1:以当前执行目录 (业务项目根目录) 为起点进行解析
66
+ // 关键修复 2:解析主入口文件,而不是 package.json,避开 exports 限制
67
+ const mainPath = require.resolve("customs-ui-kit", { paths: [process.cwd()] });
68
+ // 向上两级找到 customs-ui-kit 的包根目录 (从 dist/xxx.js 回到包根目录)
69
+ const pkgRoot = path.resolve(path.dirname(mainPath), "..");
70
+ const uiPkgPath = path.join(pkgRoot, "package.json");
71
+ if (fs.existsSync(uiPkgPath)) {
72
+ const pkg = JSON.parse(fs.readFileSync(uiPkgPath, "utf-8"));
73
+ version = pkg.version;
74
+ }
75
+ // 寻找 src/components 目录
76
+ const resolvedPath = path.join(pkgRoot, "src/components");
58
77
  if (fs.existsSync(resolvedPath)) {
59
- return resolvedPath;
78
+ uiPath = resolvedPath;
60
79
  }
61
80
  }
62
81
  catch (e) {
63
82
  // 忽略错误
64
83
  }
65
- // 3. 如果找不到,返回一个默认路径防止报错崩溃
66
- return localPath;
84
+ return { path: uiPath, version };
85
+ }
86
+ const uiInfo = getUiPackageInfo();
87
+ const UI_PACKAGE_PATH = uiInfo.path;
88
+ // 检查版本是否落后
89
+ function checkVersionWarning() {
90
+ const now = Date.now();
91
+ // 每 1 小时检查一次,避免频繁调用 npm view
92
+ if (now - versionCheckCache.lastChecked > 3600000) {
93
+ try {
94
+ versionCheckCache.localVersion = uiInfo.version;
95
+ // 同步调用 npm view 获取最新版本
96
+ const latest = (0, child_process_1.execSync)("npm view customs-ui-kit version", { encoding: "utf-8" }).trim();
97
+ versionCheckCache.latestVersion = latest;
98
+ versionCheckCache.lastChecked = now;
99
+ }
100
+ catch (e) {
101
+ // 网络错误等情况,静默失败
102
+ versionCheckCache.lastChecked = now; // 依然更新时间,避免一直重试报错
103
+ }
104
+ }
105
+ const { localVersion, latestVersion } = versionCheckCache;
106
+ if (localVersion && latestVersion && localVersion !== "unknown" && localVersion !== latestVersion) {
107
+ return `\n\n⚠️ [系统提示] 您当前项目安装的 customs-ui-kit 版本为 v${localVersion},而 npm 上的最新版本为 v${latestVersion}。您可能无法使用最新的组件特性,建议运行 \`npm install customs-ui-kit@latest\` 进行升级。`;
108
+ }
109
+ return "";
67
110
  }
68
- const UI_PACKAGE_PATH = getUiPackagePath();
69
111
  const server = new index_js_1.Server({
70
112
  name: "customs-ui-kit-mcp",
71
113
  version: "1.0.0",
@@ -135,6 +177,14 @@ server.setRequestHandler(types_js_1.ListToolsRequestSchema, async () => {
135
177
  required: ["componentName"],
136
178
  },
137
179
  },
180
+ {
181
+ name: "get_versions",
182
+ description: "Get the version history and publish times of the Customs UI Kit from npm",
183
+ inputSchema: {
184
+ type: "object",
185
+ properties: {},
186
+ },
187
+ },
138
188
  ],
139
189
  };
140
190
  });
@@ -146,14 +196,39 @@ function getComponentExamples(componentName) {
146
196
  }
147
197
  return fs.readFileSync(storyPath, "utf-8");
148
198
  }
199
+ // 获取 npm 上的版本历史和时间
200
+ function getVersionsHistory() {
201
+ try {
202
+ // 使用 npm view 获取 time 字段,它包含了所有版本及其发布时间
203
+ const timeData = (0, child_process_1.execSync)("npm view customs-ui-kit time --json", { encoding: "utf-8" });
204
+ const times = JSON.parse(timeData);
205
+ // 过滤掉内部字段 (created, modified),只保留真实的语义化版本号
206
+ const versions = Object.keys(times)
207
+ .filter(v => v !== 'created' && v !== 'modified')
208
+ .map(v => ({
209
+ version: v,
210
+ publishTime: times[v]
211
+ }))
212
+ // 按时间倒序排列,最新的在前面
213
+ .sort((a, b) => new Date(b.publishTime).getTime() - new Date(a.publishTime).getTime());
214
+ return JSON.stringify({
215
+ currentLocalVersion: uiInfo.version,
216
+ npmVersions: versions
217
+ }, null, 2);
218
+ }
219
+ catch (e) {
220
+ return JSON.stringify({ error: "Failed to fetch version history from npm." });
221
+ }
222
+ }
149
223
  server.setRequestHandler(types_js_1.CallToolRequestSchema, async (request) => {
224
+ const versionWarning = checkVersionWarning();
150
225
  if (request.params.name === "list_components") {
151
226
  const components = getComponents();
152
227
  return {
153
228
  content: [
154
229
  {
155
230
  type: "text",
156
- text: JSON.stringify(components, null, 2),
231
+ text: JSON.stringify(components, null, 2) + versionWarning,
157
232
  },
158
233
  ],
159
234
  };
@@ -168,7 +243,7 @@ server.setRequestHandler(types_js_1.CallToolRequestSchema, async (request) => {
168
243
  content: [
169
244
  {
170
245
  type: "text",
171
- text: api,
246
+ text: api + versionWarning,
172
247
  },
173
248
  ],
174
249
  };
@@ -183,7 +258,18 @@ server.setRequestHandler(types_js_1.CallToolRequestSchema, async (request) => {
183
258
  content: [
184
259
  {
185
260
  type: "text",
186
- text: examples,
261
+ text: examples + versionWarning,
262
+ },
263
+ ],
264
+ };
265
+ }
266
+ if (request.params.name === "get_versions") {
267
+ const versionsInfo = getVersionsHistory();
268
+ return {
269
+ content: [
270
+ {
271
+ type: "text",
272
+ text: versionsInfo,
187
273
  },
188
274
  ],
189
275
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "customs-ui-kit-mcp",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "main": "dist/index.js",
5
5
  "bin": {
6
6
  "customs-ui-kit-mcp": "dist/index.js"