@@ -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" ) ,
@@ -82,6 +85,28 @@ const GetResourceReferenceSchema = z.object({
8285 . describe ( "ID of the resource to reference (1-100)" ) ,
8386} ) ;
8487
88+ const StructuredContentSchema = {
89+ input : z . object ( {
90+ location : z
91+ . string ( )
92+ . trim ( )
93+ . min ( 1 )
94+ . describe ( "City name or zip code" ) ,
95+ } ) ,
96+
97+ output : z . object ( {
98+ temperature : z
99+ . number ( )
100+ . describe ( "Temperature in celsius" ) ,
101+ conditions : z
102+ . string ( )
103+ . describe ( "Weather conditions description" ) ,
104+ humidity : z
105+ . number ( )
106+ . describe ( "Humidity percentage" ) ,
107+ } )
108+ } ;
109+
85110enum ToolName {
86111 ECHO = "echo" ,
87112 ADD = "add" ,
@@ -91,6 +116,7 @@ enum ToolName {
91116 GET_TINY_IMAGE = "getTinyImage" ,
92117 ANNOTATED_MESSAGE = "annotatedMessage" ,
93118 GET_RESOURCE_REFERENCE = "getResourceReference" ,
119+ STRUCTURED_CONTENT = "structuredContent"
94120}
95121
96122enum PromptName {
@@ -463,6 +489,13 @@ export const createServer = () => {
463489 "Returns a resource reference that can be used by MCP clients" ,
464490 inputSchema : zodToJsonSchema ( GetResourceReferenceSchema ) as ToolInput ,
465491 } ,
492+ {
493+ name : ToolName . STRUCTURED_CONTENT ,
494+ description :
495+ "Returns structured content along with an output schema for client data validation" ,
496+ inputSchema : zodToJsonSchema ( StructuredContentSchema . input ) as ToolInput ,
497+ outputSchema : zodToJsonSchema ( StructuredContentSchema . output ) as ToolOutput ,
498+ } ,
466499 ] ;
467500
468501 return { tools } ;
@@ -652,6 +685,28 @@ export const createServer = () => {
652685 } ;
653686 }
654687
688+ if ( name === ToolName . STRUCTURED_CONTENT ) {
689+ // The same response is returned for every input.
690+ const validatedArgs = StructuredContentSchema . input . parse ( args ) ;
691+
692+ const weather = {
693+ temperature : 22.5 ,
694+ conditions : "Partly cloudy" ,
695+ humidity : 65
696+ }
697+
698+ const backwardCompatiblecontent = {
699+ type : "text" ,
700+ text : JSON . stringify ( weather )
701+ }
702+
703+ return {
704+ content : [ backwardCompatiblecontent ] ,
705+ structuredContent : weather
706+ } ;
707+ }
708+
709+
655710 throw new Error ( `Unknown tool: ${ name } ` ) ;
656711 } ) ;
657712
0 commit comments