Add MCP server and REST API for direct AI-to-site connectivity#2
Conversation
Closes #1. Exposes all Haydi tools over a WordPress REST API (haydi/v1) authenticated by long-lived Bearer tokens generated in the WP-Admin sidebar. Includes a native MCP endpoint (POST /wp-json/haydi/v1/mcp) that speaks the Streamable HTTP transport so Claude Code and other MCP-compatible tools can connect directly — no external process needed. Internals: extracted a service layer (execute_* methods) from each tool class so the AJAX, REST, and MCP layers are all thin wrappers over the same logic with no duplication. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
dmallory42
left a comment
There was a problem hiding this comment.
I haven't fully reviewed the code, but I ran a test using my local agent (pi running gpt5.5) and it worked a charm!
I was able to authenticate with a generated Bearer token, call initialize, list MCP tools, and create/read a file via MCP using haydi_write_file + haydi_read_file.
One small thing I ran into: my first write attempt targeted /wp-content/uploads/..., which failed with:
Path is outside all allowed directories.
That makes sense because Haydi_Filesystem_Guard only allows plugins/themes by default, plus /wp-content/uploads/ai-edits if that directory already exists. Retrying under /wp-content/plugins/_haydi-mcp-smoke-test/smoke.txt worked.
It might be helpful for clients if the REST/MCP API exposed the allowed writable roots somehow, either in /haydi/v1/status or as a dedicated MCP tool like haydi_get_allowed_roots. That would help external MCP clients choose valid paths instead of guessing and hitting guard errors.
Adds `allowed_roots` to `GET /haydi/v1/status` and a new `haydi_get_allowed_roots` MCP tool so external clients can discover valid write paths without guessing and hitting guard errors. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
|
@dmallory42 great news, thanks for the thorough review! 🙇♂️ Added MCP |
Fixes #1.
What this adds
Exposes all Haydi tools over a WordPress REST API (
haydi/v1) and a native MCP endpoint so local AI tools like Claude Code can manage a WordPress site directly — no browser, no extra process.Token management: generate long-lived Bearer tokens in the sidebar under Advanced settings → Remote access. Tokens are stored SHA-256 hashed (plaintext shown once). Revocable at any time.
REST API (
/wp-json/haydi/v1/): full CRUD for files, plugins, SQL, PHP, and URL fetch. Write operations execute immediately when token-authenticated — the token is the approval gate.MCP endpoint (
POST /wp-json/haydi/v1/mcp): speaks the MCP Streamable HTTP transport (JSON-RPC 2.0). Exposes all Haydi tools as MCP tools. Add to.mcp.json:{ "mcpServers": { "haydi": { "url": "https://yoursite.com/wp-json/haydi/v1/mcp", "headers": { "Authorization": "Bearer <token>" } } } }Service layer: extracted
execute_*methods from each tool class so the AJAX, REST, and MCP layers are all thin wrappers over the same logic — no duplication.Screenshot
Claude Code using Haydi via MCP to create a file on a live WordPress site:
Test plan
Automated
Manual — token UI
my-laptop) and click GenerateManual — REST API
Manual — MCP endpoint
Manual — Claude Code MCP connection
.mcp.jsonin your project directory (shown in the UI after generating a token).claude/settings.local.json:{ "enabledMcpjsonServers": ["haydi"] }haydiappears in/mcp/var/www/html/wp-content/plugins/_mcp-test/hello.php"npm run env:run -- wp eval 'echo file_get_contents("/var/www/html/wp-content/plugins/_mcp-test/hello.php");'🤖 Generated with Claude Code