@@ -305,7 +305,7 @@ func NewListResourceTemplatesResult(
305305// NewReadResourceResult creates a new ReadResourceResult with text content 
306306func  NewReadResourceResult (text  string ) * ReadResourceResult  {
307307	return  & ReadResourceResult {
308- 		Contents : []interface {} {
308+ 		Contents : []ResourceContents {
309309			TextResourceContents {
310310				Text : text ,
311311			},
@@ -410,22 +410,12 @@ func ParseContent(contentMap map[string]any) (Content, error) {
410410			return  nil , fmt .Errorf ("resource is missing" )
411411		}
412412
413- 		uri  :=  ExtractString (resourceMap , "uri" )
414- 		mimeType  :=  ExtractString (resourceMap , "mimeType" )
415- 		text  :=  ExtractString (resourceMap , "text" )
416- 
417- 		if  uri  ==  ""  ||  mimeType  ==  ""  {
418- 			return  nil , fmt .Errorf ("resource uri or mimeType is missing" )
413+ 		resourceContents , err  :=  ParseResourceContents (resourceMap )
414+ 		if  err  !=  nil  {
415+ 			return  nil , err 
419416		}
420417
421- 		if  text  !=  ""  {
422- 			return  NewEmbeddedResource (
423- 				ResourceContents {
424- 					URI :      uri ,
425- 					MIMEType : mimeType ,
426- 				},
427- 			), nil 
428- 		}
418+ 		return  NewEmbeddedResource (resourceContents ), nil 
429419	}
430420
431421	return  nil , fmt .Errorf ("unsupported content type: %s" , contentType )
@@ -543,3 +533,74 @@ func ParseCallToolResult(rawMessage *json.RawMessage) (*CallToolResult, error) {
543533
544534	return  & result , nil 
545535}
536+ 
537+ func  ParseResourceContents (contentMap  map [string ]any ) (ResourceContents , error ) {
538+ 	uri  :=  ExtractString (contentMap , "uri" )
539+ 	if  uri  ==  ""  {
540+ 		return  nil , fmt .Errorf ("resource uri is missing" )
541+ 	}
542+ 
543+ 	mimeType  :=  ExtractString (contentMap , "mimeType" )
544+ 
545+ 	if  text  :=  ExtractString (contentMap , "text" ); text  !=  ""  {
546+ 		return  TextResourceContents {
547+ 			URI :      uri ,
548+ 			MIMEType : mimeType ,
549+ 			Text :     text ,
550+ 		}, nil 
551+ 	}
552+ 
553+ 	if  blob  :=  ExtractString (contentMap , "blob" ); blob  !=  ""  {
554+ 		return  BlobResourceContents {
555+ 			URI :      uri ,
556+ 			MIMEType : mimeType ,
557+ 			Blob :     blob ,
558+ 		}, nil 
559+ 	}
560+ 
561+ 	return  nil , fmt .Errorf ("unsupported resource type" )
562+ }
563+ 
564+ func  ParseReadResourceResult (rawMessage  * json.RawMessage ) (* ReadResourceResult , error ) {
565+ 	var  jsonContent  map [string ]any 
566+ 	if  err  :=  json .Unmarshal (* rawMessage , & jsonContent ); err  !=  nil  {
567+ 		return  nil , fmt .Errorf ("failed to unmarshal response: %w" , err )
568+ 	}
569+ 
570+ 	var  result  ReadResourceResult 
571+ 
572+ 	meta , ok  :=  jsonContent ["_meta" ]
573+ 	if  ok  {
574+ 		if  metaMap , ok  :=  meta .(map [string ]any ); ok  {
575+ 			result .Meta  =  metaMap 
576+ 		}
577+ 	}
578+ 
579+ 	contents , ok  :=  jsonContent ["contents" ]
580+ 	if  ! ok  {
581+ 		return  nil , fmt .Errorf ("contents is missing" )
582+ 	}
583+ 
584+ 	contentArr , ok  :=  contents .([]any )
585+ 	if  ! ok  {
586+ 		return  nil , fmt .Errorf ("contents is not an array" )
587+ 	}
588+ 
589+ 	for  _ , content  :=  range  contentArr  {
590+ 		// Extract content 
591+ 		contentMap , ok  :=  content .(map [string ]any )
592+ 		if  ! ok  {
593+ 			return  nil , fmt .Errorf ("content is not an object" )
594+ 		}
595+ 
596+ 		// Process content 
597+ 		content , err  :=  ParseResourceContents (contentMap )
598+ 		if  err  !=  nil  {
599+ 			return  nil , err 
600+ 		}
601+ 
602+ 		result .Contents  =  append (result .Contents , content )
603+ 	}
604+ 
605+ 	return  & result , nil 
606+ }
0 commit comments