Add McpAdapter::on_init() helper for safe server registration#217
Open
arunshenoy99 wants to merge 2 commits into
Open
Add McpAdapter::on_init() helper for safe server registration#217arunshenoy99 wants to merge 2 commits into
arunshenoy99 wants to merge 2 commits into
Conversation
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.
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
McpAdapter::on_init(), a small convenience wrapper aroundadd_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_idcrash 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_initaction name is global. When more than one active plugin vendorswordpress/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
$adapterpassed to itsmcp_adapter_initcallback is already safe.on_init()simply makes that guarantee explicit and harder to get wrong: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
$adapteroverMcpAdapter::instance()insidemcp_adapter_initcallbacks.