Skip to main content

A decorator for creating Anthropic tool definitions

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tool_decorator-0.1.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tool_decorator-0.1.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file tool_decorator-0.1.0.tar.gz.

File metadata

  • Download URL: tool_decorator-0.1.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.0

File hashes

Hashes for tool_decorator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7daf040a5aafca0533c08eab1473d1b3381245cee161cdf6e765977535d2a14f
MD5 3d24fbd7ee9191ca5eb50d6843319aa9
BLAKE2b-256 3262ffe2ca851ad18296e811d8e73591f41a09fb295b412d77957525fa9f046c

See more details on using hashes here.

File details

Details for the file tool_decorator-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tool_decorator-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.0

File hashes

Hashes for tool_decorator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f12c0d98468e5b95e438712f5c9144faf819291f2603c71c6df9e4f05ecd8a7
MD5 7279fe0ef381677ccac9e8bb4bcc8ee2
BLAKE2b-256 20a6097e2a58e278536b125e1a8b2720dcbaa8a870d8cb330d1e50169c2a74be

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page