|
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 all content types. |
| 16 | +// ToWire converts content to its jsonrpc2 wire format. |
17 | 17 | type Content interface {
|
18 |
| - toProtocol() any |
| 18 | + ToWire() protocol.Content |
19 | 19 | }
|
20 | 20 |
|
21 |
| -func marshalContent(content []Content) []json.RawMessage { |
22 |
| - var msgs []json.RawMessage |
23 |
| - for _, c := range content { |
24 |
| - msg, err := json.Marshal(c.toProtocol()) |
25 |
| - if err != nil { |
26 |
| - panic(fmt.Sprintf("marshaling content: %v", err)) |
27 |
| - } |
28 |
| - msgs = append(msgs, msg) |
29 |
| - } |
30 |
| - return msgs |
| 21 | +// TextContent is a textual content. |
| 22 | +type TextContent struct { |
| 23 | + Text string |
31 | 24 | }
|
32 | 25 |
|
33 |
| -func unmarshalContent(msgs []json.RawMessage) ([]Content, error) { |
34 |
| - var content []Content |
35 |
| - for _, msg := range msgs { |
36 |
| - var allContent struct { |
37 |
| - Type string `json:"type"` |
38 |
| - Text json.RawMessage |
39 |
| - } |
40 |
| - if err := json.Unmarshal(msg, &allContent); err != nil { |
41 |
| - return nil, fmt.Errorf("content missing \"type\"") |
42 |
| - } |
43 |
| - switch allContent.Type { |
44 |
| - case "text": |
45 |
| - var text string |
46 |
| - if err := json.Unmarshal(allContent.Text, &text); err != nil { |
47 |
| - return nil, fmt.Errorf("unmarshalling text content: %v", err) |
48 |
| - } |
49 |
| - content = append(content, TextContent{Text: text}) |
50 |
| - default: |
51 |
| - return nil, fmt.Errorf("unsupported content type %q", allContent.Type) |
52 |
| - } |
| 26 | +func (c TextContent) ToWire() protocol.Content { |
| 27 | + return protocol.Content{Type: "text", Text: c.Text} |
| 28 | +} |
| 29 | + |
| 30 | +// ImageContent contains base64-encoded image data. |
| 31 | +type ImageContent struct { |
| 32 | + Data string |
| 33 | + MimeType string |
| 34 | +} |
| 35 | + |
| 36 | +func (c ImageContent) 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 | +type AudioContent struct { |
| 42 | + Data string |
| 43 | + MimeType string |
| 44 | +} |
| 45 | + |
| 46 | +func (c AudioContent) ToWire() protocol.Content { |
| 47 | + return protocol.Content{Type: "audio", MIMEType: c.MimeType, Data: c.Data} |
| 48 | +} |
| 49 | + |
| 50 | +// ResourceContent contains embedded resources. |
| 51 | +type ResourceContent struct { |
| 52 | + Resource Resource |
| 53 | +} |
| 54 | + |
| 55 | +func (r ResourceContent) ToWire() protocol.Content { |
| 56 | + res := r.Resource.ToWire() |
| 57 | + return protocol.Content{Type: "resource", Resource: &res} |
| 58 | +} |
| 59 | + |
| 60 | +type Resource interface { |
| 61 | + ToWire() protocol.Resource |
| 62 | +} |
| 63 | + |
| 64 | +type TextResource struct { |
| 65 | + URI string |
| 66 | + MimeType string |
| 67 | + Text string |
| 68 | +} |
| 69 | + |
| 70 | +func (r TextResource) ToWire() protocol.Resource { |
| 71 | + return protocol.Resource{ |
| 72 | + URI: r.URI, |
| 73 | + MIMEType: r.MimeType, |
| 74 | + Text: r.Text, |
53 | 75 | }
|
54 |
| - return content, nil |
55 | 76 | }
|
56 | 77 |
|
57 |
| -// TextContent is a textual content. |
58 |
| -type TextContent struct { |
59 |
| - Text string |
| 78 | +type BlobResource struct { |
| 79 | + URI string |
| 80 | + MimeType string |
| 81 | + Blob string |
60 | 82 | }
|
61 | 83 |
|
62 |
| -func (c TextContent) toProtocol() any { |
63 |
| - return protocol.TextContent{Type: "text", Text: c.Text} |
| 84 | +func (r BlobResource) 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 | +func ContentFromWireContent(c protocol.Content) Content { |
| 96 | + switch c.Type { |
| 97 | + case "text": |
| 98 | + return TextContent{Text: c.Text} |
| 99 | + case "image": |
| 100 | + return ImageContent{Data: c.Data, MimeType: c.MIMEType} |
| 101 | + case "audio": |
| 102 | + return AudioContent{Data: c.Data, MimeType: c.MIMEType} |
| 103 | + case "resource": |
| 104 | + r := ResourceContent{} |
| 105 | + if c.Resource != nil { |
| 106 | + if c.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 | + return r |
| 121 | + default: |
| 122 | + panic(fmt.Sprintf("unrecognized wire content type %q", c.Type)) |
| 123 | + } |
64 | 124 | }
|
0 commit comments