@microsoft/powerbi-modeling-mcp
Version:
Power BI Authoring MCP Server - Node.js client packaging to author Power BI semantic models in Power BI Desktop, Microsoft Fabric workspaces, and local PBIP and TMDL files.
151 lines (126 loc) ⢠5.39 kB
JavaScript
// -----------------------------------------------------------------------
// <copyright company="Microsoft Corporation">
// copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------
const os = require('os')
const { execFileSync } = require('child_process')
const packageJson = require('./package.json')
const { get } = require('http')
// Check if DEBUG environment variable is set
const isDebugMode = process.env.DEBUG && (
process.env.DEBUG.toLowerCase() === 'true' ||
process.env.DEBUG.includes('mcp') ||
process.env.DEBUG === '*'
)
// Helper function for debug logging
function debugLog(...args) {
if (isDebugMode) {
console.error(...args)
}
}
// Helper function to get command line argument value
const getArgument = (argName) => {
let argIndex = process.argv.indexOf(argName);
if (argIndex !== -1 && argIndex < process.argv.length - 1) {
return process.argv[argIndex + 1] || null;
}
return null;
}
const packageName = packageJson.name
// Check for --help argument
if (process.argv.includes('--help') || process.argv.includes('-h')) {
console.log(`
Usage: ${packageName} [options]
Options:
--help, -h Show this help message
--use-version Specify a specific version to use
Environment Variables:
DEBUG Enable debug logging (true, mcp, or *)
Examples:
${packageName} --help
${packageName} --use-version 1.0.0
DEBUG=true ${packageName}
`)
process.exit(0)
}
debugLog('PowerBI Authoring MCP core package starting')
debugLog(`Argument count: ${Math.max(process.argv.length - 2, 0)}`)
const platform = os.platform()
const arch = os.arch()
const SEMVER_RE = /^\d+\.\d+\.\d+(-[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*)?(\+[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*)?$/
const DIST_TAG_RE = /^[a-zA-Z][a-zA-Z0-9._-]*$/
const version = getArgument('--use-version') || packageJson.version || 'latest'
if (!SEMVER_RE.test(version) && !DIST_TAG_RE.test(version)) {
console.error(`Invalid version string: ${version}`)
console.error('Version must be a valid semver version (e.g., 1.0.0, 1.0.0-beta.1) or a dist tag (e.g., latest)')
process.exit(1)
}
const platformPackageName = `${packageName}-${platform}-${arch}`
debugLog(`Detected platform: ${platform}, architecture: ${arch}`)
debugLog(`Using ${platformPackageName} version: ${version}`)
// Try to load the platform package
let platformPackage
try {
debugLog(`Attempting to require platform package: ${platformPackageName}`)
platformPackage = require(platformPackageName)
} catch (err) {
debugLog(`Failed to require ${platformPackageName}, attempting auto-install: ${err.message}`)
// Try to automatically install the missing platform package
try {
console.error(`Installing missing platform package: ${platformPackageName}`)
// Try to install the platform package
try {
execFileSync('npm', ['install', `${platformPackageName}@${version}`], {
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
execFileSync('npm', ['install', `${platformPackageName}@${version}`, '--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:`)
console.error(` npm cache clean --force`)
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(`\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)
}
}
platformPackage.runExecutable(process.argv.slice(2))
.then((code) => {
debugLog(`Process exited with code: ${code}`)
process.exit(code)
})
.catch((err) => {
console.error(`Error: ${err.message}`)
process.exit(1)
})