Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-listchanged-backport.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modelcontextprotocol/sdk': patch
---

Respected explicit `listChanged: false` capability settings in `McpServer`. Previously, registering a tool, resource, or prompt would unconditionally overwrite `listChanged` to `true`, ignoring any explicit `false` set at construction time.
5 changes: 4 additions & 1 deletion src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,10 @@ export class Server<
return this._clientVersion;
}

private getCapabilities(): ServerCapabilities {
/**
* Returns the current server capabilities.
*/
public getCapabilities(): ServerCapabilities {
return this._capabilities;
}

Expand Down
6 changes: 3 additions & 3 deletions src/server/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class McpServer {

this.server.registerCapabilities({
tools: {
listChanged: true
listChanged: this.server.getCapabilities().tools?.listChanged ?? true
}
});

Expand Down Expand Up @@ -493,7 +493,7 @@ export class McpServer {

this.server.registerCapabilities({
resources: {
listChanged: true
listChanged: this.server.getCapabilities().resources?.listChanged ?? true
}
});

Expand Down Expand Up @@ -573,7 +573,7 @@ export class McpServer {

this.server.registerCapabilities({
prompts: {
listChanged: true
listChanged: this.server.getCapabilities().prompts?.listChanged ?? true
}
});

Expand Down
90 changes: 90 additions & 0 deletions test/server/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,96 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
expect(capabilities?.extensions).toBeDefined();
expect(capabilities?.extensions?.['io.modelcontextprotocol/test-extension']).toEqual({ streaming: true });
});

/***
* Test: listChanged capability should default to true when not specified
*/
test('should default tools.listChanged to true when not explicitly set', async () => {
const mcpServer = new McpServer({
name: 'test server',
version: '1.0'
});
const client = new Client({
name: 'test client',
version: '1.0'
});

mcpServer.registerTool('test', {}, async () => ({
content: [{ type: 'text', text: 'Test' }]
}));

const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
await Promise.all([client.connect(clientTransport), mcpServer.connect(serverTransport)]);

const capabilities = client.getServerCapabilities();
expect(capabilities?.tools?.listChanged).toBe(true);
});

/***
* Test: listChanged capability should respect explicit false setting
*/
test('should respect tools.listChanged: false when explicitly set', async () => {
const mcpServer = new McpServer({ name: 'test server', version: '1.0' }, { capabilities: { tools: { listChanged: false } } });
const client = new Client({
name: 'test client',
version: '1.0'
});

mcpServer.registerTool('test', {}, async () => ({
content: [{ type: 'text', text: 'Test' }]
}));

const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
await Promise.all([client.connect(clientTransport), mcpServer.connect(serverTransport)]);

const capabilities = client.getServerCapabilities();
expect(capabilities?.tools?.listChanged).toBe(false);
});

/***
* Test: resources.listChanged should respect explicit false setting
*/
test('should respect resources.listChanged: false when explicitly set', async () => {
const mcpServer = new McpServer(
{ name: 'test server', version: '1.0' },
{ capabilities: { resources: { listChanged: false } } }
);
const client = new Client({
name: 'test client',
version: '1.0'
});

mcpServer.registerResource('test', 'test://resource', {}, async () => ({
contents: [{ uri: 'test://resource', text: 'Test', mimeType: 'text/plain' }]
}));

const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
await Promise.all([client.connect(clientTransport), mcpServer.connect(serverTransport)]);

const capabilities = client.getServerCapabilities();
expect(capabilities?.resources?.listChanged).toBe(false);
});

/***
* Test: prompts.listChanged should respect explicit false setting
*/
test('should respect prompts.listChanged: false when explicitly set', async () => {
const mcpServer = new McpServer({ name: 'test server', version: '1.0' }, { capabilities: { prompts: { listChanged: false } } });
const client = new Client({
name: 'test client',
version: '1.0'
});

mcpServer.registerPrompt('test-prompt', {}, async () => ({
messages: [{ role: 'assistant', content: { type: 'text', text: 'Test' } }]
}));

const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
await Promise.all([client.connect(clientTransport), mcpServer.connect(serverTransport)]);

const capabilities = client.getServerCapabilities();
expect(capabilities?.prompts?.listChanged).toBe(false);
});
});

describe('ResourceTemplate', () => {
Expand Down
Loading