go-sdk icon indicating copy to clipboard operation
go-sdk copied to clipboard

Allow configuring additionalProperties on generated tool schemas

Open bokelley opened this issue 7 months ago • 3 comments

Problem

mcp.AddTool generates JSON schemas from Go structs via jsonschema-go, which sets additionalProperties: false on all object types. This means tool inputs that include extra fields (common in protocol envelopes) are rejected at schema validation before reaching the handler.

Use case

The Ad Context Protocol (AdCP) sends protocol-level fields like adcp_major_version alongside tool arguments. These fields are not part of any individual tool's schema but are part of the protocol envelope. The strict additionalProperties: false rejects these requests.

Current workaround

We use the low-level server.AddTool with a manually-patched schema:

schema, _ := jsonschema.ForType(reflect.TypeFor[MyInput](), &jsonschema.ForOptions{})
// Walk schema tree and set AdditionalProperties = nil on all objects
server.AddTool(&mcp.Tool{InputSchema: schema, ...}, rawHandler)

This works but loses the typed handler benefits of mcp.AddTool.

Proposed solution

Either:

  1. Add AllowAdditionalProperties bool to jsonschema.ForOptions (upstream in google/jsonschema-go)
  2. Add a SchemaOptions parameter to mcp.AddTool that allows configuring schema generation
  3. Allow overriding the generated schema on the Tool struct even when using the generic AddTool

Option 1 is the cleanest since it fixes the root cause.

Context

  • AdCP Go SDK: https://github.com/adcontextprotocol/adcp-go/pull/27
  • The workaround is in adcp/addtool.go — allowAdditionalProperties() function

bokelley avatar Apr 10 '26 10:04 bokelley

Agreed, option 1 makes the most sense. Please file a bug on the jsonschema repo.

jba avatar Apr 10 '26 12:04 jba

Filed upstream: https://github.com/google/jsonschema-go/issues/71

bokelley avatar Apr 15 '26 20:04 bokelley

I just encountered this developing an MCP server too. The agentic harness includes some extra metadata fields and it caused a validation error.

This is our workaround, which allows smooth (temporary) migration from mcp.AddTool (same function prototype):

// AddToolFlexible behaves exactly like [mcp.AddTool], but generates a JSON
// schema that allows additional unknown input properties.
//
// If we don't do this, and the caller adds an extra field we haven't declared
// in our own struct, the tool call fails before it reaches our code.
//
// TODO(aktau): Remove once
// https://github.com/modelcontextprotocol/go-sdk/issues/892 is resolved.
func AddToolFlexible[In, Out any](s *mcp.Server, t *mcp.Tool, h mcp.ToolHandlerFor[In, Out]) {
	if t.InputSchema != nil {
		panic("AddToolFlexible: InputSchema must not be set")
	}

	// 1. Generate the schema for In.
	schema, err := jsonschema.ForType(reflect.TypeFor[In](), &jsonschema.ForOptions{})
	if err != nil {
		panic(fmt.Sprintf("AddToolFlexible: failed to generate schema for %T: %v", *new(In), err))
	}

	// 2. Strip additionalProperties constraints recursively
	allowAdditionalProperties(schema)

	// 3. Assign to InputSchema. The SDK will reuse this pre-populated schema.
	t.InputSchema = schema

	// 4. Fall back to standard mcp.AddTool
	mcp.AddTool(s, t, h)
}

func allowAdditionalProperties(s *jsonschema.Schema) {
	if s == nil {
		return
	}
	s.AdditionalProperties = nil
	for _, prop := range s.Properties {
		allowAdditionalProperties(prop)
	}
	if s.Items != nil {
		allowAdditionalProperties(s.Items)
	}
	for _, item := range s.ItemsArray {
		allowAdditionalProperties(item)
	}
	for _, item := range s.PrefixItems {
		allowAdditionalProperties(item)
	}
}

aktau avatar Jun 17 '26 22:06 aktau