Repository navigation
feat: @client_tool decorator for client tools - #936
Conversation
The decorator reads a function's name, docstring and type hints into a ClientTool builder (on fn.tool) and validates it when defined. The same function is the handler: ClientTools.register_tools() registers it and passes only the params it declares, dropping tool_call_id. Co-Authored-By: Claude Opus 5.5 <[email protected]>
PR SummaryMedium Risk Overview
README documents deploy-time schema push vs runtime registration and shared-schema versioning guidance. Reviewed by Cursor Bugbot for commit cda16ce. Bugbot is set up for automated code reviews on this repo. Configure here. |
ConversationalAiClient and its tools client get hand-maintained subclasses (conversational_ai_custom.py, like music_custom.py) whose create() and update() build the request from a Tool or a decorated function. Passing a ToolRequestModel works as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Chained builders keep build(), the step every SDK shares. Passing one directly now raises a TypeError that says to call build(). Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Match typing and typing_extensions special forms, so typing.Literal maps to choices on Python 3.8 and 3.9. - Don't apply docstring text to from_variable or constant params; a description makes the value LLM-filled. - Keep a Param description inside Optional[Annotated[...]] instead of overwriting it with the docstring. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Safe to merge, but also, I'm a bit concerned this will cause confusion as the schema is stored serverside for future conversations with the agent, which might be shared with other (older) versions of this code causing a data-race between schemas. This feature is ideal for ephemeral (or like here, created just in time for the conversation) agents - persisted agents, not so much IMO and I think that should be front and center.
…ment Co-Authored-By: Claude Opus 5.5 <[email protected]>
… shared The example created the tool right before each conversation, so every run made a new tool and the last client to start won. Split it into a deploy step and a runtime step, and say plainly that the schema is shared by every running client, including older ones. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Good point, thanks. It applies to any client tool, but the decorator makes the schema look like local code, and the README example made it worse by creating the tool right before each conversation. Addressed in cf94b29:
The docs get the same split and warning. I'd rather frame it as treating the schema as an API contract than limit the decorator to temporary agents, which would mean creating new agents and tools each run. 🤖 Addressed by Claude Code |
…he agent's tools Run at startup, it reports each function the agent has no tool for, and each param whose name, type, choices or required status differs from the tool on the server, as a ToolSchemaDriftWarning (or a ToolSchemaDriftError with strict=True). Descriptions, value sources and server-filled defaults are ignored. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit dc15176. Configure here.
The API can store a type as a list such as ["integer", "null"], or leave it out of an object or array schema. Normalize both sides with _kind_of so these don't report false mismatches or skip nested checks. Co-Authored-By: Claude Opus 5.5 <[email protected]>

Summary
Client tools can now be defined as ordinary Python functions.
@client_toolreads the function's name, docstring and type hints into a tool, and the same function runs when the agent calls it. This is in the style of the OpenAI Agents SDK's@function_tool.tools.create()andtools.update()also take the decorated function directly, so it needs nobuild()step. Chained builders keepbuild(). Nothing existing changes: passing a request model works as before, and the decorator produces the sameClientToolbuilder asTool.client().Details
str,int,floatandboolmap to the matching schema types.Literalof strings becomeschoices.List[T]becomes an array, andTypedDictbecomes an object.Optionaltype is optional.TypedDictfield,NotRequiredmakes it optional andRequiredmakes it required.Args:section or reST:param x:lines.TypedDictfield descriptions come from itsAttributes:section.Annotated[T, Param(...)]sets a description, a dynamic variable or a constant instead.fn.tool.TypeErrornaming the param. That coversdict, unions other thanOptional, non-stringLiterals, missing hints, a missing docstring, and*argsor positional-only params.ToolValidationErrorat import, not atbuild().ClientToolsmixestool_call_idinto the params dict, so other keys are dropped unless the function takes**kwargs.NotRequiredis read from the annotation itself.TypedDict.__required_keys__is wrong underfrom __future__ import annotations.ClientTools.register_tools(*fns)duck-types the functions it's given, soconversation.pyhas no runtime import ofagent_tools.tools.create()andtools.update()accept a decorated function.ConversationalAiClientand its tools client get hand-maintained subclasses inconversational_ai_custom.py(now in.fernignore), wired in throughclient.pythe same way asmusic_custom.py.create()andupdate()acceptToolRequestModel | ClientToolFunction.build()followed by the generatedcreate()is the step every SDK can share, and Go, Rust and Java couldn't take a builder without changing every generated signature.TypeErrorthat says to callbuild(), rather than failing during serialization.tools.check_client_tools(agent_id, *fns, strict=False)lists the agent's tools (paginated,used_by_agent_id) and compares each function with the client tool of the same name. It compares only what changes the calls the function receives: param names, types,enumchoices andrequired, recursively through objects and arrays. Descriptions, value sources and server-filled defaults ("",false) are ignored. Each difference is aToolSchemaDriftWarningand a returnedToolSchemaDrift;strict=TrueraisesToolSchemaDriftError. The README now pushes the schema at deploy time and calls the check at startup, because the schema is shared by every running client, older versions included.Testing
tests/agent_tools/test_client_tool_decorator.py. One checks that the decorator gives byte-identical JSON to the same tool written with the builder. Others cover nesting, docstring formats, errors, handlers,register_toolswith sync and async functions, sync and asynctools.create()/tools.update()sending the same body for a function or a model, and theTypeErrorfor a chained builder.tests/agent_tools/test_client_tool_drift.pycover the drift check against mocked server responses with server-filled defaults: a match, each kind of difference, nesting, pagination,strict=Trueand async.tests/agent_toolsandtests/test_client_tools.pypass on Python 3.13 (104 tests, with the client tests);tests/agent_toolspasses on 3.8 (87). mypy and ruff are clean.test_convai_disconnect.pythat fail on main as well.tools.create,check_client_toolsagainst the real API, or a conversation.🤖 Generated with Claude Code