Optimizely.Opal.Tools
0.2.0
Prefix Reserved
See the version list below for details.
dotnet add package Optimizely.Opal.Tools --version 0.2.0
NuGet\Install-Package Optimizely.Opal.Tools -Version 0.2.0
<PackageReference Include="Optimizely.Opal.Tools" Version="0.2.0" />
<PackageVersion Include="Optimizely.Opal.Tools" Version="0.2.0" />
<PackageReference Include="Optimizely.Opal.Tools" />
paket add Optimizely.Opal.Tools --version 0.2.0
#r "nuget: Optimizely.Opal.Tools, 0.2.0"
#:package Optimizely.Opal.Tools@0.2.0
#addin nuget:?package=Optimizely.Opal.Tools&version=0.2.0
#tool nuget:?package=Optimizely.Opal.Tools&version=0.2.0
Opal Tools SDK for C#
This SDK helps you create tool services for the Opal AI Assistant platform using C# and ASP.NET Core.
Installation
You can install the SDK using NuGet:
dotnet add package OpalToolsSDK
Quick Start
Here's a simple example of how to create a tools service with two tools:
using Microsoft.AspNetCore.Builder;
using OpalToolsSDK;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// Create and register the tools service
var toolsService = app.AddOpalToolsService();
// Register all tools from this assembly
toolsService.RegisterToolsFromExecutingAssembly(app);
// Start the app
app.Run();
// Tool parameter models
public class GreetingParameters
{
[Required]
[Description("Name of the person to greet")]
public string Name { get; set; } = string.Empty;
[Description("Language for greeting (defaults to random)")]
public string? Language { get; set; }
}
public class DateParameters
{
[Description("Date format (defaults to ISO format)")]
public string Format { get; set; } = "yyyy-MM-dd";
}
// Tool implementations
public static class Tools
{
[Tool("greeting", "Greets a person in a random language (English, Spanish, or French)")]
public static async Task<object> Greeting(GreetingParameters parameters)
{
// Get parameters
var name = parameters.Name;
var language = parameters.Language;
// If language not specified, choose randomly
if (string.IsNullOrEmpty(language))
{
var random = new Random();
var languages = new[] { "english", "spanish", "french" };
language = languages[random.Next(languages.Length)];
}
// Generate greeting based on language
string greeting;
if (language.ToLower() == "spanish")
{
greeting = $"¡Hola, {name}! ¿Cómo estás?";
}
else if (language.ToLower() == "french")
{
greeting = $"Bonjour, {name}! Comment ça va?";
}
else // Default to English
{
greeting = $"Hello, {name}! How are you?";
}
return new
{
greeting,
language
};
}
[Tool("todays-date", "Returns today's date in the specified format")]
public static async Task<object> TodaysDate(DateParameters parameters)
{
// Get parameters
var format = parameters.Format;
// Get today's date
var today = DateTime.Now;
// Format the date
var formattedDate = today.ToString(format);
return new
{
date = formattedDate,
format,
timestamp = ((DateTimeOffset)today).ToUnixTimeSeconds()
};
}
}
Authentication
To create a tool that requires authentication:
[Tool("get-calendar", "Gets user's calendar events")]
[RequiresAuth("google", "calendar")]
public static async Task<object> GetCalendar(CalendarParameters parameters, AuthData authData)
{
// Use authData.Provider and authData.Credentials to authenticate
// ...
return new
{
events = new[] { "Event 1", "Event 2" }
};
}
Tool Request/Response Format
The tool request format follows the Opal Tools Management Service specification:
{
"parameters": {
"name": "John",
"language": "spanish"
},
"auth": {
"provider": "google",
"credentials": {
"token": "access_token_value"
}
}
}
The response is the direct JSON serialization of the object returned by your tool method:
{
"greeting": "¡Hola, John! ¿Cómo estás?",
"language": "spanish"
}
API Reference
Models
ParameterType
: Enum for parameter types (String, Integer, Number, Boolean, Array, Object)Parameter
: Parameter definition for a toolAuthRequirement
: Authentication requirements for a toolFunction
: Function definition for a toolAuthData
: Authentication data from a request
Attributes
[Tool(name, description)]
: Marks a method as an Opal tool[RequiresAuth(provider, scopeBundle, required)]
: Specifies authentication requirements
ToolsService
AddOpalToolsService()
: Extension method to add the tools service to a WebApplicationRegisterToolsFromCallingAssembly()
: Registers all tools from the calling assemblyRegisterToolsFromExecutingAssembly()
: Registers all tools from the executing assemblyRegisterToolsFromAssembly(assembly)
: Registers all tools from the specified assembly
Best Practices
- Use the
[Tool]
attribute on static methods - Define parameter models with clear property descriptions
- Return simple objects that can be serialized to JSON
- Use async methods for tools that perform I/O operations
- Handle errors gracefully within your tool methods
- Provide clear descriptions for tools and parameters
Advanced Usage
Custom Parameter Validation
You can use standard .NET validation attributes:
public class GreetingParameters
{
[Required]
[StringLength(100)]
public string Name { get; set; } = string.Empty;
[RegularExpression("english|spanish|french", ErrorMessage = "Language must be english, spanish, or french")]
public string? Language { get; set; }
}
Dependency Injection
For tools that need access to services:
[Tool("get-weather", "Gets the weather for a location")]
public static async Task<object> GetWeather(WeatherParameters parameters, HttpContext context)
{
var weatherService = context.RequestServices.GetRequiredService<IWeatherService>();
var forecast = await weatherService.GetForecastAsync(parameters.Location);
return new
{
location = parameters.Location,
forecast
};
}
License
MIT License
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.