Skip to main content

Tool Decorator for Anthropic SDK

A Python decorator that simplifies the creation of tool definitions for the Anthropic SDK.

Overview

This package provides a @tool decorator that automatically generates Anthropic-compatible tool definitions from Python functions. It extracts parameter types, descriptions, and other metadata directly from function signatures and docstrings.

Installation

pip install tool-decorator

Usage

Basic Usage

from tool_decorator import tool

@tool(description="Search for information on the web")
def search_web(query: str, max_results: int = 10):
    """
    Performs a web search and returns results.

    Args:
        query: The search query string
        max_results: Maximum number of results to return
    """
    # Implementation here
    pass

# Access the tool definition
tool_definition = search_web.definition

Using with Anthropic SDK

from tool_decorator import tool
from anthropic import Anthropic
from datetime import datetime

# Define a simple tool with the decorator
@tool()
def get_time(timezone: str = "UTC"):
    """
    Get the current time.

    Args:
        timezone: The timezone to get the time for (default: UTC)
    """
    # Simple implementation that ignores timezone for demo purposes
    current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    return {"current_time": current_time, "timezone": timezone}

# Create a client
client = Anthropic()

# Use the tool definition with the SDK
response = client.messages.create(
    model="claude-3-7-sonnet-20250219",
    max_tokens=1000,
    messages=[
        {"role": "user", "content": "What is the current time?"}
    ],
    tools=[
        get_time.definition  # Use definition attribute directly
    ]
)

# Print the response
print("Response received successfully!")
print(f"Model: {response.model}")
print("\nResponse content:")
print(response.content)

Comparison with Manual Definition

Without the decorator, you would need to manually define the tool definition:

# Manual tool definition
get_time_definition = {
    "name": "get_time",
    "description": "Get the current time.",
    "input_schema": {
        "type": "object",
        "properties": {
            "timezone": {
                "type": "string",
                "description": "The timezone to get the time for (default: UTC)",
                "default": "UTC"
            }
        },
        "required": []
    }
}

# Use with Anthropic SDK
tools=[get_time_definition]

The decorator approach automatically generates this definition from your function signature and docstring, keeping everything in sync and reducing boilerplate code.

Generated Tool Definition Format

The decorator generates tool definitions in the required schema format for the Anthropic API.

Features

  • Automatically converts Python type hints to JSON Schema types
  • Extracts parameter descriptions from docstrings
  • Supports optional parameters with default values
  • Generates Anthropic-compatible tool definitions with the required input_schema field
  • Validates parameter documentation in strict mode

Development

For information on development, building, and contributing to this project, see README_DEV.md.

Requirements

  • Python 3.8+

License

MIT License - See LICENSE file for details

Metadata

Release files for tool-decorator 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tool-decorator 0.1.0
File Size Uploaded
tool_decorator-0.1.0.tar.gz 8.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tool-decorator 0.1.0
File Interpreter ABI Platform
tool_decorator-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.6 kB

Release files / tool_decorator-0.1.0.tar.gz

Download URL tool_decorator-0.1.0.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
7daf040a5aafca0533c08eab1473d1b3381245cee161cdf6e765977535d2a14f
BLAKE2b-256 checksum
How to use checksums
3262ffe2ca851ad18296e811d8e73591f41a09fb295b412d77957525fa9f046c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.0

Release files / tool_decorator-0.1.0-py3-none-any.whl

Download URL tool_decorator-0.1.0-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2f12c0d98468e5b95e438712f5c9144faf819291f2603c71c6df9e4f05ecd8a7
BLAKE2b-256 checksum
How to use checksums
20a6097e2a58e278536b125e1a8b2720dcbaa8a870d8cb330d1e50169c2a74be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page