Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ jobs:
run: |
npm run test:php

- name: Run WP-CLI command registration tests
run: npm run test:cli

- name: Upload code coverage report
continue-on-error: true
if: ${{ matrix.coverage }}
Expand Down
4 changes: 2 additions & 2 deletions docs/guides/cli-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ Serves an MCP server via STDIO transport for communication with MCP clients.
#### Syntax

```bash
wp mcp-adapter serve [--server=<server-id>] [--user=<id|login|email>]
wp mcp-adapter serve [--server=<server-id>]
```

#### Options

- `--server=<server-id>` - The ID of the MCP server to serve. If not specified, uses the first available server.
- `--user=<id|login|email>` - Run as a specific WordPress user for permission checks. Without this, runs as unauthenticated (limited capabilities).
- `--user=<id|login|email>` is a WP-CLI global option that selects the WordPress user for permission checks. It is not a `mcp-adapter serve` option. Without it, the server runs as unauthenticated with limited capabilities.

#### Examples

Expand Down
6 changes: 3 additions & 3 deletions includes/Cli/McpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ final class McpCommand extends \WP_CLI_Command {
* ## EXAMPLES
*
* # Serve the default MCP server as admin user
* wp mcp serve --user=admin
* wp mcp-adapter serve --user=admin
*
* # Serve a specific server as user with ID 1
* wp mcp serve --server=my-mcp-server --user=1
* wp mcp-adapter serve --server=my-mcp-server --user=1
*
* # Serve without authentication (limited capabilities)
* wp mcp serve --server=public-server
* wp mcp-adapter serve --server=public-server
*
* @when after_wp_load
* @synopsis [--server=<server-id>]
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"lint:php:stan": "wp-env run cli --env-cwd=wp-content/plugins/$(basename \"$(pwd)\")/ composer run-script phpstan",
"plugin-zip": "wp-scripts plugin-zip",
"test:php": "npm run wp-env:test -- run cli --env-cwd=wp-content/plugins/$(basename \"$(pwd)\")/ vendor/bin/phpunit -c phpunit.xml.dist",
"test:cli": "npm run wp-env:test -- run cli --env-cwd=wp-content/plugins/$(basename \"$(pwd)\")/ sh tests/cli/test-serve-user-context.sh",
"wp-env": "node bin/patch-wp-env-bullseye.mjs && wp-env",
"wp-env:cli": "wp-env run cli --env-cwd=wp-content/plugins/$(basename \"$(pwd)\")/",
"wp-env:test": "node bin/patch-wp-env-bullseye.mjs && wp-env --config=.wp-env.test.json"
Expand Down
22 changes: 22 additions & 0 deletions tests/cli/assert-serve-user-context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Validates the user context seen by the serve command in a real WP-CLI run.
*
* @package WP\MCP\Tests
*/

\WP_CLI::add_wp_hook(
'plugins_loaded',
static function (): void {
add_filter(
'mcp_adapter_enable_stdio_transport',
static function (): bool {
if ( 1 !== get_current_user_id() || ! current_user_can( 'read' ) ) {
\WP_CLI::error( 'WP-CLI global --user was not applied to the MCP server user context.' );
}

return false;
}
);
}
);
35 changes: 35 additions & 0 deletions tests/cli/test-serve-user-context.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh

set -eu

help_output="$(wp mcp-adapter serve --help 2>&1)"

case "$help_output" in
*"already registered"*)
printf '%s\n' "$help_output" >&2
exit 1
;;
esac

case "$help_output" in
*"global \`--user\` flag"*)
;;
*)
printf '%s\n' "$help_output" >&2
exit 1
;;
esac

if serve_output="$(wp mcp-adapter serve --server=mcp-adapter-default-server --user=admin --require=tests/cli/assert-serve-user-context.php 2>&1)"; then
printf '%s\n' "$serve_output" >&2
exit 1
fi

case "$serve_output" in
*"The STDIO transport is disabled."*)
;;
*)
printf '%s\n' "$serve_output" >&2
exit 1
;;
esac
Loading