-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from modelcontextprotocol/justin/custom-requests
Fix typing for custom requests, notification, and result types
- Loading branch information
Showing
4 changed files
with
212 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
/* eslint-disable no-constant-binary-expression */ | ||
/* eslint-disable @typescript-eslint/no-unused-expressions */ | ||
import { Client } from "./index.js"; | ||
import { z } from "zod"; | ||
import { RequestSchema, NotificationSchema, ResultSchema } from "../types.js"; | ||
|
||
/* | ||
Test that custom request/notification/result schemas can be used with the Client class. | ||
*/ | ||
test("should typecheck", () => { | ||
const GetWeatherRequestSchema = RequestSchema.extend({ | ||
method: z.literal("weather/get"), | ||
params: z.object({ | ||
city: z.string(), | ||
}), | ||
}); | ||
|
||
const GetForecastRequestSchema = RequestSchema.extend({ | ||
method: z.literal("weather/forecast"), | ||
params: z.object({ | ||
city: z.string(), | ||
days: z.number(), | ||
}), | ||
}); | ||
|
||
const WeatherForecastNotificationSchema = NotificationSchema.extend({ | ||
method: z.literal("weather/alert"), | ||
params: z.object({ | ||
severity: z.enum(["warning", "watch"]), | ||
message: z.string(), | ||
}), | ||
}); | ||
|
||
const WeatherRequestSchema = GetWeatherRequestSchema.or( | ||
GetForecastRequestSchema, | ||
); | ||
const WeatherNotificationSchema = WeatherForecastNotificationSchema; | ||
const WeatherResultSchema = ResultSchema.extend({ | ||
temperature: z.number(), | ||
conditions: z.string(), | ||
}); | ||
|
||
type WeatherRequest = z.infer<typeof WeatherRequestSchema>; | ||
type WeatherNotification = z.infer<typeof WeatherNotificationSchema>; | ||
type WeatherResult = z.infer<typeof WeatherResultSchema>; | ||
|
||
// Create a typed Client for weather data | ||
const weatherClient = new Client< | ||
WeatherRequest, | ||
WeatherNotification, | ||
WeatherResult | ||
>({ | ||
name: "WeatherClient", | ||
version: "1.0.0", | ||
}); | ||
|
||
// Typecheck that only valid weather requests/notifications/results are allowed | ||
false && | ||
weatherClient.request( | ||
{ | ||
method: "weather/get", | ||
params: { | ||
city: "Seattle", | ||
}, | ||
}, | ||
WeatherResultSchema, | ||
); | ||
|
||
false && | ||
weatherClient.notification({ | ||
method: "weather/alert", | ||
params: { | ||
severity: "warning", | ||
message: "Storm approaching", | ||
}, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
/* eslint-disable no-constant-binary-expression */ | ||
/* eslint-disable @typescript-eslint/no-unused-expressions */ | ||
import { Server } from "./index.js"; | ||
import { z } from "zod"; | ||
import { RequestSchema, NotificationSchema, ResultSchema } from "../types.js"; | ||
|
||
/* | ||
Test that custom request/notification/result schemas can be used with the Server class. | ||
*/ | ||
test("should typecheck", () => { | ||
const GetWeatherRequestSchema = RequestSchema.extend({ | ||
method: z.literal("weather/get"), | ||
params: z.object({ | ||
city: z.string(), | ||
}), | ||
}); | ||
|
||
const GetForecastRequestSchema = RequestSchema.extend({ | ||
method: z.literal("weather/forecast"), | ||
params: z.object({ | ||
city: z.string(), | ||
days: z.number(), | ||
}), | ||
}); | ||
|
||
const WeatherForecastNotificationSchema = NotificationSchema.extend({ | ||
method: z.literal("weather/alert"), | ||
params: z.object({ | ||
severity: z.enum(["warning", "watch"]), | ||
message: z.string(), | ||
}), | ||
}); | ||
|
||
const WeatherRequestSchema = GetWeatherRequestSchema.or( | ||
GetForecastRequestSchema, | ||
); | ||
const WeatherNotificationSchema = WeatherForecastNotificationSchema; | ||
const WeatherResultSchema = ResultSchema.extend({ | ||
temperature: z.number(), | ||
conditions: z.string(), | ||
}); | ||
|
||
type WeatherRequest = z.infer<typeof WeatherRequestSchema>; | ||
type WeatherNotification = z.infer<typeof WeatherNotificationSchema>; | ||
type WeatherResult = z.infer<typeof WeatherResultSchema>; | ||
|
||
// Create a typed Server for weather data | ||
const weatherServer = new Server< | ||
WeatherRequest, | ||
WeatherNotification, | ||
WeatherResult | ||
>({ | ||
name: "WeatherServer", | ||
version: "1.0.0", | ||
}); | ||
|
||
// Typecheck that only valid weather requests/notifications/results are allowed | ||
weatherServer.setRequestHandler(GetWeatherRequestSchema, (request) => { | ||
return { | ||
temperature: 72, | ||
conditions: "sunny", | ||
}; | ||
}); | ||
|
||
weatherServer.setNotificationHandler( | ||
WeatherForecastNotificationSchema, | ||
(notification) => { | ||
console.log(`Weather alert: ${notification.params.message}`); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters