server_structured_test.go

 1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
 2//
 3// SPDX-License-Identifier: LicenseRef-MutuaL-1.2
 4
 5package mcp
 6
 7import (
 8	"encoding/json"
 9	"testing"
10
11	sdk "github.com/modelcontextprotocol/go-sdk/mcp"
12)
13
14func requireTextContent(t *testing.T, result *sdk.CallToolResult) string {
15	t.Helper()
16
17	if len(result.Content) != 1 {
18		t.Fatalf("text content length = %d, want 1", len(result.Content))
19	}
20	textContent, ok := result.Content[0].(*sdk.TextContent)
21	if !ok {
22		t.Fatalf("content type = %T, want *sdk.TextContent", result.Content[0])
23	}
24
25	return textContent.Text
26}
27
28func requireStructuredContent(t *testing.T, result *sdk.CallToolResult, want any) {
29	t.Helper()
30	// tools.RESPONSES.5
31
32	if result == nil {
33		t.Fatal("result is nil")
34	}
35	if result.StructuredContent == nil {
36		t.Fatal("structured content is nil")
37	}
38
39	gotJSON, err := json.Marshal(result.StructuredContent)
40	if err != nil {
41		t.Fatalf("marshal structured content: %v", err)
42	}
43	wantJSON, err := json.Marshal(want)
44	if err != nil {
45		t.Fatalf("marshal wanted structured content: %v", err)
46	}
47	if string(gotJSON) != string(wantJSON) {
48		t.Fatalf("structured content = %s, want %s", gotJSON, wantJSON)
49	}
50
51	var object map[string]any
52	if err := json.Unmarshal(gotJSON, &object); err != nil {
53		t.Fatalf("structured content is not a JSON object: %v", err)
54	}
55}