Skip to content

Commit 6656fa2

Browse files
committed
fix: npm version
1 parent 1124442 commit 6656fa2

4 files changed

Lines changed: 50 additions & 5 deletions

File tree

mcp/package-lock.json

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mcp/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"@rollup/plugin-inject": "^5.0.5",
7272
"@rollup/plugin-json": "^6.1.0",
7373
"@rollup/plugin-node-resolve": "^16.0.0",
74+
"@rollup/plugin-replace": "^6.0.2",
7475
"@rollup/plugin-terser": "^0.4.4",
7576
"@rollup/plugin-typescript": "^12.1.2",
7677
"@types/express": "^5.0.3",

mcp/rollup.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import typescript from '@rollup/plugin-typescript';
44
import json from '@rollup/plugin-json';
55
import dts from 'rollup-plugin-dts';
66
import terser from '@rollup/plugin-terser';
7+
import replace from '@rollup/plugin-replace';
8+
import { readFileSync } from 'fs';
9+
10+
// 读取 package.json 获取版本号
11+
const packageJson = JSON.parse(readFileSync('./package.json', 'utf-8'));
12+
const version = packageJson.version;
713

814
// 只将 Node.js 内置模块设为外部依赖,npm 依赖都打包进最终产物
915
const external = [
@@ -128,6 +134,13 @@ const baseConfig = {
128134
unknownGlobalSideEffects: false
129135
},
130136
plugins: [
137+
// 版本号注入,需要在 typescript 插件之前
138+
replace({
139+
preventAssignment: true,
140+
values: {
141+
'__MCP_VERSION__': JSON.stringify(version)
142+
}
143+
}),
131144
resolve({
132145
preferBuiltins: true,
133146
exportConditions: ['node'],

mcp/src/utils/telemetry.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import os from 'os';
22
import crypto from 'crypto';
33
import https from 'https';
44
import http from 'http';
5-
import { createRequire } from 'module';
65
import { debug } from './logger.js';
76
import {loadEnvIdFromUserConfig } from '../tools/interactive.js';
87

9-
const require = createRequire(import.meta.url);
10-
const packageJson = require('../../package.json');
8+
// 构建时注入的版本号
9+
declare const __MCP_VERSION__: string;
1110

1211
/**
1312
* 数据上报类
@@ -61,8 +60,8 @@ class TelemetryReporter {
6160
const nodeVersion = process.version; // Node.js版本
6261
const arch = os.arch(); // 系统架构
6362

64-
// 从package.json获取MCP版本信息
65-
const mcpVersion = process.env.npm_package_version || packageJson.version || 'unknown';
63+
// 从构建时注入的版本号获取MCP版本信息
64+
const mcpVersion = process.env.npm_package_version || __MCP_VERSION__ || 'unknown';
6665

6766
return {
6867
userAgent: `${osType} ${osRelease} ${arch} ${nodeVersion} CloudBase-MCP/${mcpVersion}`,

0 commit comments

Comments
 (0)