proxy

package
v0.0.0-...-2c8931b Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTestServer

func NewTestServer(server *ProxyServer, opts ...SSEOption) *httptest.Server

NewTestServer creates a test server for testing purposes

func ServeStdio

func ServeStdio(server *ProxyServer, opts ...StdioOption) error

ServeStdio is a convenience function that creates and starts a StdioServer with os.Stdin and os.Stdout. It sets up signal handling for graceful shutdown on SIGTERM and SIGINT. Returns an error if the server encounters any issues during operation.

Types

type BaseMessage

type BaseMessage struct {
	JSONRPC string        `json:"jsonrpc"`
	Method  mcp.MCPMethod `json:"method"`
	ID      any           `json:"id,omitempty"`
}

type ProxyServer

type ProxyServer struct {
	*server.MCPServer
	BaseUrl    string
	BasePath   string
	Status     string
	Error      string
	ServerInfo mcp.Implementation
	Client     client.MCPClient
	Config     *common.McpServerConfig
}

func NewProxyServer

func NewProxyServer(cfg *common.McpServerConfig) (*ProxyServer, error)

func (*ProxyServer) HandleMessage

func (s *ProxyServer) HandleMessage(
	ctx context.Context,
	message json.RawMessage,
) mcp.JSONRPCMessage

HandleMessage processes an incoming JSON-RPC message and returns an appropriate response

type SSEContextFunc

type SSEContextFunc func(ctx context.Context, r *http.Request) context.Context

SSEContextFunc is a function that takes an existing context and the current request and returns a potentially modified context based on the request content. This can be used to inject context values from headers, for example.

type SSEOption

type SSEOption func(*SSEServer)

SSEOption defines a function type for configuring SSEServer

func WithBasePath

func WithBasePath(basePath string) SSEOption

Add a new option for setting base path

func WithBaseURL

func WithBaseURL(baseURL string) SSEOption

WithBaseURL sets the base URL for the SSE server

func WithHTTPServer

func WithHTTPServer(srv *http.Server) SSEOption

WithHTTPServer sets the HTTP server instance

func WithMessageEndpoint

func WithMessageEndpoint(endpoint string) SSEOption

WithMessageEndpoint sets the message endpoint path

func WithSSEContextFunc

func WithSSEContextFunc(fn SSEContextFunc) SSEOption

WithContextFunc sets a function that will be called to customise the context to the server using the incoming request.

func WithSSEEndpoint

func WithSSEEndpoint(endpoint string) SSEOption

WithSSEEndpoint sets the SSE endpoint path

type SSEServer

type SSEServer struct {
	// contains filtered or unexported fields
}

SSEServer implements a Server-Sent Events (SSE) based MCP server. It provides real-time communication capabilities over HTTP using the SSE protocol.

func NewSSEServer

func NewSSEServer(server *ProxyServer, opts ...SSEOption) *SSEServer

NewSSEServer creates a new SSE server instance with the given MCP server and options.

func (*SSEServer) CompleteMessageEndpoint

func (s *SSEServer) CompleteMessageEndpoint() string

func (*SSEServer) CompleteMessagePath

func (s *SSEServer) CompleteMessagePath() string

func (*SSEServer) CompleteSseEndpoint

func (s *SSEServer) CompleteSseEndpoint() string

func (*SSEServer) CompleteSsePath

func (s *SSEServer) CompleteSsePath() string

func (*SSEServer) GetUrlPath

func (s *SSEServer) GetUrlPath(input string) (string, error)

func (*SSEServer) SendEventToSession

func (s *SSEServer) SendEventToSession(
	sessionID string,
	event interface{},
) error

SendEventToSession sends an event to a specific SSE session identified by sessionID. Returns an error if the session is not found or closed.

func (*SSEServer) ServeHTTP

func (s *SSEServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface.

func (*SSEServer) Shutdown

func (s *SSEServer) Shutdown(ctx context.Context) error

Shutdown gracefully stops the SSE server, closing all active sessions and shutting down the HTTP server.

func (*SSEServer) Start

func (s *SSEServer) Start(addr string) error

Start begins serving SSE connections on the specified address. It sets up HTTP handlers for SSE and message endpoints.

type StdioContextFunc

type StdioContextFunc func(ctx context.Context) context.Context

StdioContextFunc is a function that takes an existing context and returns a potentially modified context. This can be used to inject context values from environment variables, for example.

type StdioOption

type StdioOption func(*StdioServer)

StdioOption defines a function type for configuring StdioServer

func WithErrorLogger

func WithErrorLogger(logger *log.Logger) StdioOption

WithErrorLogger sets the error logger for the server

func WithStdioContextFunc

func WithStdioContextFunc(fn StdioContextFunc) StdioOption

WithContextFunc sets a function that will be called to customise the context to the server. Note that the stdio server uses the same context for all requests, so this function will only be called once per server instance.

type StdioServer

type StdioServer struct {
	// contains filtered or unexported fields
}

StdioServer wraps a MCPServer and handles stdio communication. It provides a simple way to create command-line MCP servers that communicate via standard input/output streams using JSON-RPC messages.

func NewStdioServer

func NewStdioServer(server *ProxyServer) *StdioServer

NewStdioServer creates a new stdio server wrapper around an MCPServer. It initializes the server with a default error logger that discards all output.

func (*StdioServer) Listen

func (s *StdioServer) Listen(
	ctx context.Context,
	stdin io.Reader,
	stdout io.Writer,
) error

Listen starts listening for JSON-RPC messages on the provided input and writes responses to the provided output. It runs until the context is cancelled or an error occurs. Returns an error if there are issues with reading input or writing output.

func (*StdioServer) SetContextFunc

func (s *StdioServer) SetContextFunc(fn StdioContextFunc)

SetContextFunc sets a function that will be called to customise the context to the server. Note that the stdio server uses the same context for all requests, so this function will only be called once per server instance.

func (*StdioServer) SetErrorLogger

func (s *StdioServer) SetErrorLogger(logger *log.Logger)

SetErrorLogger configures where error messages from the StdioServer are logged. The provided logger will receive all error messages generated during server operation.

type UnparseableMessageError

type UnparseableMessageError struct {
	// contains filtered or unexported fields
}

fails on the request.

func (*UnparseableMessageError) Error

func (e *UnparseableMessageError) Error() string

func (*UnparseableMessageError) GetMessage

func (e *UnparseableMessageError) GetMessage() json.RawMessage

func (*UnparseableMessageError) GetMethod

func (e *UnparseableMessageError) GetMethod() mcp.MCPMethod

func (*UnparseableMessageError) Unwrap

func (e *UnparseableMessageError) Unwrap() error

Jump to

Keyboard shortcuts

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