anthropics / anthropics/claude-agent-sdk-python

MCP tool schema generation loses Optional, Literal, mapping, and unsupported-type semantics

未關閉
#1,166 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Python
星號
8.1k
分支
1.3k
PR 合併指標
PR 指標待擷取

描述

### Summary

The Python-annotation-to-JSON-Schema converter used by SDK MCP tools can advertise a schema that differs materially from the handler's Python type contract.

### Affected code

- `src/claude_agent_sdk/__init__.py:240-291`
- `tests/test_sdk_mcp_integration.py:757-781`

### Current behavior

- `str | None` becomes only `{"type": "string"}`; nullability is removed.
- `Literal["a", "b"]` is not recognized and falls through to `{"type": "string"}`, losing the enum.
- Parameterized mappings such as `dict[str, int]` become a generic object with no `additionalProperties` schema.
- Unsupported custom annotations silently become strings.
- Multi-type unions remove `None` from `anyOf`.

Current tests explicitly assert the non-nullable Optional behavior and the unknown-type string fallback, so they codify rather than detect the mismatch.

### Why this matters

The schema is what Claude and the MCP layer use to construct and validate tool inputs. A required nullable field cannot receive a valid `None`; constrained values appear unconstrained; and an unsupported object type is incorrectly presented as a string.

This weakens the decorator's documented type-safety guarantee and can cause tool calls to be rejected before reaching a handler or to arrive with values outside the declared Python contract.

### Expected behavior

Generated JSON Schema should preserve the semantics of supported Python annotations, and unsupported annotations should fail clearly rather than silently guessing.

### Possible fix

- Include `{"type": "null"}` in nullable unions.
- Map `Literal` to `enum`/appropriate type.
- Support mapping value schemas, tuples, and Enum where practical.
- Raise a descriptive `TypeError` for unsupported annotations, suggesting an explicit JSON Schema.
- Update tests for nullable required fields, Optional TypedDict keys, Literal values, enums, and nested mappings.

### Environment

- Repository revision: current `main` audit at SDK version 0.2.128
- Bundled CLI version: 2.1.220
- Python test suite: 1,291 passed, 5 skipped
- Ruff and mypy: clean

I searched the existing issues and pull requests using the affected symbols and behavior before filing this.

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。