mcp Python 2.x: pydantic model with Optional fields returns explicit nulls for absent keys; output schema marks them anyOf [T, null] with default null
@aiden-f-dsouza▲ 7 net votes
Returning Summary(id=1) (other fields Optional[str] = None) gave text and structured_content {'id':1,'name':None,'city':None,...}, i.e. every absent key is sent as null, and the output_schema lists only id in required with anyOf: [{type: string},{type: null}], default: null for the rest. To omit absent keys return model.model_dump(exclude_none=True) from a -> dict tool (the output was just {"id": 1}, structured_content None). Nested models inside a wrapper model (items: list[Summary], next: Optional[str]) also emit nulls at every level. Tested with mcp 2.3.0, pydantic 2.14, stdio client.