Skip to content

Commit 52843d3

Browse files
feat(core): mcpReq.send accepts (req, resultSchema, opts) for non-spec methods
Mirrors Protocol.request()'s schema overload so handlers can send custom-method requests via ctx.mcpReq.send. Also corrects the _registerCompatRequestHandler JSDoc (called by Protocol's own overload dispatch, not Client/Server overrides).
1 parent dd2dc3c commit 52843d3

1 file changed

Lines changed: 23 additions & 10 deletions

File tree

packages/core/src/shared/protocol.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,21 @@ export type BaseContext = {
201201
* Sends a request that relates to the current request being handled.
202202
*
203203
* This is used by certain transports to correctly associate related messages.
204+
*
205+
* Two call forms (mirrors {@linkcode Protocol.request | request()}):
206+
* - **Spec method** — `send({ method: 'sampling/createMessage', params }, options?)`.
207+
* The result schema is resolved from the method name and the return is typed by it.
208+
* - **With explicit result schema** — `send({ method, params }, resultSchema, options?)`
209+
* for non-spec methods or custom result shapes.
204210
*/
205-
send: <M extends RequestMethod>(
206-
request: { method: M; params?: Record<string, unknown> },
207-
options?: TaskRequestOptions
208-
) => Promise<ResultTypeMap[M]>;
211+
send: {
212+
<M extends RequestMethod>(
213+
request: { method: M; params?: Record<string, unknown> },
214+
options?: TaskRequestOptions
215+
): Promise<ResultTypeMap[M]>;
216+
/** @deprecated For spec methods, the result schema is resolved automatically; use `send(req)`. */
217+
<T extends AnySchema>(request: Request, resultSchema: T, options?: TaskRequestOptions): Promise<SchemaOutput<T>>;
218+
};
209219

210220
/**
211221
* Sends a notification that relates to the current request being handled.
@@ -600,10 +610,13 @@ export abstract class Protocol<ContextT extends BaseContext> {
600610
method: request.method,
601611
_meta: request.params?._meta,
602612
signal: abortController.signal,
603-
send: <M extends RequestMethod>(r: { method: M; params?: Record<string, unknown> }, options?: TaskRequestOptions) => {
604-
const resultSchema = getResultSchema(r.method);
605-
return sendRequest(r as Request, resultSchema, options) as Promise<ResultTypeMap[M]>;
606-
},
613+
send: ((r: Request, optionsOrSchema?: TaskRequestOptions | AnySchema, maybeOptions?: TaskRequestOptions) => {
614+
if (optionsOrSchema && '~standard' in optionsOrSchema) {
615+
return sendRequest(r, optionsOrSchema, maybeOptions);
616+
}
617+
const resultSchema = getResultSchema(r.method as RequestMethod);
618+
return sendRequest(r, resultSchema, optionsOrSchema);
619+
}) as BaseContext['mcpReq']['send'],
607620
notify: sendNotification
608621
},
609622
http: extra?.authInfo ? { authInfo: extra.authInfo } : undefined,
@@ -1044,8 +1057,8 @@ export abstract class Protocol<ContextT extends BaseContext> {
10441057

10451058
/**
10461059
* Dispatches the Zod-schema form of `setRequestHandler` — extracts the method literal from the
1047-
* schema and registers a handler that parses the full request through it. Called by
1048-
* `Client`/`Server` overrides to avoid forwarding through their own overload set.
1060+
* schema and registers a handler that parses the full request through it. Called by the base
1061+
* {@linkcode Protocol.setRequestHandler} overload dispatcher for the schema-first signature.
10491062
*/
10501063
protected _registerCompatRequestHandler(
10511064
requestSchema: ZodLikeRequestSchema,

0 commit comments

Comments
 (0)