SDK for creating Opal-compatible tools services
Project description
Opal Tools SDK for Python
This SDK simplifies the creation of tools services compatible with the Opal Tools Management Service.
Features
- Easy definition of tool functions with decorators
- Automatic generation of discovery endpoints
- Parameter validation and type checking
- Authentication helpers
- FastAPI integration
- Island components for interactive UI responses
Installation
pip install optimizely-opal.opal-tools-sdk
Note: While the package is installed as optimizely-opal.opal-tools-sdk, you'll still import it in your code as opal_tools_sdk:
# Import using the package name
from opal_tools_sdk import ToolsService, tool, IslandResponse, IslandConfig
Usage
from opal_tools_sdk import ToolsService, tool
from pydantic import BaseModel
from fastapi import FastAPI
app = FastAPI()
tools_service = ToolsService(app)
class WeatherParameters(BaseModel):
location: str
units: str = "metric"
@tool("get_weather", "Gets current weather for a location")
async def get_weather(parameters: WeatherParameters):
# Implementation...
return {"temperature": 22, "condition": "sunny"}
# Discovery endpoint is automatically created at /discovery
Authentication
The SDK provides two ways to require authentication for your tools:
1. Using the @requires_auth decorator
from opal_tools_sdk import ToolsService, tool, AuthData
from opal_tools_sdk.auth import requires_auth
from pydantic import BaseModel
from fastapi import FastAPI
from typing import Optional
app = FastAPI()
tools_service = ToolsService(app)
class CalendarParameters(BaseModel):
date: str
timezone: str = "UTC"
# Single authentication requirement
@requires_auth(provider="google", scope_bundle="calendar", required=True)
@tool("get_calendar_events", "Gets calendar events for a date")
async def get_calendar_events(parameters: CalendarParameters, auth_data: Optional[AuthData] = None):
# The auth_data parameter contains authentication information
if auth_data:
token = auth_data["credentials"]["access_token"]
# Use the token to make authenticated requests
# ...
return {"events": ["Meeting at 10:00", "Lunch at 12:00"]}
# Multiple authentication requirements (tool can work with either provider)
@requires_auth(provider="google", scope_bundle="calendar", required=True)
@requires_auth(provider="microsoft", scope_bundle="outlook", required=True)
@tool("get_calendar_availability", "Check calendar availability")
async def get_calendar_availability(parameters: CalendarParameters, auth_data: Optional[AuthData] = None):
provider = ""
token = ""
if auth_data:
provider = auth_data["provider"]
token = auth_data["credentials"]["access_token"]
if provider == "google":
# Use Google Calendar API
pass
elif provider == "microsoft":
# Use Microsoft Outlook API
pass
return {"available": True, "provider_used": provider}
2. Specifying auth requirements in the @tool decorator
@tool(
"get_email",
"Gets emails from the user's inbox",
auth_requirements=[
{"provider": "google", "scope_bundle": "gmail", "required": True}
]
)
async def get_email(parameters: EmailParameters, auth_data: Optional[AuthData] = None):
# Implementation...
return {"emails": ["Email 1", "Email 2"]}
Island Components
The SDK includes Island components for creating interactive UI responses that allow users to input data and trigger actions.
Weather Tool with Interactive Island
from opal_tools_sdk import ToolsService, tool, IslandResponse, IslandConfig
from pydantic import BaseModel
from fastapi import FastAPI
app = FastAPI()
tools_service = ToolsService(app)
class WeatherParameters(BaseModel):
location: str
units: str = "metric"
@tool("get_weather", "Gets current weather for a location")
async def get_weather(parameters: WeatherParameters):
# Get weather data (implementation details omitted)
weather_data = {"temperature": 22, "condition": "sunny", "humidity": 65}
# Create an interactive island for weather settings
island = IslandConfig(
fields=[
IslandConfig.Field(
name="location",
label="Location",
type="string",
value=parameters.location
),
IslandConfig.Field(
name="units",
label="Temperature Units",
type="string",
value=parameters.units,
options=["metric", "imperial", "kelvin"]
),
IslandConfig.Field(
name="current_temp",
label="Current Temperature",
type="string",
value=f"{weather_data['temperature']}°{'C' if parameters.units == 'metric' else 'F'}"
)
],
actions=[
IslandConfig.Action(
name="refresh_weather",
label="Refresh Weather",
type="button",
endpoint="/tools/get_weather",
operation="update"
)
]
)
return IslandResponse.create([island])
Island Components
IslandConfig.Field
Fields represent data inputs in the UI:
name: Programmatic field identifierlabel: Human-readable labeltype: Field type ("string","boolean","json")value: Current field value (optional)hidden: Whether to hide from user (optional, default: False)options: Available options for selection (optional)
IslandConfig.Action
Actions represent buttons or operations:
name: Programmatic action identifierlabel: Human-readable button labeltype: UI element type (typically"button")endpoint: API endpoint to calloperation: Operation type (default:"create")
IslandConfig
Contains the complete island configuration:
fields: List of IslandConfig.Field objectsactions: List of IslandConfig.Action objectstype: Island type for UI rendering (optional, default:None)icon: Icon to display in the island (optional, default:None)
IslandResponse
The response wrapper for islands:
- Use
IslandResponse.create([islands])to create responses - Supports multiple islands per response
Type Definitions
The SDK provides several TypedDict and dataclass definitions for better type safety:
Authentication Types
AuthData: TypedDict containing provider and credentials informationCredentials: TypedDict with access_token, org_sso_id, customer_id, instance_id, and product_skuAuthRequirement: Dataclass for specifying authentication requirements
Execution Environment
Environment: TypedDict specifying execution mode ("headless"or"interactive")
Parameter Types
ParameterType: Enum for supported parameter types (string, integer, number, boolean, list, dictionary)Parameter: Dataclass for tool parameter definitionsFunction: Dataclass for complete tool function definitions
These types are automatically imported when you import from opal_tools_sdk and provide better IDE support and type checking.
Documentation
See full documentation for more examples and configuration options.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file optimizely_opal_opal_tools_sdk-0.1.18.dev0.tar.gz.
File metadata
- Download URL: optimizely_opal_opal_tools_sdk-0.1.18.dev0.tar.gz
- Upload date:
- Size: 26.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f71daa39cd208a85d3c0861315c511a62eb27bcbe49e59f61eef0140c1aa8ad6
|
|
| MD5 |
df1fa5bc6b3966058eaa1061b0a7a354
|
|
| BLAKE2b-256 |
e8b6b2b2944bca538e9066780443e9a1be98abc9c4235accc5a909cc623b4509
|
File details
Details for the file optimizely_opal_opal_tools_sdk-0.1.18.dev0-py3-none-any.whl.
File metadata
- Download URL: optimizely_opal_opal_tools_sdk-0.1.18.dev0-py3-none-any.whl
- Upload date:
- Size: 23.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f52a6912747872c370943f1d33a79d94133bc9f45a3b7071f947b95bbcfa0f5
|
|
| MD5 |
0b0fed89e353b197fc2b83025c2ff594
|
|
| BLAKE2b-256 |
5f93ea17a2abce510e68b7aa962b47d6e541eea0d5e037c7e463bd95dbe02ec3
|