myaidev-method
Version:
Comprehensive development framework with SPARC methodology for AI-assisted software development, multi-platform publishing (WordPress, PayloadCMS, Astro, Docusaurus, Mintlify), and Coolify deployment
181 lines (151 loc) โข 6.69 kB
JavaScript
import fs from 'fs-extra';
import path from 'path';
import dotenv from 'dotenv';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* Configure WordPress MCP integration for Claude Code
* This script creates the .mcp.json file required for Claude Code to recognize
* and use the WordPress MCP server
*/
export async function configureWordPressMCP(projectDir = process.cwd()) {
try {
console.log('๐ง Configuring WordPress MCP integration...');
// Load environment variables from .env file
const envPath = path.join(projectDir, '.env');
if (!fs.existsSync(envPath)) {
throw new Error('.env file not found. Please run WordPress configuration first.');
}
dotenv.config({ path: envPath });
// Validate required environment variables
const requiredVars = ['WORDPRESS_URL', 'WORDPRESS_USERNAME', 'WORDPRESS_APP_PASSWORD'];
const missing = requiredVars.filter(varName => !process.env[varName]);
if (missing.length > 0) {
throw new Error(`Missing required environment variables: ${missing.join(', ')}`);
}
// Check if MCP server files exist
const mcpServerPath = path.join(projectDir, '.claude/mcp/wordpress-server.js');
const gutenbergConverterPath = path.join(projectDir, '.claude/mcp/gutenberg-converter.js');
if (!fs.existsSync(mcpServerPath)) {
throw new Error(`WordPress MCP server not found at: ${mcpServerPath}`);
}
if (!fs.existsSync(gutenbergConverterPath)) {
throw new Error(`Gutenberg converter not found at: ${gutenbergConverterPath}`);
}
// Create MCP configuration
const mcpConfigPath = path.join(projectDir, '.mcp.json');
// Use relative path for portability across different environments
const relativeMcpServerPath = ".claude/mcp/wordpress-server.js";
const mcpConfig = {
mcpServers: {
wordpress: {
command: "node",
args: [relativeMcpServerPath],
env: {
WORDPRESS_URL: process.env.WORDPRESS_URL,
WORDPRESS_USERNAME: process.env.WORDPRESS_USERNAME,
WORDPRESS_APP_PASSWORD: process.env.WORDPRESS_APP_PASSWORD,
WORDPRESS_USE_GUTENBERG: process.env.WORDPRESS_USE_GUTENBERG || "false"
}
}
}
};
// If .mcp.json already exists, merge with existing configuration
let existingConfig = {};
if (fs.existsSync(mcpConfigPath)) {
try {
existingConfig = JSON.parse(fs.readFileSync(mcpConfigPath, 'utf8'));
console.log('๐ Found existing .mcp.json file, merging configuration...');
} catch (error) {
console.warn('โ ๏ธ Existing .mcp.json file is invalid, creating new one...');
}
}
// Merge configurations
const finalConfig = {
...existingConfig,
mcpServers: {
...existingConfig.mcpServers,
...mcpConfig.mcpServers
}
};
// Write MCP configuration file
fs.writeFileSync(mcpConfigPath, JSON.stringify(finalConfig, null, 2));
console.log('โ
Created .mcp.json configuration file');
// Set executable permissions on MCP server
try {
fs.chmodSync(mcpServerPath, 0o755);
console.log('โ
Set executable permissions on WordPress MCP server');
} catch (error) {
console.warn('โ ๏ธ Could not set executable permissions:', error.message);
}
// Test MCP server startup
console.log('๐งช Testing MCP server startup...');
try {
const { spawn } = await import('child_process');
return new Promise((resolve, reject) => {
const testProcess = spawn('node', [relativeMcpServerPath], {
cwd: projectDir, // Set working directory to project root
env: { ...process.env, ...mcpConfig.mcpServers.wordpress.env },
stdio: ['pipe', 'pipe', 'pipe']
});
let stderr = '';
testProcess.stderr.on('data', (data) => {
stderr += data.toString();
});
// Give the server 3 seconds to start up
setTimeout(() => {
testProcess.kill('SIGTERM');
if (stderr.includes('WordPress MCP Server running') || stderr.includes('Server running')) {
console.log('โ
MCP server startup test successful');
resolve(true);
} else if (stderr.includes('Error') || stderr.includes('Missing required environment')) {
console.error('โ MCP server startup failed:', stderr);
reject(new Error('MCP server failed to start'));
} else {
console.log('โ
MCP server appears to be working');
resolve(true);
}
}, 3000);
testProcess.on('error', (error) => {
reject(new Error(`Failed to start MCP server: ${error.message}`));
});
});
} catch (testError) {
console.warn('โ ๏ธ Could not test MCP server startup:', testError.message);
console.log(' MCP configuration created, but please test manually if needed');
}
console.log('\n๐ WordPress MCP integration configured successfully!');
console.log('\n๐ Configuration Summary:');
console.log(` โข WordPress URL: ${process.env.WORDPRESS_URL}`);
console.log(` โข Username: ${process.env.WORDPRESS_USERNAME}`);
console.log(` โข Gutenberg mode: ${process.env.WORDPRESS_USE_GUTENBERG || 'false'}`);
console.log(` โข MCP config: ${mcpConfigPath}`);
console.log(` โข MCP server: ${relativeMcpServerPath}`);
console.log('\n๐ Next steps:');
console.log(' 1. Restart Claude Code to load the new MCP configuration');
console.log(' 2. Use WordPress MCP tools in your agents and commands');
console.log(' 3. Test with: /myai-wordpress-admin or /myai-wordpress-publish');
return true;
} catch (error) {
console.error('โ WordPress MCP configuration failed:', error.message);
console.log('\n๐ ๏ธ Troubleshooting:');
console.log(' โข Ensure WordPress credentials are configured in .env');
console.log(' โข Check that .claude/mcp/wordpress-server.js exists');
console.log(' โข Verify Node.js version is 18+ (required for MCP SDK)');
console.log(' โข Run: npm install to ensure dependencies are installed');
return false;
}
}
// Allow running as standalone script
if (import.meta.url === `file://${process.argv[1]}`) {
configureWordPressMCP()
.then((success) => {
process.exit(success ? 0 : 1);
})
.catch((error) => {
console.error('Configuration script error:', error);
process.exit(1);
});
}