| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package mcp_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"encoding/json" |
| 9 |
"fmt" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/google/go-cmp/cmp" |
| 13 |
"github.com/modelcontextprotocol/go-sdk/mcp" |
| 14 |
) |
| 15 |
|
| 16 |
func TestContent(t *testing.T) { |
| 17 |
tests := []struct { |
| 18 |
in mcp.Content |
| 19 |
want string |
| 20 |
}{ |
| 21 |
{ |
| 22 |
&mcp.TextContent{Text: "hello"}, |
| 23 |
`{"type":"text","text":"hello"}`, |
| 24 |
}, |
| 25 |
{ |
| 26 |
&mcp.TextContent{Text: ""}, |
| 27 |
`{"type":"text","text":""}`, |
| 28 |
}, |
| 29 |
{ |
| 30 |
&mcp.TextContent{}, |
| 31 |
`{"type":"text","text":""}`, |
| 32 |
}, |
| 33 |
{ |
| 34 |
&mcp.TextContent{ |
| 35 |
Text: "hello", |
| 36 |
Meta: mcp.Meta{"key": "value"}, |
| 37 |
Annotations: &mcp.Annotations{Priority: 1.0}, |
| 38 |
}, |
| 39 |
`{"type":"text","text":"hello","_meta":{"key":"value"},"annotations":{"priority":1}}`, |
| 40 |
}, |
| 41 |
{ |
| 42 |
&mcp.ImageContent{ |
| 43 |
Data: []byte("a1b2c3"), |
| 44 |
MIMEType: "image/png", |
| 45 |
}, |
| 46 |
`{"type":"image","mimeType":"image/png","data":"YTFiMmMz"}`, |
| 47 |
}, |
| 48 |
{ |
| 49 |
&mcp.ImageContent{MIMEType: "image/png", Data: []byte{}}, |
| 50 |
`{"type":"image","mimeType":"image/png","data":""}`, |
| 51 |
}, |
| 52 |
{ |
| 53 |
&mcp.ImageContent{Data: []byte("test")}, |
| 54 |
`{"type":"image","mimeType":"","data":"dGVzdA=="}`, |
| 55 |
}, |
| 56 |
{ |
| 57 |
&mcp.ImageContent{ |
| 58 |
Data: []byte("a1b2c3"), |
| 59 |
MIMEType: "image/png", |
| 60 |
Meta: mcp.Meta{"key": "value"}, |
| 61 |
Annotations: &mcp.Annotations{Priority: 1.0}, |
| 62 |
}, |
| 63 |
`{"type":"image","mimeType":"image/png","data":"YTFiMmMz","_meta":{"key":"value"},"annotations":{"priority":1}}`, |
| 64 |
}, |
| 65 |
{ |
| 66 |
&mcp.AudioContent{ |
| 67 |
Data: []byte("a1b2c3"), |
| 68 |
MIMEType: "audio/wav", |
| 69 |
}, |
| 70 |
`{"type":"audio","mimeType":"audio/wav","data":"YTFiMmMz"}`, |
| 71 |
}, |
| 72 |
{ |
| 73 |
&mcp.AudioContent{MIMEType: "audio/wav", Data: []byte{}}, |
| 74 |
`{"type":"audio","mimeType":"audio/wav","data":""}`, |
| 75 |
}, |
| 76 |
{ |
| 77 |
&mcp.AudioContent{Data: []byte("test")}, |
| 78 |
`{"type":"audio","mimeType":"","data":"dGVzdA=="}`, |
| 79 |
}, |
| 80 |
{ |
| 81 |
&mcp.AudioContent{ |
| 82 |
Data: []byte("a1b2c3"), |
| 83 |
MIMEType: "audio/wav", |
| 84 |
Meta: mcp.Meta{"key": "value"}, |
| 85 |
Annotations: &mcp.Annotations{Priority: 1.0}, |
| 86 |
}, |
| 87 |
`{"type":"audio","mimeType":"audio/wav","data":"YTFiMmMz","_meta":{"key":"value"},"annotations":{"priority":1}}`, |
| 88 |
}, |
| 89 |
{ |
| 90 |
&mcp.EmbeddedResource{ |
| 91 |
Resource: &mcp.ResourceContents{URI: "file://foo", MIMEType: "text", Text: "abc"}, |
| 92 |
}, |
| 93 |
`{"type":"resource","resource":{"uri":"file://foo","mimeType":"text","text":"abc"}}`, |
| 94 |
}, |
| 95 |
{ |
| 96 |
&mcp.EmbeddedResource{ |
| 97 |
Resource: &mcp.ResourceContents{URI: "file://foo", MIMEType: "image/png", Blob: []byte("a1b2c3")}, |
| 98 |
}, |
| 99 |
`{"type":"resource","resource":{"uri":"file://foo","mimeType":"image/png","blob":"YTFiMmMz"}}`, |
| 100 |
}, |
| 101 |
{ |
| 102 |
&mcp.EmbeddedResource{ |
| 103 |
Resource: &mcp.ResourceContents{URI: "file://foo", MIMEType: "text", Text: "abc"}, |
| 104 |
Meta: mcp.Meta{"key": "value"}, |
| 105 |
Annotations: &mcp.Annotations{Priority: 1.0}, |
| 106 |
}, |
| 107 |
`{"type":"resource","resource":{"uri":"file://foo","mimeType":"text","text":"abc"},"_meta":{"key":"value"},"annotations":{"priority":1}}`, |
| 108 |
}, |
| 109 |
{ |
| 110 |
&mcp.ResourceLink{ |
| 111 |
URI: "file:///path/to/file.txt", |
| 112 |
Name: "file.txt", |
| 113 |
}, |
| 114 |
`{"type":"resource_link","uri":"file:///path/to/file.txt","name":"file.txt"}`, |
| 115 |
}, |
| 116 |
{ |
| 117 |
&mcp.ResourceLink{ |
| 118 |
URI: "https://example.com/resource", |
| 119 |
Name: "Example Resource", |
| 120 |
Title: "A comprehensive example resource", |
| 121 |
Description: "This resource demonstrates all fields", |
| 122 |
MIMEType: "text/plain", |
| 123 |
Meta: mcp.Meta{"custom": "metadata"}, |
| 124 |
Icons: []mcp.Icon{{Source: "foobar", MIMEType: "image/png", Sizes: []string{"48x48"}, Theme: mcp.IconThemeLight}}, |
| 125 |
}, |
| 126 |
`{"type":"resource_link","mimeType":"text/plain","uri":"https://example.com/resource","name":"Example Resource","title":"A comprehensive example resource","description":"This resource demonstrates all fields","_meta":{"custom":"metadata"},"icons":[{"src":"foobar","mimeType":"image/png","sizes":["48x48"],"theme":"light"}]}`, |
| 127 |
}, |
| 128 |
} |
| 129 |
|
| 130 |
for _, test := range tests { |
| 131 |
got, err := json.Marshal(test.in) |
| 132 |
if err != nil { |
| 133 |
t.Fatal(err) |
| 134 |
} |
| 135 |
if diff := cmp.Diff(test.want, string(got)); diff != "" { |
| 136 |
t.Errorf("json.Marshal(%v) mismatch (-want +got):\n%s", test.in, diff) |
| 137 |
} |
| 138 |
result := fmt.Sprintf(`{"content":[%s]}`, string(got)) |
| 139 |
var out mcp.CallToolResult |
| 140 |
if err := json.Unmarshal([]byte(result), &out); err != nil { |
| 141 |
t.Fatal(err) |
| 142 |
} |
| 143 |
if diff := cmp.Diff(test.in, out.Content[0]); diff != "" { |
| 144 |
t.Errorf("json.Unmarshal(%q) mismatch (-want +got):\n%s", string(got), diff) |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func TestEmbeddedResource(t *testing.T) { |
| 150 |
for _, tt := range []struct { |
| 151 |
rc *mcp.ResourceContents |
| 152 |
want string |
| 153 |
}{ |
| 154 |
{ |
| 155 |
&mcp.ResourceContents{URI: "u", Text: "t"}, |
| 156 |
`{"uri":"u","text":"t"}`, |
| 157 |
}, |
| 158 |
{ |
| 159 |
&mcp.ResourceContents{URI: "u", MIMEType: "m", Text: "t", Meta: mcp.Meta{"key": "value"}}, |
| 160 |
`{"uri":"u","mimeType":"m","text":"t","_meta":{"key":"value"}}`, |
| 161 |
}, |
| 162 |
{ |
| 163 |
&mcp.ResourceContents{URI: "u"}, |
| 164 |
`{"uri":"u"}`, |
| 165 |
}, |
| 166 |
{ |
| 167 |
&mcp.ResourceContents{URI: "u", Blob: []byte{}}, |
| 168 |
`{"uri":"u","blob":""}`, |
| 169 |
}, |
| 170 |
{ |
| 171 |
&mcp.ResourceContents{URI: "u", Blob: []byte{1}}, |
| 172 |
`{"uri":"u","blob":"AQ=="}`, |
| 173 |
}, |
| 174 |
{ |
| 175 |
&mcp.ResourceContents{URI: "u", MIMEType: "m", Blob: []byte{1}, Meta: mcp.Meta{"key": "value"}}, |
| 176 |
`{"uri":"u","mimeType":"m","blob":"AQ==","_meta":{"key":"value"}}`, |
| 177 |
}, |
| 178 |
} { |
| 179 |
data, err := json.Marshal(tt.rc) |
| 180 |
if err != nil { |
| 181 |
t.Fatal(err) |
| 182 |
} |
| 183 |
if got := string(data); got != tt.want { |
| 184 |
t.Errorf("%#v:\ngot %s\nwant %s", tt.rc, got, tt.want) |
| 185 |
} |
| 186 |
urc := new(mcp.ResourceContents) |
| 187 |
if err := json.Unmarshal(data, urc); err != nil { |
| 188 |
t.Fatal(err) |
| 189 |
} |
| 190 |
|
| 191 |
if diff := cmp.Diff(tt.rc, urc); diff != "" { |
| 192 |
t.Errorf("mismatch (-want, +got):\n%s", diff) |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
func TestContentUnmarshal(t *testing.T) { |
| 200 |
valInt64 := int64(24) |
| 201 |
tests := []struct { |
| 202 |
name string |
| 203 |
json string |
| 204 |
content mcp.Content |
| 205 |
expectContent mcp.Content |
| 206 |
}{ |
| 207 |
{ |
| 208 |
name: "ResourceLink", |
| 209 |
json: `{"type":"resource_link","mimeType":"text/plain","uri":"https://example.com/resource","name":"Example Resource","title":"A comprehensive example resource","description":"This resource demonstrates all fields","_meta":{"custom":"metadata"},"icons":[{"src":"foobar","mimeType":"image/png","sizes":["48x48"],"theme":"light"}], "size":24,"annotations":{"audience":["user","assistant"],"lastModified":"2025-01-12T15:00:58Z","priority":0.5}}`, |
| 210 |
content: &mcp.ResourceLink{}, |
| 211 |
expectContent: &mcp.ResourceLink{ |
| 212 |
URI: "https://example.com/resource", |
| 213 |
Name: "Example Resource", |
| 214 |
Title: "A comprehensive example resource", |
| 215 |
Description: "This resource demonstrates all fields", |
| 216 |
MIMEType: "text/plain", |
| 217 |
|
| 218 |
Size: &valInt64, |
| 219 |
Annotations: &mcp.Annotations{Audience: []mcp.Role{"user", "assistant"}, LastModified: "2025-01-12T15:00:58Z", Priority: 0.5}, |
| 220 |
Icons: []mcp.Icon{{Source: "foobar", MIMEType: "image/png", Sizes: []string{"48x48"}, Theme: mcp.IconThemeLight}}, |
| 221 |
}, |
| 222 |
}, |
| 223 |
} |
| 224 |
|
| 225 |
for _, tt := range tests { |
| 226 |
t.Run(tt.name, func(t *testing.T) { |
| 227 |
|
| 228 |
defer func() { |
| 229 |
if r := recover(); r != nil { |
| 230 |
t.Errorf("UnmarshalJSON panicked: %v", r) |
| 231 |
} |
| 232 |
}() |
| 233 |
|
| 234 |
err := json.Unmarshal([]byte(tt.json), tt.content) |
| 235 |
if err != nil { |
| 236 |
t.Errorf("UnmarshalJSON failed: %v", err) |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
if cmp.Diff(tt.expectContent, tt.content) != "" { |
| 241 |
t.Errorf("Content is not equal: %v", cmp.Diff(tt.expectContent, tt.content)) |
| 242 |
} |
| 243 |
}) |
| 244 |
} |
| 245 |
} |