helpers

package
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleAPIError

func HandleAPIError(err error, label string) (*mcp.CallToolResult, error)

HandleAPIError processes an error returned from the Teamwork API and converts it into an appropriate MCP tool result or error.

func IntSliceToInt64 added in v1.6.1

func IntSliceToInt64(slice []int) []int64

IntSliceToInt64 converts a slice of int to a slice of int64

func NewToolResultJSON added in v1.6.2

func NewToolResultJSON(v any) (*mcp.CallToolResult, error)

NewToolResultJSON creates a new JSON-based tool result.

func NewToolResultText added in v1.6.2

func NewToolResultText(format string, args ...any) *mcp.CallToolResult

NewToolResultText creates a new text-based tool result.

func NewToolResultTextError added in v1.6.2

func NewToolResultTextError(text string) *mcp.CallToolResult

NewToolResultTextError creates a new MCP tool result representing an error with the given text message.

func ParamGroup

func ParamGroup(params map[string]any, funcs ...ParamFunc) error

ParamGroup applies a series of functions to a map of parameters.

func SliceToAny added in v1.5.3

func SliceToAny[T any](slice []T) []any

SliceToAny converts a slice of any type to []any for use with filter.In()

func WebLinker

func WebLinker(
	ctx context.Context,
	data []byte,
	buildPath func(map[string]any) string,
	opts ...WebLinkerOption,
) []byte

WebLinker processes JSON data to inject web links into entities based on their structure. It decodes the input data as JSON, traverses the top-level fields, and adds a "webLink" field in the meta section to qualifying objects using the provided buildPath function and customer URL from context.

The function handles two types of structures for each top-level field:

  • Single objects: {"field": {"id": 123, ...}} → adds webLink to the object
  • Arrays of objects: {"field": [{"id": 123, ...}, ...]} → adds webLink to each object in the array

Behavior:

  • Returns original data unchanged if JSON parsing fails, customer URL is missing, or buildPath is nil
  • Skips fields listed in the ignoreFields option (defaults to "meta" and "included")
  • Only processes objects within arrays; non-object array items are left unchanged
  • The webLink is constructed as: "{customerURL}/{path}" where path comes from buildPath()
  • If buildPath returns an empty string for an object, no webLink is added to that object

Parameters:

  • ctx: Context containing customer URL via config.CustomerURLFromContext
  • data: Raw JSON data as bytes
  • buildPath: Function that generates a path string from an object (e.g., "#users/123")
  • opts: Optional configuration (e.g., WebLinkerWithIgnoreFields to skip additional fields)

Returns the modified JSON data as bytes, or the original data if processing fails.

func WebLinkerWithIDPathBuilder

func WebLinkerWithIDPathBuilder(prefix string) func(map[string]any) string

WebLinkerWithIDPathBuilder creates a path builder function for entities with an "id" field. It returns a function that builds a path in the format "prefix/id" for objects containing a non-zero "id" field. Returns an empty string if the "id" field is missing or has a zero value.

Types

type ParamFunc

type ParamFunc func(map[string]any) error

ParamFunc defines a function type that takes a map of parameters and returns an error. This is used to define functions that can retrieve parameters from a map, converting them to a specific type and applying middleware functions if necessary.

func OptionalCustomNumericListParam

func OptionalCustomNumericListParam[T interface{ Add(float64) }](target T, key string) ParamFunc

OptionalCustomNumericListParam retrieves an optional list of numeric parameters from a map, converting each item to the specified numeric type using a custom type that implements the Add method. It returns an error if the key is not found or if the type conversion fails.

func OptionalDateParam

func OptionalDateParam(
	target *twapi.Date,
	key string,
	middlewares ...ParamMiddleware[string],
) ParamFunc

OptionalDateParam retrieves an optional date parameter from a map, converting it to a twapi.Date type. It returns an error if the type conversion fails. The date format is expected to be "YYYY-MM-DD". If the target is nil, it returns an error. If the key is not found, it does not set the target.

func OptionalDatePointerParam

func OptionalDatePointerParam(
	target **twapi.Date,
	key string,
	middlewares ...ParamMiddleware[string],
) ParamFunc

OptionalDatePointerParam retrieves an optional date parameter from a map and sets it to a pointer target. It converts the value to a twapi.Date type and applies middleware functions to the value before setting it. The date format is expected to be "YYYY-MM-DD". If the target is nil, it returns an error.

func OptionalLegacyDateParam

func OptionalLegacyDateParam(
	target *projects.LegacyDate,
	key string,
	middlewares ...ParamMiddleware[string],
) ParamFunc

OptionalLegacyDateParam retrieves an optional legacy date parameter from a map, converting it to a projects.LegacyDate type. It returns an error if the type conversion fails. The date format is expected to be "YYYYMMDD". If the target is nil, it returns an error. If the key is not found, it does not set the target.

func OptionalLegacyDatePointerParam

func OptionalLegacyDatePointerParam(
	target **projects.LegacyDate,
	key string,
	middlewares ...ParamMiddleware[string],
) ParamFunc

OptionalLegacyDatePointerParam retrieves an optional date parameter from a map and sets it to a pointer target. It converts the value to a projects.LegacyDate type and applies middleware functions to the value before setting it. The date format is expected to be "YYYYMMDD". If the target is nil, it returns an error.

func OptionalListParam

func OptionalListParam[T any](target *[]T, key string) ParamFunc

OptionalListParam retrieves an optional list parameter from a map, converting each item to the specified type. It returns an error if the key is not found or if the type conversion fails. If the target is nil, it returns an error.

func OptionalNumericListParam

func OptionalNumericListParam[T int8 | int16 | int32 | int64 |
	uint8 | uint16 | uint32 | uint64 |
	float32 | float64 |
	projects.LegacyNumber](
	target *[]T, key string,
) ParamFunc

OptionalNumericListParam retrieves an optional list of numeric parameters from a map, converting each item to the specified numeric type. It returns an error if the key is not found or if the type conversion fails. If the target is nil, it returns an error.

func OptionalNumericParam

func OptionalNumericParam[T int8 | int16 | int32 | int64 |
	uint8 | uint16 | uint32 | uint64 |
	float32 | float64 |
	projects.LegacyNumber](
	target *T,
	key string,
	middlewares ...ParamMiddleware[T],
) ParamFunc

OptionalNumericParam retrieves an optional numeric parameter from a map, converting it to the target numeric type. It returns an error if the type conversion fails. If the target is nil, it returns an error.

func OptionalNumericPointerParam

func OptionalNumericPointerParam[T int8 | int16 | int32 | int64 |
	uint8 | uint16 | uint32 | uint64 |
	float32 | float64 |
	projects.LegacyNumber](
	target **T,
	key string,
	middlewares ...ParamMiddleware[T],
) ParamFunc

OptionalNumericPointerParam retrieves an optional numeric parameter from a map and sets it to a pointer target. It converts the value to the specified numeric type and applies middleware functions to the value before setting it. If the target is nil, it returns an error.

func OptionalParam

func OptionalParam[T any](target *T, key string, middlewares ...ParamMiddleware[T]) ParamFunc

OptionalParam retrieves an optional parameter from a map, converting it to the specified type. It returns an error if the type conversion fails. If the target is nil, it returns an error. It also allows for middleware functions to be applied to the value before setting it to the target. Each middleware function should return a boolean indicating whether to continue processing and an error if any issue occurs.

func OptionalPointerParam

func OptionalPointerParam[T any](target **T, key string, middlewares ...ParamMiddleware[T]) ParamFunc

OptionalPointerParam retrieves an optional parameter from a map and sets it to a pointer target. It converts the value to the specified type and applies middleware functions to the value before setting it. If the target is nil, it returns an error. The middleware functions should return a boolean indicating whether to continue processing and an error if any issue occurs. If the parameter is not found, it does not set the target pointer.

func OptionalTimeOnlyParam

func OptionalTimeOnlyParam(
	target *twapi.Time,
	key string,
	middlewares ...ParamMiddleware[string],
) ParamFunc

OptionalTimeOnlyParam retrieves an optional time parameter from a map, converting it to a twapi.Time type. It returns an error if the type conversion fails. If the target is nil, it returns an error.

func OptionalTimeOnlyPointerParam

func OptionalTimeOnlyPointerParam(
	target **twapi.Time,
	key string,
	middlewares ...ParamMiddleware[string],
) ParamFunc

OptionalTimeOnlyPointerParam retrieves an optional time parameter from a map and sets it to a pointer target. It converts the value to a twapi.Time type and applies middleware functions to the value before setting it. If the target is nil, it returns an error.

func OptionalTimeParam

func OptionalTimeParam(
	target *time.Time,
	key string,
	middlewares ...ParamMiddleware[string],
) ParamFunc

OptionalTimeParam retrieves an optional time parameter from a map, converting it to a time.Time type. It returns an error if the type conversion fails. If the target is nil, it returns an error.

func OptionalTimePointerParam

func OptionalTimePointerParam(
	target **time.Time,
	key string,
	middlewares ...ParamMiddleware[string],
) ParamFunc

OptionalTimePointerParam retrieves an optional time parameter from a map and sets it to a pointer target. It converts the value to a time.Time type and applies middleware functions to the value before setting it. If the target is nil, it returns an error.

func RequiredDateParam

func RequiredDateParam(
	target *twapi.Date,
	key string,
	middlewares ...ParamMiddleware[string],
) ParamFunc

RequiredDateParam retrieves a required date parameter from a map, converting it to a twapi.Date type. It returns an error if the key is not found or if the type conversion fails. The date format is expected to be "YYYY-MM-DD". If the target is nil, it returns an error.

func RequiredLegacyDateParam

func RequiredLegacyDateParam(
	target *projects.LegacyDate,
	key string,
	middlewares ...ParamMiddleware[string],
) ParamFunc

RequiredLegacyDateParam retrieves a required legacy date parameter from a map, converting it to a projects.LegacyDate type. It returns an error if the key is not found or if the type conversion fails. The date format is expected to be "YYYYMMDD". If the target is nil, it returns an error.

func RequiredNumericParam

func RequiredNumericParam[T int8 | int16 | int32 | int64 |
	uint8 | uint16 | uint32 | uint64 |
	float32 | float64 |
	projects.LegacyNumber](
	target *T,
	key string,
	middlewares ...ParamMiddleware[T],
) ParamFunc

RequiredNumericParam retrieves a required numeric parameter from a map, converting it to the target numeric type. It returns an error if the key is not found or if the type conversion fails. If the target is nil, it returns an error.

func RequiredParam

func RequiredParam[T any](target *T, key string, middlewares ...ParamMiddleware[T]) ParamFunc

RequiredParam retrieves a required parameter from a map, converting it to the specified type. It returns an error if the key is not found or if the type conversion fails. If the target is nil, it returns an error. It also allows for middleware functions to be applied to the value before setting it to the target. Each middleware function should return a boolean indicating whether to continue processing and an error if any issue occurs.

func RequiredTimeOnlyParam

func RequiredTimeOnlyParam(
	target *twapi.Time,
	key string,
	middlewares ...ParamMiddleware[string],
) ParamFunc

RequiredTimeOnlyParam retrieves a required time parameter from a map, converting it to a twapi.Time type. It returns an error if the key is not found or if the type conversion fails. If the target is nil, it returns an error.

func RequiredTimeParam

func RequiredTimeParam(
	target *time.Time,
	key string,
	middlewares ...ParamMiddleware[string],
) ParamFunc

RequiredTimeParam retrieves a required time parameter from a map, converting it to a time.Time type. It returns an error if the key is not found or if the type conversion fails. If the target is nil, it returns an error.

type ParamMiddleware

type ParamMiddleware[T any] func(*T) (bool, error)

ParamMiddleware defines a function type that takes a pointer to a specific type and returns a boolean indicating whether to continue processing and an error if any issue occurs. This is used to apply middleware functions to parameters before they are set to the target.

func RestrictValues

func RestrictValues[T comparable](allowedValues ...T) ParamMiddleware[T]

RestrictValues restricts the values of a parameter to a predefined set of allowed values. It can be used as a middleware function in the Param or OptionalParam functions.

type ToolArguments added in v1.6.2

type ToolArguments map[string]any

ToolArguments is a map of string keys to arbitrary values.

func NewToolArguments added in v1.6.2

func NewToolArguments(request *mcp.CallToolRequest) (ToolArguments, error)

NewToolArguments creates a ToolArguments instance from a CallToolRequest.

func (ToolArguments) GetBool added in v1.6.2

func (t ToolArguments) GetBool(key string, defaultValue bool) bool

GetBool returns a bool argument by key, or the default value if not found.

func (ToolArguments) GetBoolSlice added in v1.6.2

func (t ToolArguments) GetBoolSlice(key string, defaultValue []bool) []bool

GetBoolSlice returns a bool slice argument by key, or the default value if not found.

func (ToolArguments) GetFloat added in v1.6.2

func (t ToolArguments) GetFloat(key string, defaultValue float64) float64

GetFloat returns a float64 argument by key, or the default value if not found.

func (ToolArguments) GetFloatSlice added in v1.6.2

func (t ToolArguments) GetFloatSlice(key string, defaultValue []float64) []float64

GetFloatSlice returns a float64 slice argument by key, or the default value if not found.

func (ToolArguments) GetInt added in v1.6.2

func (t ToolArguments) GetInt(key string, defaultValue int) int

GetInt returns an int argument by key, or the default value if not found.

func (ToolArguments) GetIntSlice added in v1.6.2

func (t ToolArguments) GetIntSlice(key string, defaultValue []int) []int

GetIntSlice returns an int slice argument by key, or the default value if not found.

func (ToolArguments) GetString added in v1.6.2

func (t ToolArguments) GetString(key string, defaultValue string) string

GetString returns a string argument by key, or the default value if not found.

func (ToolArguments) GetStringSlice added in v1.6.2

func (t ToolArguments) GetStringSlice(key string, defaultValue []string) []string

GetStringSlice returns a string slice argument by key, or the default value if not found.

func (ToolArguments) RequireBool added in v1.6.2

func (t ToolArguments) RequireBool(key string) (bool, error)

RequireBool returns a bool argument by key, or an error if not found or not convertible to bool.

func (ToolArguments) RequireBoolSlice added in v1.6.2

func (t ToolArguments) RequireBoolSlice(key string) ([]bool, error)

RequireBoolSlice returns a bool slice argument by key, or an error if not found or not convertible to bool slice.

func (ToolArguments) RequireFloat added in v1.6.2

func (t ToolArguments) RequireFloat(key string) (float64, error)

RequireFloat returns a float64 argument by key, or an error if not found or not convertible to float64.

func (ToolArguments) RequireFloatSlice added in v1.6.2

func (t ToolArguments) RequireFloatSlice(key string) ([]float64, error)

RequireFloatSlice returns a float64 slice argument by key, or an error if not found or not convertible to float64 slice.

func (ToolArguments) RequireInt added in v1.6.2

func (t ToolArguments) RequireInt(key string) (int, error)

RequireInt returns an int argument by key, or an error if not found or not convertible to int.

func (ToolArguments) RequireIntSlice added in v1.6.2

func (t ToolArguments) RequireIntSlice(key string) ([]int, error)

RequireIntSlice returns an int slice argument by key, or an error if not found or not convertible to int slice.

func (ToolArguments) RequireString added in v1.6.2

func (t ToolArguments) RequireString(key string) (string, error)

RequireString returns a string argument by key, or an error if not found or not a string.

func (ToolArguments) RequireStringSlice added in v1.6.2

func (t ToolArguments) RequireStringSlice(key string) ([]string, error)

RequireStringSlice returns a string slice argument by key, or an error if not found or not convertible to string slice.

type WebLinkerOption

type WebLinkerOption func(*WebLinkerOptions)

WebLinkerOption is a function that configures the WebLinkerOptions.

func WebLinkerWithIgnoreFields

func WebLinkerWithIgnoreFields(fields ...string) WebLinkerOption

WebLinkerWithIgnoreFields creates an option to specify additional fields that should be ignored when processing JSON data for web link injection. These fields will be skipped in addition to the default knownRootFields ("meta" and "included").

type WebLinkerOptions

type WebLinkerOptions struct {
	// contains filtered or unexported fields
}

WebLinkerOptions holds configuration options for the WebLinker function.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL