Skip to content

Commit 230f718

Browse files
committed
Address review: default missing 'required' and fix variant count
- server.py: JSON Schema 'required' is optional, so default it to an empty list before membership testing. External schemas with only optional properties no longer raise KeyError at import time. - README.md: greet_dynamic publishes the same schema as greet_pydantic, so the client bullet now says four typed variants, not three.
1 parent 83a8ed6 commit 230f718

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

examples/stories/schema_validators/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ uv run python -m stories.schema_validators.client --http --server server_lowleve
3333
`greet_pydantic` variant. A `create_model` result is opaque to static type
3434
checkers, so a `TYPE_CHECKING` branch aliases it to a declared model of the
3535
same shape — the runtime still uses the dynamic class.
36-
- `client.py` — the listed `inputSchema` for the three typed variants nests a
36+
- `client.py` — the listed `inputSchema` for the four typed variants (including
37+
the runtime `create_model` one, whose schema matches `greet_pydantic`) nests a
3738
`$defs`/`$ref` object with a `name` property; `greet_dict` publishes only
3839
`{"type": "object", "additionalProperties": true}` — no field validation.
3940
- `server_lowlevel.py` — the same schemas written by hand. There is no

examples/stories/schema_validators/server.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ class PersonDC:
4444
# `name`/`title`; at runtime the dynamic class below is what @mcp.tool() sees.
4545
PersonDynamic = PersonModel
4646
else:
47+
# `required` is optional in JSON Schema — a schema of all-optional properties
48+
# omits it — so default to an empty list rather than indexing it directly.
49+
_required = PERSON_JSON_SCHEMA.get("required", [])
4750
_dynamic_fields: dict[str, Any] = {
48-
field_name: (str, ... if field_name in PERSON_JSON_SCHEMA["required"] else field_schema.get("default"))
51+
field_name: (str, ... if field_name in _required else field_schema.get("default"))
4952
for field_name, field_schema in PERSON_JSON_SCHEMA["properties"].items()
5053
}
5154
PersonDynamic = create_model("PersonDynamic", **_dynamic_fields)

0 commit comments

Comments
 (0)