|
5 | 5 | package mcp |
6 | 6 |
|
7 | 7 | import ( |
8 | | -"encoding/json" |
9 | 8 | "fmt" |
10 | 9 |
|
11 | 10 | "golang.org/x/tools/internal/mcp/internal/protocol" |
12 | 11 | ) |
13 | 12 |
|
14 | | -// Content is the abstract result of a Tool call. |
| 13 | +// Content is the union of supported content types: [TextContent], |
| 14 | +// [ImageContent], [AudioContent], and [ResourceContent]. |
15 | 15 | // |
16 | | -//TODO: support allcontenttypes. |
| 16 | +//ToWire convertscontentto its jsonrpc2 wire format. |
17 | 17 | typeContentinterface { |
18 | | -toProtocol()any |
| 18 | +ToWire()protocol.Content |
19 | 19 | } |
20 | 20 |
|
21 | | -funcmarshalContent(content []Content) []json.RawMessage { |
22 | | -varmsgs []json.RawMessage |
23 | | -for_,c:=rangecontent { |
24 | | -msg,err:=json.Marshal(c.toProtocol()) |
25 | | -iferr!=nil { |
26 | | -panic(fmt.Sprintf("marshaling content: %v",err)) |
27 | | -} |
28 | | -msgs=append(msgs,msg) |
29 | | -} |
30 | | -returnmsgs |
| 21 | +// TextContent is a textual content. |
| 22 | +typeTextContentstruct { |
| 23 | +Textstring |
31 | 24 | } |
32 | 25 |
|
33 | | -funcunmarshalContent(msgs []json.RawMessage) ([]Content,error) { |
34 | | -varcontent []Content |
35 | | -for_,msg:=rangemsgs { |
36 | | -varallContentstruct { |
37 | | -Typestring`json:"type"` |
38 | | -Text json.RawMessage |
39 | | -} |
40 | | -iferr:=json.Unmarshal(msg,&allContent);err!=nil { |
41 | | -returnnil,fmt.Errorf("content missing\"type\"") |
42 | | -} |
43 | | -switchallContent.Type { |
44 | | -case"text": |
45 | | -vartextstring |
46 | | -iferr:=json.Unmarshal(allContent.Text,&text);err!=nil { |
47 | | -returnnil,fmt.Errorf("unmarshalling text content: %v",err) |
48 | | -} |
49 | | -content=append(content,TextContent{Text:text}) |
50 | | -default: |
51 | | -returnnil,fmt.Errorf("unsupported content type %q",allContent.Type) |
52 | | -} |
| 26 | +func (cTextContent)ToWire() protocol.Content { |
| 27 | +return protocol.Content{Type:"text",Text:c.Text} |
| 28 | +} |
| 29 | + |
| 30 | +// ImageContent contains base64-encoded image data. |
| 31 | +typeImageContentstruct { |
| 32 | +Datastring |
| 33 | +MimeTypestring |
| 34 | +} |
| 35 | + |
| 36 | +func (cImageContent)ToWire() protocol.Content { |
| 37 | +return protocol.Content{Type:"image",MIMEType:c.MimeType,Data:c.Data} |
| 38 | +} |
| 39 | + |
| 40 | +// AudioContent contains base64-encoded audio data. |
| 41 | +typeAudioContentstruct { |
| 42 | +Datastring |
| 43 | +MimeTypestring |
| 44 | +} |
| 45 | + |
| 46 | +func (cAudioContent)ToWire() protocol.Content { |
| 47 | +return protocol.Content{Type:"audio",MIMEType:c.MimeType,Data:c.Data} |
| 48 | +} |
| 49 | + |
| 50 | +// ResourceContent contains embedded resources. |
| 51 | +typeResourceContentstruct { |
| 52 | +ResourceResource |
| 53 | +} |
| 54 | + |
| 55 | +func (rResourceContent)ToWire() protocol.Content { |
| 56 | +res:=r.Resource.ToWire() |
| 57 | +return protocol.Content{Type:"resource",Resource:&res} |
| 58 | +} |
| 59 | + |
| 60 | +typeResourceinterface { |
| 61 | +ToWire() protocol.Resource |
| 62 | +} |
| 63 | + |
| 64 | +typeTextResourcestruct { |
| 65 | +URIstring |
| 66 | +MimeTypestring |
| 67 | +Textstring |
| 68 | +} |
| 69 | + |
| 70 | +func (rTextResource)ToWire() protocol.Resource { |
| 71 | +return protocol.Resource{ |
| 72 | +URI:r.URI, |
| 73 | +MIMEType:r.MimeType, |
| 74 | +Text:r.Text, |
53 | 75 | } |
54 | | -returncontent,nil |
55 | 76 | } |
56 | 77 |
|
57 | | -// TextContent is a textual content. |
58 | | -typeTextContentstruct { |
59 | | -Textstring |
| 78 | +typeBlobResourcestruct { |
| 79 | +URIstring |
| 80 | +MimeTypestring |
| 81 | +Blobstring |
60 | 82 | } |
61 | 83 |
|
62 | | -func (cTextContent)toProtocol()any { |
63 | | -return protocol.TextContent{Type:"text",Text:c.Text} |
| 84 | +func (rBlobResource)ToWire() protocol.Resource { |
| 85 | +blob:=r.Blob |
| 86 | +return protocol.Resource{ |
| 87 | +URI:r.URI, |
| 88 | +MIMEType:r.MimeType, |
| 89 | +Blob:&blob, |
| 90 | +} |
| 91 | +} |
| 92 | + |
| 93 | +// ContentFromWireContent converts content from the jsonrpc2 wire format to a |
| 94 | +// typed Content value. |
| 95 | +funcContentFromWireContent(c protocol.Content)Content { |
| 96 | +switchc.Type { |
| 97 | +case"text": |
| 98 | +returnTextContent{Text:c.Text} |
| 99 | +case"image": |
| 100 | +returnImageContent{Data:c.Data,MimeType:c.MIMEType} |
| 101 | +case"audio": |
| 102 | +returnAudioContent{Data:c.Data,MimeType:c.MIMEType} |
| 103 | +case"resource": |
| 104 | +r:=ResourceContent{} |
| 105 | +ifc.Resource!=nil { |
| 106 | +ifc.Resource.Blob!=nil { |
| 107 | +r.Resource=BlobResource{ |
| 108 | +URI:c.Resource.URI, |
| 109 | +MimeType:c.Resource.MIMEType, |
| 110 | +Blob:*c.Resource.Blob, |
| 111 | +} |
| 112 | +}else { |
| 113 | +r.Resource=TextResource{ |
| 114 | +URI:c.Resource.URI, |
| 115 | +MimeType:c.Resource.MIMEType, |
| 116 | +Text:c.Resource.Text, |
| 117 | +} |
| 118 | +} |
| 119 | +} |
| 120 | +returnr |
| 121 | +default: |
| 122 | +panic(fmt.Sprintf("unrecognized wire content type %q",c.Type)) |
| 123 | +} |
64 | 124 | } |