@mcpcn/mcp-computer-env 1.0.2 → 1.0.4

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 +66 -18
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -122,29 +122,77 @@ function getMemoryInfo() {
122
122
  function getNetworkInfo() {
123
123
  return os.networkInterfaces();
124
124
  }
125
+ function normalizeWindowsDiskRow(mountPoint, fileSystem, sizeValue, freeValue) {
126
+ const drive = mountPoint.trim();
127
+ const total = Number.parseInt(sizeValue, 10) || 0;
128
+ const available = Number.parseInt(freeValue, 10) || 0;
129
+ return {
130
+ filesystem: drive,
131
+ mount: drive ? `${drive}\\` : '',
132
+ type: fileSystem.trim(),
133
+ total,
134
+ used: Math.max(total - available, 0),
135
+ available,
136
+ };
137
+ }
138
+ async function getDiskInfoWindowsByWmic() {
139
+ const { stdout } = await execAsync('wmic logicaldisk get Caption,FileSystem,Size,FreeSpace /format:csv');
140
+ const lines = stdout
141
+ .split(/\r?\n/)
142
+ .map((line) => line.trim())
143
+ .filter(Boolean);
144
+ if (lines.length < 2) {
145
+ throw new Error('wmic returned empty output');
146
+ }
147
+ const header = lines[0].replace(/^\uFEFF/, '').split(',').map((item) => item.trim());
148
+ const colIndex = {
149
+ caption: header.indexOf('Caption'),
150
+ fileSystem: header.indexOf('FileSystem'),
151
+ size: header.indexOf('Size'),
152
+ freeSpace: header.indexOf('FreeSpace'),
153
+ };
154
+ if (colIndex.caption < 0 || colIndex.fileSystem < 0 || colIndex.size < 0 || colIndex.freeSpace < 0) {
155
+ throw new Error('wmic returned unexpected columns');
156
+ }
157
+ return lines
158
+ .slice(1)
159
+ .map((line) => line.split(','))
160
+ .map((parts) => normalizeWindowsDiskRow(parts[colIndex.caption] || '', parts[colIndex.fileSystem] || '', parts[colIndex.size] || '', parts[colIndex.freeSpace] || ''))
161
+ .filter((item) => item.filesystem);
162
+ }
163
+ async function getDiskInfoWindowsByPowerShell() {
164
+ const command = 'powershell -NoProfile -Command "Get-CimInstance Win32_LogicalDisk | Select-Object DeviceID,FileSystem,Size,FreeSpace | ConvertTo-Json -Compress"';
165
+ const { stdout } = await execAsync(command);
166
+ const raw = stdout.trim();
167
+ if (!raw) {
168
+ throw new Error('PowerShell returned empty output');
169
+ }
170
+ const parsed = JSON.parse(raw);
171
+ const rows = Array.isArray(parsed) ? parsed : [parsed];
172
+ return rows
173
+ .filter((row) => Boolean(row) && typeof row === 'object')
174
+ .map((row) => normalizeWindowsDiskRow(String((row.DeviceID ?? row.Caption ?? '')), String((row.FileSystem ?? '')), String((row.Size ?? '0')), String((row.FreeSpace ?? '0'))))
175
+ .filter((item) => item.filesystem);
176
+ }
125
177
  // 获取磁盘分区信息(跨平台)
126
178
  async function getDiskInfo() {
127
179
  try {
128
180
  const platform = os.platform();
129
181
  if (platform === 'win32') {
130
- // Windows: 使用wmic
131
- const { stdout } = await execAsync('wmic logicaldisk get Caption,FileSystem,Size,FreeSpace');
132
- const lines = stdout.trim().split(/\r?\n/).filter(Boolean);
133
- const header = lines.shift();
134
- return lines.map(line => {
135
- const parts = line.trim().split(/\s+/);
136
- const [Caption, FileSystem, Size, FreeSpace] = parts;
137
- const total = parseInt(Size, 10) || 0;
138
- const free = parseInt(FreeSpace, 10) || 0;
139
- return {
140
- filesystem: Caption,
141
- mount: Caption + '\\',
142
- type: FileSystem,
143
- total,
144
- used: total - free,
145
- available: free
146
- };
147
- });
182
+ // Windows: 先尝试 wmic,失败后回退到 PowerShell
183
+ try {
184
+ return await getDiskInfoWindowsByWmic();
185
+ }
186
+ catch (wmicError) {
187
+ try {
188
+ return await getDiskInfoWindowsByPowerShell();
189
+ }
190
+ catch (powerShellError) {
191
+ const wmicMessage = wmicError instanceof Error ? wmicError.message : String(wmicError);
192
+ const powerShellMessage = powerShellError instanceof Error ? powerShellError.message : String(powerShellError);
193
+ throw new Error(`wmic failed: ${wmicMessage}; PowerShell failed: ${powerShellMessage}`);
194
+ }
195
+ }
148
196
  }
149
197
  else {
150
198
  // macOS/Linux: 使用df -kP
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mcpcn/mcp-computer-env",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "获取用户电脑环境信息的MCP服务器",
5
5
  "packageManager": "npm@10.9.0",
6
6
  "main": "dist/index.js",