anthropics / anthropics/claude-agent-sdk-python
MCP tool schema generation loses Optional, Literal, mapping, and unsupported-type semantics
- 主要语言
- 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 还没有评估数据。