Skip to content

Commit c49488d

Browse files
urisimchoniuriv58
andauthored
Set test client info (#692)
* allow test to set client info This allows testing of mcp server behavior that depends on the type of the mcp client. * test allowing test to set client info * CR comment check Start() return code and use ctx consistently --------- Co-authored-by: Uri Simchoni <uri@vfunction.com>
1 parent edc91f5 commit c49488d

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

mcptest/mcptest.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Server struct {
2424
prompts []server.ServerPrompt
2525
resources []server.ServerResource
2626
resourceTemplates []server.ServerResourceTemplate
27+
clientInfo mcp.Implementation
2728

2829
cancel func()
2930

@@ -120,6 +121,11 @@ func (s *Server) AddResourceTemplates(templates ...server.ServerResourceTemplate
120121
s.resourceTemplates = append(s.resourceTemplates, templates...)
121122
}
122123

124+
// SetClientInfo sets the client info for the test client.
125+
func (s *Server) SetClientInfo(info mcp.Implementation) {
126+
s.clientInfo = info
127+
}
128+
123129
// Start starts the server in a goroutine. Make sure to defer Close() after Start().
124130
// When using NewServer(), the returned server is already started.
125131
func (s *Server) Start(ctx context.Context) error {
@@ -157,6 +163,7 @@ func (s *Server) Start(ctx context.Context) error {
157163

158164
var initReq mcp.InitializeRequest
159165
initReq.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
166+
initReq.Params.ClientInfo = s.clientInfo
160167
if _, err := s.client.Initialize(ctx, initReq); err != nil {
161168
return fmt.Errorf("client.Initialize(): %w", err)
162169
}

mcptest/mcptest_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,47 @@ func TestListToolsWithHeader(t *testing.T) {
411411
t.Fatalf("Expected value is %s, got %s", expectedHeaderValue, gotHeaderValue)
412412
}
413413
}
414+
415+
func TestSimulateClientInfo(t *testing.T) {
416+
ctx := context.Background()
417+
418+
srv := mcptest.NewUnstartedServer(t)
419+
defer srv.Close()
420+
srv.AddTool(mcp.NewTool("whoami", mcp.WithDescription("Says hello to client.")),
421+
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
422+
clientName := ""
423+
if clientSession := server.ClientSessionFromContext(ctx); clientSession != nil {
424+
if sessionWithClientInfo, ok := clientSession.(server.SessionWithClientInfo); ok {
425+
clientName = sessionWithClientInfo.GetClientInfo().Name
426+
}
427+
}
428+
return mcp.NewToolResultText(fmt.Sprintf("Hello, %s!", clientName)), nil
429+
})
430+
srv.SetClientInfo(mcp.Implementation{
431+
Name: "test-client",
432+
})
433+
err := srv.Start(ctx)
434+
if err != nil {
435+
t.Fatal("Start:", err)
436+
}
437+
438+
client := srv.Client()
439+
440+
var req mcp.CallToolRequest
441+
req.Params.Name = "whoami"
442+
443+
result, err := client.CallTool(ctx, req)
444+
if err != nil {
445+
t.Fatal("CallTool:", err)
446+
}
447+
448+
got, err := resultToString(result)
449+
if err != nil {
450+
t.Fatal(err)
451+
}
452+
453+
want := "Hello, test-client!"
454+
if got != want {
455+
t.Errorf("Got %q, want %q", got, want)
456+
}
457+
}

0 commit comments

Comments
 (0)