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

オープン 初心者向け
#3,883 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

  • #3904 @MrSpy00 による — マージされずにクローズ
  • #3925 @EnesKartal による — マージされずにクローズ

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
backend

調査の方向性

src/mcp/server/elicitation.py の _ElicitationJsonSchema と _validate_rendered_properties() から始め、issue にある repro を実行して出力される $defs/$ref の値を確認します。修正は、検証の前に $defs の enum スキーマ(単一、省略可能、および list[...] のケース)をインライン化し、他のモデル参照は拒否されるままにすることです。StrEnum と (str, Enum) のフィールドが Literal と同じく {"type": "string", "enum": [...]} としてレンダリングされ、既存の elicitation スキーマテストと並んで回帰テストがある状態で完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
25k
フォーク
4.2k
平均マージ
10分
マージ済み PR(30日)
1

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

modelcontextprotocol/python-sdk のほかの issue

modelcontextprotocol/python-sdk の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。