anthropics / anthropics/claude-agent-sdk-python
MCP tool schema generation loses Optional, Literal, mapping, and unsupported-type semantics
- Lenguaje dominante
- Python
- Estrellas
- 8.1k
- Forks
- 1.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
### 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.
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Evaluación
Este issue todavía no se ha evaluado.