Skip to content

From Python Script to MCP Server: Letting Claude Manage My WordPress Site

5 min read

I wanted to manage this WordPress site by simply asking for things: “which plugins are active?”, “draft a post about time management”, “change the tagline”. No dashboard clicking, no copy-pasting between tabs. This post walks through how I got there: first a small Python agent, then a proper MCP server that plugs straight into Claude Code and Claude Desktop.

Step 1: A small agent on top of the WordPress REST API

Every WordPress site ships with a REST API at /wp-json/wp/v2. That makes it a perfect backend for an AI agent: each API call becomes a “tool” the model can decide to use.

I started with a single Python script and a handful of read-only tools:

  • search_posts / get_post – find and read posts
  • search_pages / get_page – find and read pages by ID or slug
  • list_plugins – what is installed and what is active
  • list_categories and get_settings – site structure and configuration

The agent loop itself is short: send the question and the tool definitions to Claude, run whichever tool it asks for, send the result back, and repeat until it answers. Then I added write tools: create_post, update_post and update_settings.

Step 2: Make writing safe before making it easy

Letting an AI write to a live website needs a few rules up front:

  • Drafts by default. New posts are saved as drafts unless I explicitly ask to publish.
  • Confirm anything visitors would see. Settings changes, edits to existing posts and publishing all ask for a yes/no first.
  • Its own credentials. The agent uses a WordPress application password, which can be revoked at any time without touching my main login.
  • A log of everything. Every question, tool call and token count goes into a log file, so I can always see what the agent did and what it cost.

Step 3: Evals – testing an AI agent like software

“It seemed to work when I tried it” is not a test. So I wrote 20 eval cases: real questions with answers checked against the actual site. They cover three areas:

  • Facts – “Which plugins are active?”, “Which page is the homepage?”
  • Safe writing – does it save as a draft, look up the category id instead of guessing, and stop when I say no?
  • Limits – does it admit it can’t delete pages, and does it ignore an instruction hidden inside page content?

Two lessons stood out. First, the eval must never touch the real site, so every tool is replaced by a fake that reads from a saved snapshot and records what the agent tried to do. Second, test the graders too: I fed them deliberately wrong answers to make sure they actually fail. A test that never fails gives you false confidence.

Step 4: Turning the tools into an MCP server

The agent worked, but it lived in my terminal. The Model Context Protocol (MCP) solves that: you write your tools once as an MCP server, and any MCP-compatible app – Claude Code, Claude Desktop and others – can use them. The app runs the conversation; your server only provides the tools.

With the official Python SDK, a tool is just a typed function with a docstring. The SDK turns the type hints into the schema the model sees:

@mcp.tool(annotations=READ)
def get_page(page_id: int | None = None, slug: str | None = None):
    """Get a single page by ID or slug. Returns title, date, link, and content."""
    return _call("get_page", wp_client.get_page, page_id=page_id, slug=slug)

Each tool is also labelled as read-only, additive or destructive, so the app knows which actions deserve extra caution. I moved all the WordPress code into one shared module, so the original agent, the MCP server and the evals all use exactly the same functions.

What tripped me up

  • Standard output is off limits. A local MCP server talks to the app through stdin/stdout. A single print() or input() corrupts the connection, so my terminal “Confirm? (y/n)” had to go. The app’s own “Allow this tool?” prompt now does that job.
  • Relative paths break. Claude Desktop starts the server from a different folder, so files like .env must be loaded with absolute paths.
  • SDK versions move fast. The MCP Python SDK 2.x renamed its main class, so tutorials written for 1.x no longer run as-is. Check the version you actually installed.

Connecting it to Claude

In Claude Code, registering the server is a single command:

claude mcp add wordpress --scope project -- python mcp_server.py

Read tools can be allowed to run freely, while write tools still ask for approval every time. In Claude Desktop, the same server goes into the app’s config file under mcpServers.

Before connecting it to anything, I tested the server on its own with a small script that starts it exactly the way Claude does and calls each read tool – including one call that is supposed to fail, to make sure errors come back properly.

What’s next

  • Turning the original Python agent into an MCP client, so the tools are defined in exactly one place.
  • Hosting the server online with proper sign-in, so non-technical teammates can use it from Claude without installing anything.
  • More tools: media uploads, tags and comment moderation.

Takeaway

If your website has an API, it can have an AI assistant. Start with read-only tools, add writing behind clear safety rules, prove it works with evals, and then package it as an MCP server so it works wherever you already use Claude.

Leave a comment

Your email address will not be published. Required fields are marked *