Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@
],
"type": "wordpress-plugin",
"license": "GPLv2",
"repositories": [
{
"type": "vcs",
"url": "https://github.com/WordPress/mcp-adapter"
}
],
"require": {
"php": ">=7.4",
"yahnis-elsts/plugin-update-checker": "5.1",
"aws/aws-sdk-php": "^3.300",
"woocommerce/action-scheduler": "3.8.1",
"wordpress/abilities-api": "^0.1"
"wordpress/abilities-api": "^0.1",
"wordpress/mcp-adapter": "dev-trunk"
},
"autoload": {
"psr-4": {
Expand Down
99 changes: 97 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions includes/Classifai/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php
namespace Classifai;

use WP\MCP\Core\McpAdapter;
use WP\MCP\Transport\Http\RestTransport;
use WP\MCP\Infrastructure\ErrorHandling\ErrorLogMcpErrorHandler;
use WP\MCP\Infrastructure\Observability\NullMcpObservabilityHandler;

class Plugin {

/**
Expand All @@ -18,6 +23,11 @@ class Plugin {
*/
public $admin_helpers = [];

/**
* @var McpAdapter $mcp_adapter The MCP adapter.
*/
public $mcp_adapter = null;

/**
* Lazy initialize the plugin
*/
Expand All @@ -42,6 +52,10 @@ public function enable() {
add_filter( 'plugin_action_links_' . CLASSIFAI_PLUGIN_BASENAME, [ $this, 'filter_plugin_action_links' ] );
add_filter( 'robots_txt', [ $this, 'maybe_block_ai_crawlers' ] );
add_action( 'after_classifai_init', [ $this, 'load_action_scheduler' ] );

// Initialize the MCP adapter so the hooks are loaded.
$this->mcp_adapter = McpAdapter::instance();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my testing, this was needed to ensure the MCP code was loaded. Not ideal but I'm assuming this will get better as this package matures

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So looked at this a bit closer and the issue here is when loading the plugin via composer, composer will add the autoloaded files from the plugin to our autoloader but that doesn't include the main plugin file (as WordPress takes care of loading that). In our case, we aren't loading this as a normal plugin and so that main file never loads, which is what initializes everything.

If you look at that file, it does this:

// Load the plugin.
if ( class_exists( WP\MCP\Plugin::class ) ) {
	WP\MCP\Plugin::instance();
}

That ends up calling McpAdapter::instance(); which is what we are doing here. So we can either leave this as-is or we can swap this out for the above

add_action( 'mcp_adapter_init', [ $this, 'mcp_adapter_init' ] );
}

/**
Expand Down Expand Up @@ -341,6 +355,30 @@ public function load_action_scheduler() {
}
}

/**
* Initialize the MCP adapter.
*
* @param McpAdapter $adapter The MCP adapter.
*/
public function mcp_adapter_init( McpAdapter $adapter ) {
$adapter->create_server(
'classifai',
'classifai',
'mcp',
'ClassifAI MCP Server',
'Custom MCP Server for ClassifAI Features',
'v1.0.0',
[
RestTransport::class,
],
ErrorLogMcpErrorHandler::class,
NullMcpObservabilityHandler::class,
[
'classifai/generate-title',
],
);
}

/**
* Migrate the existing settings to v3 if necessary.
*
Expand Down