@bestmfy/vision-understanding-mcp
Version:
MCP Server for Image/Video Understanding using Doubao Vision Model
161 lines (145 loc) • 4.78 kB
JavaScript
/**
* Vision Understanding MCP Server
*
* This package provides a Model Context Protocol (MCP) server for image and video analysis
* using the Doubao Vision Model from Volcengine.
*
* Usage:
* npx @your-username/vision-understanding-mcp
*
* Or programmatically:
* const { startServer } = require('@your-username/vision-understanding-mcp');
* startServer();
*/
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
/**
* Start the MCP server
* @param {Object} options - Configuration options
* @param {string} options.pythonCmd - Python command to use (default: auto-detect)
* @param {boolean} options.installDeps - Whether to install dependencies (default: true)
*/
function startServer(options = {}) {
const {
pythonCmd = null,
installDeps = true
} = options;
const packageDir = __dirname;
const serverScript = path.join(packageDir, 'vision_mcp_server.py');
// Check if Python script exists
if (!fs.existsSync(serverScript)) {
throw new Error('vision_mcp_server.py not found in package directory');
}
return new Promise((resolve, reject) => {
// Auto-detect Python if not provided
if (!pythonCmd) {
detectPython().then(detectedCmd => {
if (!detectedCmd) {
reject(new Error('Python 3.9+ is required but not found'));
return;
}
launchServer(detectedCmd);
}).catch(reject);
} else {
launchServer(pythonCmd);
}
function launchServer(cmd) {
if (installDeps) {
installDependencies(cmd).then(() => {
const server = spawn(cmd, [serverScript], {
stdio: 'inherit',
cwd: packageDir,
env: {
...process.env,
PYTHONPATH: packageDir
}
});
resolve(server);
}).catch(reject);
} else {
const server = spawn(cmd, [serverScript], {
stdio: 'inherit',
cwd: packageDir,
env: {
...process.env,
PYTHONPATH: packageDir
}
});
resolve(server);
}
}
});
}
/**
* Detect available Python command
*/
function detectPython() {
return new Promise((resolve) => {
const python3 = spawn('python3', ['--version']);
python3.on('close', (code) => {
if (code === 0) {
resolve('python3');
} else {
const python = spawn('python', ['--version']);
python.on('close', (fallbackCode) => {
resolve(fallbackCode === 0 ? 'python' : null);
});
}
});
});
}
/**
* Install Python dependencies
*/
function installDependencies(pythonCmd) {
return new Promise((resolve, reject) => {
const requirementsFile = path.join(__dirname, 'requirements.txt');
if (!fs.existsSync(requirementsFile)) {
resolve();
return;
}
console.log('Installing Python dependencies...');
const pip = spawn(pythonCmd, ['-m', 'pip', 'install', '-r', requirementsFile], {
stdio: 'inherit',
cwd: __dirname
});
pip.on('close', (code) => {
if (code !== 0) {
console.warn('Warning: Failed to install some Python dependencies');
}
resolve();
});
pip.on('error', (err) => {
reject(new Error(`Failed to install dependencies: ${err.message}`));
});
});
}
// Export for programmatic use
module.exports = {
startServer,
detectPython,
installDependencies
};
// CLI usage
if (require.main === module) {
console.log('Starting Vision Understanding MCP Server...');
startServer()
.then((server) => {
console.log('MCP Server started successfully');
// Handle process termination
process.on('SIGINT', () => {
console.log('\nShutting down MCP Server...');
server.kill('SIGINT');
});
process.on('SIGTERM', () => {
console.log('\nShutting down MCP Server...');
server.kill('SIGTERM');
});
})
.catch((error) => {
console.error('Failed to start MCP Server:', error.message);
process.exit(1);
});
}