transport

package
v1.0.178 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package transport defines the minimal interfaces and shared types for MCP transports used by the client and server. Concrete implementations live in subpackages (stdio, httptransport, sse, localtransport).

Typical usage is indirect through mcp.Client and mcp.Server, which accept a transport.Transport.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseJSONRPCError

type BaseJSONRPCError struct {
	// Error corresponds to the JSON schema field "error".
	Error BaseJSONRPCErrorInner `json:"error" yaml:"error" mapstructure:"error"`

	// Id corresponds to the JSON schema field "id".
	Id RequestId `json:"id" yaml:"id" mapstructure:"id"`

	// Jsonrpc corresponds to the JSON schema field "jsonrpc".
	Jsonrpc string `json:"jsonrpc" yaml:"jsonrpc" mapstructure:"jsonrpc"`
}

A response to a request that indicates an error occurred.

type BaseJSONRPCErrorInner

type BaseJSONRPCErrorInner struct {
	// The error type that occurred.
	Code int `json:"code" yaml:"code" mapstructure:"code"`

	// Additional information about the error. The value of this member is defined by
	// the sender (e.g. detailed error information, nested errors etc.).
	Data any `json:"data,omitempty" yaml:"data,omitempty" mapstructure:"data,omitempty"`

	// A short description of the error. The message SHOULD be limited to a concise
	// single sentence.
	Message string `json:"message" yaml:"message" mapstructure:"message"`
}

type BaseJSONRPCNotification

type BaseJSONRPCNotification struct {
	// Jsonrpc corresponds to the JSON schema field "jsonrpc".
	Jsonrpc string `json:"jsonrpc" yaml:"jsonrpc" mapstructure:"jsonrpc"`

	// Method corresponds to the JSON schema field "method".
	Method string `json:"method" yaml:"method" mapstructure:"method"`

	// Params corresponds to the JSON schema field "params".
	// It is stored as a []byte to enable efficient marshaling and unmarshaling into custom types later on in the protocol
	Params json.RawMessage `json:"params,omitempty" yaml:"params,omitempty" mapstructure:"params,omitempty"`
}

func (*BaseJSONRPCNotification) UnmarshalJSON

func (m *BaseJSONRPCNotification) UnmarshalJSON(data []byte) error

Custom Notification unmarshaling Requires a Jsonrpc and Method

type BaseJSONRPCRequest

type BaseJSONRPCRequest struct {
	// Id corresponds to the JSON schema field "id".
	Id RequestId `json:"id" yaml:"id" mapstructure:"id"`

	// Jsonrpc corresponds to the JSON schema field "jsonrpc".
	Jsonrpc string `json:"jsonrpc" yaml:"jsonrpc" mapstructure:"jsonrpc"`

	// Method corresponds to the JSON schema field "method".
	Method string `json:"method" yaml:"method" mapstructure:"method"`

	// Params corresponds to the JSON schema field "params".
	// It is stored as a []byte to enable efficient marshaling and unmarshaling into custom types later on in the protocol
	Params json.RawMessage `json:"params,omitempty" yaml:"params,omitempty" mapstructure:"params,omitempty"`
}

func (*BaseJSONRPCRequest) UnmarshalJSON

func (m *BaseJSONRPCRequest) UnmarshalJSON(data []byte) error

Custom Request unmarshaling Requires an Id, Jsonrpc and Method

type BaseJSONRPCResponse

type BaseJSONRPCResponse struct {
	// Id corresponds to the JSON schema field "id".
	Id RequestId `json:"id" yaml:"id" mapstructure:"id"`

	// Jsonrpc corresponds to the JSON schema field "jsonrpc".
	Jsonrpc string `json:"jsonrpc" yaml:"jsonrpc" mapstructure:"jsonrpc"`

	// Result corresponds to the JSON schema field "result".
	Result json.RawMessage `json:"result" yaml:"result" mapstructure:"result"`
}

func (*BaseJSONRPCResponse) UnmarshalJSON

func (m *BaseJSONRPCResponse) UnmarshalJSON(data []byte) error

Custom Response unmarshaling Requires an Id, Jsonrpc and Result

type BaseJsonRpcMessage

type BaseJsonRpcMessage struct {
	Type                BaseMessageType
	JsonRpcRequest      *BaseJSONRPCRequest
	JsonRpcNotification *BaseJSONRPCNotification
	JsonRpcResponse     *BaseJSONRPCResponse
	JsonRpcError        *BaseJSONRPCError
}

func NewBaseMessageError

func NewBaseMessageError(error *BaseJSONRPCError) *BaseJsonRpcMessage

func NewBaseMessageNotification

func NewBaseMessageNotification(notification *BaseJSONRPCNotification) *BaseJsonRpcMessage

func NewBaseMessageRequest

func NewBaseMessageRequest(request *BaseJSONRPCRequest) *BaseJsonRpcMessage

func NewBaseMessageResponse

func NewBaseMessageResponse(response *BaseJSONRPCResponse) *BaseJsonRpcMessage

func ParseMessage added in v1.0.178

func ParseMessage(data []byte) (*BaseJsonRpcMessage, error)

ParseMessage decodes a single JSON-RPC message and classifies it by the members it carries: "method" with "id" is a request, "method" without "id" is a notification, "error" is an error response and "result" is a successful response. Transports use it to dispatch a received payload as its real type; a payload that is none of these, or whose "error" member is not an object, is an error, never a zero-valued error response.

func (*BaseJsonRpcMessage) MarshalJSON

func (m *BaseJsonRpcMessage) MarshalJSON() ([]byte, error)

func (*BaseJsonRpcMessage) MessageID added in v0.13.70

func (m *BaseJsonRpcMessage) MessageID() RequestId

type BaseMessageType

type BaseMessageType string
const (
	BaseMessageTypeJSONRPCRequestType      BaseMessageType = "request"
	BaseMessageTypeJSONRPCNotificationType BaseMessageType = "notification"
	BaseMessageTypeJSONRPCResponseType     BaseMessageType = "response"
	BaseMessageTypeJSONRPCErrorType        BaseMessageType = "error"
)

type JSONRPCMessage

type JSONRPCMessage any

type JsonRpcBody

type JsonRpcBody any

type RequestId

type RequestId int64

type Transport

type Transport interface {
	// Start starts processing messages on the transport, including any connection steps that might need to be taken.
	//
	// This method should only be called after callbacks are installed, or else messages may be lost.
	//
	// NOTE: This method should not be called explicitly when using Client, Server, or Protocol classes,
	// as they will implicitly call start().
	Start(ctx context.Context) error

	// Send sends a JSON-RPC message (request, notification or response).
	Send(ctx context.Context, message *BaseJsonRpcMessage) error

	// Close closes the connection.
	Close() error

	// SetCloseHandler sets the callback for when the connection is closed for any reason.
	// This should be invoked when Close() is called as well.
	SetCloseHandler(handler func())

	// SetErrorHandler sets the callback for when an error occurs.
	// Note that errors are not necessarily fatal; they are used for reporting any kind of exceptional condition out of band.
	SetErrorHandler(handler func(error))

	// SetMessageHandler sets the callback for when a message (request, notification or response) is received over the connection.
	// Partially deserializes the messages to pass a BaseJsonRpcMessage
	SetMessageHandler(handler func(ctx context.Context, message *BaseJsonRpcMessage))
}

Transport describes the minimal contract for a MCP transport that a client or server can communicate over.

Directories

Path Synopsis
Package httptransport implements stateless HTTP transports for MCP: a server transport that accepts JSON-RPC requests on an endpoint, and a client transport that posts them.
Package httptransport implements stateless HTTP transports for MCP: a server transport that accepts JSON-RPC requests on an endpoint, and a client transport that posts them.
Package localtransport implements local transport mechanisms for MCP, enabling efficient in-process and local communication for agentic flows.
Package localtransport implements local transport mechanisms for MCP, enabling efficient in-process and local communication for agentic flows.
sse
Package sse implements Server-Sent Events (SSE) transport for MCP, enabling real-time, bidirectional communication for agentic flows over HTTP.
Package sse implements Server-Sent Events (SSE) transport for MCP, enabling real-time, bidirectional communication for agentic flows over HTTP.
internal/sse
Package sse provides the low-level implementation of Server-Sent Events (SSE) transport for MCP, handling connection management, message streaming, and protocol details.
Package sse provides the low-level implementation of Server-Sent Events (SSE) transport for MCP, handling connection management, message streaming, and protocol details.
Package stdio implements the MCP stdio transport, used by servers that are launched as a subprocess by an MCP host and exchange newline-delimited JSON-RPC frames over standard input and output.
Package stdio implements the MCP stdio transport, used by servers that are launched as a subprocess by an MCP host and exchange newline-delimited JSON-RPC frames over standard input and output.
internal/stdio
This file implements the stdio transport layer for JSON-RPC communication.
This file implements the stdio transport layer for JSON-RPC communication.

Jump to

Keyboard shortcuts

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