Skip to content

Enhancement: Add validation for tool schema generation at registration time #731

@andylbrummer

Description

@andylbrummer

Enhancement: Add validation for tool schema generation at registration time

Summary

The MCP Go SDK should validate that tool input/output struct fields have sufficient type information for JSON schema generation when tools are registered, rather than failing silently until MCP Inspector validates the schemas.

Problem

Currently, when using mcp.AddTool() with struct fields that have ambiguous types like interface{} or map[string]interface{}, the SDK generates invalid JSON schemas but doesn't report any errors. The issue only becomes apparent when:

  1. An MCP Inspector validates the tool schemas
  2. A client attempts to use the tool and gets validation errors

This creates a poor developer experience with confusing, delayed error messages.

Current Behavior

// This compiles and registers successfully, but generates invalid schema
type SnapshotOutput struct {
	Success bool        `json:"success"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`  // No error!
}

mcp.AddTool(server, &mcp.Tool{
	Name: "snapshot",
	Description: "...",
}, handler) // No error at registration

When validated by MCP Inspector:

{
  "error": "[{\"code\":\"custom\",\"path\":[\"tools\",8,\"outputSchema\",\"properties\",\"data\"],\"message\":\"Invalid input\"}]"
}

Expected Behavior

The SDK should validate schemas during mcp.AddTool() and fail fast with a clear error:

panic: Tool 'snapshot' registration failed: output schema validation error
  Field 'Data' has type 'interface{}' which cannot be represented in JSON Schema

  Solution: Add a jsonschema tag to provide type hints:
    Data interface{} `json:"data,omitempty" jsonschema:"oneof_type=object;array"`

  Or use a concrete type instead of interface{}

Suggested Implementation

Add schema validation in the tool registration process:

  1. When mcp.AddTool() is called, validate the generated JSON schema
  2. Check for problematic types:
    • interface{} without jsonschema tags
    • map[string]interface{} without jsonschema tags
    • Other types that produce ambiguous schemas
  3. Provide actionable error messages with suggested fixes

Example Fix

// Valid: Provides schema hints
type SnapshotOutput struct {
	Success bool        `json:"success"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty" jsonschema:"oneof_type=object;array"`
}

// Also valid: Uses concrete type
type LogEntryOutput struct {
	Type      string                 `json:"type"`
	Timestamp time.Time              `json:"timestamp"`
	Data      map[string]interface{} `json:"data" jsonschema:"type=object"`
}

Impact

This enhancement would:

  • Catch schema errors at compile/startup time instead of runtime
  • Provide clear, actionable error messages
  • Improve developer experience
  • Prevent invalid MCP servers from running

Related Issues

Workaround

Until this is implemented, developers must:

  1. Test with MCP Inspector after every schema change
  2. Manually add jsonschema tags to all interface{} and map[string]interface{} fields
  3. Hope runtime validation catches issues before production

Environment:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions