Skip to content

Commit 7657c15

Browse files
Crescent617ezynda3
andauthored
feat: add support for call tool result annotations (#547)
* fix: tool call result do not have annotations * fix: annotations parse * fix: ut * fix: cr --------- Co-authored-by: Ed Zynda <[email protected]>
1 parent aefdc0d commit 7657c15

File tree

2 files changed

+395
-8
lines changed

2 files changed

+395
-8
lines changed

mcp/utils.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,27 @@ func ExtractString(data map[string]any, key string) string {
536536
return ""
537537
}
538538

539+
func ParseAnnotations(data map[string]any) *Annotations {
540+
if data == nil {
541+
return nil
542+
}
543+
annotations := &Annotations{}
544+
if value, ok := data["priority"]; ok {
545+
annotations.Priority = cast.ToFloat64(value)
546+
}
547+
548+
if value, ok := data["audience"]; ok {
549+
for _, a := range cast.ToStringSlice(value) {
550+
a := Role(a)
551+
if a == RoleUser || a == RoleAssistant {
552+
annotations.Audience = append(annotations.Audience, a)
553+
}
554+
}
555+
}
556+
return annotations
557+
558+
}
559+
539560
func ExtractMap(data map[string]any, key string) map[string]any {
540561
if value, ok := data[key]; ok {
541562
if m, ok := value.(map[string]any); ok {
@@ -548,26 +569,37 @@ func ExtractMap(data map[string]any, key string) map[string]any {
548569
func ParseContent(contentMap map[string]any) (Content, error) {
549570
contentType := ExtractString(contentMap, "type")
550571

572+
var annotations *Annotations
573+
if annotationsMap := ExtractMap(contentMap, "annotations"); annotationsMap != nil {
574+
annotations = ParseAnnotations(annotationsMap)
575+
}
576+
551577
switch contentType {
552578
case ContentTypeText:
553579
text := ExtractString(contentMap, "text")
554-
return NewTextContent(text), nil
580+
c := NewTextContent(text)
581+
c.Annotations = annotations
582+
return c, nil
555583

556584
case ContentTypeImage:
557585
data := ExtractString(contentMap, "data")
558586
mimeType := ExtractString(contentMap, "mimeType")
559587
if data == "" || mimeType == "" {
560588
return nil, fmt.Errorf("image data or mimeType is missing")
561589
}
562-
return NewImageContent(data, mimeType), nil
590+
c := NewImageContent(data, mimeType)
591+
c.Annotations = annotations
592+
return c, nil
563593

564594
case ContentTypeAudio:
565595
data := ExtractString(contentMap, "data")
566596
mimeType := ExtractString(contentMap, "mimeType")
567597
if data == "" || mimeType == "" {
568598
return nil, fmt.Errorf("audio data or mimeType is missing")
569599
}
570-
return NewAudioContent(data, mimeType), nil
600+
c := NewAudioContent(data, mimeType)
601+
c.Annotations = annotations
602+
return c, nil
571603

572604
case ContentTypeLink:
573605
uri := ExtractString(contentMap, "uri")
@@ -577,7 +609,9 @@ func ParseContent(contentMap map[string]any) (Content, error) {
577609
if uri == "" || name == "" {
578610
return nil, fmt.Errorf("resource_link uri or name is missing")
579611
}
580-
return NewResourceLink(uri, name, description, mimeType), nil
612+
c := NewResourceLink(uri, name, description, mimeType)
613+
c.Annotations = annotations
614+
return c, nil
581615

582616
case ContentTypeResource:
583617
resourceMap := ExtractMap(contentMap, "resource")
@@ -590,7 +624,9 @@ func ParseContent(contentMap map[string]any) (Content, error) {
590624
return nil, err
591625
}
592626

593-
return NewEmbeddedResource(resourceContents), nil
627+
c := NewEmbeddedResource(resourceContents)
628+
c.Annotations = annotations
629+
return c, nil
594630
}
595631

596632
return nil, fmt.Errorf("unsupported content type: %s", contentType)

0 commit comments

Comments
 (0)