render_elicitation_schema rejects StrEnum and (str, Enum) fields because Pydantic emits $defs/$ref

Aperta Adatta ai principianti
#3,883 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

  • #3904 di @MrSpy00 — chiusa senza merge
  • #3925 di @EnesKartal — chiusa senza merge

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia in src/mcp/server/elicitation.py da _ElicitationJsonSchema e _validate_rendered_properties(), ed esegui il repro nell'issue per vedere i valori $defs/$ref emessi. La correzione consiste nell'inlineare gli schemi enum di $defs (casi singolo, opzionale e list[...]) prima della validazione, lasciando che gli altri ref dei modelli vengano rifiutati. Fatto quando i campi StrEnum e (str, Enum) vengono renderizzati come {"type": "string", "enum": [...]} come Literal, con test di regressione accanto ai test esistenti dello schema di elicitation.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug v2
Initial Checks
Release line

2.x (current stable)

Description

render_elicitation_schema() in src/mcp/server/elicitation.py rejects Pydantic models whose fields use StrEnum or (str, Enum) — both single-select (color: Color, color: Color | None = None) and multi-select (colors: list[Color]) — even though the equivalent Literal["red", "blue"] and list[Literal["red", "blue"]] fields are accepted and MCP elicitation explicitly supports string enums and multi-select enums.

Root cause: Pydantic emits a $defs entry and a {"$ref": "#/$defs/Color"} property for Enum subclasses, and _ElicitationJsonSchema does not inline enum schemas before _validate_rendered_properties() validates each property against PrimitiveSchemaDefinition. As a result, ctx.elicit() and Resolve(..., Elicit(...)) both raise TypeError before sending the request.

Example Code
from enum import StrEnum
from typing import Literal
from pydantic import BaseModel
from mcp.server.elicitation import render_elicitation_schema


class Color(StrEnum):
    RED = "red"
    BLUE = "blue"


class LiteralForm(BaseModel):
    color: Literal["red", "blue"]
    colors: list[Literal["red", "blue"]]


class EnumForm(BaseModel):
    color: Color


class MultiEnumForm(BaseModel):
    colors: list[Color]


print("LiteralForm OK:", render_elicitation_schema(LiteralForm)["properties"]["color"])
for cls in (EnumForm, MultiEnumForm):
    try:
        render_elicitation_schema(cls)
    except TypeError as exc:
        print(f"{cls.__name__} TypeError: {exc}")

Observed output:

LiteralForm OK: {'enum': ['red', 'blue'], 'title': 'Color', 'type': 'string'}
EnumForm TypeError: Elicitation schema field 'color' rendered as {'$ref': '#/$defs/Color'}, which is not a valid PrimitiveSchemaDefinition
MultiEnumForm TypeError: Elicitation schema field 'colors' rendered as {'items': {'$ref': '#/$defs/Color'}, 'title': 'Colors', 'type': 'array'}, which is not a valid PrimitiveSchemaDefinition

Expected behavior: StrEnum / string Enum fields (single, optional, and list[Enum] multi-select) should render inline {"type": "string", "enum": [...]} (or array-of-enum) schemas matching Literal without emitting $defs/$ref, while non-primitive models still get rejected.

I would be happy to submit a PR with regression tests if assigned.

Python & MCP Python SDK
  • Python: 3.14.8
  • mcp: 2.3.1.dev20+77001c96 (main @ 77001c96)

AI disclosure: AI assistance was used to help investigate and draft this issue; the reproduction and observed output were verified locally.

Lingua principale
Python
Stelle
25k
Fork
4.2k
Merge medio
10m
PR unite (30g)
1

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di modelcontextprotocol/python-sdk

Tutte le issue di modelcontextprotocol/python-sdk

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.