test

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package test provides utilities for testing the MCP implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewServer

func NewServer(name string) server.Server

NewServer creates a new server for testing

func WithLogger

func WithLogger(logger *slog.Logger) server.Option

WithLogger creates a logger option for the server

Types

type ClientInfo

type ClientInfo struct {
	SamplingSupported bool
	SamplingCaps      SamplingCapabilities
	ProtocolVersion   string
}

ClientInfo represents information about a client

type ClientSession

type ClientSession struct {
	ID         SessionID
	ClientInfo ClientInfo
	Metadata   map[string]interface{}
}

ClientSession represents a client session

type Context

type Context struct {
	Request  *Request
	Response *Response
	Version  string
	Metadata map[string]interface{}
}

Context represents the context for a request/response cycle

type MockTransport

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

MockTransport is a simple mock transport for testing

func NewMockTransport

func NewMockTransport() *MockTransport

NewMockTransport creates a new MockTransport

func (*MockTransport) ClearMessages

func (m *MockTransport) ClearMessages()

ClearMessages clears the messages

func (*MockTransport) ClearRequestHistory

func (m *MockTransport) ClearRequestHistory()

ClearRequestHistory clears the request history

func (*MockTransport) Connect

func (m *MockTransport) Connect() error

Connect simulates connecting the transport

func (*MockTransport) ConnectWithContext

func (m *MockTransport) ConnectWithContext(ctx context.Context) error

ConnectWithContext simulates connecting the transport with context

func (*MockTransport) Disconnect

func (m *MockTransport) Disconnect() error

Disconnect simulates disconnecting the transport

func (*MockTransport) GetMessages

func (m *MockTransport) GetMessages() [][]byte

GetMessages returns the messages sent to the transport

func (*MockTransport) GetRequestHistory

func (m *MockTransport) GetRequestHistory() [][]byte

GetRequestHistory returns the request history

func (*MockTransport) GetSendHandler

func (m *MockTransport) GetSendHandler() func([]byte)

GetSendHandler returns a function to call HandleJSONRPCResponse

func (*MockTransport) RegisterNotificationHandler

func (m *MockTransport) RegisterNotificationHandler(handler func(method string, params []byte))

RegisterNotificationHandler registers a handler for notifications

func (*MockTransport) Send

func (m *MockTransport) Send(message []byte) ([]byte, error)

Send implements the server.Transport interface

func (*MockTransport) SendAsync

func (m *MockTransport) SendAsync(ctx context.Context, message []byte) error

SendAsync implements the server.Transport interface

func (*MockTransport) SendWithContext

func (m *MockTransport) SendWithContext(ctx context.Context, message []byte) ([]byte, error)

SendWithContext sends a message with context

func (*MockTransport) SetConnectionTimeout

func (m *MockTransport) SetConnectionTimeout(timeout time.Duration)

SetConnectionTimeout sets the timeout for connection

func (*MockTransport) SetHandler

func (m *MockTransport) SetHandler(handler func([]byte))

SetHandler implements the server.Transport interface

func (*MockTransport) SetRequestTimeout

func (m *MockTransport) SetRequestTimeout(timeout time.Duration)

SetRequestTimeout sets the timeout for requests

func (*MockTransport) SetResponseDelay

func (m *MockTransport) SetResponseDelay(delay time.Duration)

SetResponseDelay sets the delay for responses

func (*MockTransport) SetSendCallback

func (m *MockTransport) SetSendCallback(callback func(message []byte) ([]byte, error))

SetSendCallback sets the callback for Send

func (*MockTransport) SetSendFunc

func (m *MockTransport) SetSendFunc(sendFunc func(data []byte) error)

SetSendFunc sets the function for Send

func (*MockTransport) SimulateMessage

func (m *MockTransport) SimulateMessage(message []byte)

SimulateMessage simulates receiving a message from the client

type Request

type Request struct {
	JSONRPC  string                 `json:"jsonrpc"`
	ID       interface{}            `json:"id,omitempty"`
	Method   string                 `json:"method"`
	Params   map[string]interface{} `json:"params,omitempty"`
	ToolName string                 `json:"name,omitempty"`
	ToolArgs map[string]interface{} `json:"arguments,omitempty"`
}

Request represents a JSON-RPC request

type RequestSamplingOptions

type RequestSamplingOptions struct {
	Timeout          time.Duration
	MaxRetries       int
	RetryInterval    time.Duration
	IgnoreCapability bool
	ForceSession     bool
}

RequestSamplingOptions defines options for sampling requests

func DefaultSamplingOptions

func DefaultSamplingOptions() RequestSamplingOptions

DefaultSamplingOptions returns the default options for sampling requests

type Response

type Response struct {
	JSONRPC string      `json:"jsonrpc"`
	ID      interface{} `json:"id,omitempty"`
	Result  interface{} `json:"result,omitempty"`
	Error   interface{} `json:"error,omitempty"`
}

Response represents a JSON-RPC response

type SamplingCapabilities

type SamplingCapabilities struct {
	Supported    bool
	TextSupport  bool
	ImageSupport bool
	AudioSupport bool
}

SamplingCapabilities represents client capabilities for sampling

func DetectClientCapabilities

func DetectClientCapabilities(protocolVersion string) SamplingCapabilities

DetectClientCapabilities detects client capabilities based on protocol version

type SamplingMessage

type SamplingMessage struct {
	Role    string                 `json:"role"`
	Content SamplingMessageContent `json:"content"`
}

SamplingMessage represents a message in a sampling conversation

func CreateAudioSamplingMessage

func CreateAudioSamplingMessage(role, audioData, mimeType string) SamplingMessage

CreateAudioSamplingMessage creates an audio sampling message

func CreateImageSamplingMessage

func CreateImageSamplingMessage(role, imageData, mimeType string) SamplingMessage

CreateImageSamplingMessage creates an image sampling message

func CreateTextSamplingMessage

func CreateTextSamplingMessage(role, text string) SamplingMessage

CreateTextSamplingMessage creates a text sampling message

type SamplingMessageContent

type SamplingMessageContent struct {
	Type     string `json:"type"`
	Text     string `json:"text,omitempty"`
	Data     string `json:"data,omitempty"`
	MimeType string `json:"mimeType,omitempty"`
}

SamplingMessageContent represents the content of a sampling message

func (SamplingMessageContent) IsValidForVersion

func (c SamplingMessageContent) IsValidForVersion(version string) bool

IsValidForVersion checks if the content type is valid for the given protocol version

type SamplingModelHint

type SamplingModelHint struct {
	Name string `json:"name"`
}

SamplingModelHint represents a hint for model selection in sampling requests

type SamplingModelPreferences

type SamplingModelPreferences struct {
	Hints                []SamplingModelHint `json:"hints,omitempty"`
	CostPriority         *float64            `json:"costPriority,omitempty"`
	SpeedPriority        *float64            `json:"speedPriority,omitempty"`
	IntelligencePriority *float64            `json:"intelligencePriority,omitempty"`
}

SamplingModelPreferences represents the model preferences for a sampling request

type SamplingResponse

type SamplingResponse struct {
	Role       string                 `json:"role"`
	Content    SamplingMessageContent `json:"content"`
	Model      string                 `json:"model,omitempty"`
	StopReason string                 `json:"stopReason,omitempty"`
}

SamplingResponse represents the response to a sampling/createMessage request

type SessionID

type SessionID string

SessionID represents the ID of a client session

type SessionManager

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

SessionManager manages client sessions

func NewSessionManager

func NewSessionManager() *SessionManager

NewSessionManager creates a new session manager

func (*SessionManager) CloseSession

func (sm *SessionManager) CloseSession(id SessionID) bool

CloseSession closes a client session

func (*SessionManager) CreateSession

func (sm *SessionManager) CreateSession(clientInfo ClientInfo, protocolVersion string) *ClientSession

CreateSession creates a new client session

func (*SessionManager) GetSession

func (sm *SessionManager) GetSession(id SessionID) (*ClientSession, bool)

GetSession gets a client session by ID

func (*SessionManager) UpdateClientCapabilities

func (sm *SessionManager) UpdateClientCapabilities(id SessionID, caps SamplingCapabilities) bool

UpdateClientCapabilities updates client capabilities

func (*SessionManager) UpdateSession

func (sm *SessionManager) UpdateSession(id SessionID, updateFn func(*ClientSession)) bool

UpdateSession updates a client session

type ToolHandler

type ToolHandler func(*Context, interface{}) (interface{}, error)

ToolHandler is the type of function that handles tool calls

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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