usecase

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrToolNotFound = errors.New("tool not found")
)

Standard errors returned by use cases and adapters.

Functions

This section is empty.

Types

type InvocationDetails

type InvocationDetails struct {
	// Type helps the invoker understand the nature of the call (e.g., "http", "connect_http", "grpc").
	Type string `json:"type"`

	// Host is the base URL of the target service (e.g., "http://localhost:8080" or "grpc://localhost:50051").
	Host string `json:"host"`

	// BasePath is the extracted from OpenAPI servers (e.g., "/api/v1").
	BasePath string `json:"base_path,omitempty"`

	// HTTPMethod is the HTTP verb (e.g., "POST", "GET").
	HTTPMethod string `json:"http_method,omitempty"`

	// HTTPPath is the request path (e.g., "/users/{userId}", "/com.example.UserService/GetUser").
	HTTPPath string `json:"http_path,omitempty"`

	// PathParams lists the names of parameters expected to be substituted into the HTTPPath.
	PathParams []string `json:"path_params,omitempty"`

	// QueryParams lists the names of parameters expected to be sent as URL query arguments.
	QueryParams []string `json:"query_params,omitempty"`

	// HeaderParams defines static headers to be included in the request.
	// Dynamic headers (e.g., from tool parameters) might be handled separately by the invoker.
	HeaderParams map[string]string `json:"header_params,omitempty"`

	// BodyParam indicates which single tool input parameter should be marshalled as the HTTP request body.
	// If empty, the request body might be constructed from multiple parameters or be absent.
	BodyParam string `json:"body_param,omitempty"`

	// gRPC specific fields
	// GRPCService is the full service name (e.g., "hello.HelloService")
	GRPCService string `json:"grpc_service,omitempty"`

	// GRPCMethod is the method name (e.g., "SayHello")
	GRPCMethod string `json:"grpc_method,omitempty"`

	// For .proto files: Server is the actual gRPC server endpoint
	Server string `json:"server,omitempty"`

	// For .proto files: Method is the full method path (e.g., "/package.Service/Method")
	Method string `json:"method,omitempty"`

	// For .proto files: Input and Output type names
	InputType  string `json:"input_type,omitempty"`
	OutputType string `json:"output_type,omitempty"`

	// For .proto files: File descriptor for dynamic invocation
	FileDescriptor interface{} `json:"file_descriptor,omitempty"`

	// ContentType indicates the expected Content-Type for the request body (e.g., "application/json").
	// Defaults to application/json if involving a body.
	ContentType string `json:"content_type,omitempty"`
}

InvocationDetails holds the necessary information to call an upstream API corresponding to a tool. Supports both HTTP-based calls (including Connect RPC) and native gRPC calls.

type InvokeToolUseCase

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

InvokeToolUseCase handles receiving a tool invocation request and executing it.

func NewInvokeToolUseCase

func NewInvokeToolUseCase(repo ToolRepository, invoker ToolInvoker, logger *slog.Logger) *InvokeToolUseCase

NewInvokeToolUseCase creates a new InvokeToolUseCase.

func (*InvokeToolUseCase) Execute

func (uc *InvokeToolUseCase) Execute(ctx context.Context, toolName string, params map[string]interface{}) (interface{}, error)

Execute finds the tool, finds invocation details, validates parameters (optional), and uses the ToolInvoker to call the upstream API.

type MCPServerAdapter

type MCPServerAdapter interface {
	// AddTool registers a tool and its handler with the server.
	// The handlerFunc signature must match the expected signature of the specific
	// MCP server library being adapted.
	// Use the specific type from the mcp-go/server package.
	AddTool(tool mcp.Tool, handlerFunc mcpGoServer.ToolHandlerFunc)
}

MCPServerAdapter defines the interface required by the SyncSchemaUseCase to interact with the underlying MCP server (like mcp-go). This avoids direct dependency on a specific server implementation in the use case.

type SchemaFetcher

type SchemaFetcher interface {
	Fetch(ctx context.Context, source string) (domain.APISchema, error)
	FetchWithConfig(ctx context.Context, config SchemaSourceConfig) (domain.APISchema, error)
}

SchemaFetcher defines the interface for fetching API schemas from various sources.

type SchemaSourceConfig

type SchemaSourceConfig struct {
	URL     string
	Headers map[string]string
	Server  string // For .proto files, the gRPC server endpoint
	Type    string // Schema type override (e.g., "connect" for Connect-RPC)
	Mode    string // Invocation mode (e.g., "http" or "grpc" for Connect-RPC)
}

SchemaSourceConfig represents a schema source with optional configuration

type ServeToolsUseCase

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

ServeToolsUseCase provides the functionality to list available tools.

func NewServeToolsUseCase

func NewServeToolsUseCase(repository ToolRepository, logger *slog.Logger) *ServeToolsUseCase

NewServeToolsUseCase creates a new ServeToolsUseCase.

func (*ServeToolsUseCase) Execute

func (uc *ServeToolsUseCase) Execute(ctx context.Context) ([]domain.Tool, error)

Execute retrieves all tools currently stored in the repository.

type SyncSchemaUseCase

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

SyncSchemaUseCase orchestrates fetching, generating, and registering tools with an MCP server.

func NewSyncSchemaUseCase

func NewSyncSchemaUseCase(
	schemaSources []SchemaSourceConfig,
	fetchers map[domain.SchemaType]SchemaFetcher,
	generators map[domain.SchemaType]ToolGenerator,
	mcpSrv MCPServerAdapter,
	invoker ToolInvoker,
	logger *slog.Logger,
) *SyncSchemaUseCase

NewSyncSchemaUseCase creates a new SyncSchemaUseCase.

func (*SyncSchemaUseCase) Execute

func (uc *SyncSchemaUseCase) Execute(ctx context.Context, source string) error

Execute method now uses the interface implicitly via processSingleSourceAndRegister

func (*SyncSchemaUseCase) SyncAllConfiguredSources

func (uc *SyncSchemaUseCase) SyncAllConfiguredSources(ctx context.Context) error

SyncAllConfiguredSources fetches schemas from all configured sources, generates tools for each, and registers them with the MCP server. It returns a joined error if any source fails, but attempts to process all sources.

type ToolGenerator

type ToolGenerator interface {
	Generate(schema domain.APISchema) ([]domain.Tool, []InvocationDetails, error)
}

ToolGenerator defines the interface for generating Tools and InvocationDetails from a fetched APISchema.

type ToolInvoker

type ToolInvoker interface {
	Invoke(ctx context.Context, details InvocationDetails, params map[string]interface{}) (interface{}, error)
}

ToolInvoker defines the contract for executing the actual upstream API call. Implementations will handle making HTTP requests (potentially using Connect client).

type ToolRepository

type ToolRepository interface {
	// Save stores a list of tools and their associated invocation details.
	// It should ensure that the length of tools and details match and correspond by index,
	// or handle potential mismatches appropriately.
	Save(ctx context.Context, tools []domain.Tool, details []InvocationDetails) error

	// List retrieves all currently stored tools.
	List(ctx context.Context) ([]domain.Tool, error)

	// FindToolByName retrieves a specific tool definition by its unique name.
	FindToolByName(ctx context.Context, name string) (*domain.Tool, error)

	// FindInvocationDetailsByName retrieves the invocation details for a specific tool by name.
	FindInvocationDetailsByName(ctx context.Context, name string) (*InvocationDetails, error)
}

ToolRepository defines the contract for storing and retrieving generated Tools and their InvocationDetails. Implementations could range from in-memory stores to persistent databases.

Jump to

Keyboard shortcuts

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