Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions mcp/reflection_validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package mcp

import (
"encoding/json"
"fmt"
"reflect"

"github.com/google/jsonschema-go/jsonschema"
)

// ReflectionValidator handles validation using dynamically created types.
// It uses SchemaTypeBuilder to create appropriate struct types for validation.
type ReflectionValidator struct {
builder *SchemaTypeBuilder
}

// NewReflectionValidator creates a new ReflectionValidator with a SchemaTypeBuilder.
func NewReflectionValidator() *ReflectionValidator {
return &ReflectionValidator{
builder: NewSchemaTypeBuilder(),
}
}

// SchemaValidationError represents an error that occurred during schema validation.
type SchemaValidationError struct {
Operation string // The operation that failed
Schema *jsonschema.Schema // The schema being processed
Resolved *jsonschema.Resolved // The resolved schema
Data json.RawMessage // The data being validated
Cause error // The underlying error
}

// Error returns a formatted error message.
func (e *SchemaValidationError) Error() string {
return fmt.Sprintf("schema validation failed during %s: %v", e.Operation, e.Cause)
}

// Unwrap returns the underlying error.
func (e *SchemaValidationError) Unwrap() error {
return e.Cause
}

// ValidateAndApply validates data and applies defaults using reflection.
// It creates a typed struct from the schema, unmarshals directly into it for type validation,
// then converts to map format for applying defaults and final validation.
func (v *ReflectionValidator) ValidateAndApply(data json.RawMessage, resolved *jsonschema.Resolved) (json.RawMessage, error) {
if resolved == nil {
// If no schema is provided, return data as-is
return data, nil
}

// Get the schema from the resolved schema
schema := resolved.Schema()
if schema == nil {
return nil, &SchemaValidationError{
Operation: "schema_extraction",
Resolved: resolved,
Data: data,
Cause: fmt.Errorf("resolved schema contains no schema definition"),
}
}

// Build the struct type from the schema for type validation
structType, err := v.builder.BuildType(schema)
if err != nil {
return nil, &SchemaValidationError{
Operation: "schema_conversion",
Schema: schema,
Resolved: resolved,
Data: data,
Cause: err,
}
}

var mapData map[string]any
// Handle empty data case
if len(data) == 0 {
mapData = make(map[string]any)
} else {
// First, unmarshal into a map to preserve the original structure
if err := json.Unmarshal(data, &mapData); err != nil {
return nil, &SchemaValidationError{
Operation: "unmarshaling",
Schema: schema,
Resolved: resolved,
Data: data,
Cause: fmt.Errorf("unmarshaling into map: %w", err),
}
}

// Create a new instance of the struct type for type validation
structValue := reflect.New(structType)
structPtr := structValue.Interface()

// Unmarshal directly into the typed struct for reflection-based type validation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the part I don't understand. What is the value of the struct? Wouldn't resolved.Validate(mapData) catch all the errors that this would catch?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What i'm understand is current implementation is apply to a generic map and re-marshalled

meaning that:
it not considering Go struct field tags such as ",string". As a result, a numeric default remains a JSON number, which cannot be unmarshalled into a field expecting a string-encoded number.

Can Try Below Unit Test Or Refer To Image

func TestApplySchema_DefaultTypeMismatchWithStringTag(t *testing.T) {
	schema := &jsonschema.Schema{
		Type: "object",
		Properties: map[string]*jsonschema.Schema{
			// Note: default is a JSON number (3), not a quoted string.
			"n": {Type: "integer", Default: json.RawMessage("3")},
		},
	}
	resolved, err := schema.Resolve(&jsonschema.ResolveOptions{ValidateDefaults: true})
	if err != nil {
		t.Fatalf("resolve schema: %v", err)
	}

	// No arguments provided; expect defaults to be applied.
	var input json.RawMessage = json.RawMessage(`{}`)
	input, err = applySchema(input, resolved)
	if err != nil {
		t.Fatalf("applySchema: %v", err)
	}

	// Struct expects a string-encoded number because of ",string" tag.
	type In struct {
		N int `json:"n,string"`
	}
	var got In
	// Current behavior: json.Unmarshal will fail with:
	// cannot unmarshal number into Go struct field In.n of type string
	// because the JSON contains {"n":3} after defaults, but the field expects a string.
	if err := json.Unmarshal(input, &got); err == nil {
		t.Fatalf("expected unmarshal to fail due to ,string tag with numeric default; got success with value: %+v", got)
	} else {
		// Tighten expectation to document the exact failure mode.
		if !strings.Contains(err.Error(), "cannot unmarshal number into Go struct field") {
			t.Fatalf("unexpected unmarshal error: %v", err)
		}
	}
}
image

That's why we need reflection

or can we close this PR first? let me clean up all this first into a proposal and raise an issue for this @jba

// This will fail if the types don't match (e.g., string where integer expected)
if err := json.Unmarshal(data, structPtr); err != nil {
return nil, &SchemaValidationError{
Operation: "reflection_validation",
Schema: schema,
Resolved: resolved,
Data: data,
Cause: fmt.Errorf("reflection-based type validation failed: %w", err),
}
}
}

// Apply defaults using the resolved schema
if err := resolved.ApplyDefaults(&mapData); err != nil {
return nil, &SchemaValidationError{
Operation: "applying_defaults",
Schema: schema,
Resolved: resolved,
Data: data,
Cause: fmt.Errorf("applying schema defaults: %w", err),
}
}

// Validate the data with defaults applied
if err := resolved.Validate(&mapData); err != nil {
return nil, &SchemaValidationError{
Operation: "validation",
Schema: schema,
Resolved: resolved,
Data: data,
Cause: err,
}
}

// Marshal the final result with defaults applied
result, err := json.Marshal(mapData)
if err != nil {
return nil, &SchemaValidationError{
Operation: "final_marshaling",
Schema: schema,
Resolved: resolved,
Data: data,
Cause: fmt.Errorf("marshaling final result: %w", err),
}
}

return result, nil
}
Loading