Skip to content

Commit 4be31d0

Browse files
committed
Remove explicit constants of method names, factor out ProgressSchema
1 parent 0e2b156 commit 4be31d0

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

src/shared/protocol.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import {
88
McpError,
99
METHOD_NOT_FOUND,
1010
Notification,
11-
PING_REQUEST_METHOD,
11+
PingRequestSchema,
1212
Progress,
13-
PROGRESS_NOTIFICATION_METHOD,
1413
ProgressNotification,
14+
ProgressNotificationSchema,
1515
Request,
1616
Result,
1717
} from "../types.js";
@@ -78,14 +78,14 @@ export class Protocol<
7878

7979
constructor() {
8080
this.setNotificationHandler(
81-
PROGRESS_NOTIFICATION_METHOD,
81+
ProgressNotificationSchema.shape.method.value,
8282
(notification) => {
8383
this._onprogress(notification as unknown as ProgressNotification);
8484
},
8585
);
8686

8787
this.setRequestHandler(
88-
PING_REQUEST_METHOD,
88+
PingRequestSchema.shape.method.value,
8989
// Automatic pong by default.
9090
(_request) => ({}) as SendResultT,
9191
);

src/types.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -263,24 +263,27 @@ export const PingRequestSchema = RequestSchema.extend({
263263
});
264264

265265
/* Progress notifications */
266+
export const ProgressSchema = z.object({
267+
/**
268+
* The progress thus far. This should increase every time progress is made, even if the total is unknown.
269+
*/
270+
progress: z.number(),
271+
/**
272+
* Total number of items to process (or total progress required), if known.
273+
*/
274+
total: z.optional(z.number()),
275+
});
276+
266277
/**
267278
* An out-of-band notification used to inform the receiver of a progress update for a long-running request.
268279
*/
269280
export const ProgressNotificationSchema = NotificationSchema.extend({
270281
method: z.literal("notifications/progress"),
271-
params: z.object({
282+
params: ProgressSchema.extend({
272283
/**
273284
* The progress token which was given in the initial request, used to associate this notification with the request that is proceeding.
274285
*/
275286
progressToken: ProgressTokenSchema,
276-
/**
277-
* The progress thus far. This should increase every time progress is made, even if the total is unknown.
278-
*/
279-
progress: z.number(),
280-
/**
281-
* Total number of items to process (or total progress required), if known.
282-
*/
283-
total: z.optional(z.number()),
284287
}),
285288
});
286289

@@ -810,14 +813,3 @@ export class McpError extends Error {
810813
super(`MCP error ${code}: ${message}`);
811814
}
812815
}
813-
814-
export type Progress = Pick<
815-
z.infer<typeof ProgressNotificationSchema>["params"],
816-
"progress" | "total"
817-
>;
818-
819-
export const PROGRESS_NOTIFICATION_METHOD: z.infer<
820-
typeof ProgressNotificationSchema
821-
>["method"] = "notifications/progress";
822-
export const PING_REQUEST_METHOD: z.infer<typeof PingRequestSchema>["method"] =
823-
"ping";

0 commit comments

Comments
 (0)