|
| 1 | +// Copyright 2025 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package mcp_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "iter" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/google/go-cmp/cmp" |
| 13 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 14 | + "golang.org/x/tools/internal/mcp" |
| 15 | + "golang.org/x/tools/internal/mcp/jsonschema" |
| 16 | +) |
| 17 | + |
| 18 | +func TestListTools(t *testing.T) { |
| 19 | + toolA := mcp.NewTool("apple", "apple tool", SayHi) |
| 20 | + toolB := mcp.NewTool("banana", "banana tool", SayHi) |
| 21 | + toolC := mcp.NewTool("cherry", "cherry tool", SayHi) |
| 22 | + tools := []*mcp.ServerTool{toolA, toolB, toolC} |
| 23 | + |
| 24 | + wantTools := []*mcp.Tool{toolA.Tool, toolB.Tool, toolC.Tool} |
| 25 | + ctx := context.Background() |
| 26 | + clientSession, serverSession, server := createSessions(ctx) |
| 27 | + defer clientSession.Close() |
| 28 | + defer serverSession.Close() |
| 29 | + server.AddTools(tools...) |
| 30 | + t.Run("ListTools", func(t *testing.T) { |
| 31 | + res, err := clientSession.ListTools(ctx, nil) |
| 32 | + if err != nil { |
| 33 | + t.Fatal("ListTools() failed:", err) |
| 34 | + } |
| 35 | + if diff := cmp.Diff(wantTools, res.Tools, cmpopts.IgnoreUnexported(jsonschema.Schema{})); diff != "" { |
| 36 | + t.Fatalf("ListTools() mismatch (-want +got):\n%s", diff) |
| 37 | + } |
| 38 | + }) |
| 39 | + t.Run("ToolsIterator", func(t *testing.T) { |
| 40 | + testIterator(ctx, t, clientSession.Tools(ctx, nil), wantTools) |
| 41 | + }) |
| 42 | +} |
| 43 | + |
| 44 | +func TestListResources(t *testing.T) { |
| 45 | + resourceA := &mcp.ServerResource{Resource: &mcp.Resource{URI: "http://apple"}} |
| 46 | + resourceB := &mcp.ServerResource{Resource: &mcp.Resource{URI: "http://banana"}} |
| 47 | + resourceC := &mcp.ServerResource{Resource: &mcp.Resource{URI: "http://cherry"}} |
| 48 | + wantResources := []*mcp.Resource{resourceA.Resource, resourceB.Resource, resourceC.Resource} |
| 49 | + |
| 50 | + resources := []*mcp.ServerResource{resourceA, resourceB, resourceC} |
| 51 | + ctx := context.Background() |
| 52 | + clientSession, serverSession, server := createSessions(ctx) |
| 53 | + defer clientSession.Close() |
| 54 | + defer serverSession.Close() |
| 55 | + server.AddResources(resources...) |
| 56 | + t.Run("ListResources", func(t *testing.T) { |
| 57 | + res, err := clientSession.ListResources(ctx, nil) |
| 58 | + if err != nil { |
| 59 | + t.Fatal("ListResources() failed:", err) |
| 60 | + } |
| 61 | + if diff := cmp.Diff(wantResources, res.Resources, cmpopts.IgnoreUnexported(jsonschema.Schema{})); diff != "" { |
| 62 | + t.Fatalf("ListResources() mismatch (-want +got):\n%s", diff) |
| 63 | + } |
| 64 | + }) |
| 65 | + t.Run("ResourcesIterator", func(t *testing.T) { |
| 66 | + testIterator(ctx, t, clientSession.Resources(ctx, nil), wantResources) |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +func TestListResourceTemplates(t *testing.T) { |
| 71 | + resourceTmplA := &mcp.ServerResourceTemplate{ResourceTemplate: &mcp.ResourceTemplate{URITemplate: "http://apple/{x}"}} |
| 72 | + resourceTmplB := &mcp.ServerResourceTemplate{ResourceTemplate: &mcp.ResourceTemplate{URITemplate: "http://banana/{x}"}} |
| 73 | + resourceTmplC := &mcp.ServerResourceTemplate{ResourceTemplate: &mcp.ResourceTemplate{URITemplate: "http://cherry/{x}"}} |
| 74 | + wantResourceTemplates := []*mcp.ResourceTemplate{ |
| 75 | + resourceTmplA.ResourceTemplate, resourceTmplB.ResourceTemplate, |
| 76 | + resourceTmplC.ResourceTemplate, |
| 77 | + } |
| 78 | + resourceTemplates := []*mcp.ServerResourceTemplate{resourceTmplA, resourceTmplB, resourceTmplC} |
| 79 | + ctx := context.Background() |
| 80 | + clientSession, serverSession, server := createSessions(ctx) |
| 81 | + defer clientSession.Close() |
| 82 | + defer serverSession.Close() |
| 83 | + server.AddResourceTemplates(resourceTemplates...) |
| 84 | + t.Run("ListResourceTemplates", func(t *testing.T) { |
| 85 | + res, err := clientSession.ListResourceTemplates(ctx, nil) |
| 86 | + if err != nil { |
| 87 | + t.Fatal("ListResourceTemplates() failed:", err) |
| 88 | + } |
| 89 | + if diff := cmp.Diff(wantResourceTemplates, res.ResourceTemplates, cmpopts.IgnoreUnexported(jsonschema.Schema{})); diff != "" { |
| 90 | + t.Fatalf("ListResourceTemplates() mismatch (-want +got):\n%s", diff) |
| 91 | + } |
| 92 | + }) |
| 93 | + t.Run("ResourceTemplatesIterator", func(t *testing.T) { |
| 94 | + testIterator(ctx, t, clientSession.ResourceTemplates(ctx, nil), wantResourceTemplates) |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +func TestListPrompts(t *testing.T) { |
| 99 | + promptA := mcp.NewPrompt("apple", "apple prompt", testPromptHandler[struct{}]) |
| 100 | + promptB := mcp.NewPrompt("banana", "banana prompt", testPromptHandler[struct{}]) |
| 101 | + promptC := mcp.NewPrompt("cherry", "cherry prompt", testPromptHandler[struct{}]) |
| 102 | + wantPrompts := []*mcp.Prompt{promptA.Prompt, promptB.Prompt, promptC.Prompt} |
| 103 | + |
| 104 | + prompts := []*mcp.ServerPrompt{promptA, promptB, promptC} |
| 105 | + ctx := context.Background() |
| 106 | + clientSession, serverSession, server := createSessions(ctx) |
| 107 | + defer clientSession.Close() |
| 108 | + defer serverSession.Close() |
| 109 | + server.AddPrompts(prompts...) |
| 110 | + t.Run("ListPrompts", func(t *testing.T) { |
| 111 | + res, err := clientSession.ListPrompts(ctx, nil) |
| 112 | + if err != nil { |
| 113 | + t.Fatal("ListPrompts() failed:", err) |
| 114 | + } |
| 115 | + if diff := cmp.Diff(wantPrompts, res.Prompts, cmpopts.IgnoreUnexported(jsonschema.Schema{})); diff != "" { |
| 116 | + t.Fatalf("ListPrompts() mismatch (-want +got):\n%s", diff) |
| 117 | + } |
| 118 | + }) |
| 119 | + t.Run("PromptsIterator", func(t *testing.T) { |
| 120 | + testIterator(ctx, t, clientSession.Prompts(ctx, nil), wantPrompts) |
| 121 | + }) |
| 122 | +} |
| 123 | + |
| 124 | +func testIterator[T any](ctx context.Context, t *testing.T, seq iter.Seq2[*T, error], want []*T) { |
| 125 | + t.Helper() |
| 126 | + var got []*T |
| 127 | + for x, err := range seq { |
| 128 | + if err != nil { |
| 129 | + t.Fatalf("iteration failed: %v", err) |
| 130 | + } |
| 131 | + got = append(got, x) |
| 132 | + } |
| 133 | + if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(jsonschema.Schema{})); diff != "" { |
| 134 | + t.Fatalf("mismatch (-want +got):\n%s", diff) |
| 135 | + } |
| 136 | +} |
0 commit comments