From a7f7f7ea989ebbe0309c9663915b3da6868f5e18 Mon Sep 17 00:00:00 2001 From: arimu1 <19286898+arimu1@users.noreply.github.com> Date: Sat, 25 Jul 2026 22:31:48 +0700 Subject: [PATCH 1/2] fix(server): warn once when sendResourceUpdated lacks resources.subscribe Resource update notifications only reach clients that opted in via resources/subscribe, which is gated on the advertised subscribe capability. Warn once on sendResourceUpdated when that bit is missing so missing capabilities are obvious during development. Closes #2545 --- packages/server/src/server/server.ts | 15 +++++++++++ packages/server/test/server/server.test.ts | 31 ++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/packages/server/src/server/server.ts b/packages/server/src/server/server.ts index dc00724587..fdb103ca35 100644 --- a/packages/server/src/server/server.ts +++ b/packages/server/src/server/server.ts @@ -298,6 +298,8 @@ export class Server extends Protocol { private _inputRequiredServing: { maxRounds: number; roundTimeoutMs: number; legacyShim: boolean }; private _legacyShim?: LegacyInputRequiredShim; private _boundFromTransport = false; + /** Emit at most one warn when sendResourceUpdated is used without resources.subscribe. */ + private _warnedResourceUpdatedWithoutSubscribe = false; /** Lazily-built legacy shim; the loop lives in legacyInputRequiredShim.ts behind a narrow host contract. */ private _legacyInputRequiredShim(): LegacyInputRequiredShim { @@ -1341,6 +1343,19 @@ export class Server extends Protocol { } async sendResourceUpdated(params: ResourceUpdatedNotification['params']) { + // Resource update notifications only reach clients that subscribed via + // resources/subscribe. That method is gated on the advertised + // `resources.subscribe` capability — without it, clients cannot opt in + // and the notification is effectively a no-op for them. Warn once so + // the missing capability is easy to spot during development (#2545). + if (!this._capabilities.resources?.subscribe && !this._warnedResourceUpdatedWithoutSubscribe) { + this._warnedResourceUpdatedWithoutSubscribe = true; + console.warn( + '[mcp-sdk] sendResourceUpdated() called without advertising capabilities.resources.subscribe. ' + + 'Clients cannot subscribe to resource updates unless the server sets ' + + '{ resources: { subscribe: true } } in its capabilities.' + ); + } return this.notification({ method: 'notifications/resources/updated', params diff --git a/packages/server/test/server/server.test.ts b/packages/server/test/server/server.test.ts index ff60a1fe75..6b2d66e052 100644 --- a/packages/server/test/server/server.test.ts +++ b/packages/server/test/server/server.test.ts @@ -268,4 +268,35 @@ describe('Server', () => { } }); }); + + describe('sendResourceUpdated capability warning', () => { + async function connectServer(server: Server): Promise { + const [, serverTransport] = InMemoryTransport.createLinkedPair(); + await server.connect(serverTransport); + } + + it('warns once when resources.subscribe is not advertised', async () => { + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + const server = new Server({ name: 'test', version: '1.0.0' }, { capabilities: { resources: { listChanged: true } } }); + await connectServer(server); + + await server.sendResourceUpdated({ uri: 'test://resource' }); + await server.sendResourceUpdated({ uri: 'test://resource-2' }); + + expect(warnSpy).toHaveBeenCalledTimes(1); + expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('resources.subscribe')); + warnSpy.mockRestore(); + }); + + it('does not warn when resources.subscribe is advertised', async () => { + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + const server = new Server({ name: 'test', version: '1.0.0' }, { capabilities: { resources: { subscribe: true } } }); + await connectServer(server); + + await server.sendResourceUpdated({ uri: 'test://resource' }); + + expect(warnSpy).not.toHaveBeenCalled(); + warnSpy.mockRestore(); + }); + }); }); From dc164123bc996b23ad009245599dae7baf3f8242 Mon Sep 17 00:00:00 2001 From: arimu1 <19286898+arimu1@users.noreply.github.com> Date: Sun, 26 Jul 2026 07:42:00 +0700 Subject: [PATCH 2/2] fix(server): throw on resource subscribe capability mismatches Align with other local capability checks: sendResourceUpdated throws SdkError(CapabilityNotSupported) via assertNotificationCapability when resources.subscribe is missing, and setRequestHandler gates resources/subscribe|unsubscribe the same way. Covers both halves of #2545 (notification send + handler registration). --- packages/server/src/server/server.ts | 44 ++++++++++++++-------- packages/server/test/server/server.test.ts | 36 +++++++++++------- 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/packages/server/src/server/server.ts b/packages/server/src/server/server.ts index fdb103ca35..cfcad101f1 100644 --- a/packages/server/src/server/server.ts +++ b/packages/server/src/server/server.ts @@ -298,8 +298,6 @@ export class Server extends Protocol { private _inputRequiredServing: { maxRounds: number; roundTimeoutMs: number; legacyShim: boolean }; private _legacyShim?: LegacyInputRequiredShim; private _boundFromTransport = false; - /** Emit at most one warn when sendResourceUpdated is used without resources.subscribe. */ - private _warnedResourceUpdatedWithoutSubscribe = false; /** Lazily-built legacy shim; the loop lives in legacyInputRequiredShim.ts behind a narrow host contract. */ private _legacyInputRequiredShim(): LegacyInputRequiredShim { @@ -834,7 +832,19 @@ export class Server extends Protocol { break; } - case 'notifications/resources/updated': + case 'notifications/resources/updated': { + // Resource updates only reach clients that opted in via + // resources/subscribe, which requires the advertised + // resources.subscribe capability (#2545). + if (!this._capabilities.resources?.subscribe) { + throw new SdkError( + SdkErrorCode.CapabilityNotSupported, + `Server does not support resource subscriptions (required for ${method})` + ); + } + break; + } + case 'notifications/resources/list_changed': { if (!this._capabilities.resources) { throw new SdkError( @@ -924,6 +934,20 @@ export class Server extends Protocol { break; } + case 'resources/subscribe': + case 'resources/unsubscribe': { + // Handler registration must match the advertised bit; otherwise + // clients that subscribe (or the server's own handlers) see a + // silent dead-end when the capability was never declared (#2545). + if (!this._capabilities.resources?.subscribe) { + throw new SdkError( + SdkErrorCode.CapabilityNotSupported, + `Server does not support resource subscriptions (required for ${method})` + ); + } + break; + } + case 'tools/call': case 'tools/list': { if (!this._capabilities.tools) { @@ -1343,19 +1367,7 @@ export class Server extends Protocol { } async sendResourceUpdated(params: ResourceUpdatedNotification['params']) { - // Resource update notifications only reach clients that subscribed via - // resources/subscribe. That method is gated on the advertised - // `resources.subscribe` capability — without it, clients cannot opt in - // and the notification is effectively a no-op for them. Warn once so - // the missing capability is easy to spot during development (#2545). - if (!this._capabilities.resources?.subscribe && !this._warnedResourceUpdatedWithoutSubscribe) { - this._warnedResourceUpdatedWithoutSubscribe = true; - console.warn( - '[mcp-sdk] sendResourceUpdated() called without advertising capabilities.resources.subscribe. ' + - 'Clients cannot subscribe to resource updates unless the server sets ' + - '{ resources: { subscribe: true } } in its capabilities.' - ); - } + // assertNotificationCapability requires resources.subscribe (#2545). return this.notification({ method: 'notifications/resources/updated', params diff --git a/packages/server/test/server/server.test.ts b/packages/server/test/server/server.test.ts index 6b2d66e052..d8516703b5 100644 --- a/packages/server/test/server/server.test.ts +++ b/packages/server/test/server/server.test.ts @@ -269,34 +269,42 @@ describe('Server', () => { }); }); - describe('sendResourceUpdated capability warning', () => { + describe('resource subscription capabilities (#2545)', () => { async function connectServer(server: Server): Promise { const [, serverTransport] = InMemoryTransport.createLinkedPair(); await server.connect(serverTransport); } - it('warns once when resources.subscribe is not advertised', async () => { - const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + it('sendResourceUpdated throws without resources.subscribe', async () => { const server = new Server({ name: 'test', version: '1.0.0' }, { capabilities: { resources: { listChanged: true } } }); await connectServer(server); - await server.sendResourceUpdated({ uri: 'test://resource' }); - await server.sendResourceUpdated({ uri: 'test://resource-2' }); - - expect(warnSpy).toHaveBeenCalledTimes(1); - expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('resources.subscribe')); - warnSpy.mockRestore(); + await expect(server.sendResourceUpdated({ uri: 'test://resource' })).rejects.toThrow(/resource subscriptions/); }); - it('does not warn when resources.subscribe is advertised', async () => { - const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + it('sendResourceUpdated succeeds when resources.subscribe is advertised', async () => { const server = new Server({ name: 'test', version: '1.0.0' }, { capabilities: { resources: { subscribe: true } } }); await connectServer(server); - await server.sendResourceUpdated({ uri: 'test://resource' }); + await expect(server.sendResourceUpdated({ uri: 'test://resource' })).resolves.toBeUndefined(); + }); + + it('setRequestHandler rejects resources/subscribe without the capability', () => { + const server = new Server({ name: 'test', version: '1.0.0' }, { capabilities: { resources: { listChanged: true } } }); + + expect(() => server.setRequestHandler('resources/subscribe', async () => ({}))).toThrow(/resource subscriptions/); + }); + + it('setRequestHandler rejects resources/unsubscribe without the capability', () => { + const server = new Server({ name: 'test', version: '1.0.0' }, { capabilities: { resources: {} } }); + + expect(() => server.setRequestHandler('resources/unsubscribe', async () => ({}))).toThrow(/resource subscriptions/); + }); + + it('setRequestHandler allows resources/subscribe when the capability is advertised', () => { + const server = new Server({ name: 'test', version: '1.0.0' }, { capabilities: { resources: { subscribe: true } } }); - expect(warnSpy).not.toHaveBeenCalled(); - warnSpy.mockRestore(); + expect(() => server.setRequestHandler('resources/subscribe', async () => ({}))).not.toThrow(); }); }); });