Defining Tools
Relevant source files
The following files were used as context for generating this wiki page:In the Universal Tool Calling Protocol (UTCP), a "Tool" is the fundamental, callable unit of functionality. Defining tools is the first step in exposing capabilities to a UTCP client, such as an AI agent. The protocol is designed for interoperability, allowing tools to be defined in a language-agnostic way using JSON or programmatically in Python through decorators.
A well-defined tool includes a name, a description, and strongly-typed schemas for its inputs and outputs. These definitions are collected into a UtcpManual, which serves as a discoverable catalog that clients use to understand and interact with the available tools. This system enables both manual creation of tool definitions for existing APIs and automatic generation for new Python-based tools.
Sources: core/src/utcp/data/tool.py, core/src/utcp/data/utcp_manual.py, README.md
Core Data Structures
The definition of a tool relies on a few core Pydantic models that structure its metadata, execution details, and data schemas.
The Tool Model
The Tool model is the central data structure for defining a single callable function. It encapsulates all the necessary information for a client to understand what the tool does, what parameters it expects, and what it returns.
Sources: core/src/utcp/data/tool.py:101-117
The following diagram shows the relationship between the Tool model and its constituent parts, JsonSchema and CallTemplate.
Sources: core/src/utcp/data/tool.py:101-117
The table below details the fields of the Tool model.
| Field | Type | Description |
|---|---|---|
name | str | A unique identifier for the tool. By convention, it is namespaced as manual_name.tool_name. |
description | str | A human-readable description of the tool's purpose and functionality. |
inputs | JsonSchema | A JSON Schema object defining the tool's input parameters. |
outputs | JsonSchema | A JSON Schema object defining the structure of the tool's return value. |
tags | List[str] | A list of tags for categorization, filtering, and search. |
average_response_size | Optional[int] | An optional hint about the typical response size in bytes. |
tool_call_template | CallTemplate | The configuration that specifies how to execute the tool (e.g., via HTTP, CLI). |
Sources: core/src/utcp/data/tool.py:101-117, README.md
The JsonSchema Model
The JsonSchema model is used to define the structure and constraints of the inputs and outputs for a Tool. It is a Pydantic model that represents a subset of the JSON Schema standard.
| Field | Alias | Type | Description |
|---|---|---|---|
schema_ | $schema | Optional[str] | Optional schema identifier (e.g., "http://json-schema.org/draft-07/schema#"). |
id_ | $id | Optional[str] | Optional schema identifier. |
type | Optional[str] | The data type (e.g., "object", "string", "number"). | |
properties | Optional[Dict] | A dictionary of child properties for objects. | |
required | Optional[List[str]] | A list of required property names for objects. | |
default | Optional[JsonType] | A default value for the field. |
Sources: core/src/utcp/data/tool.py:27-56
The UtcpManual
Tools are not defined in isolation; they are grouped into a UtcpManual. This manual acts as a discoverable catalog for a collection of tools from a single source or provider. When a UtcpClient connects to a tool provider, it first fetches the UtcpManual to learn about all available tools.
A UtcpManual contains:
utcp_version: The version of the UTCP protocol the manual adheres to.manual_version: The version of the manual itself.tools: A list ofToolobjects.
Sources: core/src/utcp/data/utcp_manual.py:20-51
The following diagram illustrates the discovery process where a client retrieves a manual.
Methods of Tool Definition
UTCP supports two primary methods for defining tools: manually via a structured dictionary (like JSON) or programmatically in Python using a decorator.
Manual Definition (JSON)
This method is language-agnostic and ideal for exposing existing APIs or for tool providers not implemented in Python. You create a JSON file that conforms to the UtcpManual structure.
The UtcpClient can load tools from such a file using a text call template, which simply reads the file content.
Sources: plugins/communication_protocols/text/README.md
The following flowchart shows how a JSON-defined manual is loaded.
Below is an example of a tool definition within a JSON manual.
{
"manual_version": "1.0.0",
"tools": [
{
"name": "get_weather",
"description": "Get current weather for a location",
"tags": ["weather"],
"inputs": {
"type": "object",
"properties": {
"location": {"type": "string"}
}
},
"outputs": {
"type": "object",
"properties": {
"temperature": {"type": "number"},
"conditions": {"type": "string"}
}
},
"tool_call_template": {
"call_template_type": "http",
"url": "https://example.com/api/weather",
"http_method": "GET"
}
}
]
}
Sources: README.md
Python Decorator (@utcp_tool)
For Python applications, the @utcp_tool decorator provides a convenient way to automatically generate Tool definitions from Python functions. The decorator inspects the function's signature, type hints, and docstring to infer the name, description, inputs, and outputs.
Decorated tools are registered in a global ToolContext. The UtcpManual.create_from_decorators() static method can then be used to assemble a manual from all registered tools.
Sources: core/src/utcp/python_specific_tooling/tool_decorator.py:23-28, core/src/utcp/data/utcp_manual.py:54-78
Here is an example of using the decorator:
from utcp_http.http_call_template import HttpCallTemplate
from utcp.python_specific_tooling.tool_decorator import utcp_tool
@utcp_tool(
tool_call_template=HttpCallTemplate(
name="get_weather",
url=f"https://example.com/api/weather",
http_method="GET"
),
tags=["weather"]
)
def get_weather(location: str):
"""Get current weather for a location"""
return {"temperature": 22.5, "conditions": "Sunny"}
Sources: README.md
The UtcpManual can then be created and served, for example, in a FastAPI application:
from fastapi import FastAPI
from utcp.data.utcp_manual import UtcpManual
app = FastAPI()
# The discovery endpoint returns the tool manual
@app.get("/utcp")
def utcp_discovery():
return UtcpManual.create_from_decorators(manual_version="1.0.0")
Sources: README.md, core/src/utcp/data/utcp_manual.py:54-57
Tool Naming and Namespacing
When a UtcpManual is registered with a UtcpClient, the client automatically namespaces the tools within it. If a manual has the name my_api and contains a tool named get_data, the tool will be registered and callable in the client as my_api.get_data. This prevents naming conflicts between tools from different manuals.
This namespacing is handled by the UtcpClient during the register_manual process.
Sources: core/tests/client/test_utcp_client.py:202-203, README.md
Summary
Defining tools is a core concept in UTCP, enabling services and applications to expose their functionality in a standardized way. The protocol offers flexibility through two main definition methods: a language-agnostic JSON format for maximum interoperability and a Python-specific decorator for ease of use and automatic schema generation. These definitions are bundled into a UtcpManual, which clients use for discovery, ensuring that tools are self-describing and easily consumable by AI agents and other clients.
Sources: README.md, core/src/utcp/data/tool.py, core/src/utcp/python_specific_tooling/tool_decorator.py