WordPress MCP服务器插件

将WordPress网站暴露为MCP服务器,支持通过JSON-RPC协议访问WordPress和WooCommerce数据。

  • by bhavik-dreamz
  • PHP
  • 本地部署
  • 更新于
在 GitHub 查看 bhavik-dreamz/WP-MCP-Server

使用说明

WP MCP服务器

将WordPress站点公开为MCP(模型上下文协议)服务器,以便像Claude Desktop这样的MCP客户端可以通过HTTP(JSON-RPC)调用工具。此插件实现了REST端点和一组用于WordPress和WooCommerce数据访问的工具。

特性

  • 暴露 /wp-json/wp-mcp/v1/info (公众)和 /wp-json/wp-mcp/v1/mcp (JSON-RPC入口点)
  • 通过WordPress应用程序密码进行身份验证(基本身份验证标头)
  • 工具:search_posts、search_pages、search_post_categories、search_tags、create_post、create_page、create_category、search_custom_post_types、create_custom_post、search_products、create_product、search_prooduct_categories、create_order、get_orders、get_order_details、create_user、recommendation_products
  • 管理员设置页面,用于配置站点URL、启用的工具、CPT白名单和测试凭据
  • WooCommerce感知:产品/订单工具仅在WooCommerce存在时处于活动状态

安装

  1. 复制 wp-mcp-server 将文件夹放入WordPress wp-content/plugins/ 目录(或将此仓库用作插件文件夹)。
  2. 从插件文件夹中,安装Composer依赖项:
cd e:\path\to\wp-mcp-server
composer install
  1. 在WordPress中激活插件(插件→ 已安装的插件)。
  2. 转到“设置”→ WP MCP服务器,用于配置站点URL、启用工具和将CPT列入白名单。
  3. 为用户创建应用程序密码(用户→ 个人资料→ 应用程序密码),并将这些凭据用于MCP客户端。

端点

  • GET{site_url}/wp-json/wp-mcp/v1/info

    • Public:返回插件版本、站点名称、可用工具。
  • POST{site_url}/wp-json/wp-mcp/v1/mcp

    • 受保护:需要标头 Authorization: Basic base64(username:application_password)
    • 接受JSON-RPC 2.0请求。支持的方法: tools/list 和 tools/call.

JSON-RPC请求示例(工具/列表):

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/list",
  "params": {}
}

JSON-RPC请求示例(调用工具):

{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/call",
  "params": {
    "name": "search_posts",
    "arguments": { "query": "hello", "per_page": 5 }
  }
}

MCP工具参考

使用 tools/list 为您的网站/用户上下文发现当前可用的工具。该列表取决于:

  • 在“设置”中启用了哪些工具→ WP MCP服务器
  • WooCommerce是否处于活动状态
  • 当前用户能力(用于订购工具)

内容工具(WordPress核心)

这些工具允许MCP客户端处理WordPress内容数据(帖子、页面和自定义帖子类型),而不仅仅是WooCommerce。

  1. search_posts

    • 目的:搜索公共帖子(或其他有效的帖子类型)。
    • 论据:
      • query (字符串,可选)
      • post_type (字符串,可选,默认值 post)
      • per_page (整数,可选,默认值 10)
      • page (整数,可选,默认值 1)
    • 退货: { "results": [...], "total": <number> }
  2. search_pages

    • 目的:搜索WordPress页面。
    • 论据:
      • query (字符串,可选)
      • per_page (整数,可选,默认值 10)
      • page (整数,可选,默认值 1)
    • 返回:结构与 search_posts.
  3. search_custom_post_types

    • 目的:搜索插件设置中白名单中的自定义帖子类型。
    • 论据:
      • post_type (字符串,必填)
      • query (字符串,可选)
      • meta_filters (数组,可选)例如。 [{"key":"city","value":"London"}]
      • per_page (整数,可选,默认值 10)
      • page (整数,可选,默认值 1)
    • 退货: { "results": [...], "total": <number> }
  4. search_post_categories

    • 目的:搜索WordPress博客类别。
    • 论据:
      • query (字符串,可选)
      • parent_id (整数,可选)
      • per_page (整数,可选,默认值 20)
    • 退货: { "results": [...] }
  5. search_tags

    • 目的:搜索WordPress博客标签。
    • 论据:
      • query (字符串,可选)
      • per_page (整数,可选,默认值 20)
    • 退货: { "results": [...] }
  6. create_post

  • 目的:创建一个新的WordPress帖子。
  • 论据:
    • title (字符串,必填)
    • content (字符串,可选)
    • excerpt (字符串,可选)
    • status (字符串,可选,默认值 draft)
    • slug (字符串,可选)
    • author_id (整数,可选)
  • 返回:使用创建的post对象 id, post_type, status, url.
  1. create_page
  • 目的:创建一个新的WordPress页面。
  • 论点:与 create_post.
  • 返回:已创建的页面对象。
  1. create_category
  • 目的:创建一个WordPress博客类别。
  • 论据:
    • name (字符串,必填)
    • slug (字符串,可选)
    • parent_id (整数,可选)
    • description (字符串,可选)
  • 返回:使用创建的类别对象 id, taxonomy, name, url.

分类和订购工具

  • WordPress分类和标签工具可用 search_post_categories 和 search_tags.
  • WooCommerce产品类别搜索可用 search_product_categories.
  • 订单详情可通过 get_order_details (WooCommerce+需要适当的能力)。

WooCommerce工具(仅在安装WooCommerce时)

  1. search_products

    • 目的:搜索具有可选类别、库存和价格过滤的产品。
    • 论据:
      • query (字符串,可选)
      • category_id (整数,可选)
      • min_price (数字,可选)
      • max_price (数字,可选)
      • in_stock (布尔值,可选)
      • per_page (整数,可选,默认值 10)
      • page (整数,可选,默认值 1)
    • 退货: { "results": [...] }
  2. create_custom_post_type

  • 目的:创建CPT项目(插件设置中必须允许CPT)。
  • 论据:
    • post_type (字符串,必填)
    • title (字符串,必填)
    • content (字符串,可选)
    • excerpt (字符串,可选)
    • status (字符串,可选,默认值 draft)
    • slug (字符串,可选)
    • author_id (整数,可选)
  • 返回:已创建的项目对象 id, post_type, status, url.
  1. search_product_categories
  • 目的:搜索/列出产品类别。
  • 论据:
    • query (字符串,可选)
    • parent_id (整数,可选)
    • per_page (整数,可选,默认值 20)
  • 退货: { "results": [...] }
  1. create_product
  • 目的:创建WooCommerce产品。
  • 访问:需要具有以下权限的用户 manage_woocommerce 能力。
  • 论据:
    • name (字符串,必填)
    • description (字符串,可选)
    • short_description (字符串,可选)
    • status (字符串,可选,默认值 draft)
    • regular_price (数字,可选)
    • sale_price (数字,可选)
    • sku (字符串,可选)
    • manage_stock (布尔值,可选)
    • stock_quantity (整数,可选)
    • category_ids (数组,可选)
  • 返回:已创建的产品对象。
  1. create_order
  • 目的:创建一个包含行项目的WooCommerce订单。
  • 访问:需要具有以下权限的用户 manage_woocommerce 能力。
  • 论据:
    • line_items (数组,必填)例如。 [{"product_id":123,"quantity":2}]
    • customer_id (整数,可选)
    • billing (对象,可选)
    • shipping (对象,可选)
    • status (字符串,可选)
  • 退货:已创建订单摘要(id, status, total, currency).
  1. get_orders
  • 目的:列出带有过滤器的订单。
  • 访问:需要具有以下权限的用户 manage_woocommerce 能力。
  • 论据:
    • status (字符串,可选)
    • customer_id (整数,可选)
    • date_from (字符串,可选, YYYY-MM-DD)
    • date_to (字符串,可选, YYYY-MM-DD)
    • per_page (整数,可选,默认值 10)
    • page (整数,可选,默认值 1)
  • 退货: { "results": [...] }
  1. get_order_details
  • 目的:获取完整的订单详细信息(账单/运费、行项目、总计、备注)。
  • 访问:需要具有以下权限的用户 manage_woocommerce 能力。
  • 论据:
    • order_id (整数,必填)
  • 返回:订单对象(未包裹在 results).
  1. create_user
  • 目的:创建一个WordPress用户。
  • 访问:需要具有以下权限的用户 create_users 能力。
  • 论据:
    • username (字符串,必填)
    • email (字符串,必填)
    • password (字符串,可选)
    • display_name (字符串,可选)
    • role (字符串,可选,默认值 subscriber)
  • 返回:创建的用户对象。
  1. recommend_products
  • 目的:返回产品推荐。
  • 论据:
    • strategy (字符串,可选): related, upsell, crosssell, bestseller, new_arrivals
    • product_id (整数,必填 related, upsell, crosssell)
    • category_id (整数,可选)
    • limit (整数,可选,默认值 5)
  • 退货: { "results": [...] }

如何使用工具(复制/粘贴流程)

  1. 呼叫 tools/list
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/list",
  "params": {}
}
  1. 从中选择工具名称 tools[].name

  2. 呼叫 tools/call 随着 params.name 和 params.arguments

示例:搜索价格低于50的库存产品

{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/call",
  "params": {
    "name": "search_products",
    "arguments": {
      "query": "shirt",
      "in_stock": true,
      "max_price": 50,
      "per_page": 5
    }
  }
}

示例:获取单个订单详细信息

{
  "jsonrpc": "2.0",
  "id": 3,
  "method": "tools/call",
  "params": {
    "name": "get_order_details",
    "arguments": {
      "order_id": 1234
    }
  }
}

更多示例(帖子+页面+CPT+类别/标签+订单)

示例:搜索博客文章

{
  "jsonrpc": "2.0",
  "id": 10,
  "method": "tools/call",
  "params": {
    "name": "search_posts",
    "arguments": {
      "query": "shipping policy",
      "post_type": "post",
      "per_page": 5,
      "page": 1
    }
  }
}

示例:搜索页面

{
  "jsonrpc": "2.0",
  "id": 11,
  "method": "tools/call",
  "params": {
    "name": "search_pages",
    "arguments": {
      "query": "returns",
      "per_page": 5
    }
  }
}

示例:搜索自定义帖子类型(CPT)

{
  "jsonrpc": "2.0",
  "id": 12,
  "method": "tools/call",
  "params": {
    "name": "search_custom_post_types",
    "arguments": {
      "post_type": "event",
      "query": "summer",
      "meta_filters": [
        { "key": "city", "value": "London" }
      ],
      "per_page": 10
    }
  }
}

示例:搜索WordPress帖子类别

{
  "jsonrpc": "2.0",
  "id": 13,
  "method": "tools/call",
  "params": {
    "name": "search_post_categories",
    "arguments": {
      "query": "news",
      "per_page": 20
    }
  }
}

示例:搜索WordPress标签

{
  "jsonrpc": "2.0",
  "id": 14,
  "method": "tools/call",
  "params": {
    "name": "search_tags",
    "arguments": {
      "query": "featured",
      "per_page": 20
    }
  }
}

示例:创建帖子

{
  "jsonrpc": "2.0",
  "id": 15,
  "method": "tools/call",
  "params": {
    "name": "create_post",
    "arguments": {
      "title": "New Shipping Update",
      "content": "We now support express shipping.",
      "status": "publish"
    }
  }
}

示例:创建类别

{
  "jsonrpc": "2.0",
  "id": 16,
  "method": "tools/call",
  "params": {
    "name": "create_category",
    "arguments": {
      "name": "Announcements",
      "slug": "announcements"
    }
  }
}

示例:创建产品

{
  "jsonrpc": "2.0",
  "id": 17,
  "method": "tools/call",
  "params": {
    "name": "create_product",
    "arguments": {
      "name": "Classic Hoodie",
      "regular_price": 49.99,
      "status": "publish",
      "manage_stock": true,
      "stock_quantity": 25
    }
  }
}

示例:创建订单

{
  "jsonrpc": "2.0",
  "id": 18,
  "method": "tools/call",
  "params": {
    "name": "create_order",
    "arguments": {
      "customer_id": 12,
      "line_items": [
        { "product_id": 101, "quantity": 2 }
      ],
      "status": "processing"
    }
  }
}

示例:创建用户

{
  "jsonrpc": "2.0",
  "id": 19,
  "method": "tools/call",
  "params": {
    "name": "create_user",
    "arguments": {
      "username": "new_customer",
      "email": "new_customer@example.com",
      "role": "customer"
    }
  }
}

示例:搜索产品类别

{
  "jsonrpc": "2.0",
  "id": 20,
  "method": "tools/call",
  "params": {
    "name": "search_product_categories",
    "arguments": {
      "query": "hoodie",
      "per_page": 20
    }
  }
}

示例:获取订单详细信息

{
  "jsonrpc": "2.0",
  "id": 21,
  "method": "tools/call",
  "params": {
    "name": "get_order_details",
    "arguments": {
      "order_id": 1234
    }
  }
}

您可能会看到的常见错误

  • woocommerce_missing:WooCommerce未安装/未激活Woo工具。
  • insufficient_capability:用户缺少订购工具的权限。
  • invalid_post_type:帖子类型值无效。
  • invalid_taxonomy:分类法必须为 category 或 post_tag WP分类工具。
  • post_type not allowed:CPT未在插件设置中列入白名单。
  • name parameter required / Unknown tool:错误 tools/call 有效载荷。

Claude桌面配置(示例)

{
  "mcpServers": {
    "wordpress-site": {
      "command": "curl",
      "args": [
        "-X", "POST",
        "-H", "Content-Type: application/json",
        "-H", "Authorization: Basic {base64_credentials}",
        "-d", "@-",
        "{site_url}/wp-json/wp-mcp/v1/mcp"
      ]
    }
  }
}

替换 {base64_credentials} 使用base64 username:application_password 和 {site_url} 使用您配置的网站URL。

安全

  • 为所有人使用WordPress应用程序密码 /mcp 请求:
  • Admin AJAX使用nonce和能力检查。
  • 使用WordPress助手对输入进行净化。

备注

  • 如果WooCommerce未处于活动状态,WooCommerce特定的工具将被隐藏且不可用。
  • 该插件将使用 logiscape/mcp-sdk-php 如果存在(通过Composer安装)。如果缺少SDK,插件将退回到仍然支持工具/列表和工具/调用的最小JSON-RPC路由器。

贡献

  • 请遵循WordPress编码标准,并使用内置API进行数据访问(无直接数据库查询)。

许可证

  • MIT(根据需要添加或更改)