adapter

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package adapter provides protocol-agnostic tool format conversion. It enables bidirectional transformation between MCP, OpenAI, and Anthropic tool definitions through a canonical intermediate representation.

This is a pure data-transform library with no I/O, network, or runtime execution.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter interface {
	// Name returns the adapter's identifier (e.g., "mcp", "openai", "anthropic")
	Name() string

	// ToCanonical converts a protocol-specific tool to canonical format.
	// The raw parameter type depends on the adapter (e.g., mcp.Tool, OpenAIFunction).
	ToCanonical(raw any) (*CanonicalTool, error)

	// FromCanonical converts a canonical tool to the protocol-specific format.
	// The returned type depends on the adapter.
	FromCanonical(tool *CanonicalTool) (any, error)

	// SupportsFeature returns whether this adapter supports a schema feature.
	// Features not supported will generate warnings during conversion.
	SupportsFeature(feature SchemaFeature) bool
}

Adapter defines the contract for protocol-specific tool adapters. Each adapter handles bidirectional conversion between a specific tool format (MCP, OpenAI, Anthropic) and the canonical representation.

Contract:

  • Thread-safety: implementations must be safe for concurrent use unless documented otherwise.
  • Ownership: must not mutate caller-owned inputs; return new objects on conversion.
  • Errors: ToCanonical/FromCanonical must return typed errors (e.g., *ConversionError) for invalid input.
  • Determinism: same input yields structurally equivalent canonical output.

type AdapterRegistry

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

AdapterRegistry is a thread-safe registry of protocol adapters.

func NewRegistry

func NewRegistry() *AdapterRegistry

NewRegistry creates a new empty adapter registry.

func (*AdapterRegistry) Convert

func (r *AdapterRegistry) Convert(tool any, fromFormat, toFormat string) (*ConversionResult, error)

Convert transforms a tool from one format to another. It uses the source adapter's ToCanonical and the target adapter's FromCanonical. Returns warnings if schema features are lost during conversion.

func (*AdapterRegistry) Get

func (r *AdapterRegistry) Get(name string) (Adapter, error)

Get retrieves an adapter by name. Returns an error if the adapter is not found.

func (*AdapterRegistry) List

func (r *AdapterRegistry) List() []string

List returns the names of all registered adapters.

func (*AdapterRegistry) Register

func (r *AdapterRegistry) Register(a Adapter) error

Register adds an adapter to the registry. Returns an error if an adapter with the same name is already registered.

func (*AdapterRegistry) Unregister

func (r *AdapterRegistry) Unregister(name string) error

Unregister removes an adapter from the registry. Returns an error if the adapter is not found.

type CanonicalTool

type CanonicalTool struct {
	// Namespace groups related tools (e.g., "github", "slack")
	Namespace string

	// Name is the tool's identifier (required)
	Name string

	// Version is the semantic version of the tool
	Version string

	// Description explains what the tool does
	Description string

	// Category classifies the tool's purpose
	Category string

	// Tags are keywords for discovery
	Tags []string

	// InputSchema defines the tool's input parameters (required)
	InputSchema *JSONSchema

	// OutputSchema defines the tool's output format
	OutputSchema *JSONSchema

	// Timeout is the maximum execution time
	Timeout time.Duration

	// SourceFormat is the original format (e.g., "mcp", "openai", "anthropic")
	SourceFormat string

	// SourceMeta contains format-specific metadata for round-trip conversion
	SourceMeta map[string]any

	// RequiredScopes are authorization scopes needed to use the tool
	RequiredScopes []string
}

CanonicalTool is the protocol-agnostic representation of a tool definition. It serves as the intermediate format for converting between MCP, OpenAI, and Anthropic tool formats.

func (*CanonicalTool) ID

func (t *CanonicalTool) ID() string

ID returns the tool's fully qualified identifier. If Namespace is set, returns "namespace:name", otherwise just "name".

func (*CanonicalTool) Validate

func (t *CanonicalTool) Validate() error

Validate checks that the tool has all required fields. Returns an error if Name or InputSchema is missing.

type ConversionError

type ConversionError struct {
	// Adapter is the name of the adapter that encountered the error
	Adapter string

	// Direction is "to_canonical" or "from_canonical"
	Direction string

	// Cause is the underlying error
	Cause error
}

ConversionError represents an error during tool format conversion.

func (*ConversionError) Error

func (e *ConversionError) Error() string

Error returns a formatted error message including adapter, direction, and cause.

func (*ConversionError) Unwrap

func (e *ConversionError) Unwrap() error

Unwrap returns the underlying cause for use with errors.Is and errors.As.

type ConversionResult

type ConversionResult struct {
	// Tool is the converted tool in the target format
	Tool any

	// Warnings lists features that may have been lost during conversion
	Warnings []FeatureLossWarning
}

ConversionResult contains the result of a format conversion.

type FeatureLossWarning

type FeatureLossWarning struct {
	// Feature is the schema feature that will be lost
	Feature SchemaFeature

	// FromAdapter is the source adapter name
	FromAdapter string

	// ToAdapter is the target adapter name
	ToAdapter string
}

FeatureLossWarning indicates that a schema feature will be lost during conversion. This is a warning, not an error - the conversion proceeds but with reduced fidelity.

func (FeatureLossWarning) String

func (w FeatureLossWarning) String() string

String returns a human-readable warning message.

type JSONSchema

type JSONSchema struct {
	// Type is the JSON type (object, array, string, number, integer, boolean, null)
	Type string

	// Properties maps property names to their schemas (for object types)
	Properties map[string]*JSONSchema

	// Required lists property names that must be present
	Required []string

	// Items is the schema for array elements
	Items *JSONSchema

	// Description explains the schema
	Description string

	// Enum restricts values to a fixed set
	Enum []any

	// Const restricts to a single value
	Const any

	// Default is the default value
	Default any

	// Minimum is the minimum numeric value
	Minimum *float64

	// Maximum is the maximum numeric value
	Maximum *float64

	// MinLength is the minimum string length
	MinLength *int

	// MaxLength is the maximum string length
	MaxLength *int

	// Pattern is a regex pattern for string validation
	Pattern string

	// Format is a semantic format (e.g., "email", "uri", "date-time")
	Format string

	// Ref is a JSON Pointer reference to another schema ($ref)
	Ref string

	// Defs contains schema definitions ($defs)
	Defs map[string]*JSONSchema

	// AdditionalProperties controls whether extra properties are allowed
	AdditionalProperties *bool

	// AnyOf allows any of the listed schemas
	AnyOf []*JSONSchema

	// OneOf requires exactly one of the listed schemas
	OneOf []*JSONSchema

	// AllOf requires all of the listed schemas
	AllOf []*JSONSchema

	// Not disallows the specified schema
	Not *JSONSchema
}

JSONSchema represents a JSON Schema definition. It is a superset supporting features from MCP, OpenAI, and Anthropic formats.

func (*JSONSchema) DeepCopy

func (s *JSONSchema) DeepCopy() *JSONSchema

DeepCopy creates a deep copy of the JSONSchema. Returns nil if the receiver is nil.

func (*JSONSchema) ToMap

func (s *JSONSchema) ToMap() map[string]any

ToMap converts the JSONSchema to a map[string]any representation. Zero-valued fields are omitted from the output.

type SchemaFeature

type SchemaFeature int

SchemaFeature represents a JSON Schema feature that may or may not be supported by a particular protocol adapter.

const (
	// FeatureRef is the $ref keyword for schema references
	FeatureRef SchemaFeature = iota
	// FeatureDefs is the $defs keyword for schema definitions
	FeatureDefs
	// FeatureAnyOf allows any of the listed schemas
	FeatureAnyOf
	// FeatureOneOf requires exactly one of the listed schemas
	FeatureOneOf
	// FeatureAllOf requires all of the listed schemas
	FeatureAllOf
	// FeatureNot disallows the specified schema
	FeatureNot
	// FeaturePattern is regex pattern validation
	FeaturePattern
	// FeatureFormat is semantic format validation
	FeatureFormat
	// FeatureAdditionalProperties controls extra property handling
	FeatureAdditionalProperties
	// FeatureMinimum is minimum numeric value
	FeatureMinimum
	// FeatureMaximum is maximum numeric value
	FeatureMaximum
	// FeatureMinLength is minimum string length
	FeatureMinLength
	// FeatureMaxLength is maximum string length
	FeatureMaxLength
	// FeatureEnum restricts to a set of values
	FeatureEnum
	// FeatureConst restricts to a single value
	FeatureConst
	// FeatureDefault provides a default value
	FeatureDefault
)

func AllFeatures

func AllFeatures() []SchemaFeature

AllFeatures returns all known schema features in a stable order.

func (SchemaFeature) String

func (f SchemaFeature) String() string

String returns the JSON Schema keyword name for this feature.

Jump to

Keyboard shortcuts

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