11import { Server } from "@modelcontextprotocol/sdk/server/index.js" ;
2+ import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js" ;
23import {
34 CallToolRequestSchema ,
45 ClientCapabilities ,
56 CompleteRequestSchema ,
67 CreateMessageRequest ,
78 CreateMessageResultSchema ,
9+ ElicitRequest ,
10+ ElicitResultSchema ,
811 GetPromptRequestSchema ,
912 ListPromptsRequestSchema ,
1013 ListResourcesRequestSchema ,
@@ -14,6 +17,8 @@ import {
1417 ReadResourceRequestSchema ,
1518 Resource ,
1619 RootsListChangedNotificationSchema ,
20+ ServerNotification ,
21+ ServerRequest ,
1722 SubscribeRequestSchema ,
1823 Tool ,
1924 ToolSchema ,
@@ -36,6 +41,8 @@ type ToolInput = z.infer<typeof ToolInputSchema>;
3641const ToolOutputSchema = ToolSchema . shape . outputSchema ;
3742type ToolOutput = z . infer < typeof ToolOutputSchema > ;
3843
44+ type SendRequest = RequestHandlerExtra < ServerRequest , ServerNotification > [ "sendRequest" ] ;
45+
3946/* Input schemas for tools implemented in this server */
4047const EchoSchema = z . object ( {
4148 message : z . string ( ) . describe ( "Message to echo" ) ,
@@ -220,7 +227,8 @@ export const createServer = () => {
220227 const requestSampling = async (
221228 context : string ,
222229 uri : string ,
223- maxTokens : number = 100
230+ maxTokens : number = 100 ,
231+ sendRequest : SendRequest
224232 ) => {
225233 const request : CreateMessageRequest = {
226234 method : "sampling/createMessage" ,
@@ -241,22 +249,24 @@ export const createServer = () => {
241249 } ,
242250 } ;
243251
244- return await server . request ( request , CreateMessageResultSchema ) ;
252+ return await sendRequest ( request , CreateMessageResultSchema ) ;
253+
245254 } ;
246255
247256 const requestElicitation = async (
248257 message : string ,
249- requestedSchema : any
258+ requestedSchema : any ,
259+ sendRequest : SendRequest
250260 ) => {
251- const request = {
261+ const request : ElicitRequest = {
252262 method : 'elicitation/create' ,
253263 params : {
254264 message,
255- requestedSchema
256- }
265+ requestedSchema,
266+ } ,
257267 } ;
258268
259- return await server . request ( request , z . any ( ) ) ;
269+ return await sendRequest ( request , ElicitResultSchema ) ;
260270 } ;
261271
262272 const ALL_RESOURCES : Resource [ ] = Array . from ( { length : 100 } , ( _ , i ) => {
@@ -334,12 +344,12 @@ export const createServer = () => {
334344 throw new Error ( `Unknown resource: ${ uri } ` ) ;
335345 } ) ;
336346
337- server . setRequestHandler ( SubscribeRequestSchema , async ( request ) => {
347+ server . setRequestHandler ( SubscribeRequestSchema , async ( request , extra ) => {
338348 const { uri } = request . params ;
339349 subscriptions . add ( uri ) ;
340350
341351 // Request sampling from client when someone subscribes
342- await requestSampling ( "A new subscription was started" , uri ) ;
352+ await requestSampling ( "A new subscription was started" , uri , undefined , extra . sendRequest ) ;
343353 return { } ;
344354 } ) ;
345355
@@ -615,7 +625,8 @@ export const createServer = () => {
615625 const result = await requestSampling (
616626 prompt ,
617627 ToolName . SAMPLE_LLM ,
618- maxTokens
628+ maxTokens ,
629+ extra . sendRequest
619630 ) ;
620631 return {
621632 content : [
@@ -734,14 +745,20 @@ export const createServer = () => {
734745 type : 'object' ,
735746 properties : {
736747 color : { type : 'string' , description : 'Favorite color' } ,
737- number : { type : 'integer' , description : 'Favorite number' , minimum : 1 , maximum : 100 } ,
748+ number : {
749+ type : 'integer' ,
750+ description : 'Favorite number' ,
751+ minimum : 1 ,
752+ maximum : 100 ,
753+ } ,
738754 pets : {
739755 type : 'string' ,
740756 enum : [ 'cats' , 'dogs' , 'birds' , 'fish' , 'reptiles' ] ,
741- description : 'Favorite pets'
757+ description : 'Favorite pets' ,
742758 } ,
743- }
744- }
759+ } ,
760+ } ,
761+ extra . sendRequest
745762 ) ;
746763
747764 // Handle different response actions
0 commit comments