fix: allow registering tools/resources/prompts after connect when capabilities pre-declared (#893)#1666
Open
Vadaski wants to merge 3 commits intomodelcontextprotocol:mainfrom
Open
Conversation
…abilities pre-declared (modelcontextprotocol#893) When McpServer is constructed with pre-declared capabilities (e.g. `{ capabilities: { tools: { listChanged: true } } }`), calling `registerTool()` / `registerResource()` / `registerPrompt()` after `connect()` used to throw "Cannot register capabilities after connecting to transport" because the first handler registration always called `server.registerCapabilities()` unconditionally. Fix: - Pass capabilities through `withDefaultListChangedCapabilities()` so `listChanged` defaults are applied before `connect()`. - Eagerly initialize the corresponding request handlers in the constructor when capabilities are pre-declared, so the lazy path in `set*RequestHandlers()` is already done before `connect()`. - Guard every `registerCapabilities()` call inside the four `set*RequestHandlers()` / `setCompletionRequestHandler()` methods with `!this.server.transport`, so post-connect invocations skip the now-redundant re-registration. Fixes modelcontextprotocol#893 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
…lcontextprotocol#893) { listChanged: X ?? true, ...capabilities.tools } puts the default first so the spread overwrites it — explicit undefined defeats the default. Fix to { ...capabilities.tools, listChanged: X ?? true } so the ?? true only applies when the user did not set listChanged. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
Fixes #893.
When
McpServeris constructed with pre-declared capabilities (e.g.{ capabilities: { tools: { listChanged: true } } }), callingregisterTool()/registerResource()/registerPrompt()afterconnect()used to throw:Because the first handler registration always called
server.registerCapabilities()unconditionally, which fails post-connect.Root Cause
setToolRequestHandlers(),setResourceRequestHandlers(),setPromptRequestHandlers(), andsetCompletionRequestHandler()all unconditionally calledthis.server.registerCapabilities()on first invocation — even when those capabilities were already declared at construction time.Fix
withDefaultListChangedCapabilities()helper that applieslistChangeddefaults when capabilities are pre-declared inoptions.set*RequestHandlers()in theMcpServerconstructor (beforeconnect()is ever called), so the handlers are already registered when the user later callsregister*()after connect.registerCapabilities()call insideset*RequestHandlers()andsetCompletionRequestHandler()with!this.server.transport, so post-connect invocations skip the now-redundant re-registration.Test Plan
test/integration/test/issues/test893.post-connect-registration.test.ts— 5 tests covering:capabilities.toolspre-declaredcapabilities.resourcespre-declaredcapabilities.promptspre-declaredsetCompletionRequestHandlerguard)pnpm test:all)pnpm check:allpasses (typecheck + lint)🤖 Generated with Claude Code