-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmcp_content_test.go
More file actions
75 lines (64 loc) · 2.65 KB
/
Copy pathmcp_content_test.go
File metadata and controls
75 lines (64 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package cogito
import (
"strings"
"testing"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
// TestContentToStringImageDoesNotPanic pins the fix for the crash reported in
// mudler/LocalAI#10101: an MCP tool that returns a non-text content block (e.g.
// an image from osmmcp's get_map_image) used to make contentToString panic with
// "interface conversion: mcp.Content is *mcp.ImageContent, not *mcp.TextContent",
// taking the whole process down. Mixed text + image content must be handled
// gracefully instead.
func TestContentToStringImageDoesNotPanic(t *testing.T) {
content := []mcp.Content{
&mcp.TextContent{Text: "here is your map: "},
&mcp.ImageContent{MIMEType: "image/png", Data: []byte("\x89PNGfakebytes")},
}
result := contentToString(content)
// The text block must still be preserved verbatim.
if !strings.Contains(result, "here is your map: ") {
t.Fatalf("expected text content to be preserved, got %q", result)
}
// The image block must contribute a descriptive, non-empty marker rather
// than panicking or being silently lost, so the model knows an image came back.
if !strings.Contains(result, "image/png") {
t.Fatalf("expected image mime type to surface in result, got %q", result)
}
}
// TestContentToStringTextOnly pins the original behavior: plain text blocks are
// concatenated verbatim with no extra decoration.
func TestContentToStringTextOnly(t *testing.T) {
content := []mcp.Content{
&mcp.TextContent{Text: "hello "},
&mcp.TextContent{Text: "world"},
}
if got := contentToString(content); got != "hello world" {
t.Fatalf("expected %q, got %q", "hello world", got)
}
}
// TestContentToStringEmbeddedResourceText surfaces the text of an embedded
// resource so it remains usable by the model.
func TestContentToStringEmbeddedResourceText(t *testing.T) {
content := []mcp.Content{
&mcp.EmbeddedResource{Resource: &mcp.ResourceContents{URI: "file://x", Text: "embedded body"}},
}
if got := contentToString(content); !strings.Contains(got, "embedded body") {
t.Fatalf("expected embedded resource text in result, got %q", got)
}
}
// TestContentToStringAudioAndResourceLink ensures the remaining non-text content
// variants are summarized rather than panicking.
func TestContentToStringAudioAndResourceLink(t *testing.T) {
content := []mcp.Content{
&mcp.AudioContent{MIMEType: "audio/wav", Data: []byte("RIFFfake")},
&mcp.ResourceLink{URI: "https://example.com/r"},
}
got := contentToString(content)
if !strings.Contains(got, "audio/wav") {
t.Fatalf("expected audio mime type in result, got %q", got)
}
if !strings.Contains(got, "https://example.com/r") {
t.Fatalf("expected resource link URI in result, got %q", got)
}
}