Skip to content
Open
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
43 changes: 43 additions & 0 deletions lib/compat/plugin/connectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ function _gutenberg_connectors_preload_paths( array $preload_paths ): array {
// hardcoded ID to check whether the WP AI plugin is installed/active.
$preload_paths[] = '/wp/v2/plugins/ai/ai?context=edit';

// McpAccessPanel in routes/connectors-home/mcp-access-panel.tsx queries this
// hardcoded ID to check whether the MCP Adapter is installed/active.
$preload_paths[] = '/wp/v2/plugins/mcp-adapter/mcp-adapter?context=edit';

// getEntityRecord( 'root', 'plugin', <basename> ) per connector in use-connector-plugin.ts.
foreach ( wp_get_connectors() as $connector_data ) {
if ( empty( $connector_data['plugin']['file'] ) ) {
Expand All @@ -73,3 +77,42 @@ function _gutenberg_connectors_preload_paths( array $preload_paths ): array {
return $preload_paths;
}
add_filter( 'gutenberg_options_connectors_wp_admin_preload_paths', '_gutenberg_connectors_preload_paths' );

/**
* Adds the MCP Adapter's state to the Connectors page data.
*
* Reading the plugin record over REST needs `activate_plugins`, so users without
* it rely on this state, and the endpoint has to follow the site's REST URL
* settings, which only the server knows.
*
* @access private
*
* @param array<string, mixed> $data Connectors page data.
* @return array<string, mixed> Data with the MCP Adapter state added.
*/
function _gutenberg_connectors_mcp_adapter_data( array $data ): array {
if ( ! function_exists( 'validate_plugin' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

$file = 'mcp-adapter/mcp-adapter.php';
$status = 'not-installed';

if ( is_multisite() && is_plugin_active_for_network( $file ) ) {
$status = 'network-active';
} elseif ( is_plugin_active( $file ) ) {
$status = 'active';
} elseif ( 0 === validate_plugin( $file ) ) {
$status = 'inactive';
}

$data['mcpAdapter'] = array(
'status' => $status,
'endpoint' => rest_url( 'mcp/mcp-adapter-default-server' ),
'canInstall' => current_user_can( 'install_plugins' ),
'canActivate' => current_user_can( 'activate_plugins' ),
);

return $data;
}
add_filter( 'script_module_data_options-connectors-wp-admin', '_gutenberg_connectors_mcp_adapter_data' );
1 change: 1 addition & 0 deletions package-lock.json

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

122 changes: 122 additions & 0 deletions phpunit/connectors-mcp-adapter-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
/**
* Tests the MCP Adapter data sent to the Connectors page.
*
* @package gutenberg
*/

class Gutenberg_Connectors_Mcp_Adapter_Test extends WP_UnitTestCase {
const PLUGIN_FILE = 'mcp-adapter/mcp-adapter.php';

public function tear_down() {
$dir = WP_PLUGIN_DIR . '/mcp-adapter';
if ( file_exists( $dir . '/mcp-adapter.php' ) ) {
unlink( $dir . '/mcp-adapter.php' );
rmdir( $dir );
}
wp_clean_plugins_cache( false );

parent::tear_down();
}

private function install_stub_plugin() {
$dir = WP_PLUGIN_DIR . '/mcp-adapter';
if ( ! is_dir( $dir ) ) {
mkdir( $dir );
}
file_put_contents( $dir . '/mcp-adapter.php', "<?php\n/**\n * Plugin Name: MCP Adapter\n */\n" );
wp_clean_plugins_cache( false );
}

public function test_reports_missing_plugin() {
$data = _gutenberg_connectors_mcp_adapter_data( array() );

$this->assertSame( 'not-installed', $data['mcpAdapter']['status'] );
}

public function test_reports_installed_plugin_as_inactive() {
$this->install_stub_plugin();

$data = _gutenberg_connectors_mcp_adapter_data( array() );

$this->assertSame( 'inactive', $data['mcpAdapter']['status'] );
}

public function test_reports_active_plugin() {
$this->install_stub_plugin();
update_option( 'active_plugins', array( self::PLUGIN_FILE ) );

$data = _gutenberg_connectors_mcp_adapter_data( array() );

$this->assertSame( 'active', $data['mcpAdapter']['status'] );
}

public function test_reports_network_active_plugin() {
if ( ! is_multisite() ) {
$this->markTestSkipped( 'Network activation only exists on multisite.' );
}

$this->install_stub_plugin();
update_site_option( 'active_sitewide_plugins', array( self::PLUGIN_FILE => time() ) );

$data = _gutenberg_connectors_mcp_adapter_data( array() );

$this->assertSame( 'network-active', $data['mcpAdapter']['status'] );
}

public function test_endpoint_follows_the_site_address() {
// Filtered rather than updated: the test site pins both with constants.
add_filter(
'option_home',
static function () {
return 'http://example.org';
},
20
);
add_filter(
'option_siteurl',
static function () {
return 'http://example.org/wp';
},
20
);

$data = _gutenberg_connectors_mcp_adapter_data( array() );

$this->assertSame( rest_url( 'mcp/mcp-adapter-default-server' ), $data['mcpAdapter']['endpoint'] );
$this->assertStringStartsWith( 'http://example.org/', $data['mcpAdapter']['endpoint'] );
$this->assertStringNotContainsString( 'example.org/wp/', $data['mcpAdapter']['endpoint'] );
}

public function test_reports_plugin_management_permissions() {
$admin = self::factory()->user->create( array( 'role' => 'administrator' ) );
if ( is_multisite() ) {
grant_super_admin( $admin );
}
wp_set_current_user( $admin );

$data = _gutenberg_connectors_mcp_adapter_data( array() );

$this->assertTrue( $data['mcpAdapter']['canInstall'] );
$this->assertTrue( $data['mcpAdapter']['canActivate'] );

wp_set_current_user( self::factory()->user->create( array( 'role' => 'subscriber' ) ) );

$data = _gutenberg_connectors_mcp_adapter_data( array() );

$this->assertFalse( $data['mcpAdapter']['canInstall'] );
$this->assertFalse( $data['mcpAdapter']['canActivate'] );
}

public function test_keeps_existing_data() {
$data = _gutenberg_connectors_mcp_adapter_data( array( 'isFileModDisabled' => true ) );

$this->assertTrue( $data['isFileModDisabled'] );
}

public function test_preloads_the_mcp_adapter_plugin_record() {
$paths = _gutenberg_connectors_preload_paths( array() );

$this->assertContains( '/wp/v2/plugins/mcp-adapter/mcp-adapter?context=edit', $paths );
}
}
12 changes: 12 additions & 0 deletions routes/connectors-home/default-connectors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ interface ConnectorData {
authentication: NonNullable< ConnectorConfig[ 'authentication' ] >;
}

export interface McpAdapterData {
status: 'not-installed' | 'inactive' | 'active' | 'network-active';
endpoint: string;
canInstall: boolean;
canActivate: boolean;
}

interface ConnectorScriptModuleData {
connectors?: Record< string, ConnectorData >;
isFileModDisabled?: boolean;
mcpAdapter?: McpAdapterData;
}

function getConnectorScriptModuleData(): ConnectorScriptModuleData {
Expand All @@ -65,6 +73,10 @@ export function getIsFileModDisabled(): boolean {
return !! getConnectorScriptModuleData().isFileModDisabled;
}

export function getMcpAdapterData(): McpAdapterData | undefined {
return getConnectorScriptModuleData().mcpAdapter;
}

const CONNECTOR_LOGOS: Record< string, React.ComponentType > = {
google: GeminiLogo,
openai: OpenAILogo,
Expand Down
Loading
Loading