Skip to content

Set tool annotations by decorating the tool function, not in chat.register_tool() #170

@gadenbuie

Description

@gadenbuie

Tool annotations should be attached to the tool definition as closely as possible. It's currently awkward to create to a tool function that has annotations because those annotations need to be passed to chat.register_tool() at registration, which might be in a different place than where the tool is defined. (The tool could be in packaged code and the registration in user code.)

Instead of this

import faicons
from chatlas import ChatOpenAI


def get_weather_forecast(
    lat: float, lon: float, location_name: str
) -> ContentToolResult:
    """Get the weather forecast for a location."""
    # Mocked weather data for demonstration purposes
    return {"temperature_2m": 18, "condition": "Partly Cloudy"}


# Create chat client and register tool
chat_client = ChatOpenAI(model="gpt-4.1-nano")
chat_client.register_tool(
    get_weather_forecast,
    annotations={  # << THIS COULD HAPPEN FAR FROM TOOL DEL
        "title": "Weather Forecast",
        "icon": faicons.icon_svg("cloud"),
    },
)

Maybe this

import faicons
from chatlas import ChatOpenAI, add_tool_annotations

@add_tool_annotations(
  title="Weather Forecast",
  icon=faicons.icon_svg("cloud")
)
def get_weather_forecast(
    lat: float, lon: float, location_name: str
) -> ContentToolResult:
    """Get the weather forecast for a location."""
    # Mocked weather data for demonstration purposes
    return {"temperature_2m": 18, "condition": "Partly Cloudy"}


# Create chat client and register tool
chat_client = ChatOpenAI(model="gpt-4.1-nano")
chat_client.register_tool(get_weather_forecast)

Advantages

This would also resolve some of the underlying issue in #169 where known standard MCP Tool Annotation properties could be named arguments that are documented, but additional kwargs are just added as tool annotations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions