docxmcp
Version:
A Model Context Protocol (MCP) server for processing .docx files into markdown with image extraction
120 lines (99 loc) • 3.26 kB
JavaScript
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { readFileSync } from 'fs';
// Get the directory where this script is located
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Read package.json for version
const packageJson = JSON.parse(readFileSync(join(__dirname, 'package.json'), 'utf8'));
// Parse command line arguments
const args = process.argv.slice(2);
const options = {
port: 'stdio',
logLevel: 'info'
};
// Help text
const helpText = `
DocxMCP - Model Context Protocol server for processing .docx files
Usage: npx -y docxmcp@latest [options]
Options:
--port <port> Port to listen on (default: stdio)
--log-level <level> Log level: debug, info, warn, error (default: info)
--help, -h Show this help message
--version, -v Show version
Example:
npx -y docxmcp@latest
npx -y docxmcp@latest --log-level debug
npx -y docxmcp@latest --port 3000
The server provides a single MCP tool 'process_docx' that converts .docx files
to markdown format with extracted images.
`;
// Process arguments
for (let i = 0; i < args.length; i++) {
const arg = args[i];
switch (arg) {
case '--help':
case '-h':
console.log(helpText);
process.exit(0);
break;
case '--version':
case '-v':
console.log(packageJson.version);
process.exit(0);
break;
case '--port':
if (i + 1 < args.length) {
options.port = args[++i];
} else {
console.error('Error: --port requires a value');
process.exit(1);
}
break;
case '--log-level':
if (i + 1 < args.length) {
const level = args[++i];
if (['debug', 'info', 'warn', 'error'].includes(level)) {
options.logLevel = level;
} else {
console.error(`Error: Invalid log level '${level}'. Must be: debug, info, warn, or error`);
process.exit(1);
}
} else {
console.error('Error: --log-level requires a value');
process.exit(1);
}
break;
default:
console.error(`Error: Unknown argument '${arg}'`);
console.log('Use --help for usage information');
process.exit(1);
}
}
// Path to the main server script
const serverPath = join(__dirname, 'src', 'index.js');
// Environment variables to pass options
const env = {
...process.env,
DOCXMCP_PORT: options.port,
DOCXMCP_LOG_LEVEL: options.logLevel
};
// Start the server process
const server = spawn('node', [serverPath], {
stdio: 'inherit',
cwd: __dirname,
env: env
});
// Handle process events
server.on('error', (error) => {
console.error('Failed to start server:', error);
process.exit(1);
});
server.on('exit', (code, signal) => {
if (code !== 0) {
console.error(`Server exited with code ${code}`);
process.exit(code);
}
});