Skip to content

Commit 614dbe8

Browse files
fix: response result-schema mismatch throws SdkError(InvalidResult) instead of ProtocolError(InternalError)
1 parent dc09b1d commit 614dbe8

4 files changed

Lines changed: 8 additions & 7 deletions

File tree

.changeset/custom-methods-minimal.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
Add custom (non-spec) method support: a 3-arg `setRequestHandler(method, schemas, handler)` / `setNotificationHandler(method, schemas, handler)` form for vendor-prefixed methods, and a `request(req, resultSchema)` overload (also on `ctx.mcpReq.send`) for typed custom-method results. Spec-method calls are unchanged.
88

9-
Response result-schema validation failure now rejects with `ProtocolError(InternalError)` instead of a raw `ZodError`.
9+
Response result-schema validation failure now rejects with `SdkError(InvalidResult)` instead of a raw `ZodError`. Adds `SdkErrorCode.InvalidResult`.

packages/core/src/errors/sdkErrors.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export enum SdkErrorCode {
2626
ConnectionClosed = 'CONNECTION_CLOSED',
2727
/** Failed to send message */
2828
SendFailed = 'SEND_FAILED',
29+
/** Response result failed local schema validation */
30+
InvalidResult = 'INVALID_RESULT',
2931

3032
// Transport errors
3133
ClientHttpNotImplemented = 'CLIENT_HTTP_NOT_IMPLEMENTED',

packages/core/src/shared/protocol.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -925,9 +925,7 @@ export abstract class Protocol<ContextT extends BaseContext> {
925925
if (parseResult.success) {
926926
resolve(parseResult.data);
927927
} else {
928-
reject(
929-
new ProtocolError(ProtocolErrorCode.InternalError, `Invalid result for ${request.method}: ${parseResult.error}`)
930-
);
928+
reject(new SdkError(SdkErrorCode.InvalidResult, `Invalid result for ${request.method}: ${parseResult.error}`));
931929
}
932930
}, reject);
933931
});

packages/core/test/shared/customMethods.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { z } from 'zod/v4';
33

44
import { Protocol } from '../../src/shared/protocol.js';
55
import type { BaseContext, JSONRPCRequest, Result, StandardSchemaV1 } from '../../src/exports/public/index.js';
6-
import { ProtocolError, ProtocolErrorCode } from '../../src/types/index.js';
6+
import { ProtocolError } from '../../src/types/index.js';
7+
import { SdkErrorCode } from '../../src/errors/sdkErrors.js';
78
import { InMemoryTransport } from '../../src/util/inMemory.js';
89

910
class TestProtocol extends Protocol<BaseContext> {
@@ -142,12 +143,12 @@ describe('Protocol custom-method support', () => {
142143
expect(() => a.request({ method: 'acme/unknown' } as never)).toThrow(TypeError);
143144
});
144145

145-
it('rejects with ProtocolError(InternalError) when the response fails the result schema', async () => {
146+
it('rejects with SdkError(InvalidResult) when the response fails the result schema', async () => {
146147
const [a, b] = await pair();
147148
b.setRequestHandler('acme/bad', { params: z.object({}) }, async () => ({ wrong: 123 }));
148149

149150
await expect(a.request({ method: 'acme/bad', params: {} }, z.object({ echoed: z.string() }))).rejects.toMatchObject({
150-
code: ProtocolErrorCode.InternalError
151+
code: SdkErrorCode.InvalidResult
151152
});
152153
});
153154

0 commit comments

Comments
 (0)