|
| 1 | +// Copyright 2025 The Go MCP SDK Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by an MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package mcp_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/google/go-cmp/cmp" |
| 12 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 13 | +) |
| 14 | + |
| 15 | +func TestReference(t *testing.T) { |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + in mcp.Reference |
| 19 | + want string |
| 20 | + wantErr bool |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "PromptReference", |
| 24 | + in: mcp.Reference{Type: "ref/prompt", Name: "my_prompt"}, |
| 25 | + want: `{"type":"ref/prompt","name":"my_prompt"}`, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "ResourceReference", |
| 29 | + in: mcp.Reference{Type: "ref/resource", URI: "file:///path/to/resource.txt"}, |
| 30 | + want: `{"type":"ref/resource","uri":"file:///path/to/resource.txt"}`, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "PromptReference with empty name", |
| 34 | + in: mcp.Reference{Type: "ref/prompt", Name: ""}, |
| 35 | + want: `{"type":"ref/prompt"}`, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "ResourceReference with empty URI", |
| 39 | + in: mcp.Reference{Type: "ref/resource", URI: ""}, |
| 40 | + want: `{"type":"ref/resource"}`, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "Unrecognized Type", |
| 44 | + in: mcp.Reference{Type: "ref/unknown", Name: "something"}, |
| 45 | + want: `{"type":"ref/unknown","name":"something"}`, |
| 46 | + wantErr: true, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "Missing Type Field", |
| 50 | + in: mcp.Reference{Name: "missing"}, |
| 51 | + want: `{"type":"","name":"missing"}`, |
| 52 | + wantErr: true, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "Invalid JSON Format", |
| 56 | + in: mcp.Reference{}, |
| 57 | + want: `{"type":""}`, |
| 58 | + wantErr: true, |
| 59 | + }, |
| 60 | + } |
| 61 | + for _, test := range tests { |
| 62 | + t.Run(test.name, func(t *testing.T) { |
| 63 | + // Test Marshal |
| 64 | + got, err := json.Marshal(test.in) |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("json.Marshal(%v) failed: %v", test.in, err) |
| 67 | + } |
| 68 | + if diff := cmp.Diff(test.want, string(got)); diff != "" { |
| 69 | + t.Errorf("json.Marshal(%v) mismatch (-want +got):\n%s", test.in, diff) |
| 70 | + } |
| 71 | + |
| 72 | + // Test Unmarshal |
| 73 | + var unmarshaled mcp.Reference |
| 74 | + err = json.Unmarshal([]byte(test.want), &unmarshaled) |
| 75 | + |
| 76 | + if test.wantErr { |
| 77 | + if err == nil { |
| 78 | + t.Fatalf("json.Unmarshal(%q) should have failed", test.want) |
| 79 | + } |
| 80 | + return |
| 81 | + } |
| 82 | + if err != nil { |
| 83 | + t.Fatalf("json.Unmarshal(%q) failed: %v", test.want, err) |
| 84 | + } |
| 85 | + if diff := cmp.Diff(test.in, unmarshaled); diff != "" { |
| 86 | + t.Errorf("json.Unmarshal(%q) mismatch (-want +got):\n%s", test.want, diff) |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestCompleteParams(t *testing.T) { |
| 93 | + // Test CompleteParams |
| 94 | + params := mcp.CompleteParams{ |
| 95 | + Ref: &mcp.Reference{ |
| 96 | + Type: "ref/prompt", |
| 97 | + Name: "my_prompt", |
| 98 | + }, |
| 99 | + Argument: mcp.CompleteParamsArgument{ |
| 100 | + Name: "language", |
| 101 | + Value: "go", |
| 102 | + }, |
| 103 | + } |
| 104 | + wantParamsJSON := `{"argument":{"name":"language","value":"go"},"ref":{"type":"ref/prompt","name":"my_prompt"}}` |
| 105 | + |
| 106 | + gotParamsJSON, err := json.Marshal(params) |
| 107 | + if err != nil { |
| 108 | + t.Fatalf("json.Marshal(CompleteParams) failed: %v", err) |
| 109 | + } |
| 110 | + if diff := cmp.Diff(wantParamsJSON, string(gotParamsJSON)); diff != "" { |
| 111 | + t.Errorf("CompleteParams marshal mismatch (-want +got):\n%s", diff) |
| 112 | + } |
| 113 | + |
| 114 | + var unmarshaledParams mcp.CompleteParams |
| 115 | + if err := json.Unmarshal([]byte(wantParamsJSON), &unmarshaledParams); err != nil { |
| 116 | + t.Fatalf("json.Unmarshal(CompleteParams) failed: %v", err) |
| 117 | + } |
| 118 | + if diff := cmp.Diff(params, unmarshaledParams); diff != "" { |
| 119 | + t.Errorf("CompleteParams unmarshal mismatch (-want +got):\n%s", diff) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestCompleteResult(t *testing.T) { |
| 124 | + res := mcp.CompleteResult{ |
| 125 | + Completion: mcp.CompletionResultDetails{ |
| 126 | + Values: []string{"golang", "google", "goroutine"}, |
| 127 | + Total: 10, |
| 128 | + HasMore: true, |
| 129 | + }, |
| 130 | + } |
| 131 | + wantResJSON := `{"completion":{"hasMore":true,"total":10,"values":["golang","google","goroutine"]}}` |
| 132 | + gotResJSON, err := json.Marshal(res) |
| 133 | + if err != nil { |
| 134 | + t.Fatalf("json.Marshal(CompleteResult) failed: %v", err) |
| 135 | + } |
| 136 | + if diff := cmp.Diff(wantResJSON, string(gotResJSON)); diff != "" { |
| 137 | + t.Errorf("CompleteResult marshal mismatch (-want +got):\n%s", diff) |
| 138 | + } |
| 139 | + |
| 140 | + var unmarshaledRes mcp.CompleteResult |
| 141 | + if err := json.Unmarshal([]byte(wantResJSON), &unmarshaledRes); err != nil { |
| 142 | + t.Fatalf("json.Unmarshal(CompleteResult) failed: %v", err) |
| 143 | + } |
| 144 | + if diff := cmp.Diff(res, unmarshaledRes); diff != "" { |
| 145 | + t.Errorf("CompleteResult unmarshal mismatch (-want +got):\n%s", diff) |
| 146 | + } |
| 147 | +} |
0 commit comments