-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix: allow registering tools/resources/prompts after connect when capabilities pre-declared (#893) #1666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Vadaski
wants to merge
4
commits into
modelcontextprotocol:main
Choose a base branch
from
Vadaski:fix/893-post-connect-registration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix: allow registering tools/resources/prompts after connect when capabilities pre-declared (#893) #1666
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2a3ba79
fix: allow registering tools/resources/prompts after connect when cap…
Vadaski d53b63b
ci: retrigger CI (infra flake on pnpm/action-setup@v4)
Vadaski b6fa68f
fix: correct spread order in withDefaultListChangedCapabilities (#893)
Vadaski 0257fe3
Merge branch 'main' into fix/893-post-connect-registration
felixweinberger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
160 changes: 160 additions & 0 deletions
160
test/integration/test/issues/test893.post-connect-registration.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import { Client } from '@modelcontextprotocol/client'; | ||
| import { InMemoryTransport } from '@modelcontextprotocol/core'; | ||
| import { completable, McpServer, ResourceTemplate } from '@modelcontextprotocol/server'; | ||
| import * as z from 'zod/v4'; | ||
|
|
||
| describe('Issue #893: post-connect registration with pre-declared capabilities', () => { | ||
| test('registers a tool after connect when capabilities.tools is pre-declared', async () => { | ||
| const server = new McpServer({ name: 'test-server', version: '1.0' }, { capabilities: { tools: { listChanged: true } } }); | ||
| const client = new Client({ name: 'test-client', version: '1.0' }); | ||
| const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); | ||
|
|
||
| await Promise.all([client.connect(clientTransport), server.connect(serverTransport)]); | ||
|
|
||
| expect(() => | ||
| server.registerTool('echo', {}, async () => ({ | ||
| content: [{ type: 'text', text: 'tool registered after connect' }] | ||
| })) | ||
| ).not.toThrow(); | ||
|
|
||
| const result = await client.callTool({ name: 'echo' }); | ||
| expect(result.content).toEqual([{ type: 'text', text: 'tool registered after connect' }]); | ||
| }); | ||
|
|
||
| test('registers a resource after connect when capabilities.resources is pre-declared', async () => { | ||
| const server = new McpServer({ name: 'test-server', version: '1.0' }, { capabilities: { resources: { listChanged: true } } }); | ||
| const client = new Client({ name: 'test-client', version: '1.0' }); | ||
| const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); | ||
|
|
||
| await Promise.all([client.connect(clientTransport), server.connect(serverTransport)]); | ||
|
|
||
| expect(() => | ||
| server.registerResource('settings', 'test://settings', {}, async () => ({ | ||
| contents: [{ uri: 'test://settings', text: 'resource registered after connect' }] | ||
| })) | ||
| ).not.toThrow(); | ||
|
|
||
| const result = await client.readResource({ uri: 'test://settings' }); | ||
| expect(result.contents).toEqual([{ uri: 'test://settings', text: 'resource registered after connect' }]); | ||
| }); | ||
|
|
||
| test('registers a prompt after connect when capabilities.prompts is pre-declared', async () => { | ||
| const server = new McpServer({ name: 'test-server', version: '1.0' }, { capabilities: { prompts: { listChanged: true } } }); | ||
| const client = new Client({ name: 'test-client', version: '1.0' }); | ||
| const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); | ||
|
|
||
| await Promise.all([client.connect(clientTransport), server.connect(serverTransport)]); | ||
|
|
||
| expect(() => | ||
| server.registerPrompt('review', {}, async () => ({ | ||
| messages: [ | ||
| { | ||
| role: 'assistant', | ||
| content: { type: 'text', text: 'prompt registered after connect' } | ||
| } | ||
| ] | ||
| })) | ||
| ).not.toThrow(); | ||
|
|
||
| const result = await client.request({ | ||
| method: 'prompts/get', | ||
| params: { name: 'review' } | ||
| }); | ||
| expect(result.messages).toEqual([ | ||
| { | ||
| role: 'assistant', | ||
| content: { type: 'text', text: 'prompt registered after connect' } | ||
| } | ||
| ]); | ||
| }); | ||
|
|
||
| test('registers a completable prompt after connect when capabilities.prompts is pre-declared', async () => { | ||
| const server = new McpServer( | ||
| { name: 'test-server', version: '1.0' }, | ||
| { capabilities: { prompts: { listChanged: true }, completions: {} } } | ||
| ); | ||
| const client = new Client({ name: 'test-client', version: '1.0' }); | ||
| const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); | ||
|
|
||
| await Promise.all([client.connect(clientTransport), server.connect(serverTransport)]); | ||
|
|
||
| expect(() => | ||
| server.registerPrompt( | ||
| 'review-with-completion', | ||
| { | ||
| argsSchema: z.object({ | ||
| tone: completable(z.string(), () => ['direct', 'formal']) | ||
| }) | ||
| }, | ||
| async ({ tone }) => ({ | ||
| messages: [ | ||
| { | ||
| role: 'assistant', | ||
| content: { type: 'text', text: `tone=${tone}` } | ||
| } | ||
| ] | ||
| }) | ||
| ) | ||
| ).not.toThrow(); | ||
|
|
||
| const result = await client.request({ | ||
| method: 'completion/complete', | ||
| params: { | ||
| ref: { | ||
| type: 'ref/prompt', | ||
| name: 'review-with-completion' | ||
| }, | ||
| argument: { | ||
| name: 'tone', | ||
| value: '' | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| expect(result.completion.values).toEqual(['direct', 'formal']); | ||
| expect(result.completion.total).toBe(2); | ||
| }); | ||
|
|
||
| test('registers a completable resource template after connect when capabilities.resources is pre-declared', async () => { | ||
| const server = new McpServer( | ||
| { name: 'test-server', version: '1.0' }, | ||
| { capabilities: { resources: { listChanged: true }, completions: {} } } | ||
| ); | ||
| const client = new Client({ name: 'test-client', version: '1.0' }); | ||
| const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); | ||
|
|
||
| await Promise.all([client.connect(clientTransport), server.connect(serverTransport)]); | ||
|
|
||
| expect(() => | ||
| server.registerResource( | ||
| 'repo', | ||
| new ResourceTemplate('test://repos/{name}', { | ||
| complete: { | ||
| name: () => ['alpha', 'beta'] | ||
| } | ||
| }), | ||
| {}, | ||
| async () => ({ | ||
| contents: [{ uri: 'test://repos/alpha', text: 'resource registered after connect' }] | ||
| }) | ||
| ) | ||
| ).not.toThrow(); | ||
|
|
||
| const result = await client.request({ | ||
| method: 'completion/complete', | ||
| params: { | ||
| ref: { | ||
| type: 'ref/resource', | ||
| uri: 'test://repos/{name}' | ||
| }, | ||
| argument: { | ||
| name: 'name', | ||
| value: '' | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| expect(result.completion.values).toEqual(['alpha', 'beta']); | ||
| expect(result.completion.total).toBe(2); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 When a user pre-declares
promptsorresourcescapability but omitscompletions, registering a completable prompt or resource template afterconnect()throwsSdkError: "Server does not support completions"atregisterPrompt/registerResourcecall time. The PR tests 4 and 5 work around this by explicitly co-declaringcompletions: {}alongside the primary capability, but this undocumented requirement means the natural pattern (pre-declare onlyprompts, then register completable prompts post-connect) fails with a misleading error rather than clear guidance to also declarecompletions.Extended reasoning...
Mechanism (addressing the refutation)
The refuter correctly identifies that the failure is not silent. Here is the exact trace when
setCompletionRequestHandler()is called post-connect withcompletionsabsent from pre-declared capabilities:assertCanSetRequestHandler('completion/complete')(protocol.ts:1031) only checks if a handler already exists. Passes, since none was registered.if (!this.server.transport)evaluatesfalsepost-connect, soregisterCapabilities({ completions: {} })is skipped (mcp.ts:361-365).this.server.setRequestHandler('completion/complete', ...)(protocol.ts:1008) immediately callsassertRequestHandlerCapability('completion/complete')(protocol.ts:1012), which at server.ts:372 checksthis._capabilities.completions. It is not set so it throwsSdkError: "Server does not support completions (required for completion/complete)".The exception propagates up through
_createRegisteredPromptto theregisterPromptcaller. The handler is never registered; the client never gets to callcomplete(). The "silent failure" framing in the synthesis is inaccurate.The real bug: incomplete fix with confusing error
The PR's stated goal is to allow post-connect registration when capabilities are pre-declared. For completable items this requires two pre-declared capabilities: the primary one (
prompts/resources) ANDcompletions. The constructor eagerly initialises other handlers when their capabilities are present, but omitssetCompletionRequestHandler():Tests 4 and 5 work because they explicitly co-declare
completions: {}, which satisfies theassertRequestHandlerCapabilitycheck at step 3. Users following tests 1-3 as a template who addcompletable()fields will hit the confusing error.Step-by-step proof
Error message regression
Before this PR, the same scenario threw
"Cannot register capabilities after connecting to transport"-- directly actionable. After the PR, a user who pre-declares onlypromptsgets"Server does not support completions", which falsely implies completions are fundamentally unsupported rather than simply not pre-declared alongsideprompts.Recommended fix
Add
if (capabilities.completions) { this.setCompletionRequestHandler(); }to the constructor after the existing capability checks (mcp.ts:84-87), matching the established pattern for tools/resources/prompts.