A Node.js server that connects to your WordPress site and exposes functionality via the Model Context Protocol (MCP).
- Introduction
- Prerequisites
- Installation
- Usage
- Configuration
- Available Tools (Endpoints)
- Error Handling
- Contributing
- License
This project showcases how to build a Model Context Protocol (MCP) server that interacts with a WordPress website. It leverages:
The server listens for incoming requests through STDIO transport, executes the requested tools (endpoints), and returns results in JSON format.
-
Clone this repository (or copy the relevant files into your own project):
git clone https://github.com/yourorg/wordpress-mcp-server.git cd wordpress-mcp-server
-
Install Dependencies:
npm install
This will install:
@modelcontextprotocol/sdk
zod
- any other dependencies defined in the
package.json
.
-
Set Environment Variables:
-
WP_URL – The URL of your WordPress site (e.g.,
https://example.com/wp-json
orhttps://example.com
depending on your setup). - WP_USERNAME – The WordPress username.
- WP_PASSWORD – The corresponding password or application password.
Example:
export WP_URL="https://your-wordpress.com" export WP_USERNAME="admin" export WP_PASSWORD="admin_password"
-
WP_URL – The URL of your WordPress site (e.g.,
-
Run the Server:
npm start
or
node index.js
(Depending on how you have set up your entry point in
package.json
.)You should see console output indicating that the server has started.
Starting server... Server started!
-
Connect to the MCP Server:
- The server uses the StdIOServerTransport by default. Typically, the MCP orchestrator (or any MCP-compatible client) will handle communication via STDIO.
- Refer to the Model Context Protocol documentation for instructions on how to link this server with your MCP client or orchestrator.
Aside from the environment variables, you can fine-tune the following aspects in the code (index.js
or server.js
):
-
Server Name and Description: You can modify the parameters passed to the
McpServer
constructor:const server = new McpServer({ name: "Wordpress Plugin", description: "A plugin for Wordpress", version: "1.0.0" });
-
Tool Names and Descriptions: Each “tool” (endpoint) includes a name, description, and input schema defined with
zod
. -
Default Values (e.g., post status, default pagination, etc.) in the
buildPostInput
/buildCategoryInput
helper functions.
Below are the tools exposed by this MCP server. They can be called via the MCP client, orchestrator, or any code that interacts with the MCP over STDIO.
Name: fetch_posts
Description: Fetch posts from a WordPress blog.
-
Parameters:
-
page
(number, optional) – The page number to retrieve. -
perPage
(number, optional) – The number of posts per page.
-
Returns
A JSON list of posts with their title, excerpt, content, slug, and date.
Name: fetch_categories
Description: Fetches all categories from the WordPress blog.
- Parameters: None
-
Returns
A JSON list of categories with their corresponding information (ID, name, slug, etc.).
Name: fetch_media
Description: Fetches all media items from the WordPress site.
- Parameters: None
-
Returns
A JSON list of media items with relevant information (e.g., title, source URL, etc.).
Name: create_post
Description: Creates a new post in WordPress.
-
Parameters:
-
title
(string) – Title of the post. -
content
(string) – Content/body of the post. -
categories
(array of numbers, optional) – Array of WordPress category IDs. -
tags
(array of numbers, optional) – Array of WordPress tag IDs. -
slug
(string, optional) – Custom slug for the post. -
status
(enum, optional) – Post status (publish
,draft
, orpending
). Defaults todraft
.
-
-
Returns
A JSON object containing information about the newly created post (e.g., ID, link, status, etc.).
Name: create_category
Description: Creates a category in WordPress.
-
Parameters:
-
name
(string) – Name of the new category. -
description
(string) – Description of the category. -
slug
(string, optional) – Custom slug for the category. -
parent
(number, optional) – Parent category ID.
-
-
Returns
A JSON object containing information about the newly created category.
Each tool wraps the WordPressService calls in a try/catch
block. In case of an error:
- The code logs the error to the console.
- Responds with a JSON content object containing
"Error <action>..."
. - Sets
isError
totrue
when relevant so the client/orchestrator can detect and handle errors accordingly.
Example error response:
{
"content": [
{
"type": "text",
"text": "Error creating post"
}
],
"isError": true
}
- Fork this repository.
- Create a new branch with your feature or bug fix.
- Commit your changes, including tests (if applicable).
- Open a Pull Request describing your modifications.
All contributions are welcome! Please ensure your changes align with the Model Context Protocol TypeScript SDK best practices.