Skip to content

fix: allow dynamic handler registration after connect with pre-supplied capabilities#1602

Open
giulio-leone wants to merge 2 commits intomodelcontextprotocol:v1.xfrom
giulio-leone:fix/capabilities-reregistration-after-connect
Open

fix: allow dynamic handler registration after connect with pre-supplied capabilities#1602
giulio-leone wants to merge 2 commits intomodelcontextprotocol:v1.xfrom
giulio-leone:fix/capabilities-reregistration-after-connect

Conversation

@giulio-leone
Copy link

Summary

When McpServer is created with capabilities in ServerOptions, registering tools/resources/prompts after connect() throws:

Cannot register capabilities after connecting to transport

This occurs because setToolRequestHandlers, setResourceRequestHandlers, and setPromptRequestHandlers unconditionally call registerCapabilities(), which rejects all post-connect calls.

Fix

registerCapabilities is now idempotent for already-present capability keys: if the transport is connected and all requested capability keys already exist in the server's capabilities, the call is a silent no-op. Genuinely new capabilities still throw the error, maintaining the safety invariant.

Test

Added 2 tests:

  1. Dynamic tool registration after connect with pre-supplied tools capability
  2. Dynamic resource registration after connect with pre-supplied resources capability

Verification

2 consecutive clean test runs: 1551/1551 tests passed, 0 failures.

Fixes #893

@changeset-bot
Copy link

changeset-bot bot commented Feb 28, 2026

🦋 Changeset detected

Latest commit: a0cefe7

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 28, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@modelcontextprotocol/sdk@1602

commit: 0b88e1f

g97iulio1609 added 2 commits February 28, 2026 15:50
…ed capabilities

When capabilities were provided at McpServer construction, registering
tools/resources/prompts after connect() threw 'Cannot register capabilities
after connecting to transport'. This occurred because setToolRequestHandlers,
setResourceRequestHandlers, and setPromptRequestHandlers unconditionally call
registerCapabilities, which rejected all post-connect calls.

Now registerCapabilities is idempotent for already-present capability keys:
if the transport is connected and all requested capability keys already exist
in the server's capabilities, the call is a silent no-op. Genuinely new
capabilities still throw the error, maintaining the safety invariant.

Fixes modelcontextprotocol#893
@giulio-leone
Copy link
Author

All CI checks pass. Ready for review.

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