Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions eng/npm/wrapper/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env node

const os = require('os')
const packageJson = require('./package.json')

// Check if DEBUG environment variable is set
const isDebugMode = process.env.DEBUG && (
process.env.DEBUG.toLowerCase() === 'true' ||
process.env.DEBUG.includes('azure-mcp') ||
process.env.DEBUG.includes('mcp') ||
process.env.DEBUG === '*'
)

Expand All @@ -25,7 +26,9 @@ process.argv.forEach((val, index) => {
const platform = os.platform()
const arch = os.arch()

const platformPackageName = `@azure/mcp-${platform}-${arch}`
const packageName = packageJson.name
const packageVersion = packageJson.version
const platformPackageName = `${packageName}-${platform}-${arch}`

// Try to load the platform package
let platformPackage
Expand All @@ -34,58 +37,59 @@ try {
platformPackage = require(platformPackageName)
} catch (err) {
debugLog(`Failed to require ${platformPackageName}, attempting auto-install: ${err.message}`)

// Try to automatically install the missing platform package
try {
const { execSync } = require('child_process')

console.error(`Installing missing platform package: ${platformPackageName}`)

// Try to install the platform package
try {
execSync(`npm install ${platformPackageName}@latest`, {
execSync(`npm install ${platformPackageName}@${packageVersion}`, {
stdio: ['inherit', 'inherit', 'pipe'], // Only pipe stderr to capture errors
timeout: 60000 // 60 second timeout
})
} catch (npmErr) {
// If npm install fails, try with --no-save and different install strategies
debugLog(`npm install failed, trying alternative installation methods: ${npmErr.message}`)

// Try with --no-save and --prefer-online
execSync(`npm install ${platformPackageName}@latest --no-save --prefer-online`, {
execSync(`npm install ${platformPackageName}@${packageVersion} --no-save --prefer-online`, {
stdio: ['inherit', 'inherit', 'pipe'],
timeout: 60000
})
}

// Clear module cache and try to require again after installation
Object.keys(require.cache).forEach(key => {
if (key.includes(platformPackageName)) {
delete require.cache[key]
}
})

platformPackage = require(platformPackageName)

console.error(`✅ Successfully installed and loaded ${platformPackageName}`)

} catch (installErr) {
debugLog(`Auto-install failed: ${installErr.message}`)

console.error(`\n❌ Failed to load platform specific package '${platformPackageName}'`)
console.error(`\n🔍 Troubleshooting steps:`)
console.error(`\n1. Clear npm cache and reinstall:`)
console.error(`\n1. Clear npm cache:`)
console.error(` npm cache clean --force`)
console.error(` npm uninstall -g @azure/mcp`)
console.error(` npm install -g @azure/mcp@latest`)
console.error(`\n2. If using npx, clear the cache:`)
console.error(` npx clear-npx-cache`)
console.error(` npx -y @azure/mcp@latest server start`)
console.error(`\n3. Manually install the platform package:`)
console.error(`\n2. If installing as a global tool, uninstall and reinstall:`)
console.error(` npm uninstall -g ${packageName}`)
console.error(` npm install -g ${packageName}`)
console.error(`\n3. If using npx, clear the npx cache and try again:`)
console.error(` npx -y clear-npx-cache`)
console.error(` npx -y ${packageName}@latest --version`)
console.error(`\n4. Manually install the platform package to check compatibility:`)
console.error(` npm install ${platformPackageName}@latest`)
console.error(`\n4. Check your internet connection and try again`)
console.error(`\n5. If the issue persists, please report it at:`)
console.error(` https://github.com/Azure/azure-mcp/issues`)
console.error(`\n5. Check your internet connection and try again`)
console.error(`\n6. If the issue persists, please report it at:`)
console.error(` https://github.com/microsoft/mcp/issues`)
console.error(`\nOriginal error: ${err.message}`)
console.error(`Install error: ${installErr.message}`)
process.exit(1)
Expand Down
20 changes: 8 additions & 12 deletions eng/npm/wrapper/scripts/post-install-script.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
const fs = require('fs')
const path = require('path')
const os = require('os');

const platform = os.platform();
const arch = os.arch();

const pkgJsonPath = path.join(__dirname, '..', 'package.json');
let baseName = '@azure/mcp';
try {
const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8'));
if (pkg.name) {
baseName = pkg.name;
}
} catch (e) {
// fallback to default
let baseName = '';
try{
const packageJson = require('../package.json');
baseName = packageJson.name;
}
catch (err) {
console.error('Unable to verify platform package installation. Error reading package.json.');
process.exit(1);
}

const requiredPackage = `${baseName}-${platform}-${arch}`;

try {
require.resolve(requiredPackage);
} catch (err) {
Expand Down