mcp

package
v1.0.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package mcp provides MCP message types and JSON-RPC codec utilities for the sentinel-gate proxy.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeMessage

func DecodeMessage(data []byte) (jsonrpc.Message, error)

DecodeMessage deserializes JSON-RPC wire format data into a Message. It returns either a *jsonrpc.Request or *jsonrpc.Response based on the message content. This delegates to the MCP SDK's jsonrpc package.

func EncodeMessage

func EncodeMessage(msg jsonrpc.Message) ([]byte, error)

EncodeMessage serializes a JSON-RPC message to its wire format. This delegates to the MCP SDK's jsonrpc package.

Types

type Direction

type Direction int

Direction indicates the flow direction of a message through the proxy.

const (
	// ClientToServer indicates a message flowing from client to MCP server.
	ClientToServer Direction = iota
	// ServerToClient indicates a message flowing from MCP server to client.
	ServerToClient
)

func (Direction) String

func (d Direction) String() string

String returns the string representation of the Direction.

type Message

type Message struct {
	// Raw contains the original bytes of the message.
	// Used for passthrough when no modification is needed.
	Raw []byte

	// Direction indicates whether this message is flowing from
	// client to server or server to client.
	Direction Direction

	// Decoded contains the parsed JSON-RPC message.
	// May be nil if parsing failed but passthrough is still desired.
	// The concrete type is either *jsonrpc.Request or *jsonrpc.Response.
	Decoded jsonrpc.Message

	// Timestamp records when the message was received by the proxy.
	Timestamp time.Time

	// APIKey contains the raw API key extracted from the message.
	// Extracted from JSON-RPC params by ExtractAPIKey().
	// Used by AuthInterceptor for initial authentication.
	APIKey string

	// Session contains the authenticated user's session context.
	// Set by AuthInterceptor after successful authentication.
	// Used by policy engine for RBAC evaluation.
	Session *session.Session

	// FrameworkContext is nil in OSS (framework context is a PRO feature).
	// Field retained for interface compatibility with PRO.
	FrameworkContext interface{}

	// ParsedParams contains the parsed params from a JSON-RPC request.
	// Set by ParseParams() for reuse across interceptors.
	// Nil if not a request or parsing failed.
	ParsedParams map[string]interface{}
}

Message wraps a decoded JSON-RPC message with proxy metadata. It stores both the raw bytes (for efficient passthrough) and the decoded message (for policy inspection).

func WrapMessage

func WrapMessage(raw []byte, dir Direction) (*Message, error)

WrapMessage decodes raw JSON-RPC bytes and wraps them in a Message struct with the specified direction and current timestamp.

If decoding fails, returns an error. For passthrough scenarios where the raw bytes should be preserved even on decode failure, callers can construct a Message manually.

func (*Message) ExtractAPIKey

func (m *Message) ExtractAPIKey() string

ExtractAPIKey extracts the API key from JSON-RPC params. MCP doesn't use HTTP headers, so API key is passed in JSON-RPC params. Looks in these locations (in priority order): 1. params._meta.apiKey (MCP standard metadata location) 2. params.apiKey (top-level for simplicity) Returns empty string if not found (not an error - may use cached session).

func (*Message) HasAPIKey

func (m *Message) HasAPIKey() bool

HasAPIKey returns true if the message contains an API key.

func (*Message) HasFrameworkContext

func (m *Message) HasFrameworkContext() bool

HasFrameworkContext returns true if the message has framework context.

func (*Message) IsAuthenticated

func (m *Message) IsAuthenticated() bool

IsAuthenticated returns true if the message has a valid session.

func (*Message) IsRequest

func (m *Message) IsRequest() bool

IsRequest returns true if the message is a JSON-RPC request.

func (*Message) IsResponse

func (m *Message) IsResponse() bool

IsResponse returns true if the message is a JSON-RPC response.

func (*Message) IsToolCall

func (m *Message) IsToolCall() bool

IsToolCall returns true if this is a tools/call request. This is the primary method for identifying tool invocations that need policy evaluation.

func (*Message) Method

func (m *Message) Method() string

Method returns the method name if this is a request, empty string otherwise.

func (*Message) ParseParams

func (m *Message) ParseParams() map[string]interface{}

ParseParams parses the request params and stores in ParsedParams. Safe to call multiple times (no-op if already parsed). Returns the parsed params or nil if not a request or parsing fails.

func (*Message) RawID

func (m *Message) RawID() json.RawMessage

RawID extracts the request ID from the raw message bytes as json.RawMessage. This is needed because the SDK's jsonrpc.ID type doesn't marshal correctly through interface{}, so we extract the ID directly from the raw JSON. Returns nil if no ID is found or if the message is not a request.

func (*Message) Request

func (m *Message) Request() *jsonrpc.Request

Request returns the underlying Request if this is a request message. Returns nil if this is not a request.

func (*Message) Response

func (m *Message) Response() *jsonrpc.Response

Response returns the underlying Response if this is a response message. Returns nil if this is not a response.

Jump to

Keyboard shortcuts

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