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
- Variables
- func BuildFunctionRequest(functionID string, params map[string]interface{}) (*pb.FunctionRequest, error)
- func GRPCToJSONRPCError(err error) *pb.ErrorInfo
- func JSONRPCToGRPCError(jsonError *pb.ErrorInfo) error
- func MapFromJSONRPCResponse(jsonResp map[string]interface{}, functionID, requestID string) (*pb.FunctionResponse, error)
- func MapToJSONRPCRequest(req *pb.FunctionRequest) (map[string]interface{}, error)
- func ProtoToValue(val *pb.Value) (interface{}, error)
- func ValueToProto(val interface{}) (*pb.Value, error)
- type Option
- type Transport
Constants ¶
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 ¶
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.
var ErrorMap = map[int]codes.Code{ -32700: codes.InvalidArgument, -32600: codes.InvalidArgument, -32601: codes.Unimplemented, -32602: codes.InvalidArgument, -32603: codes.Internal, -32000: codes.Internal, 1000: codes.Internal, 1001: codes.Unauthenticated, 1002: codes.PermissionDenied, 1003: codes.Aborted, 1004: codes.Internal, 1005: codes.NotFound, 1006: codes.ResourceExhausted, 1007: codes.Unavailable, 1008: codes.DeadlineExceeded, 1009: codes.Unavailable, 1010: codes.FailedPrecondition, }
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 ¶
GRPCToJSONRPCError converts a gRPC status error to a JSON-RPC error
func JSONRPCToGRPCError ¶
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 ¶
ProtoToValue converts a Protocol Buffer Value to a Go value
func ValueToProto ¶
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 WithBufferSize ¶
WithBufferSize sets the buffer size for message channels.
func WithConnectionTimeout ¶
WithConnectionTimeout sets the connection timeout.
func WithKeepAliveParams ¶
WithKeepAliveParams sets the keepalive parameters.
func WithMaxMessageSize ¶
WithMaxMessageSize sets the maximum message size.
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 ¶
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 ¶
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 ¶
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 ¶
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.