fix: elicitation scalar return, resource auto-serialization, Client.new() state, prompt errors (#3859)

* fix: elicitation scalar return, resource auto-serialization, Client.new() state, prompt errors

- Auto-wrap scalar elicitation responses for ScalarElicitationType schemas
  so handlers can return T directly for ctx.elicit("msg", str/int/float)
- Auto-serialize dict/int/float/bool/None resource returns to JSON text
  instead of crashing with TypeError
- Reset _task_registry and _submitted_task_ids in Client.new() so cloned
  clients have independent task tracking state
- Include original error message in prompt render errors (matching tool
  error behavior)

Fixes #3856

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Fix misleading comment and add list/tuple auto-serialization for resources

The comment said "list/tuple of primitives" but the isinstance check
didn't include list or tuple. Now it does, and the comment matches.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Fix TaskNotificationHandler binding in Client.new() and meta forwarding for JSON resources

Two review-identified bugs:

1. Client.new() shallow-copies _session_kwargs, so the cloned client's
   TaskNotificationHandler still dispatches to the original client.
   Fix: create a fresh _session_kwargs dict with a new handler bound
   to the new client.

2. convert_result() for dict/int/float/bool/None fell through to
   ResourceResult(raw_value) which lost component meta (CSP, permissions).
   The str/bytes path correctly wrapped in ResourceContent with meta.
   Fix: explicitly serialize JSON-native types and wrap with meta,
   matching the str/bytes path. Other types still fall through for
   error handling.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Preserve custom message handlers in Client.new()

Only replace the message handler with a new TaskNotificationHandler
if the current handler IS a TaskNotificationHandler. If the user
provided a custom message_handler, preserve it in the clone.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix client.new and add regression tests

* Honor declared MIME type for auto-serialized JSON resources

* Fix static analysis: remove unused StdioTransport import, fix ty:ignore comment

* Exclude list[ResourceContent] from JSON auto-serialization path

A bare list[ResourceContent] would match the isinstance(list) check
and get JSON-serialized instead of passing through to ResourceResult
normalization. Check for ResourceContent items first.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Bill Easton 2026-04-13 12:56:49 -05:00 committed by GitHub
commit db6d7a8a61
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 140 additions and 20 deletions

View file

@ -66,8 +66,8 @@ class TestFunctionResource:
result = await resource._read()
assert result.contents[0].content == b"Hello, world!"
async def test_dict_return_raises_type_error(self):
"""Returning dict from read() raises TypeError - use ResourceResult."""
async def test_dict_return_auto_serializes(self):
"""Returning dict from read() auto-serializes to JSON."""
def get_data() -> dict:
return {"key": "value"}
@ -81,9 +81,10 @@ class TestFunctionResource:
result = await resource.read()
assert result == {"key": "value"}
# _read() raises TypeError - must return str, bytes, or ResourceResult
with pytest.raises(TypeError, match="must be str, bytes, or list"):
await resource._read()
# _read() auto-serializes dict to JSON text
resource_result = await resource._read()
assert len(resource_result.contents) == 1
assert '"key"' in str(resource_result.contents[0].content)
async def test_error_handling(self):
"""Test error handling in FunctionResource."""