Skip to content

.Net: Bug: AIFunctionKernelFunction does not preserve definitions / $defs in function schemaΒ #13447

@hejtmii

Description

@hejtmii

Describe the bug
When MCP tool (AI function) contains repetitive or recursive parts of the schema that uses $ref to definitions or $defs property, this part of the schema is missing in the LLM call, and the schema is invalid/incomplete

To Reproduce
Steps to reproduce the behavior:

  1. Use MCP server with AI agent https://devblogs.microsoft.com/semantic-kernel/integrating-model-context-protocol-tools-with-semantic-kernel-a-step-by-step-guide/
  2. Use MCP server with tool(s) that have complex schema (recursive or repetitive) and use definitions or $defs in the schema, e.g. https://github.com/kontent-ai/mcp-server with add-taxonomy-group-mapi tool
  3. Initiate any agent action so that LLM call is made with the tools loaded from the MCP server
  4. See the LLM payload, the tool schema contains $ref: "#/definitions/..." but definitions is missing in the tool schema

LLM that validates the schema consistency (Claude recently, but they have seem to reverted it for now) throws HTTP 400 on missing definitions

Expected behavior
Schema of MCP tool should be passed to the LLM as it is, or preserve all well-defined properties of the schema according to its RFC at https://json-schema.org/

Screenshots
Here is a screenshot of Claude HTTP 400 error that we were getting for last several days. It seems they added validation for $ref targets on January 6th

Image

But as (I assume) it probably backfired, they seem to removed it again on January 8th so now it doesn't throw the error anymore.

Having said that, it doesn't change the fact that the schema is sent to the LLM incomplete and the issue may reoccur anytime later...

Platform

  • Language: C#
  • Source:
  • AI model: Claude Sonnet 4.5 (doesn't matter, the data is incomplete)
  • IDE: Rider (doesn't matter)
  • OS: Windows, Mac (doesn't matter)

Additional context

  • AIFunctionKernelFunction.MapParameterMetadata does not read definitions / $defs from the source schema
  • BuildFunctionSchema.BuildFunctionSchema (re)builds the schema only from Parameters
  • KernelFunctionSchemaModel is missing definitions property
  • Kernel Plugins only accept KernelFunctions as input
  • AIFunctionKernelFunction is sealed internal so it cannot be overriden on our side
  • KernelFunction is public, but has internal constructor, so it cannot be overriden on our side

Zod library that is most often used to build MCP tool schemas in TS produces definitions / $defs for repetitive / recursive parts of the tool schema, following https://json-schema.org/ standard

Anthropic docs refer to https://json-schema.org/ for schema format https://platform.claude.com/docs/en/agents-and-tools/tool-use/implement-tool-use#specifying-client-tools

Open AI docs refer to https://json-schema.org/ for schema format https://platform.openai.com/docs/guides/function-calling#defining-functions

I believe that in order to fully support today's MCP servers with complete and complex tool schema, the processed schema should either fully adhere to the https://json-schema.org specifications, or there should be (ideally a built-in) way to pass schema returned by MCP server unchanged to the tools in LLM request...

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETIssue or Pull requests regarding .NET codebugSomething isn't workingtriage

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions