Documentation
¶
Overview ¶
Package server provides the MCP server implementation. This file contains server options related to authentication.
Package server provides the MCP server implementation.
Package server provides the MCP server implementation.
Index ¶
- func ServeSSE(srv *Server, addr string, basePath string) error
- func ServeStdio(srv *Server) error
- func ServeWebSocket(srv *Server, addr string, path string) error
- type NotificationHandlerFunc
- type SSEContextFunc
- type Server
- func (s *Server) HandleMessage(ctx context.Context, sessionID string, rawMessage json.RawMessage) []*protocol.JSONRPCResponse
- func (s *Server) NotifyResourceUpdated(resource protocol.Resource)
- func (s *Server) RegisterNotificationHandler(method string, handler NotificationHandlerFunc) error
- func (s *Server) RegisterPrompt(prompt protocol.Prompt) error
- func (s *Server) RegisterResource(resource protocol.Resource) error
- func (s *Server) RegisterSession(session types.ClientSession) error
- func (s *Server) RegisterTool(tool protocol.Tool, handler hooks.FinalToolHandler) error
- func (s *Server) ResourceRegistry() map[string]protocol.Resource
- func (s *Server) SendProgress(sessionID string, params protocol.ProgressParams) error
- func (s *Server) SendPromptsListChanged() error
- func (s *Server) SendResourcesListChanged() error
- func (s *Server) SendToolsListChanged() error
- func (s *Server) UnregisterPrompt(uri string) error
- func (s *Server) UnregisterResource(uri string) error
- func (s *Server) UnregisterSession(sessionID string)
- type ServerOption
- func WithAfterToolCallHook(hooks ...hooks.AfterToolCallHook) ServerOption
- func WithAuth(validator auth.TokenValidator, checker auth.PermissionChecker) ServerOption
- func WithBeforeHandleMessageHook(hooks ...hooks.BeforeHandleMessageHook) ServerOption
- func WithBeforeSendResponseHook(hooks ...hooks.BeforeSendResponseHook) ServerOption
- func WithBeforeSessionDestroyHook(hooks ...hooks.BeforeSessionDestroyHook) ServerOption
- func WithBeforeToolCallHook(hooks ...hooks.BeforeToolCallHook) ServerOption
- func WithBeforeUnmarshalHook(hooks ...hooks.BeforeUnmarshalHook) ServerOption
- func WithInstructions(instructions string) ServerOption
- func WithJWTAuth(config auth.JWKSConfig, checker auth.PermissionChecker) ServerOption
- func WithLogger(logger types.Logger) ServerOption
- func WithOnSessionCreateHook(hooks ...hooks.OnSessionCreateHook) ServerOption
- func WithPromptListChanged(listChanged bool) ServerOption
- func WithResourceListChanged(listChanged bool) ServerOption
- func WithResourceSubscription(subscribe bool) ServerOption
- func WithServerBeforeHandleNotificationHook(hooks ...hooks.ServerBeforeHandleNotificationHook) ServerOption
- func WithServerBeforeHandleRequestHook(hooks ...hooks.ServerBeforeHandleRequestHook) ServerOption
- func WithServerBeforeSendNotificationHook(hooks ...hooks.ServerBeforeSendNotificationHook) ServerOption
- func WithServerCapabilities(caps protocol.ServerCapabilities) ServerOption
- func WithToolListChanged(listChanged bool) ServerOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ServeSSE ¶ added in v1.0.1
ServeSSE runs the MCP server, handling connections via Server-Sent Events (SSE) over HTTP using the implementation from the transport/sse package. It listens on the specified network address (e.g., ":8080"). The basePath argument defines the URL prefix for the SSE and message endpoints (e.g., "/mcp", resulting in "/mcp/sse" and "/mcp/message"). If empty, "/" is used.
func ServeStdio ¶ added in v1.0.1
ServeStdio runs the MCP server, listening for messages on standard input and sending responses/notifications to standard output. It blocks until the input stream is closed, an error occurs, or the context is cancelled (e.g., by SIGINT).
Types ¶
type NotificationHandlerFunc ¶
NotificationHandlerFunc defines the signature for functions that handle client-to-server notifications. Note: ToolHandlerFunc is now defined in the hooks package as hooks.FinalToolHandler
type SSEContextFunc ¶
SSEContextFunc is a function type used by the SSEServer to allow customization of the context passed to the core MCPServer's HandleMessage method, based on the incoming HTTP request for client->server messages. This allows injecting values from HTTP headers (like auth tokens) into the context.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents the core MCP server logic, independent of transport.
func NewServer ¶
func NewServer(serverName string, opts ...ServerOption) *Server
NewServer creates a new core MCP Server logic instance with the provided options.
func (*Server) HandleMessage ¶
func (s *Server) HandleMessage(ctx context.Context, sessionID string, rawMessage json.RawMessage) []*protocol.JSONRPCResponse
HandleMessage processes an incoming raw JSON message, which can be a single JSON-RPC object or a JSON array representing a batch of requests/notifications. It returns a slice of responses. For single requests, the slice will contain 0 or 1 response. For batches, it will contain responses for all processed requests in the batch (notifications produce no response). Returns nil or an empty slice if no responses should be sent.
func (*Server) NotifyResourceUpdated ¶
func (*Server) RegisterNotificationHandler ¶
func (s *Server) RegisterNotificationHandler(method string, handler NotificationHandlerFunc) error
func (*Server) RegisterResource ¶
func (*Server) RegisterSession ¶
func (s *Server) RegisterSession(session types.ClientSession) error
func (*Server) RegisterTool ¶
func (*Server) SendProgress ¶
func (s *Server) SendProgress(sessionID string, params protocol.ProgressParams) error
func (*Server) SendPromptsListChanged ¶
func (*Server) SendResourcesListChanged ¶
func (*Server) SendToolsListChanged ¶
func (*Server) UnregisterPrompt ¶
func (*Server) UnregisterResource ¶
func (*Server) UnregisterSession ¶
type ServerOption ¶ added in v1.0.1
type ServerOption func(*Server)
ServerOption defines a function signature for configuring a Server.
func WithAfterToolCallHook ¶ added in v1.0.8
func WithAfterToolCallHook(hooks ...hooks.AfterToolCallHook) ServerOption
WithAfterToolCallHook adds one or more AfterToolCallHook functions.
func WithAuth ¶ added in v1.0.8
func WithAuth(validator auth.TokenValidator, checker auth.PermissionChecker) ServerOption
WithAuth is a ServerOption to configure and enable authentication using a provided validator and checker. It registers the necessary hooks on the server.
func WithBeforeHandleMessageHook ¶ added in v1.0.8
func WithBeforeHandleMessageHook(hooks ...hooks.BeforeHandleMessageHook) ServerOption
WithBeforeHandleMessageHook adds one or more BeforeHandleMessageHook functions.
func WithBeforeSendResponseHook ¶ added in v1.0.8
func WithBeforeSendResponseHook(hooks ...hooks.BeforeSendResponseHook) ServerOption
WithBeforeSendResponseHook adds one or more BeforeSendResponseHook functions.
func WithBeforeSessionDestroyHook ¶ added in v1.0.8
func WithBeforeSessionDestroyHook(hooks ...hooks.BeforeSessionDestroyHook) ServerOption
WithBeforeSessionDestroyHook adds one or more BeforeSessionDestroyHook functions.
func WithBeforeToolCallHook ¶ added in v1.0.8
func WithBeforeToolCallHook(hooks ...hooks.BeforeToolCallHook) ServerOption
WithBeforeToolCallHook adds one or more BeforeToolCallHook functions.
func WithBeforeUnmarshalHook ¶ added in v1.0.8
func WithBeforeUnmarshalHook(hooks ...hooks.BeforeUnmarshalHook) ServerOption
WithBeforeUnmarshalHook adds one or more BeforeUnmarshalHook functions.
func WithInstructions ¶ added in v1.0.1
func WithInstructions(instructions string) ServerOption
WithInstructions sets the server instructions string returned during initialization.
func WithJWTAuth ¶ added in v1.0.8
func WithJWTAuth(config auth.JWKSConfig, checker auth.PermissionChecker) ServerOption
WithJWTAuth is a convenience ServerOption specifically for JWT/JWKS authentication. It creates the JWKSTokenValidator internally.
func WithLogger ¶ added in v1.0.1
func WithLogger(logger types.Logger) ServerOption
WithLogger provides an option to set a custom logger.
func WithOnSessionCreateHook ¶ added in v1.0.8
func WithOnSessionCreateHook(hooks ...hooks.OnSessionCreateHook) ServerOption
WithOnSessionCreateHook adds one or more OnSessionCreateHook functions.
func WithPromptListChanged ¶ added in v1.0.2
func WithPromptListChanged(listChanged bool) ServerOption
WithPromptListChanged sets the prompt listChanged capability flag. It ensures the Prompts capability struct is initialized.
func WithResourceListChanged ¶ added in v1.0.2
func WithResourceListChanged(listChanged bool) ServerOption
WithResourceListChanged sets the resource listChanged capability flag. It ensures the Resources capability struct is initialized.
func WithResourceSubscription ¶ added in v1.0.2
func WithResourceSubscription(subscribe bool) ServerOption
WithResourceSubscription sets the resource subscription capability flag. It ensures the Resources capability struct is initialized.
func WithServerBeforeHandleNotificationHook ¶ added in v1.0.8
func WithServerBeforeHandleNotificationHook(hooks ...hooks.ServerBeforeHandleNotificationHook) ServerOption
WithServerBeforeHandleNotificationHook adds one or more ServerBeforeHandleNotificationHook functions.
func WithServerBeforeHandleRequestHook ¶ added in v1.0.8
func WithServerBeforeHandleRequestHook(hooks ...hooks.ServerBeforeHandleRequestHook) ServerOption
WithServerBeforeHandleRequestHook adds one or more ServerBeforeHandleRequestHook functions.
func WithServerBeforeSendNotificationHook ¶ added in v1.0.8
func WithServerBeforeSendNotificationHook(hooks ...hooks.ServerBeforeSendNotificationHook) ServerOption
WithServerBeforeSendNotificationHook adds one or more ServerBeforeSendNotificationHook functions.
func WithServerCapabilities ¶ added in v1.0.1
func WithServerCapabilities(caps protocol.ServerCapabilities) ServerOption
WithServerCapabilities provides an option to set the server's capabilities. Note: This replaces all existing capabilities. Consider more granular options like WithToolCapabilities, WithResourceCapabilities if needed.
func WithToolListChanged ¶ added in v1.0.2
func WithToolListChanged(listChanged bool) ServerOption
WithToolListChanged sets the tool listChanged capability flag. It ensures the Tools capability struct is initialized.