|
1 | 1 | package mcp |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | 4 | "net/http" |
6 | | - "net/http/httptest" |
7 | 5 | "sync" |
8 | 6 | "testing" |
9 | 7 |
|
| 8 | + "github.com/BurntSushi/toml" |
10 | 9 | "github.com/containers/kubernetes-mcp-server/internal/test" |
11 | | - "github.com/mark3labs/mcp-go/client" |
12 | 10 | "github.com/mark3labs/mcp-go/client/transport" |
13 | 11 | "github.com/stretchr/testify/suite" |
14 | 12 | ) |
@@ -103,66 +101,25 @@ func TestMcpHeaders(t *testing.T) { |
103 | 101 |
|
104 | 102 | type ServerInstructionsSuite struct { |
105 | 103 | BaseMcpSuite |
106 | | - mockServer *test.MockServer |
107 | 104 | } |
108 | 105 |
|
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() { |
| 106 | +func (s *ServerInstructionsSuite) TestServerInstructionsEmpty() { |
| 107 | + s.InitMcpClient() |
124 | 108 | 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") |
| 109 | + s.Require().NotNil(s.InitializeResult) |
| 110 | + s.Empty(s.InitializeResult.Instructions, "instructions should be empty when not configured") |
143 | 111 | }) |
| 112 | +} |
144 | 113 |
|
| 114 | +func (s *ServerInstructionsSuite) TestServerInstructionsFromConfiguration() { |
| 115 | + s.Require().NoError(toml.Unmarshal([]byte(` |
| 116 | + server_instructions = "Always use YAML output format for kubectl commands." |
| 117 | + `), s.Cfg), "Expected to parse server instructions config") |
| 118 | + s.InitMcpClient() |
145 | 119 | 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") |
| 120 | + s.Require().NotNil(s.InitializeResult) |
| 121 | + s.Equal("Always use YAML output format for kubectl commands.", s.InitializeResult.Instructions, |
| 122 | + "instructions should match configured value") |
166 | 123 | }) |
167 | 124 | } |
168 | 125 |
|
|
0 commit comments