Documentation
¶
Index ¶
- Constants
- func CreateHTTPServerForMCP(addr string, unifiedServer *UnifiedServer, apiKey string) *http.Server
- func CreateHTTPServerForRoutedMode(addr string, unifiedServer *UnifiedServer, apiKey string) *http.Server
- func HandleHealth(unifiedServer *UnifiedServer) http.HandlerFunc
- func SessionIDFromContext(ctx context.Context) string
- func WithOTELTracing(next http.Handler, tag string) http.Handler
- func WithSDKLogging(handler http.Handler, mode string) http.Handler
- type GuardSessionState
- type HealthResponse
- type JSONRPCError
- type JSONRPCRequest
- type JSONRPCResponse
- type ServerStatus
- type Session
- type SysServer
- type ToolInfo
- type UnifiedServer
- func (us *UnifiedServer) Close() error
- func (us *UnifiedServer) GetHTTPShutdown() func(context.Context) error
- func (us *UnifiedServer) GetPayloadSizeThreshold() int
- func (us *UnifiedServer) GetServerIDs() []string
- func (us *UnifiedServer) GetServerStatus() map[string]ServerStatus
- func (us *UnifiedServer) GetToolHandler(backendID string, toolName string) ...
- func (us *UnifiedServer) GetToolsForBackend(backendID string) []ToolInfo
- func (us *UnifiedServer) InitiateShutdown() int
- func (us *UnifiedServer) IsDIFCEnabled() bool
- func (us *UnifiedServer) IsShutdown() bool
- func (us *UnifiedServer) RegisterTestTool(name string, tool *ToolInfo)
- func (us *UnifiedServer) Run(transport sdk.Transport) error
- func (us *UnifiedServer) SetHTTPShutdown(fn func(context.Context) error)
- func (us *UnifiedServer) SetTestMode(enabled bool)
- func (us *UnifiedServer) ShouldExit() bool
Constants ¶
const MCPGatewaySpecVersion = "1.9.0"
MCPGatewaySpecVersion is the MCP Gateway Specification version this implementation conforms to
const MCPProtocolVersion = mcp.MCPProtocolVersion
MCPProtocolVersion is the MCP protocol version supported by this gateway
const SessionIDContextKey = mcp.SessionIDContextKey
SessionIDContextKey is used to store MCP session ID in context This is re-exported from mcp package for backward compatibility
Variables ¶
This section is empty.
Functions ¶
func CreateHTTPServerForMCP ¶
func CreateHTTPServerForMCP(addr string, unifiedServer *UnifiedServer, apiKey string) *http.Server
CreateHTTPServerForMCP creates an HTTP server that handles MCP over streamable HTTP transport If apiKey is provided, all requests except /health require authentication (spec 7.1)
func CreateHTTPServerForRoutedMode ¶
func CreateHTTPServerForRoutedMode(addr string, unifiedServer *UnifiedServer, apiKey string) *http.Server
CreateHTTPServerForRoutedMode creates an HTTP server for routed mode In routed mode, each backend is accessible at /mcp/<server> Multiple routes from the same Authorization header share a session If apiKey is provided, all requests except /health require authentication (spec 7.1)
func HandleHealth ¶
func HandleHealth(unifiedServer *UnifiedServer) http.HandlerFunc
HandleHealth returns an http.HandlerFunc that handles the /health endpoint This function is used by both routed and unified modes to ensure consistent behavior
func SessionIDFromContext ¶ added in v0.2.10
SessionIDFromContext returns the MCP session ID stored in ctx, or "default" if the context contains no session ID (or one of the wrong type). This is the canonical place in the server package that reads SessionIDContextKey directly.
func WithOTELTracing ¶ added in v0.2.13
WithOTELTracing wraps an http.Handler with an OpenTelemetry span for each request. The span covers the full HTTP handler lifecycle and includes session ID, HTTP path, and method as span attributes. The span context is propagated into the request context so that nested spans (e.g. tool call spans) are automatically parented to it.
Incoming W3C traceparent/tracestate headers are extracted so that an agent-originated trace is continued; if no such headers are present a fresh root span (and new trace ID) is created automatically.
func WithSDKLogging ¶
WithSDKLogging wraps an SDK StreamableHTTPHandler to log JSON-RPC translation results This captures the request/response at the HTTP boundary to understand what the SDK sees and what it returns, particularly for debugging protocol state issues
Types ¶
type GuardSessionState ¶ added in v0.1.10
type GuardSessionState struct {
Initialized bool
PolicyHash string
PolicySource string
DIFCMode difc.EnforcementMode
NormalizedPolicy map[string]interface{}
}
GuardSessionState stores label_agent initialization state for a guard within a session.
type HealthResponse ¶
type HealthResponse struct {
Status string `json:"status"` // "healthy" or "unhealthy"
SpecVersion string `json:"specVersion"` // MCP Gateway Specification version
GatewayVersion string `json:"gatewayVersion"` // Gateway implementation version
Servers map[string]ServerStatus `json:"servers"` // Map of server names to their health status
}
HealthResponse represents the JSON structure for the /health endpoint response as defined in MCP Gateway Specification section 8.1.1
func BuildHealthResponse ¶
func BuildHealthResponse(unifiedServer *UnifiedServer) HealthResponse
BuildHealthResponse constructs a HealthResponse from the unified server's status
type JSONRPCError ¶
type JSONRPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Data json.RawMessage `json:"data,omitempty"`
}
JSONRPCError represents a JSON-RPC error
type JSONRPCRequest ¶
type JSONRPCRequest struct {
JSONRPC string `json:"jsonrpc"`
ID interface{} `json:"id,omitempty"`
Method string `json:"method"`
Params json.RawMessage `json:"params,omitempty"`
}
JSONRPCRequest represents an incoming JSON-RPC request
type JSONRPCResponse ¶
type JSONRPCResponse struct {
JSONRPC string `json:"jsonrpc"`
ID interface{} `json:"id,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
Error *JSONRPCError `json:"error,omitempty"`
}
JSONRPCResponse represents a JSON-RPC response
type ServerStatus ¶
type ServerStatus struct {
Status string `json:"status"` // "running" | "stopped" | "error"
Uptime int `json:"uptime"` // seconds since server was launched
}
ServerStatus represents the health status of a backend server
type Session ¶
type Session struct {
Token string
SessionID string
StartTime time.Time
GuardInit map[string]*GuardSessionState
}
Session represents a MCPG session
func NewSession ¶
NewSession creates a new Session with the given session ID and optional token
type SysServer ¶ added in v0.2.7
type SysServer struct {
// contains filtered or unexported fields
}
SysServer implements the MCPG system tools
func NewSysServer ¶ added in v0.2.7
NewSysServer creates a new system server
func (*SysServer) HandleRequest ¶ added in v0.2.7
func (s *SysServer) HandleRequest(method string, params json.RawMessage) (interface{}, error)
HandleRequest processes MCP requests for system tools
type ToolInfo ¶
type ToolInfo struct {
Name string
Description string
InputSchema map[string]interface{}
Annotations *sdk.ToolAnnotations
BackendID string // Which backend this tool belongs to
Handler func(context.Context, *sdk.CallToolRequest, interface{}) (*sdk.CallToolResult, interface{}, error)
}
ToolInfo stores metadata about a registered tool
type UnifiedServer ¶
type UnifiedServer struct {
// contains filtered or unexported fields
}
UnifiedServer implements a unified MCP server that aggregates multiple backend servers
func NewUnified ¶
NewUnified creates a new unified MCP server
func (*UnifiedServer) GetHTTPShutdown ¶ added in v0.1.7
func (us *UnifiedServer) GetHTTPShutdown() func(context.Context) error
GetHTTPShutdown returns the HTTP shutdown function, or nil if not set
func (*UnifiedServer) GetPayloadSizeThreshold ¶ added in v0.0.108
func (us *UnifiedServer) GetPayloadSizeThreshold() int
GetPayloadSizeThreshold returns the configured payload size threshold (in bytes). Payloads larger than this threshold are stored to disk, smaller ones are returned inline. This getter allows other modules to access the threshold configuration.
func (*UnifiedServer) GetServerIDs ¶
func (us *UnifiedServer) GetServerIDs() []string
GetServerIDs returns the list of backend server IDs
func (*UnifiedServer) GetServerStatus ¶
func (us *UnifiedServer) GetServerStatus() map[string]ServerStatus
GetServerStatus returns the status of all configured backend servers
func (*UnifiedServer) GetToolHandler ¶
func (us *UnifiedServer) GetToolHandler(backendID string, toolName string) func(context.Context, *sdk.CallToolRequest, interface{}) (*sdk.CallToolResult, interface{}, error)
GetToolHandler returns the handler for a specific backend tool This allows routed mode to reuse the unified server's tool handlers
func (*UnifiedServer) GetToolsForBackend ¶
func (us *UnifiedServer) GetToolsForBackend(backendID string) []ToolInfo
GetToolsForBackend returns tools for a specific backend with prefix stripped
func (*UnifiedServer) InitiateShutdown ¶
func (us *UnifiedServer) InitiateShutdown() int
InitiateShutdown initiates graceful shutdown and returns the number of servers terminated This method is idempotent - subsequent calls will return 0 servers terminated
func (*UnifiedServer) IsDIFCEnabled ¶
func (us *UnifiedServer) IsDIFCEnabled() bool
IsDIFCEnabled returns whether DIFC is enabled
func (*UnifiedServer) IsShutdown ¶
func (us *UnifiedServer) IsShutdown() bool
IsShutdown returns true if the gateway has been shut down
func (*UnifiedServer) RegisterTestTool ¶
func (us *UnifiedServer) RegisterTestTool(name string, tool *ToolInfo)
RegisterTestTool registers a tool for testing purposes This method is used by integration tests to inject mock tools into the gateway
func (*UnifiedServer) Run ¶
func (us *UnifiedServer) Run(transport sdk.Transport) error
Run starts the unified MCP server on the specified transport
func (*UnifiedServer) SetHTTPShutdown ¶ added in v0.1.7
func (us *UnifiedServer) SetHTTPShutdown(fn func(context.Context) error)
SetHTTPShutdown sets the function to call when draining in-flight HTTP requests during /close endpoint handling (spec 5.1.3). Should be called after the HTTP server is created so that the close handler can perform graceful shutdown.
func (*UnifiedServer) SetTestMode ¶
func (us *UnifiedServer) SetTestMode(enabled bool)
SetTestMode enables test mode which prevents os.Exit() calls This should only be used in unit tests
func (*UnifiedServer) ShouldExit ¶
func (us *UnifiedServer) ShouldExit() bool
ShouldExit returns whether the gateway should exit after shutdown Returns false in test mode to prevent actual process exit