WordPress MCP プラグインをサブディレクトリインストールで動作させる方法
Claude Desktopからワードプレスの操作をしたかったが、ワードプレスがルートディレクトリの場合は問題なく動作するのものの、サブディレクトリの場合はエラーが発生することが判明。
結局、ワードプレスのプラグインや公式のMCPサーバーにバグがあることがわかり、修正した記録です。
※以降、AI出力のテキストなので、実施の際はご注意ください。
Contents
目次を全て見る
問題の概要
WordPress MCP プラグイン (v0.2.5) には、サブディレクトリにインストールされた WordPress で動作しないバグが2つあります:
- クライアント側のバグ: サブディレクトリのパスを正しく処理できない
- プラグイン側のバグ: JWT認証が正しく機能しない
影響を受ける環境
- ✅ 動作する:
https://example.com/(ルートインストール) - ❌ 動作しない:
https://example.com/blog/(サブディレクトリインストール)
修正方法
パート1: クライアント側の修正(NPMパッケージ)
対象ファイル
@automattic/mcp-wordpress-remote パッケージの dist/proxy.js
修正箇所
38308行目付近の constructApiUrl 関数
修正前:
function constructApiUrl(baseUrl, defaultEndpoint) {
const cleanUrl = removeTrailingSlash(baseUrl);
try {
const urlObj = new URL(cleanUrl);
const hasCustomPath = urlObj.pathname && urlObj.pathname !== "/" && urlObj.pathname.length > 0;
const hasCustomQuery = urlObj.search && urlObj.search.length > 0;
if (hasCustomPath || hasCustomQuery) {
return cleanUrl; // ← バグ: サブディレクトリでクエリパラメータが付かない
} else {
return new URL(`/?rest_route=${defaultEndpoint}`, cleanUrl).toString();
}
} catch (error) {
return new URL(`/?rest_route=${defaultEndpoint}`, cleanUrl).toString();
}
}修正後:
function constructApiUrl(baseUrl, defaultEndpoint) {
const cleanUrl = removeTrailingSlash(baseUrl);
try {
const urlObj = new URL(cleanUrl);
const hasCustomQuery = urlObj.search && urlObj.search.length > 0;
// If there's already a custom query (like ?rest_route=...), use the URL as-is
if (hasCustomQuery) {
return cleanUrl;
} else {
// Always append ?rest_route= for all paths (including subdirectories)
// Add trailing slash before query parameter for WordPress compatibility
return `${cleanUrl}/?rest_route=${defaultEndpoint}`;
}
} catch (error) {
// Fallback: always append ?rest_route=
return `${cleanUrl}/?rest_route=${defaultEndpoint}`;
}
}変更点の説明
hasCustomPathチェックを削除: サブディレクトリの存在をチェックしていた条件を削除- URL構築方法を変更:
new URL()ではなく文字列連結を使用してパスを保持 - 末尾スラッシュを追加: WordPressのリライトルール互換性のため
修正済みパッケージのインストール方法
方法1: グローバルインストール(推奨)
修正済みパッケージをダウンロード後:
npm install -g /path/to/automattic-mcp-wordpress-remote-0.2.18-patched.tgzClaude Desktop 設定 (~/Library/Application Support/Claude/claude_desktop_config.json):
{
"mcpServers": {
"wordpress-mcp": {
"command": "npx",
"args": ["@automattic/mcp-wordpress-remote"],
"env": {
"WP_API_URL": "https://example.com/blog/",
"JWT_TOKEN": "your-jwt-token-here"
}
}
}
}方法2: ローカルパス指定
修正済みパッケージを展開後:
{
"mcpServers": {
"wordpress-mcp": {
"command": "node",
"args": ["/Users/username/mcp-packages/dist/proxy.js"],
"env": {
"WP_API_URL": "https://example.com/blog/",
"JWT_TOKEN": "your-jwt-token-here"
}
}
}
}パート2: プラグイン側の修正(WordPress プラグイン)
いくつかのファイルの修正が必要だったので、修正版をアップロードしました。
こちらから、zipファイルをダウンロードして使ってください。
インストール手順
1. バックアップ
必ず元のファイルをバックアップしてください:
wp-content/plugins/wordpress-mcp/includes/Core/にて
- McpTransportBase.php → コピーして名前を変更 TransportBase.php.backup
- McpStdioTransport.php → コピーして名前を変更 McpStdioTransport.php.backup
2. ファイルの修正
上記の修正を適用します。
3. プラグインの再起動
- WordPress管理画面 → プラグイン → インストール済みプラグイン
- 「WordPress MCP」を無効化
- 再度有効化
- 設定 → パーマリンク設定 → 「変更を保存」をクリック
4. Claude Desktop の再起動
Claude Desktop アプリを完全に終了して再起動します。
