diff --git a/.changeset/fix-capability-error.md b/.changeset/fix-capability-error.md new file mode 100644 index 000000000..48db14a66 --- /dev/null +++ b/.changeset/fix-capability-error.md @@ -0,0 +1,5 @@ +--- +"@modelcontextprotocol/sdk": patch +--- + +fix: throw SdkError for unsupported capabilities in assertCapability() diff --git a/packages/client/src/client/client.ts b/packages/client/src/client/client.ts index edb08ee58..df9e703b1 100644 --- a/packages/client/src/client/client.ts +++ b/packages/client/src/client/client.ts @@ -434,7 +434,7 @@ export class Client extends Protocol { protected assertCapability(capability: keyof ServerCapabilities, method: string): void { if (!this._serverCapabilities?.[capability]) { - throw new Error(`Server does not support ${capability} (required for ${method})`); + throw new SdkError(SdkErrorCode.CapabilityNotSupported, `Server does not support ${capability} (required for ${method})`); } } diff --git a/test/integration/test/client/client.test.ts b/test/integration/test/client/client.test.ts index 948d16e17..cfc4d180b 100644 --- a/test/integration/test/client/client.test.ts +++ b/test/integration/test/client/client.test.ts @@ -418,6 +418,12 @@ test('should respect server capabilities', async () => { argument: { name: 'test', value: 'test' } }) ).rejects.toThrow('Server does not support completions'); + + // Verify that unsupported capability errors are SdkError with CapabilityNotSupported code + await expect(client.listPrompts()).rejects.toThrow(SdkError); + await expect(client.listPrompts()).rejects.toMatchObject({ + code: SdkErrorCode.CapabilityNotSupported + }); }); /***