reqcontext

package
v0.74.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RequestIDHeader is the HTTP header name for request IDs
	RequestIDHeader = "X-Request-Id"

	// MaxRequestIDLength is the maximum allowed length for a request ID
	MaxRequestIDLength = 256
)
View Source
const MaxRunNameLen = 128

MaxRunNameLen is the longest accepted run name, in characters.

View Source
const RunNameHeader = "X-MCPProxy-Run"

RunNameHeader carries a caller-chosen label for one run of work (Spec 124 FR-003). It is a label, never a credential: it only splits the caller's own activity into named work sessions, and it is never forwarded upstream.

Variables

This section is empty.

Functions

func AttemptID added in v0.74.0

func AttemptID(ctx context.Context) string

AttemptID returns the id recorded by WithAttemptID, or "".

func ClientSurface added in v0.74.0

func ClientSurface(ctx context.Context) string

ClientSurface returns the surface recorded by WithClientSurface, or "".

func GenerateCorrelationID

func GenerateCorrelationID() string

GenerateCorrelationID generates a new unique correlation ID

func GenerateRequestID

func GenerateRequestID() string

GenerateRequestID generates a new UUID v4 request ID

func GetCorrelationID

func GetCorrelationID(ctx context.Context) string

GetCorrelationID retrieves the correlation ID from context

func GetOrGenerateRequestID

func GetOrGenerateRequestID(providedID string) string

GetOrGenerateRequestID returns the provided ID if valid, otherwise generates a new one. This is the main entry point for request ID handling in middleware.

func GetRequestID

func GetRequestID(ctx context.Context) string

GetRequestID retrieves the request ID from context

func IsValidRequestID

func IsValidRequestID(id string) bool

IsValidRequestID checks if a request ID matches the allowed pattern. Valid IDs contain only alphanumeric characters, dashes, and underscores, and are between 1 and 256 characters long.

func RunHeader added in v0.74.0

func RunHeader(ctx context.Context) (string, bool)

RunHeader returns the raw header value and whether one was sent.

func ToolResultEnvelope added in v0.72.0

func ToolResultEnvelope(ctx context.Context) bool

func ValidRunName added in v0.74.0

func ValidRunName(s string) bool

ValidRunName reports whether s is an acceptable run name: 1 to MaxRunNameLen printable characters. Control characters, invalid UTF-8 and the empty string are refused.

func WithAttemptID added in v0.74.0

func WithAttemptID(ctx context.Context, id string) context.Context

WithAttemptID records the request id a dispatch path minted for one tool call attempt. Every activity row of that attempt carries it, including a limiter rejection, which would otherwise fall back to the transport request id that several attempts of one HTTP request share (Spec 124 D2).

func WithClientSurface added in v0.74.0

func WithClientSurface(ctx context.Context, surface string) context.Context

WithClientSurface records which first-party surface made a REST call ("cli", "webui", "tray") or "rest" for anything else. It names the client of a call that has no MCP clientInfo (Spec 124 FR-002).

func WithCorrelationID

func WithCorrelationID(ctx context.Context, correlationID string) context.Context

WithCorrelationID adds a correlation ID to the context

func WithMetadata

func WithMetadata(ctx context.Context, source RequestSource) context.Context

WithMetadata adds both correlation ID and request source to context

func WithRequestID

func WithRequestID(ctx context.Context, requestID string) context.Context

WithRequestID adds a request ID to the context

func WithRequestMeta added in v0.68.0

func WithRequestMeta(ctx context.Context, meta RequestMeta) context.Context

WithRequestMeta stores meta on the context.

func WithRequestSource

func WithRequestSource(ctx context.Context, source RequestSource) context.Context

WithRequestSource adds request source to the context

func WithRunHeader added in v0.74.0

func WithRunHeader(ctx context.Context, raw string) context.Context

WithRunHeader stores the raw X-MCPProxy-Run header value. It is stored unvalidated so the reader can tell "absent" from "present but invalid" and log the latter (FR-004). An empty value stores nothing.

func WithToolResultEnvelope added in v0.72.0

func WithToolResultEnvelope(ctx context.Context) context.Context

WithToolResultEnvelope opts a REST tool call into the full MCP result shape.

Types

type ContextKey

type ContextKey string

ContextKey is the type for context keys to avoid collisions

const (
	// CorrelationIDKey is the context key for correlation IDs
	CorrelationIDKey ContextKey = "correlation_id"

	// RequestSourceKey is the context key for request source
	RequestSourceKey ContextKey = "request_source"

	// RequestIDKey is the context key for request IDs (X-Request-Id header)
	RequestIDKey ContextKey = "request_id"

	// LoggerKey is the context key for request-scoped logger
	LoggerKey ContextKey = "logger"
)
const RequestMetaKey ContextKey = "request_meta"

RequestMetaKey is the context key for RequestMeta.

type Mount added in v0.68.0

type Mount string

Mount names the HTTP mount a request entered through. It is fixed by the mount point (Spec 107 T050), never by a header.

const (
	// MountMCP is the /mcp* family (the MCP proxy surface).
	MountMCP Mount = "mcp"
	// MountAPI is the /api/v1 group and /events (the REST API).
	MountAPI Mount = "api"
)

type RequestMeta added in v0.68.0

type RequestMeta struct {
	ClientIP string
	Mount    Mount
}

RequestMeta is the per-request metadata the audit line needs (Spec 107 PR-D): the client IP as resolved through the trusted-proxy rule and the mount the request arrived on. RemoteAddr lives only on *http.Request, so the edition-neutral httpapi middleware stores it here.

func GetRequestMeta added in v0.68.0

func GetRequestMeta(ctx context.Context) (RequestMeta, bool)

GetRequestMeta returns the stored meta and whether one was set.

type RequestSource

type RequestSource string

RequestSource indicates where the request originated

const (
	// SourceRESTAPI indicates request came from HTTP REST API
	SourceRESTAPI RequestSource = "REST_API"

	// SourceCLI indicates request came from CLI command
	SourceCLI RequestSource = "CLI"

	// SourceMCP indicates request came from MCP protocol
	SourceMCP RequestSource = "MCP"

	// SourceInternal indicates internal/background operation
	SourceInternal RequestSource = "INTERNAL"

	// SourceUnknown indicates source could not be determined
	SourceUnknown RequestSource = "UNKNOWN"
)

func GetRequestSource

func GetRequestSource(ctx context.Context) RequestSource

GetRequestSource retrieves the request source from context

Jump to

Keyboard shortcuts

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