Skip to content

Commit a965357

Browse files
authored
test(mcp): store initialization results in McpClient for better instruction handling (#582)
Signed-off-by: Marc Nuri <[email protected]>
1 parent 3959fd5 commit a965357

File tree

2 files changed

+16
-58
lines changed

2 files changed

+16
-58
lines changed

internal/test/mcp.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type McpClient struct {
2525
ctx context.Context
2626
testServer *httptest.Server
2727
*client.Client
28+
InitializeResult *mcp.InitializeResult
2829
}
2930

3031
func NewMcpClient(t *testing.T, mcpHttpServer http.Handler, options ...transport.StreamableHTTPCOption) *McpClient {
@@ -37,7 +38,7 @@ func NewMcpClient(t *testing.T, mcpHttpServer http.Handler, options ...transport
3738
require.NoError(t, err, "Expected no error creating MCP client")
3839
err = ret.Start(t.Context())
3940
require.NoError(t, err, "Expected no error starting MCP client")
40-
_, err = ret.Initialize(t.Context(), McpInitRequest())
41+
ret.InitializeResult, err = ret.Initialize(t.Context(), McpInitRequest())
4142
require.NoError(t, err, "Expected no error initializing MCP client")
4243
return ret
4344
}

pkg/mcp/mcp_test.go

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package mcp
22

33
import (
4-
"context"
54
"net/http"
6-
"net/http/httptest"
75
"sync"
86
"testing"
97

8+
"github.com/BurntSushi/toml"
109
"github.com/containers/kubernetes-mcp-server/internal/test"
11-
"github.com/mark3labs/mcp-go/client"
1210
"github.com/mark3labs/mcp-go/client/transport"
1311
"github.com/stretchr/testify/suite"
1412
)
@@ -103,66 +101,25 @@ func TestMcpHeaders(t *testing.T) {
103101

104102
type ServerInstructionsSuite struct {
105103
BaseMcpSuite
106-
mockServer *test.MockServer
107104
}
108105

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()
124108
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")
143111
})
112+
}
144113

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()
145119
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")
166123
})
167124
}
168125

0 commit comments

Comments
 (0)