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/preserve-tool-list-changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modelcontextprotocol/sdk': patch
---

Preserve an explicitly configured `tools.listChanged` capability when registering tools with `McpServer`. When it is `false`, `McpServer` no longer sends `notifications/tools/list_changed`.
5 changes: 4 additions & 1 deletion src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,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 @@ -189,7 +189,7 @@ export class McpServer {

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

Expand Down Expand Up @@ -1261,10 +1261,10 @@ export class McpServer {
}

/**
* Sends a tool list changed event to the client, if connected.
* Sends a tool list changed event to the client, if connected and `tools.listChanged` is not `false`.
*/
sendToolListChanged() {
if (this.isConnected()) {
if (this.isConnected() && this.server.getCapabilities().tools?.listChanged !== false) {
this.server.sendToolListChanged();
}
}
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 @@ -56,6 +56,96 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
expect(mcpServer.server).toBeDefined();
});

test('should preserve an explicitly disabled tool listChanged capability', 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 response' }]
}));

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

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

test('should enable the tool listChanged capability by default', 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 response' }]
}));

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

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

test('should suppress sendToolListChanged notifications when tools.listChanged is explicitly false', async () => {
const mcpServer = new McpServer(
{
name: 'test server',
version: '1.0'
},
{
capabilities: {
tools: { listChanged: false }
}
}
);
const notifications: Notification[] = [];
const client = new Client({
name: 'test client',
version: '1.0'
});
client.fallbackNotificationHandler = async notification => {
notifications.push(notification);
};

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

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

expect(client.getServerCapabilities()?.tools?.listChanged).toBe(false);

mcpServer.sendToolListChanged();
mcpServer.registerTool('test2', {}, async () => ({
content: [{ type: 'text', text: 'Test response' }]
}));
tool.update({ description: 'Updated' });
tool.remove();

// Yield event loop to let any notification fly
await new Promise(process.nextTick);

expect(notifications).toHaveLength(0);
});

/***
* Test: Notification Sending via Server
*/
Expand Down
Loading