|
1 | 1 | package mcp |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "net/http" |
| 6 | + "net/http/httptest" |
5 | 7 | "sync" |
6 | 8 | "testing" |
7 | 9 |
|
8 | 10 | "github.com/containers/kubernetes-mcp-server/internal/test" |
| 11 | + "github.com/mark3labs/mcp-go/client" |
9 | 12 | "github.com/mark3labs/mcp-go/client/transport" |
10 | 13 | "github.com/stretchr/testify/suite" |
11 | 14 | ) |
@@ -97,3 +100,72 @@ func (s *McpHeadersSuite) TestAuthorizationHeaderPropagation() { |
97 | 100 | func TestMcpHeaders(t *testing.T) { |
98 | 101 | suite.Run(t, new(McpHeadersSuite)) |
99 | 102 | } |
| 103 | + |
| 104 | +type ServerInstructionsSuite struct { |
| 105 | + BaseMcpSuite |
| 106 | + mockServer *test.MockServer |
| 107 | +} |
| 108 | + |
| 109 | +func (s *ServerInstructionsSuite) SetupTest() { |
| 110 | + s.BaseMcpSuite.SetupTest() |
| 111 | + s.mockServer = test.NewMockServer() |
| 112 | + s.mockServer.Handle(&test.DiscoveryClientHandler{}) |
| 113 | + s.Cfg.KubeConfig = s.mockServer.KubeconfigFile(s.T()) |
| 114 | +} |
| 115 | + |
| 116 | +func (s *ServerInstructionsSuite) TearDownTest() { |
| 117 | + s.BaseMcpSuite.TearDownTest() |
| 118 | + if s.mockServer != nil { |
| 119 | + s.mockServer.Close() |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func (s *ServerInstructionsSuite) TestServerInstructions() { |
| 124 | + s.Run("returns empty instructions when not configured", func() { |
| 125 | + s.Cfg.ServerInstructions = "" |
| 126 | + mcpServer, err := NewServer(Configuration{StaticConfig: s.Cfg}) |
| 127 | + s.Require().NoError(err) |
| 128 | + s.T().Cleanup(mcpServer.Close) |
| 129 | + |
| 130 | + testServer := httptest.NewServer(mcpServer.ServeHTTP()) |
| 131 | + s.T().Cleanup(testServer.Close) |
| 132 | + |
| 133 | + mcpClient, err := client.NewStreamableHttpClient(testServer.URL+"/mcp", transport.WithContinuousListening()) |
| 134 | + s.Require().NoError(err) |
| 135 | + s.T().Cleanup(func() { _ = mcpClient.Close() }) |
| 136 | + |
| 137 | + err = mcpClient.Start(context.Background()) |
| 138 | + s.Require().NoError(err) |
| 139 | + |
| 140 | + result, err := mcpClient.Initialize(context.Background(), test.McpInitRequest()) |
| 141 | + s.Require().NoError(err) |
| 142 | + s.Empty(result.Instructions, "instructions should be empty when not configured") |
| 143 | + }) |
| 144 | + |
| 145 | + s.Run("returns configured instructions", func() { |
| 146 | + expectedInstructions := "Always use YAML output format for kubectl commands." |
| 147 | + s.Cfg.ServerInstructions = expectedInstructions |
| 148 | + |
| 149 | + mcpServer, err := NewServer(Configuration{StaticConfig: s.Cfg}) |
| 150 | + s.Require().NoError(err) |
| 151 | + s.T().Cleanup(mcpServer.Close) |
| 152 | + |
| 153 | + testServer := httptest.NewServer(mcpServer.ServeHTTP()) |
| 154 | + s.T().Cleanup(testServer.Close) |
| 155 | + |
| 156 | + mcpClient, err := client.NewStreamableHttpClient(testServer.URL+"/mcp", transport.WithContinuousListening()) |
| 157 | + s.Require().NoError(err) |
| 158 | + s.T().Cleanup(func() { _ = mcpClient.Close() }) |
| 159 | + |
| 160 | + err = mcpClient.Start(context.Background()) |
| 161 | + s.Require().NoError(err) |
| 162 | + |
| 163 | + result, err := mcpClient.Initialize(context.Background(), test.McpInitRequest()) |
| 164 | + s.Require().NoError(err) |
| 165 | + s.Equal(expectedInstructions, result.Instructions, "instructions should match configured value") |
| 166 | + }) |
| 167 | +} |
| 168 | + |
| 169 | +func TestServerInstructions(t *testing.T) { |
| 170 | + suite.Run(t, new(ServerInstructionsSuite)) |
| 171 | +} |
0 commit comments