Allow configuring additionalProperties on generated tool schemas
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:
- Add
AllowAdditionalProperties booltojsonschema.ForOptions(upstream ingoogle/jsonschema-go) - Add a
SchemaOptionsparameter tomcp.AddToolthat allows configuring schema generation - Allow overriding the generated schema on the
Toolstruct even when using the genericAddTool
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
Agreed, option 1 makes the most sense. Please file a bug on the jsonschema repo.
Filed upstream: https://github.com/google/jsonschema-go/issues/71
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)
}
}