Skip to content

Commit 45f5620

Browse files
fix(converge): restore setRequestHandler @example after override removal
1 parent ff1322c commit 45f5620

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

packages/client/src/client/client.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,23 @@ export type ClientOptions = ProtocolOptions & {
199199
*
200200
* The client will automatically begin the initialization flow with the server when {@linkcode connect} is called.
201201
*
202+
* @example Handling a sampling request
203+
* ```ts source="./client.examples.ts#Client_setRequestHandler_sampling"
204+
* client.setRequestHandler('sampling/createMessage', async request => {
205+
* const lastMessage = request.params.messages.at(-1);
206+
* console.log('Sampling request:', lastMessage);
207+
*
208+
* // In production, send messages to your LLM here
209+
* return {
210+
* model: 'my-model',
211+
* role: 'assistant' as const,
212+
* content: {
213+
* type: 'text' as const,
214+
* text: 'Response from the model'
215+
* }
216+
* };
217+
* });
218+
* ```
202219
*/
203220
export class Client extends Protocol<ClientContext> {
204221
private _serverCapabilities?: ServerCapabilities;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Type-checked examples for `protocol.ts`.
3+
*
4+
* These examples are synced into JSDoc comments via the sync-snippets script.
5+
* Each function's region markers define the code snippet that appears in the docs.
6+
*
7+
* @module
8+
*/
9+
10+
import { z } from 'zod';
11+
12+
import type { BaseContext, Protocol } from './protocol.js';
13+
14+
/**
15+
* Example: registering a handler for a custom (non-spec) request method.
16+
*/
17+
function Protocol_setRequestHandler_customMethod(protocol: Protocol<BaseContext>) {
18+
//#region Protocol_setRequestHandler_customMethod
19+
const SearchParams = z.object({ query: z.string(), limit: z.number().optional() });
20+
const SearchResult = z.object({ hits: z.array(z.string()) });
21+
22+
protocol.setRequestHandler('acme/search', { params: SearchParams, result: SearchResult }, async (params, _ctx) => {
23+
return { hits: [`result for ${params.query}`] };
24+
});
25+
//#endregion Protocol_setRequestHandler_customMethod
26+
void protocol;
27+
}
28+
29+
void Protocol_setRequestHandler_customMethod;

packages/core/src/shared/protocol.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,16 @@ export abstract class Protocol<ContextT extends BaseContext> {
10461046
* methods, pass `(method, schemas, handler)`; `params` are validated against
10471047
* `schemas.params` and the handler receives the parsed params object directly.
10481048
* Supplying `schemas.result` types the handler's return value.
1049+
*
1050+
* @example Custom request method
1051+
* ```ts source="./protocol.examples.ts#Protocol_setRequestHandler_customMethod"
1052+
* const SearchParams = z.object({ query: z.string(), limit: z.number().optional() });
1053+
* const SearchResult = z.object({ hits: z.array(z.string()) });
1054+
*
1055+
* protocol.setRequestHandler('acme/search', { params: SearchParams, result: SearchResult }, async (params, _ctx) => {
1056+
* return { hits: [`result for ${params.query}`] };
1057+
* });
1058+
* ```
10491059
*/
10501060
setRequestHandler<M extends RequestMethod>(
10511061
method: M,

0 commit comments

Comments
 (0)