Skip to content

Add McpAdapter::on_init() helper for safe server registration#217

Open
arunshenoy99 wants to merge 2 commits into
WordPress:trunkfrom
arunshenoy99:add/mcp-adapter-on-init-helper
Open

Add McpAdapter::on_init() helper for safe server registration#217
arunshenoy99 wants to merge 2 commits into
WordPress:trunkfrom
arunshenoy99:add/mcp-adapter-on-init-helper

Conversation

@arunshenoy99

Copy link
Copy Markdown

Summary

Adds McpAdapter::on_init(), a small convenience wrapper around add_action( 'mcp_adapter_init', ... ) that builds an adapter-identity check in for the caller.

This was split out of #173 (per review feedback) so the new public API can be discussed and documented on its own. The duplicate_server_id crash itself is fixed independently by the factory guard in #173; this helper is an ergonomic addition, not required for that fix.

Why

The mcp_adapter_init action name is global. When more than one active plugin vendors wordpress/mcp-adapter (with or without namespace prefixing), every copy's dispatch runs every copy's subscribers. A subscriber that registers servers without checking which adapter dispatched ends up registering on every dispatch, causing duplicate-registration errors.

A consumer that uses the $adapter passed to its mcp_adapter_init callback is already safe. on_init() simply makes that guarantee explicit and harder to get wrong:

\WP\MCP\Core\McpAdapter::instance()->on_init( function ( $adapter ) {
    $adapter->create_server( /* ... */ );
} );

The wrapped callback only runs when this adapter is the one dispatching.

Public API note

This adds a new public method, so it is a commitment once shipped — hence the dedicated PR and docs. Feedback on the name/shape welcome.

Tests & docs

  • Two unit tests: callback runs for the matching adapter, and is skipped when a foreign adapter dispatches the shared action.
  • README section documenting the helper alongside the existing custom-server example, plus a note to prefer the passed-in $adapter over McpAdapter::instance() inside mcp_adapter_init callbacks.
  • Full unit suite passes locally (963 tests).

The mcp_adapter_init action name is global, so when more than one copy of
this library is active in the same request (e.g. two plugins that each
vendor wordpress/mcp-adapter, with or without namespace prefixing), every
copy's dispatch runs every copy's subscribers. A subscriber that registers
servers without checking which adapter dispatched causes duplicate
registration errors on subsequent dispatches.

on_init() wraps add_action( 'mcp_adapter_init', ... ) with an identity
check so the callback only runs when this adapter is the one dispatching.
Consumers that already use the $adapter passed to their mcp_adapter_init
callback are equally safe; this helper just makes that guarantee explicit.

Adds two unit tests (matching and foreign adapter) and a README section
documenting the helper alongside the existing custom-server example.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: arunshenoy99 <arunshenoy99@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Reframe the on_init() docblock from "prefer this over add_action()" to
"an alternative to add_action() that builds the identity guard in", and
make explicit that a subscriber using the $adapter passed to its
mcp_adapter_init callback is already safe. The helper is an ergonomic
convenience, not a required replacement for the action.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant