Skip to content

Commit 0b24200

Browse files
fix(converge): nits r0-003/004/007/008 — error-code semantics, codemod ref, negative-result test
1 parent 45f5620 commit 0b24200

3 files changed

Lines changed: 12 additions & 5 deletions

File tree

docs/migration.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,6 @@ server.setRequestHandler('acme/search', { params: SearchParams, result: SearchRe
393393

394394
The handler receives the parsed `params` directly (not the full request envelope). `_meta` is stripped before validation and is available as `ctx.mcpReq._meta`. Supplying `result` types the handler's return value; omit it to return any `Result`.
395395

396-
The codemod handles this rewrite for both spec and custom methods.
397-
398396
#### Sending custom-method requests
399397

400398
`request()` and `ctx.mcpReq.send()` now accept an optional result schema for custom methods:

packages/core/src/shared/protocol.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ export abstract class Protocol<ContextT extends BaseContext> {
916916
resolve(parseResult.data);
917917
} else {
918918
reject(
919-
new ProtocolError(ProtocolErrorCode.InvalidParams, `Invalid result for ${request.method}: ${parseResult.error}`)
919+
new ProtocolError(ProtocolErrorCode.InternalError, `Invalid result for ${request.method}: ${parseResult.error}`)
920920
);
921921
}
922922
}, reject);
@@ -1162,7 +1162,7 @@ export abstract class Protocol<ContextT extends BaseContext> {
11621162
delete userParams._meta;
11631163
const parsed = await validateStandardSchema(schemas.params, userParams);
11641164
if (!parsed.success) {
1165-
throw new Error(`Invalid params for notification ${method}: ${parsed.error}`);
1165+
throw new ProtocolError(ProtocolErrorCode.InvalidParams, `Invalid params for notification ${method}: ${parsed.error}`);
11661166
}
11671167
await handler(parsed.data);
11681168
});

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

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

44
import { Protocol } from '../../src/shared/protocol.js';
55
import type { BaseContext, JSONRPCRequest, Result } from '../../src/exports/public/index.js';
6-
import { ProtocolError } from '../../src/types/index.js';
6+
import { ProtocolError, ProtocolErrorCode } from '../../src/types/index.js';
77
import { InMemoryTransport } from '../../src/util/inMemory.js';
88

99
class TestProtocol extends Protocol<BaseContext> {
@@ -127,6 +127,15 @@ describe('Protocol custom-method support', () => {
127127
const [a] = await pair();
128128
expect(() => a.request({ method: 'acme/unknown' } as never)).toThrow(TypeError);
129129
});
130+
131+
it('rejects with ProtocolError(InternalError) when the response fails the result schema', async () => {
132+
const [a, b] = await pair();
133+
b.setRequestHandler('acme/bad', { params: z.object({}) }, async () => ({ wrong: 123 }));
134+
135+
await expect(a.request({ method: 'acme/bad', params: {} }, z.object({ echoed: z.string() }))).rejects.toMatchObject({
136+
code: ProtocolErrorCode.InternalError
137+
});
138+
});
130139
});
131140

132141
describe('ctx.mcpReq.send schema overload', () => {

0 commit comments

Comments
 (0)