Pydantic V2 discriminator pattern for MCP return types
This article details a Pydantic V2 discriminated union pattern for handling multiple return types from an MCP tool. The author advocates for this approach, highlighting its clarity and efficiency over alternatives like subclassing or optional fields. The pattern ensures that clients and downstream LLMs can accurately parse the specific shape of a tool's return value based on a consistent discriminator field. AI
IMPACT This pattern can improve the reliability and clarity of data structures used in AI agent tool integrations.