modelcontextprotocol / modelcontextprotocol/python-sdk

Generated output schema uses Pydantic's validation shape while structured output uses its serialization shape

Đang mở
#3,100 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

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

bug needs confirmation P2 v1 v2
Ngôn ngữ chính
Python
Star
24.3k
Fork
4k
Merge trung bình
1 ngày 1 giờ
Pull request đã merge (30 ngày)
31

Mô tả

Initial Checks
  • I confirm that I'm using the latest version of MCP Python SDK.
  • I confirm that I searched existing issues and pull requests before opening this issue.
Description

For a Pydantic return model whose validation and serialization shapes differ, the generated outputSchema describes the validation shape while structuredContent uses the serialization shape. The SDK therefore publishes an output schema that rejects its own generated structured result.

This is related to, but not a duplicate of, #1073 / #1099. That change aligned ordinary field aliases by serializing structured output with aliases. Split validation_alias / serialization_alias values still expose different validation and serialization shapes, and serialization-only fields such as computed_field reveal the same underlying mismatch.

Example Code
from __future__ import annotations

import asyncio
import json

from jsonschema import Draft202012Validator
from pydantic import BaseModel, ConfigDict, Field, computed_field

try:
    from mcp.server.mcpserver.tools.base import Tool
except ImportError:  # MCP Python SDK 1.x
    from mcp.server.fastmcp.tools.base import Tool


class AliasOutput(BaseModel):
    model_config = ConfigDict(extra="forbid", populate_by_name=True)
    value: int = Field(validation_alias="wireIn", serialization_alias="wireOut")


def alias_output() -> AliasOutput:
    return AliasOutput(value=1)


class ComputedOutput(BaseModel):
    model_config = ConfigDict(extra="forbid")
    value: int

    @computed_field
    @property
    def doubled(self) -> int:
        return self.value * 2


def computed_output() -> ComputedOutput:
    return ComputedOutput(value=1)


async def check(function: object) -> None:
    tool = Tool.from_function(function)
    converted = await tool.run({}, None, convert_result=True)
    structured = converted[1] if isinstance(converted, tuple) else converted.structured_content
    errors = [
        error.message
        for error in Draft202012Validator(tool.output_schema).iter_errors(structured)
    ]
    print(function.__name__)
    print("schema:", json.dumps(tool.output_schema, sort_keys=True))
    print("structured:", json.dumps(structured, sort_keys=True))
    print("schema_errors:", errors)


async def main() -> None:
    await check(alias_output)
    await check(computed_output)


asyncio.run(main())

Observed validator messages:

alias_output
schema: ... "wireIn" ...
structured: {"wireOut": 1}
schema_errors: ["Additional properties are not allowed ('wireOut' was unexpected)", "'wireIn' is a required property"]

computed_output
schema: ... "value" ...
structured: {"doubled": 2, "value": 1}
schema_errors: ["Additional properties are not allowed ('doubled' was unexpected)"]
Expected behavior

The generated outputSchema should describe the serialized structured output. Generating the output model schema in Pydantic serialization mode makes both witnesses conform: the alias schema uses wireOut, and the computed-field schema includes doubled.

Python & MCP Python SDK
  • Python: 3.13.13
  • MCP Python SDK: 1.28.1 (latest stable)
  • Also reproduced on current main: 2713b53b127afc094dc97d6067df9f69b647661c (2.0.0b2)
  • Pydantic: 2.13.4

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

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.

Hướng nghiên cứu

Bắt đầu tại Tool.from_function trong phần triển khai các công cụ máy chủ MCP, sử dụng các đường dẫn import được hiển thị cho các phiên bản SDK, và theo dõi cách output_schema và structured_content được tạo ra. Tái hiện các ví dụ alias_output và computed_output bằng Draft202012Validator. Hoàn tất khi schema được tạo chấp nhận cả hai kết quả có cấu trúc đã được tuần tự hóa, bao gồm wireOut và doubled.

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

Đánh giá

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

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.