Skip to content

Add support for connecting to multiple MCP servers #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 23, 2025
Merged
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
83 changes: 83 additions & 0 deletions .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: Code Quality Checks

on:
pull_request:
branches:
- main
- master

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
actionlint:
name: Lint GitHub Actions workflows
runs-on: ubuntu-latest
steps:
- name: Check out source code
uses: actions/checkout@v4

- name: Add problem matcher
run: |
curl -s -o .github/actionlint-matcher.json https://github.com/raw/rhysd/actionlint/main/.github/actionlint-matcher.json
echo "::add-matcher::.github/actionlint-matcher.json"

- name: Check workflow files
uses: docker://rhysd/actionlint:latest
with:
args: -color -shellcheck=

lint:
name: Lint PHP files
runs-on: ubuntu-latest
steps:
- name: Check out source code
uses: actions/checkout@v4

- name: Set up PHP environment
uses: shivammathur/setup-php@v2
with:
php-version: 'latest'
ini-values: zend.assertions=1, error_reporting=-1, display_errors=On
tools: cs2pr
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Install Composer dependencies & cache dependencies
uses: "ramsey/composer-install@v3"
env:
COMPOSER_ROOT_VERSION: dev-${{ github.event.repository.default_branch }}
with:
# Bust the cache at least once a month - output format: YYYY-MM.
custom-cache-suffix: $(date -u "+%Y-%m")

- name: Run Linter
run: vendor/bin/parallel-lint -j 10 . --show-deprecated --exclude vendor --exclude .git --checkstyle | cs2pr

phpcs:
name: PHPCS
runs-on: ubuntu-latest

steps:
- name: Check out source code
uses: actions/checkout@v4

- name: Set up PHP environment
uses: shivammathur/setup-php@v2
with:
php-version: 'latest'
tools: cs2pr
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Install Composer dependencies & cache dependencies
uses: "ramsey/composer-install@v3"
env:
COMPOSER_ROOT_VERSION: dev-${{ github.event.repository.default_branch }}
with:
# Bust the cache at least once a month - output format: YYYY-MM.
custom-cache-suffix: $(date -u "+%Y-%m")

- name: Run PHPCS
run: vendor/bin/phpcs -q --report=checkstyle | cs2pr --graceful-warnings
3 changes: 1 addition & 2 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ on:
- main
- master
workflow_dispatch:
workflow_call:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down Expand Up @@ -50,7 +49,7 @@ jobs:
WP_CLI_TEST_DBUSER: wp_cli_test
WP_CLI_TEST_DBPASS: password1
WP_CLI_TEST_DBNAME: wp_cli_test
WP_CLI_TEST_DBHOST: 127.0.0.1:${{ job.services.mysql.ports[3306] }}
WP_CLI_TEST_DBHOST: 127.0.0.1:${{ job.services.mysql.ports['3306'] }}

unit: #-----------------------------------------------------------------------
name: Unit test / PHP ${{ matrix.php }}
Expand Down
115 changes: 115 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,121 @@ To install the latest development version of this package, use the following com
wp package install swissspidy/ai-command:dev-main
```


## Using

This package implements the following commands:

### wp ai

AI prompt.

~~~
wp ai <prompt> [--skip-wordpress]
~~~

**OPTIONS**

<prompt>
AI prompt.

[--skip-wordpress]
Run command without loading WordPress. (Not implemented yet)

**EXAMPLES**

# Get data from WordPress
$ wp ai "What are the titles of my last three posts?"
- Hello world
- My awesome post
- Another post

# Interact with multiple MCP servers.
$ wp ai "Take file foo.txt and create a new blog post from it"
Success: Blog post created.



### wp mcp server list

Lists available MCP servers.

~~~
wp mcp server list
~~~

**OPTIONS**

[--format=<format>]
: Render output in a particular format.
---
default: table
options:
- table
- csv
- json
- count

**EXAMPLES**

# Greet the world.
$ wp mcp server list
Success: Hello World!

# Greet the world.
$ wp ai "create 10 test posts about swiss recipes and include generated featured images"
Success: Hello World!



### wp mcp server add

Add a new MCP server to the list

~~~
wp mcp server add <name> <server>
~~~

**OPTIONS**

<name>
Name for referencing the server later

<server>
Server command or URL.

**EXAMPLES**

# Add server from URL.
$ wp mcp server add "server-github" "https://github.com/mcp"
Success: Server added.

# Add server with command to execute
$ wp mcp server add "server-filesystem" "npx -y @modelcontextprotocol/server-filesystem /my/allowed/folder/"
Success: Server added.



### wp mcp server remove

Remove a new MCP server from the list

~~~
wp mcp server remove <name>
~~~

**OPTIONS**

<name>
Name of the server to remove

**EXAMPLES**

# Remove server.
$ wp mcp server remove "server-filesystem"
Success: Server removed.


## Contributing

We appreciate you taking the initiative to contribute to this project.
Expand Down
37 changes: 3 additions & 34 deletions ai-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@

namespace WP_CLI\AiCommand;

use WP_CLI\AiCommand\ToolRepository\CollectionToolRepository;
use WP_CLI\AiCommand\Tools\ImageTools;
use WP_CLI\AiCommand\Tools\MiscTools;
use WP_CLI\AiCommand\Tools\URLTools;
use WP_CLI\AiCommand\Tools\CommunityEvents;
use WP_CLI\AiCommand\Tools\MapRESTtoMCP;
use WP_CLI\AiCommand\Tools\MapCLItoMCP;
use WP_CLI;

if ( ! class_exists( '\WP_CLI' ) ) {
Expand All @@ -21,30 +14,6 @@
require_once $ai_command_autoloader;
}

WP_CLI::add_command( 'ai', static function ( $args, $assoc_args ) {
$server = new MCP\Server();
$client = new MCP\Client($server);

$tools = new ToolCollection();

$all_tools = [
...(new ImageTools($client, $server))->get_tools(),
...(new CommunityEvents($client))->get_tools(),
...(new MiscTools($server))->get_tools(),
...(new URLTools($server))->get_tools(),
...(new MapRESTtoMCP())->map_rest_to_mcp(),
...(new MapCLItoMCP())->map_cli_to_mcp(),

];

foreach ($all_tools as $tool) {
$tools->add($tool);
}

$ai_command = new AiCommand(
new CollectionToolRepository( $tools ),
$server,
$client
);
$ai_command( $args, $assoc_args );
} );
WP_CLI::add_command( 'ai', AiCommand::class );
WP_CLI::add_command( 'mcp prompt', AiCommand::class );
WP_CLI::add_command( 'mcp server', McpServerCommand::class );
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"authors": [],
"require": {
"php": "^8.2",
"logiscape/mcp-sdk-php": "^1.0",
"wp-cli/wp-cli": "^2.11"
},
"require-dev": {
Expand All @@ -28,7 +29,9 @@
"bundled": false,
"commands": [
"ai",
"ai prompt"
"mcp server list",
"mcp server add",
"mcp server remove"
]
},
"autoload": {
Expand Down
7 changes: 7 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@
</properties>
</rule>

<rule ref="WordPress.NamingConventions.ValidVariableName">
<properties>
<property name="allowed_custom_properties" type="array">
<element value="inputSchema" />
</property>
</properties>
</rule>
</ruleset>
Loading