@@ -31,6 +31,9 @@ const instructions = readFileSync(join(__dirname, "instructions.md"), "utf-8");
3131const ToolInputSchema = ToolSchema . shape . inputSchema ;
3232type ToolInput = z . infer < typeof ToolInputSchema > ;
3333
34+ const ToolOutputSchema = ToolSchema . shape . outputSchema ;
35+ type ToolOutput = z . infer < typeof ToolOutputSchema > ;
36+
3437/* Input schemas for tools implemented in this server */
3538const EchoSchema = z . object ( {
3639 message : z . string ( ) . describe ( "Message to echo" ) ,
@@ -93,6 +96,28 @@ const GetResourceLinksSchema = z.object({
9396 . describe ( "Number of resource links to return (1-10)" ) ,
9497} ) ;
9598
99+ const StructuredContentSchema = {
100+ input : z . object ( {
101+ location : z
102+ . string ( )
103+ . trim ( )
104+ . min ( 1 )
105+ . describe ( "City name or zip code" ) ,
106+ } ) ,
107+
108+ output : z . object ( {
109+ temperature : z
110+ . number ( )
111+ . describe ( "Temperature in celsius" ) ,
112+ conditions : z
113+ . string ( )
114+ . describe ( "Weather conditions description" ) ,
115+ humidity : z
116+ . number ( )
117+ . describe ( "Humidity percentage" ) ,
118+ } )
119+ } ;
120+
96121enum ToolName {
97122 ECHO = "echo" ,
98123 ADD = "add" ,
@@ -104,6 +129,7 @@ enum ToolName {
104129 GET_RESOURCE_REFERENCE = "getResourceReference" ,
105130 ELICITATION = "startElicitation" ,
106131 GET_RESOURCE_LINKS = "getResourceLinks" ,
132+ STRUCTURED_CONTENT = "structuredContent"
107133}
108134
109135enum PromptName {
@@ -503,6 +529,13 @@ export const createServer = () => {
503529 "Returns multiple resource links that reference different types of resources" ,
504530 inputSchema : zodToJsonSchema ( GetResourceLinksSchema ) as ToolInput ,
505531 } ,
532+ {
533+ name : ToolName . STRUCTURED_CONTENT ,
534+ description :
535+ "Returns structured content along with an output schema for client data validation" ,
536+ inputSchema : zodToJsonSchema ( StructuredContentSchema . input ) as ToolInput ,
537+ outputSchema : zodToJsonSchema ( StructuredContentSchema . output ) as ToolOutput ,
538+ } ,
506539 ] ;
507540
508541 return { tools } ;
@@ -777,6 +810,27 @@ export const createServer = () => {
777810 return { content } ;
778811 }
779812
813+ if ( name === ToolName . STRUCTURED_CONTENT ) {
814+ // The same response is returned for every input.
815+ const validatedArgs = StructuredContentSchema . input . parse ( args ) ;
816+
817+ const weather = {
818+ temperature : 22.5 ,
819+ conditions : "Partly cloudy" ,
820+ humidity : 65
821+ }
822+
823+ const backwardCompatiblecontent = {
824+ type : "text" ,
825+ text : JSON . stringify ( weather )
826+ }
827+
828+ return {
829+ content : [ backwardCompatiblecontent ] ,
830+ structuredContent : weather
831+ } ;
832+ }
833+
780834 throw new Error ( `Unknown tool: ${ name } ` ) ;
781835 } ) ;
782836
0 commit comments