Skip to content

Add WordPress.com REST API support for remote sites in AI command#2992

Merged
youknowriad merged 11 commits intotrunkfrom
remote-site-wpcom-mcp
Apr 8, 2026
Merged

Add WordPress.com REST API support for remote sites in AI command#2992
youknowriad merged 11 commits intotrunkfrom
remote-site-wpcom-mcp

Conversation

@youknowriad
Copy link
Copy Markdown
Contributor

@youknowriad youknowriad commented Apr 6, 2026

Related issues

  • Related to remote site support in the AI command

How AI was used in this PR

AI-assisted implementation. All code reviewed and tested manually.

Proposed Changes

  • When a remote WordPress.com site is selected in the AI command, the agent now gets a wpcom_request tool that can call any WordPress.com REST API endpoint, instead of using local Studio tools
  • The wpcom_request tool 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 API
  • Defaults to the WordPress REST API (wp/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)
  • Local sites continue using WP-CLI and file operations as before (no behavior change)
  • System prompt is now fully site-aware with separate content/design guidelines for local vs remote:
    • Local: custom CSS, animations, scroll effects, theme files (unchanged)
    • Remote (free WP.com): informs users that design customizations require a paid plan, documents what's possible on free plans
  • The take_screenshot tool is available for remote sites (works with any URL)
  • wpcomSiteId is preserved through SiteInfo, prompt enrichment, and session recording
  • Includes hints for non-obvious API patterns (e.g., discovering the global styles ID from the active theme's _links)

Testing Instructions

  • 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

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?

@wpmobilebot
Copy link
Copy Markdown
Collaborator

wpmobilebot commented Apr 6, 2026

📊 Performance Test Results

Comparing 6cf7672 vs trunk

app-size

Metric trunk 6cf7672 Diff Change
App Size (Mac) 1252.02 MB 1252.03 MB +0.01 MB ⚪ 0.0%

site-editor

Metric trunk 6cf7672 Diff Change
load 1883 ms 1893 ms +10 ms ⚪ 0.0%

site-startup

Metric trunk 6cf7672 Diff Change
siteCreation 9131 ms 8126 ms 1005 ms 🟢 -11.0%
siteStartup 4165 ms 4169 ms +4 ms ⚪ 0.0%

Results are median values from multiple test runs.

Legend: 🟢 Improvement (faster) | 🔴 Regression (slower) | ⚪ No change (<50ms diff)

@youknowriad youknowriad requested review from epeicher and mtias April 7, 2026 08:21
@youknowriad youknowriad self-assigned this Apr 7, 2026
@youknowriad
Copy link
Copy Markdown
Contributor Author

System prompt is now site-aware: shared design/content guidelines with conditional intro sections for local vs remote workflows

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.

Copy link
Copy Markdown
Member

@sejas sejas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Image Image Image

The screenshot action worked good.

Image

Comment on lines +33 to +42
/**
* 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.',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@youknowriad youknowriad changed the title Use WP.com MCP for remote WordPress.com sites in AI command Add WordPress.com REST API support for remote sites in AI command Apr 7, 2026
@youknowriad
Copy link
Copy Markdown
Contributor Author

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)

Error: result (96,079 characters) exceeds maximum allowed tokens.

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.

@mtias
Copy link
Copy Markdown
Member

mtias commented Apr 7, 2026

I've run into that too while working on the Figma connection.

@sejas
Copy link
Copy Markdown
Member

sejas commented Apr 7, 2026

Error: result (96,079 characters) exceeds maximum allowed tokens.

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 created a separate issue STU-1524

youknowriad and others added 10 commits April 7, 2026 23:31
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>
@lezama lezama force-pushed the remote-site-wpcom-mcp branch from fd8b086 to ff2e207 Compare April 8, 2026 02:31
@lezama
Copy link
Copy Markdown
Contributor

lezama commented Apr 8, 2026

Hey Riad! I was testing the remote site flow and noticed the plan check (GET / on v1.1) returns ~103K chars which exceeds the agent's token limit — it spills to a file and the agent has to grep through it.

I pushed a small commit (ff2e207b) that adds a wpcom_site_info tool which requests only fields=ID,name,description,URL,plan, keeping the response compact. The system prompt now points to it as the mandatory first step.

Happy to revert if you'd prefer a different approach — just wanted to share what I found while testing. 🙂

@epeicher
Copy link
Copy Markdown
Contributor

epeicher commented Apr 8, 2026

Hi @youknowriad, @lezama, after the latest changes in commit ff2e207 I was still getting errors due to the big size of the plan field returned by the GET v1.1/sites/<siteId>?fields=ID,name,description,URL,plan so I have created a PR based on this branch #3005 that can be merged to this branch if required (or to trunk when this one is merged) which is based on those changes. I am not getting the error anymore:

Before After
CleanShot 2026-04-08 at 09 57 23@2x CleanShot 2026-04-08 at 09 54 57@2x

@youknowriad
Copy link
Copy Markdown
Contributor Author

youknowriad commented Apr 8, 2026

Hey Riad! I was testing the remote site flow and noticed the plan check (GET / on v1.1) returns ~103K chars which exceeds the agent's token limit — it spills to a file and the agent has to grep through it.

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.

@youknowriad youknowriad force-pushed the remote-site-wpcom-mcp branch from ff2e207 to a3c7f6a Compare April 8, 2026 09:43
@youknowriad
Copy link
Copy Markdown
Contributor Author

I reverted that commit for now, should we try to land this PR while we work on the token limit fix? Any approval?

@sejas
Copy link
Copy Markdown
Member

sejas commented Apr 8, 2026

The plan sounds good. I'm taking a final look.

Copy link
Copy Markdown
Contributor

@epeicher epeicher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Image

@youknowriad youknowriad merged commit a2cb85f into trunk Apr 8, 2026
10 checks passed
@youknowriad youknowriad deleted the remote-site-wpcom-mcp branch April 8, 2026 10:19
Copy link
Copy Markdown
Member

@sejas sejas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Image

@youknowriad
Copy link
Copy Markdown
Contributor Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants