From f41a08b9b6239c737d9cac0404f8c25549567153 Mon Sep 17 00:00:00 2001 From: Markus Kusano Date: Tue, 2 Dec 2025 20:15:04 +0000 Subject: [PATCH 1/2] mcp/mcp_test: tests for elicit content validation This adds a simple schema and checks that the content returned by an elicitation handler is validated against the schema. Previously, we had no test coverage checking if the content was validated (i.e., we would see no test failures if we removed content validation against the schema). The bulk of the actual validation logic is handled by the jsonschema library so adding comprehensive tests covering different schema-features is redundant, we simply need to see that some validation is happening. See https://github.com/modelcontextprotocol/go-sdk/issues/625 for more details on the motivation of this change. --- mcp/mcp_test.go | 123 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/mcp/mcp_test.go b/mcp/mcp_test.go index 3edfefd7..2bc327df 100644 --- a/mcp/mcp_test.go +++ b/mcp/mcp_test.go @@ -997,6 +997,11 @@ func TestElicitationUnsupportedMethod(t *testing.T) { } } +func anyPtr[T any](v T) *any { + var a any = v + return &a +} + func TestElicitationSchemaValidation(t *testing.T) { ctx := context.Background() ct, st := NewInMemoryTransports() @@ -1118,6 +1123,37 @@ func TestElicitationSchemaValidation(t *testing.T) { }, }, }, + { + name: "enum with enum schema", + schema: &jsonschema.Schema{ + Type: "object", + Properties: map[string]*jsonschema.Schema{ + "priority": { + Type: "string", + OneOf: []*jsonschema.Schema{ + { + Const: anyPtr(map[string]string{ + "const": "high", + "title": "High Priority", + }), + }, + { + Const: anyPtr(map[string]string{ + "const": "medium", + "title": "Medium Priority", + }), + }, + { + Const: anyPtr(map[string]string{ + "const": "low", + "title": "Low Priority", + }), + }, + }, + }, + }, + }, + }, } for _, tc := range validSchemas { @@ -1377,6 +1413,93 @@ func TestElicitationSchemaValidation(t *testing.T) { } } +func TestElicitContentValidation(t *testing.T) { + ctx := context.Background() + ct, st := NewInMemoryTransports() + + s := NewServer(testImpl, nil) + ss, err := s.Connect(ctx, st, nil) + if err != nil { + t.Fatal(err) + } + defer ss.Close() + + c := NewClient(testImpl, &ClientOptions{ + ElicitationHandler: func(context.Context, *ElicitRequest) (*ElicitResult, error) { + return &ElicitResult{Action: "accept", Content: map[string]any{"test": "potato"}}, nil + }, + }) + cs, err := c.Connect(ctx, ct, nil) + if err != nil { + t.Fatal(err) + } + defer cs.Close() + + testcases := []struct { + name string + schema *jsonschema.Schema + expectedError string + }{ + { + name: "string enum with schema not matching content", + schema: &jsonschema.Schema{ + Type: "object", + Properties: map[string]*jsonschema.Schema{ + "test": { + Type: "string", + OneOf: []*jsonschema.Schema{ + { + Const: anyPtr(map[string]string{ + "const": "high", + "title": "High Priority", + }), + }, + }, + }, + }, + }, + expectedError: "oneOf: did not validate against any of", + }, + { + name: "string enum with schema matching content", + schema: &jsonschema.Schema{ + Type: "object", + Properties: map[string]*jsonschema.Schema{ + "test": { + Type: "string", + OneOf: []*jsonschema.Schema{ + { + Const: anyPtr(map[string]string{ + "const": "potato", + "title": "Potato Priority", + }), + }, + }, + }, + }, + }, + expectedError: "", + }, + } + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + _, err := ss.Elicit(ctx, &ElicitParams{ + Message: "Test schema: " + tc.name, + RequestedSchema: tc.schema, + }) + if tc.expectedError != "" { + if err == nil { + t.Errorf("expected error but got no error: %s", tc.expectedError) + return + } + if !strings.Contains(err.Error(), tc.expectedError) { + t.Errorf("error message %q does not contain expected text %q", err.Error(), tc.expectedError) + } + } + }) + } +} + func TestElicitationProgressToken(t *testing.T) { ctx := context.Background() ct, st := NewInMemoryTransports() From 3f81c555fef830556d0f1f2d58c9f56813bcc4a3 Mon Sep 17 00:00:00 2001 From: Markus Kusano Date: Wed, 3 Dec 2025 15:04:50 +0000 Subject: [PATCH 2/2] mcp/mcp_test: add comment validation tests --- mcp/mcp_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mcp/mcp_test.go b/mcp/mcp_test.go index 2bc327df..8c40de08 100644 --- a/mcp/mcp_test.go +++ b/mcp/mcp_test.go @@ -1424,6 +1424,9 @@ func TestElicitContentValidation(t *testing.T) { } defer ss.Close() + // Set up a client that exercises valid/invalid elicitation: the returned + // Content from the handler ("potato") is validated against the schemas + // defined in the testcases below. c := NewClient(testImpl, &ClientOptions{ ElicitationHandler: func(context.Context, *ElicitRequest) (*ElicitResult, error) { return &ElicitResult{Action: "accept", Content: map[string]any{"test": "potato"}}, nil