grpc

package
v1.2.2 Latest Latest
Warning

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

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

Documentation

Overview

Package grpc provides a gRPC transport implementation for the MCP protocol.

This transport uses gRPC for communication between clients and servers, offering bi-directional streaming and strongly-typed interactions. It supports both client and server modes, TLS encryption, and various configuration options for fine-tuning performance and reliability.

Index

Constants

View Source
const (
	DefaultPort              = 50051
	DefaultConnectionTimeout = 10 * time.Second
	DefaultMaxMessageSize    = 4 * 1024 * 1024 // 4MB
	DefaultKeepAliveTime     = 10 * time.Second
	DefaultKeepAliveTimeout  = 3 * time.Second
	DefaultBufferSize        = 100
)

Default configuration values.

Variables

View Source
var (
	ErrNotInitialized = errors.New("transport not initialized")
	ErrAlreadyRunning = errors.New("transport already running")
	ErrNotRunning     = errors.New("transport not running")
	ErrInvalidConfig  = errors.New("invalid transport configuration")
)

Transport errors.

ErrorMap defines mappings between JSON-RPC error codes and gRPC status codes

Functions

func BuildFunctionRequest

func BuildFunctionRequest(functionID string, params map[string]interface{}) (*pb.FunctionRequest, error)

BuildFunctionRequest creates a FunctionRequest from parameters

func GRPCToJSONRPCError

func GRPCToJSONRPCError(err error) *pb.ErrorInfo

GRPCToJSONRPCError converts a gRPC status error to a JSON-RPC error

func JSONRPCToGRPCError

func JSONRPCToGRPCError(jsonError *pb.ErrorInfo) error

JSONRPCToGRPCError converts a JSON-RPC error to a gRPC status error

func MapFromJSONRPCResponse

func MapFromJSONRPCResponse(jsonResp map[string]interface{}, functionID, requestID string) (*pb.FunctionResponse, error)

MapFromJSONRPCResponse converts a JSON-RPC response to a Protocol Buffer FunctionResponse

func MapToJSONRPCRequest

func MapToJSONRPCRequest(req *pb.FunctionRequest) (map[string]interface{}, error)

MapToJSONRPCRequest converts a Protocol Buffer FunctionRequest to a JSON-RPC request

func ProtoToValue

func ProtoToValue(val *pb.Value) (interface{}, error)

ProtoToValue converts a Protocol Buffer Value to a Go value

func ValueToProto

func ValueToProto(val interface{}) (*pb.Value, error)

ValueToProto converts a Go value to a Protocol Buffer Value

Types

type Option

type Option func(*Transport)

Option represents a configuration option for the gRPC transport.

func WithAddress

func WithAddress(address string) Option

WithAddress sets the server address.

func WithBufferSize

func WithBufferSize(size int) Option

WithBufferSize sets the buffer size for message channels.

func WithConnectionTimeout

func WithConnectionTimeout(timeout time.Duration) Option

WithConnectionTimeout sets the connection timeout.

func WithKeepAliveParams

func WithKeepAliveParams(time, timeout time.Duration) Option

WithKeepAliveParams sets the keepalive parameters.

func WithMaxMessageSize

func WithMaxMessageSize(size int) Option

WithMaxMessageSize sets the maximum message size.

func WithTLS

func WithTLS(certFile, keyFile, caFile string) Option

WithTLS enables TLS with the provided credentials.

type Transport

type Transport struct {
	// Base transport functionality
	transport.BaseTransport
	// contains filtered or unexported fields
}

Transport implements the transport.Transport interface using gRPC.

It provides bidirectional communication between clients and servers using the Protocol Buffers serialization format and gRPC streaming capabilities. The Transport can operate in both server and client modes.

func NewTransport

func NewTransport(address string, isServer bool, options ...Option) *Transport

NewTransport creates a new gRPC transport.

The address parameter is used differently depending on the value of isServer:

  • For server mode (isServer=true): a local address to bind to, e.g., ":50051"
  • For client mode (isServer=false): the server address to connect to, e.g., "localhost:50051"

Optional configuration can be provided via Option functions.

func (*Transport) Initialize

func (t *Transport) Initialize() error

Initialize initializes the transport.

This method sets up internal channels and prepares the transport for operation. It must be called before Start() and any attempts to send or receive messages.

func (*Transport) Receive

func (t *Transport) Receive() ([]byte, error)

Receive receives a message from the transport.

This method blocks until a message is received, an error occurs, or the transport is stopped.

In client mode, this receives a message from the server. In server mode, this receives a message from a connected client.

func (*Transport) Send

func (t *Transport) Send(message []byte) error

Send sends a message through the transport.

In client mode, this sends a message to the server. In server mode, this sends a message to the connected client.

This method returns an error if the transport is not running or if the message cannot be sent.

func (*Transport) Start

func (t *Transport) Start() error

Start starts the transport.

In server mode, this starts a gRPC server that listens on the configured address. In client mode, this establishes a connection to the server.

This method must be called after Initialize().

func (*Transport) Stop

func (t *Transport) Stop() error

Stop stops the transport.

In server mode, this shuts down the gRPC server. In client mode, this closes the connection to the server.

Any ongoing operations will be terminated.

Directories

Path Synopsis
proto
gen

Jump to

Keyboard shortcuts

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