Add WordPress.com REST API support for remote sites in AI command#2992
Add WordPress.com REST API support for remote sites in AI command#2992youknowriad merged 11 commits intotrunkfrom
Conversation
📊 Performance Test ResultsComparing 6cf7672 vs trunk app-size
site-editor
site-startup
Results are median values from multiple test runs. Legend: 🟢 Improvement (faster) | 🔴 Regression (slower) | ⚪ No change (<50ms diff) |
I'm not sure entirely about this, I think this produces better results but there's an alternative where the conditions are expressed with a common system prompt. |
sejas
left a comment
There was a problem hiding this comment.
Works pretty good in general. I tested it by running npm run cli:build && node apps/cli/dist/cli/main.mjs ai
I left a comment about the mismatch where the title and description mention https://public-api.wordpress.com/wpcom/v2/mcp/v1, but it seems we’ve actually built a custom MCP for dotcom inside Studio AI.
For some reason listing plugins and themes didn't work. I tried them with a couple of sites. Get site info worked although it said Error: result (96,079 characters) exceeds maximum allowed tokens. and used a file to split the response.
The screenshot action worked good.
apps/cli/ai/wpcom-tools.ts
Outdated
| /** | ||
| * Creates WP.com REST API tool definitions for managing a remote WordPress.com site. | ||
| * The wpcom client is created once and captured via closure by all tool handlers. | ||
| */ | ||
| export function createWpcomToolDefinitions( token: string, siteId: number ) { | ||
| const wpcom = wpcomFactory( token, wpcomXhrRequest ); | ||
|
|
||
| const getPostsTool = tool( | ||
| 'wpcom_get_posts', | ||
| 'Lists posts or pages on the WordPress.com site. Returns ID, title, status, type, date, and URL for each item.', |
There was a problem hiding this comment.
The PR title and description say this PR connects to the WP.com MCP server https://public-api.wordpress.com/wpcom/v2/mcp/v1
Use WP.com MCP for remote WordPress.com sites in AI command
- When a remote WordPress.com site is selected in the AI command, the agent now connects to the WP.com MCP server (
https://public-api.wordpress.com/wpcom/v2/mcp/v1) via HTTP transport with Bearer auth, instead of using local Studio tools
But apps/cli/ai/wpcom-tools.ts actually wraps direct WP.com REST API calls (wpcomFactory → /sites/{id}/posts, /plugins, etc.) inside a local SDK MCP server. The agent sees MCP tools implemented locally through REST endpoints.
Should we update the title and description to match the actual code, or try to use https://wordpress.com/me/mcp if possible?
|
I started with the MCP but it was way too limiting IMO so I switched to a custom MCP over the REST API which achieves better results. (I updated the PR description and title)
I also had this error multiple times, I think our proxy endpoint is imposing extra limits on top of what the LLMS impose. My thinking is that we should track this in its own ticket, either remove that limitation entirely (defer to the llm) or override it somehow. |
|
I've run into that too while working on the Figma connection. |
I created a separate issue STU-1524 |
When a remote WordPress.com site is selected, the AI agent now connects to the WP.com MCP server instead of using local Studio tools. Local sites continue using WP-CLI and file operations as before. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Instead of connecting to the external WP.com MCP server, build local tool definitions wrapping the WP.com REST API via the wpcom npm package. This gives full control over tool names, descriptions, error handling, and response formatting — all tailored for the AI agent. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…tool Instead of hardcoding individual tools for each endpoint, provide one flexible wpcom_request tool that can call any WP.com REST API endpoint. This lets the AI agent discover and use endpoints like templates, template parts, navigation, and global styles without needing explicit tool definitions for each one. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The wpcom_request tool now defaults to apiNamespace "wp/v2" (standard WordPress REST API) which supports templates, template parts, navigation, global styles, and other block theme resources. The WP.com v1.1 API is still available by setting apiNamespace to "" for WP.com-specific endpoints like plugin management and theme switching. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Free WordPress.com sites don't support custom CSS, inline styles, or custom JavaScript. Split the shared content/design guidelines into local and remote variants. Remote guidelines focus on block attributes, global styles, theme selection, and cover blocks for visual design. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Free WordPress.com sites restrict custom CSS, inline styles, global styles editing, and custom JS to paid plans. Instead of trying to work around these limitations, the agent now tells users about the constraint and suggests upgrading. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Plugin management requires a paid WordPress.com plan. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The global styles endpoint requires an ID that must be retrieved from the active theme's _links["wp:user-global-styles"] field. Document this in the endpoint reference so the agent knows how to find it. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The agent now checks the site plan via GET / before doing anything else, so it can inform users early when a requested action (custom CSS, plugins, design customization) requires a paid WordPress.com plan. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Explicitly forbid inline styles, style attributes, and all other workarounds for design customization on free plans. The agent must refuse and stop rather than suggest alternatives that produce invalid blocks on WordPress.com. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
fd8b086 to
ff2e207
Compare
|
Hey Riad! I was testing the remote site flow and noticed the plan check ( I pushed a small commit ( Happy to revert if you'd prefer a different approach — just wanted to share what I found while testing. 🙂 |
|
Hi @youknowriad, @lezama, after the latest changes in commit ff2e207 I was still getting errors due to the big size of the
|
See the discussion here #2992 (comment) The token limit is a broader issue that is not specific to a given endpoint. I'm not sure adding a tool for each endpoint is the right call. So It seems the best path is to avoid this tool and try to find a generic solution like maybe @epeicher's approach could work here. |
ff2e207 to
a3c7f6a
Compare
|
I reverted that commit for now, should we try to land this PR while we work on the token limit fix? Any approval? |
|
The plan sounds good. I'm taking a final look. |
epeicher
left a comment
There was a problem hiding this comment.
Thanks for the changes @youknowriad! I have tested it, and it works as expected, I have verified:
- Select a local site in studio ai → verify it works as before (WP-CLI, file ops, Studio tools)
- Select a remote WordPress.com site → verify the agent uses mcp__studio__wpcom_request and can manage content, templates, and take screenshots
- Ask the agent to modify a block theme template on a remote site → verify it uses wp/v2 endpoints
- Ask the agent for design changes on a free WP.com site → verify it explains the paid plan limitation
- Verify the system prompt documents wp/v2 and v1.1 endpoints when a remote site is active
| Session |
|---|
![]() |
sejas
left a comment
There was a problem hiding this comment.
Looks good. Thanks for creating the WPCOM REST API MCP. Everything worked quite good.
I see sometimes the agent uses the wrong API namespace, but it recovers by trying another one. See the /plugins example. I'll create a follow-up issue to improve the buildRemoteIntro system prompt or tools definition to be more specific.
|
There's a balance to be found between building specific tools and keeping the number of tools very small which makes the agent efficient. I think what could work well, is a tool to discover the right endpoint, and a tool to call the endpoint (to avoid having to specify every tool in the system prompt) |



Related issues
How AI was used in this PR
AI-assisted implementation. All code reviewed and tested manually.
Proposed Changes
wpcom_requesttool that can call any WordPress.com REST API endpoint, instead of using local Studio toolswpcom_requesttool is a generic REST API client — it takes a method, path, query, and body, and the agent determines the right endpoints based on its knowledge of the WordPress REST APIwp/v2) for standard resources (posts, pages, templates, template parts, navigation, global styles, etc.), with fallback to WP.com REST API v1.1 for WP.com-specific endpoints (plugins, theme switching, site settings)take_screenshottool is available for remote sites (works with any URL)wpcomSiteIdis preserved throughSiteInfo, prompt enrichment, and session recording_links)Testing Instructions
studio ai→ verify it works as before (WP-CLI, file ops, Studio tools)mcp__studio__wpcom_requestand can manage content, templates, and take screenshotsPre-merge Checklist