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

Đang mở Phù hợp với người mới
#3,883 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

  • #3904 của @MrSpy00 — đã đóng, không merge
  • #3925 của @EnesKartal — đã đóng, không merge

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
72/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu trong src/mcp/server/elicitation.py tại _ElicitationJsonSchema và _validate_rendered_properties(), và chạy repro trong issue để xem các giá trị $defs/$ref được phát ra. Bản sửa là inline các schema enum của $defs (trường hợp đơn, tùy chọn và list[...]) trước khi xác thực, để các ref model khác bị từ chối. Hoàn tất khi các trường StrEnum và (str, Enum) được render thành {"type": "string", "enum": [...]} giống Literal, cùng với các test hồi quy bên cạnh các test schema elicitation hiện có.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
25k
Fork
4.2k
Merge trung bình
10 phút
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của modelcontextprotocol/python-sdk

Tất cả issue của modelcontextprotocol/python-sdk

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.