@azure/mcp 3.0.0-beta.45 → 3.0.0-beta.46

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/index.js +231 -35
  2. package/package.json +7 -7
package/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
 
3
+ const fs = require('fs')
3
4
  const os = require('os')
5
+ const path = require('path')
4
6
  const packageJson = require('./package.json')
5
7
 
6
8
  // Check if DEBUG environment variable is set
@@ -30,48 +32,239 @@ const packageName = packageJson.name
30
32
  const packageVersion = packageJson.version
31
33
  const platformPackageName = `${packageName}-${platform}-${arch}`
32
34
 
35
+ // Where the auto-install fallback below puts the platform package, in order of
36
+ // preference. It never goes in the caller's project: cwd is inherited from
37
+ // whoever spawned us - for an MCP client that is an unrelated user project -
38
+ // and a bare `npm install` resolves the nearest package.json from there and
39
+ // saves the platform package into it.
40
+ //
41
+ // The first choice lives inside this package, so it is tied to this version
42
+ // and goes away on uninstall. It is not writable for root-owned global installs
43
+ // or read-only filesystems, so the second is a per-user cache keyed by package
44
+ // and version. The OS temp directory is deliberately not used: on a shared
45
+ // machine another user could plant a package there for us to load.
46
+ //
47
+ // Neither directory has a package.json of its own: pointing npm at this
48
+ // package's manifest is a no-op, because the platform package is declared
49
+ // there as an optionalDependency that npm is already skipping (that skip is
50
+ // usually why we are in this code path at all).
51
+ //
52
+ // This only runs once the normal require has failed, so a working install never
53
+ // touches the home directory: os.homedir() throws when HOME is unset and the
54
+ // user has no passwd entry.
55
+ function getInstallDirs() {
56
+ const dirs = [path.join(__dirname, '.platform')]
57
+ try {
58
+ dirs.push(path.join(userCacheDir(), 'microsoft-mcp', platformPackageName, packageVersion))
59
+ } catch (err) {
60
+ debugLog(`No per-user cache directory available: ${err.message}`)
61
+ }
62
+ return dirs
63
+ }
64
+
65
+ function userCacheDir() {
66
+ if (platform === 'win32') {
67
+ const localAppData = process.env.LOCALAPPDATA
68
+ return localAppData && path.isAbsolute(localAppData)
69
+ ? localAppData
70
+ : path.join(os.homedir(), 'AppData', 'Local')
71
+ }
72
+ if (platform === 'darwin') {
73
+ return path.join(os.homedir(), 'Library', 'Caches')
74
+ }
75
+ // The XDG spec says to ignore a relative XDG_CACHE_HOME. Honoring one would
76
+ // put the cache under the caller's working directory.
77
+ const xdgCacheHome = process.env.XDG_CACHE_HOME
78
+ return xdgCacheHome && path.isAbsolute(xdgCacheHome) ? xdgCacheHome : path.join(os.homedir(), '.cache')
79
+ }
80
+
81
+ function requireFromInstallDir(installDir) {
82
+ return require(path.join(installDir, 'node_modules', platformPackageName))
83
+ }
84
+
85
+ function isWritableDir(dir) {
86
+ try {
87
+ fs.mkdirSync(dir, { recursive: true })
88
+ fs.accessSync(dir, fs.constants.W_OK)
89
+ return true
90
+ } catch (err) {
91
+ debugLog(`Cannot install platform package into ${dir}: ${err.message}`)
92
+ return false
93
+ }
94
+ }
95
+
96
+ // MCP clients often start several copies of a server at once, and they all
97
+ // share one install directory. Concurrent npm installs into one prefix undo
98
+ // each other's work (one moves away the package another has just finished), so
99
+ // a lock file lets one process install while the others wait and then load
100
+ // what it installed.
101
+ const installLockName = '.install.lock'
102
+ // Longer than both npm attempts together. The PID check below catches a dead
103
+ // holder sooner; this is the backstop for a PID that has been reused.
104
+ const installLockStaleMs = 3 * 60 * 1000
105
+ const installLockPollMs = 250
106
+
107
+ // A holder that is killed - say, by an MCP client giving up on a slow first
108
+ // start - never releases its lock, so check whether it is still running rather
109
+ // than waiting the lock out.
110
+ function isStaleLock(lockPath) {
111
+ if (Date.now() - fs.statSync(lockPath).mtimeMs > installLockStaleMs) {
112
+ return true
113
+ }
114
+ const pid = Number(fs.readFileSync(lockPath, 'utf8'))
115
+ if (!pid) {
116
+ return false // created, but the holder has not written its PID yet
117
+ }
118
+ try {
119
+ process.kill(pid, 0)
120
+ return false
121
+ } catch (err) {
122
+ return err.code === 'ESRCH'
123
+ }
124
+ }
125
+
126
+ function isInstallInProgress(dir) {
127
+ try {
128
+ return !isStaleLock(path.join(dir, installLockName))
129
+ } catch {
130
+ return false // no lock
131
+ }
132
+ }
133
+
134
+ function sleep(ms) {
135
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms)
136
+ }
137
+
138
+ function acquireInstallLock(installDir) {
139
+ const lockPath = path.join(installDir, installLockName)
140
+ let announced = false
141
+ for (;;) {
142
+ try {
143
+ fs.writeFileSync(lockPath, String(process.pid), { flag: 'wx' })
144
+ return lockPath
145
+ } catch (err) {
146
+ if (err.code !== 'EEXIST') {
147
+ throw err
148
+ }
149
+ }
150
+
151
+ try {
152
+ if (isStaleLock(lockPath)) {
153
+ debugLog(`Removing stale install lock ${lockPath}`)
154
+ fs.rmSync(lockPath, { force: true })
155
+ continue
156
+ }
157
+ } catch (err) {
158
+ if (err.code !== 'ENOENT') {
159
+ throw err
160
+ }
161
+ continue // released between our attempt and the check
162
+ }
163
+
164
+ if (!announced) {
165
+ debugLog(`Waiting for another process to finish installing into ${installDir}`)
166
+ announced = true
167
+ }
168
+ sleep(installLockPollMs)
169
+ }
170
+ }
171
+
172
+ // Call with the install lock held.
173
+ function loadOrInstall(installDir) {
174
+ // Another process may have installed it while we waited for the lock.
175
+ try {
176
+ const installed = requireFromInstallDir(installDir)
177
+ debugLog(`Loaded ${platformPackageName} installed into ${installDir} by another process`)
178
+ return installed
179
+ } catch {
180
+ // Not installed yet
181
+ }
182
+
183
+ const { execSync } = require('child_process')
184
+
185
+ console.error(`Installing missing platform package: ${platformPackageName}`)
186
+ debugLog(`Installing ${platformPackageName} into ${installDir}`)
187
+
188
+ // stdout is not inherited: for `server start` it carries the JSON-RPC
189
+ // protocol stream, and npm output written there would corrupt it.
190
+ const installOptions = {
191
+ cwd: installDir,
192
+ stdio: ['ignore', 'pipe', 'pipe'],
193
+ timeout: 60000 // 60 second timeout
194
+ }
195
+ // --prefix is relative to cwd so that no path goes through the shell, which
196
+ // would expand any `$` or backtick in it.
197
+ const installCommand = `npm install ${platformPackageName}@${packageVersion} --no-save --no-audit --no-fund --prefix .`
198
+
199
+ // Try to install the platform package
200
+ try {
201
+ execSync(installCommand, installOptions)
202
+ } catch (npmErr) {
203
+ // If npm install fails, try again against the registry
204
+ debugLog(`npm install failed, trying alternative installation methods: ${npmErr.message}`)
205
+
206
+ execSync(`${installCommand} --prefer-online`, installOptions)
207
+ }
208
+
209
+ // Clear module cache and try to require again after installation
210
+ Object.keys(require.cache).forEach(key => {
211
+ if (key.includes(platformPackageName)) {
212
+ delete require.cache[key]
213
+ }
214
+ })
215
+
216
+ const installed = requireFromInstallDir(installDir)
217
+ console.error(`✅ Successfully installed and loaded ${platformPackageName}`)
218
+ return installed
219
+ }
220
+
33
221
  // Try to load the platform package
34
222
  let platformPackage
223
+ let loadErr
224
+ let installDirs
35
225
  try {
36
226
  debugLog(`Attempting to require platform package: ${platformPackageName}`)
37
227
  platformPackage = require(platformPackageName)
38
228
  } catch (err) {
39
- debugLog(`Failed to require ${platformPackageName}, attempting auto-install: ${err.message}`)
229
+ loadErr = err
230
+ debugLog(`Failed to require ${platformPackageName}: ${err.message}`)
40
231
 
232
+ // Node never searches the install directories on its own, so an earlier
233
+ // auto-install is only found by looking there. Without this, every run would
234
+ // reinstall. A failed require (not installed, or a half-finished install)
235
+ // just falls through to installing again, and a directory another process is
236
+ // still installing into is left to the locked path below.
237
+ installDirs = getInstallDirs()
238
+ for (const dir of installDirs) {
239
+ if (isInstallInProgress(dir)) {
240
+ debugLog(`Skipping ${dir}: another process is installing into it`)
241
+ continue
242
+ }
243
+ try {
244
+ platformPackage = requireFromInstallDir(dir)
245
+ debugLog(`Loaded previously installed ${platformPackageName} from ${dir}`)
246
+ break
247
+ } catch (cachedErr) {
248
+ debugLog(`No usable ${platformPackageName} in ${dir}: ${cachedErr.message}`)
249
+ }
250
+ }
251
+ }
252
+
253
+ if (!platformPackage) {
41
254
  // Try to automatically install the missing platform package
255
+ let installDir
42
256
  try {
43
- const { execSync } = require('child_process')
44
-
45
- console.error(`Installing missing platform package: ${platformPackageName}`)
257
+ installDir = installDirs.find(isWritableDir)
258
+ if (!installDir) {
259
+ throw new Error(`No writable directory to install into (tried ${installDirs.join(', ')})`)
260
+ }
46
261
 
47
- // Try to install the platform package
262
+ const lockPath = acquireInstallLock(installDir)
48
263
  try {
49
- execSync(`npm install ${platformPackageName}@${packageVersion}`, {
50
- stdio: ['inherit', 'inherit', 'pipe'], // Only pipe stderr to capture errors
51
- timeout: 60000 // 60 second timeout
52
- })
53
- } catch (npmErr) {
54
- // If npm install fails, try with --no-save and different install strategies
55
- debugLog(`npm install failed, trying alternative installation methods: ${npmErr.message}`)
56
-
57
- // Try with --no-save and --prefer-online
58
- execSync(`npm install ${platformPackageName}@${packageVersion} --no-save --prefer-online`, {
59
- stdio: ['inherit', 'inherit', 'pipe'],
60
- timeout: 60000
61
- })
264
+ platformPackage = loadOrInstall(installDir)
265
+ } finally {
266
+ fs.rmSync(lockPath, { force: true })
62
267
  }
63
-
64
- // Clear module cache and try to require again after installation
65
- Object.keys(require.cache).forEach(key => {
66
- if (key.includes(platformPackageName)) {
67
- delete require.cache[key]
68
- }
69
- })
70
-
71
- platformPackage = require(platformPackageName)
72
-
73
- console.error(`✅ Successfully installed and loaded ${platformPackageName}`)
74
-
75
268
  } catch (installErr) {
76
269
  debugLog(`Auto-install failed: ${installErr.message}`)
77
270
 
@@ -85,12 +278,15 @@ try {
85
278
  console.error(`\n3. If using npx, clear the npx cache and try again:`)
86
279
  console.error(` npx -y clear-npx-cache`)
87
280
  console.error(` npx -y ${packageName}@latest --version`)
88
- console.error(`\n4. Manually install the platform package to check compatibility:`)
89
- console.error(` npm install ${platformPackageName}@latest`)
90
- console.error(`\n5. Check your internet connection and try again`)
91
- console.error(`\n6. If the issue persists, please report it at:`)
281
+ console.error(`\n4. Install the platform package manually to see npm's full output:`)
282
+ console.error(` npm install ${platformPackageName}@${packageVersion} --no-save --prefix "${installDir || installDirs[0]}"`)
283
+ console.error(`\n5. Check your Node version. npm silently skips the optional platform`)
284
+ console.error(` package when Node does not satisfy engines (${(packageJson.engines || {}).node}).`)
285
+ console.error(` You are running ${process.version}.`)
286
+ console.error(`\n6. Check your internet connection and try again`)
287
+ console.error(`\n7. If the issue persists, please report it at:`)
92
288
  console.error(` https://github.com/microsoft/mcp/issues`)
93
- console.error(`\nOriginal error: ${err.message}`)
289
+ console.error(`\nOriginal error: ${loadErr.message}`)
94
290
  console.error(`Install error: ${installErr.message}`)
95
291
  process.exit(1)
96
292
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azure/mcp",
3
- "version": "3.0.0-beta.45",
3
+ "version": "3.0.0-beta.46",
4
4
  "description": "Azure MCP Server - Model Context Protocol implementation for Azure",
5
5
  "author": "Microsoft",
6
6
  "homepage": "https://github.com/Microsoft/mcp/blob/main/servers/Azure.Mcp.Server#readme",
@@ -33,12 +33,12 @@
33
33
  "arm64"
34
34
  ],
35
35
  "optionalDependencies": {
36
- "@azure/mcp-darwin-x64": "3.0.0-beta.45",
37
- "@azure/mcp-linux-arm64": "3.0.0-beta.45",
38
- "@azure/mcp-linux-x64": "3.0.0-beta.45",
39
- "@azure/mcp-win32-arm64": "3.0.0-beta.45",
40
- "@azure/mcp-darwin-arm64": "3.0.0-beta.45",
41
- "@azure/mcp-win32-x64": "3.0.0-beta.45"
36
+ "@azure/mcp-darwin-arm64": "3.0.0-beta.46",
37
+ "@azure/mcp-linux-x64": "3.0.0-beta.46",
38
+ "@azure/mcp-linux-arm64": "3.0.0-beta.46",
39
+ "@azure/mcp-darwin-x64": "3.0.0-beta.46",
40
+ "@azure/mcp-win32-x64": "3.0.0-beta.46",
41
+ "@azure/mcp-win32-arm64": "3.0.0-beta.46"
42
42
  },
43
43
  "scripts": {
44
44
  "postinstall": "node ./scripts/post-install-script.js"