Skip to content

Commit b5880ec

Browse files
committed
Support custom server request, notification, and result types
1 parent 7cc7e81 commit b5880ec

File tree

2 files changed

+98
-4
lines changed

2 files changed

+98
-4
lines changed

src/server/index.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { Server } from "./index.js";
2+
import { z } from "zod";
3+
import { RequestSchema, NotificationSchema, ResultSchema } from "../types.js";
4+
5+
/*
6+
Test that custom request/notification/result schemas can be used with the Server class.
7+
*/
8+
const GetWeatherRequestSchema = RequestSchema.extend({
9+
method: z.literal("weather/get"),
10+
params: z.object({
11+
city: z.string(),
12+
}),
13+
});
14+
15+
const GetForecastRequestSchema = RequestSchema.extend({
16+
method: z.literal("weather/forecast"),
17+
params: z.object({
18+
city: z.string(),
19+
days: z.number(),
20+
}),
21+
});
22+
23+
const WeatherForecastNotificationSchema = NotificationSchema.extend({
24+
method: z.literal("weather/alert"),
25+
params: z.object({
26+
severity: z.enum(["warning", "watch"]),
27+
message: z.string(),
28+
}),
29+
});
30+
31+
const WeatherRequestSchema = GetWeatherRequestSchema.or(
32+
GetForecastRequestSchema,
33+
);
34+
const WeatherNotificationSchema = WeatherForecastNotificationSchema;
35+
const WeatherResultSchema = ResultSchema.extend({
36+
temperature: z.number(),
37+
conditions: z.string(),
38+
});
39+
40+
type WeatherRequest = z.infer<typeof WeatherRequestSchema>;
41+
type WeatherNotification = z.infer<typeof WeatherNotificationSchema>;
42+
type WeatherResult = z.infer<typeof WeatherResultSchema>;
43+
44+
// Create a typed Server for weather data
45+
const weatherServer = new Server<
46+
WeatherRequest,
47+
WeatherNotification,
48+
WeatherResult
49+
>({
50+
name: "WeatherServer",
51+
version: "1.0.0",
52+
});
53+
54+
// Typecheck that only valid weather requests/notifications/results are allowed
55+
weatherServer.setRequestHandler(GetWeatherRequestSchema, (request) => {
56+
return {
57+
temperature: 72,
58+
conditions: "sunny",
59+
};
60+
});
61+
62+
weatherServer.setNotificationHandler(
63+
WeatherForecastNotificationSchema,
64+
(notification) => {
65+
console.log(`Weather alert: ${notification.params.message}`);
66+
},
67+
);

src/server/index.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import {
66
InitializeRequest,
77
InitializeRequestSchema,
88
InitializeResult,
9+
Notification,
910
PROTOCOL_VERSION,
11+
Request,
12+
Result,
1013
ServerNotification,
1114
ServerRequest,
1215
ServerResult,
@@ -21,11 +24,35 @@ import {
2124
* An MCP server on top of a pluggable transport.
2225
*
2326
* This server will automatically respond to the initialization flow as initiated from the client.
27+
*
28+
* To use with custom types, extend the base Request/Notification/Result types and pass them as type parameters:
29+
*
30+
* ```typescript
31+
* // Custom schemas
32+
* const CustomRequestSchema = RequestSchema.extend({...})
33+
* const CustomNotificationSchema = NotificationSchema.extend({...})
34+
* const CustomResultSchema = ResultSchema.extend({...})
35+
*
36+
* // Type aliases
37+
* type CustomRequest = z.infer<typeof CustomRequestSchema>
38+
* type CustomNotification = z.infer<typeof CustomNotificationSchema>
39+
* type CustomResult = z.infer<typeof CustomResultSchema>
40+
*
41+
* // Create typed server
42+
* const server = new Server<CustomRequest, CustomNotification, CustomResult>({
43+
* name: "CustomServer",
44+
* version: "1.0.0"
45+
* })
46+
* ```
2447
*/
25-
export class Server extends Protocol<
26-
ServerRequest,
27-
ServerNotification,
28-
ServerResult
48+
export class Server<
49+
RequestT extends Request = Request,
50+
NotificationT extends Notification = Notification,
51+
ResultT extends Result = Result,
52+
> extends Protocol<
53+
ServerRequest | RequestT,
54+
ServerNotification | NotificationT,
55+
ServerResult | ResultT
2956
> {
3057
private _clientCapabilities?: ClientCapabilities;
3158
private _clientVersion?: Implementation;

0 commit comments

Comments
 (0)