Commit 7edf47d
feat(tools): support @function_tool on class instance methods via descriptor protocol
Fixes #94. Decorating a class method with @function_tool now works correctly:
- `function_schema()` detects an unannotated leading `self` or `cls` parameter
and sets `skips_receiver=True`, stripping the receiver from both the JSON
schema and the stored signature so the LLM never sees it and `to_call_args()`
never tries to populate it from model output.
- `FunctionTool` gains a `__get__` descriptor method. Accessing a method tool
on a class instance (e.g. `instance.my_tool`) returns a bound copy whose
invoker prepends the instance as the first argument, so the underlying method
receives the correct `self`. Accessing via the class returns the unbound tool.
- A `_make_impl(receiver)` factory is stored on method tools; `__get__` calls
it with the instance and wires the result into the copied tool's invoker.
- A `RunContextWrapper`/`ToolContext` parameter immediately after `self`/`cls`
is handled correctly (`takes_context=True`), as is the case where context is
in the wrong position (still raises `UserError`).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent 86739b1 commit 7edf47d
3 files changed
Lines changed: 481 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
39 | 43 | | |
40 | 44 | | |
41 | 45 | | |
| |||
286 | 290 | | |
287 | 291 | | |
288 | 292 | | |
| 293 | + | |
289 | 294 | | |
| 295 | + | |
| 296 | + | |
290 | 297 | | |
291 | 298 | | |
292 | 299 | | |
293 | 300 | | |
294 | 301 | | |
295 | | - | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
296 | 309 | | |
297 | 310 | | |
298 | 311 | | |
| 312 | + | |
299 | 313 | | |
300 | 314 | | |
| 315 | + | |
301 | 316 | | |
302 | 317 | | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
303 | 329 | | |
304 | | - | |
305 | | - | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
306 | 333 | | |
307 | 334 | | |
308 | 335 | | |
| |||
313 | 340 | | |
314 | 341 | | |
315 | 342 | | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
316 | 349 | | |
317 | 350 | | |
318 | 351 | | |
| |||
419 | 452 | | |
420 | 453 | | |
421 | 454 | | |
| 455 | + | |
422 | 456 | | |
423 | 457 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
353 | 353 | | |
354 | 354 | | |
355 | 355 | | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
356 | 381 | | |
357 | 382 | | |
358 | 383 | | |
| |||
1669 | 1694 | | |
1670 | 1695 | | |
1671 | 1696 | | |
| 1697 | + | |
| 1698 | + | |
| 1699 | + | |
| 1700 | + | |
| 1701 | + | |
| 1702 | + | |
| 1703 | + | |
| 1704 | + | |
| 1705 | + | |
| 1706 | + | |
| 1707 | + | |
| 1708 | + | |
| 1709 | + | |
| 1710 | + | |
| 1711 | + | |
| 1712 | + | |
| 1713 | + | |
| 1714 | + | |
| 1715 | + | |
| 1716 | + | |
| 1717 | + | |
| 1718 | + | |
| 1719 | + | |
| 1720 | + | |
| 1721 | + | |
| 1722 | + | |
| 1723 | + | |
| 1724 | + | |
| 1725 | + | |
| 1726 | + | |
| 1727 | + | |
| 1728 | + | |
| 1729 | + | |
| 1730 | + | |
| 1731 | + | |
| 1732 | + | |
| 1733 | + | |
| 1734 | + | |
| 1735 | + | |
| 1736 | + | |
| 1737 | + | |
| 1738 | + | |
| 1739 | + | |
| 1740 | + | |
| 1741 | + | |
| 1742 | + | |
| 1743 | + | |
| 1744 | + | |
| 1745 | + | |
| 1746 | + | |
| 1747 | + | |
| 1748 | + | |
| 1749 | + | |
| 1750 | + | |
| 1751 | + | |
| 1752 | + | |
| 1753 | + | |
| 1754 | + | |
| 1755 | + | |
| 1756 | + | |
| 1757 | + | |
| 1758 | + | |
| 1759 | + | |
| 1760 | + | |
| 1761 | + | |
| 1762 | + | |
| 1763 | + | |
| 1764 | + | |
| 1765 | + | |
| 1766 | + | |
| 1767 | + | |
| 1768 | + | |
| 1769 | + | |
| 1770 | + | |
| 1771 | + | |
| 1772 | + | |
| 1773 | + | |
| 1774 | + | |
| 1775 | + | |
| 1776 | + | |
| 1777 | + | |
| 1778 | + | |
| 1779 | + | |
| 1780 | + | |
| 1781 | + | |
| 1782 | + | |
| 1783 | + | |
| 1784 | + | |
| 1785 | + | |
| 1786 | + | |
| 1787 | + | |
1672 | 1788 | | |
1673 | 1789 | | |
1674 | 1790 | | |
| |||
1711 | 1827 | | |
1712 | 1828 | | |
1713 | 1829 | | |
1714 | | - | |
1715 | | - | |
1716 | | - | |
1717 | | - | |
1718 | | - | |
| 1830 | + | |
1719 | 1831 | | |
1720 | 1832 | | |
1721 | 1833 | | |
| |||
0 commit comments