server

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2025 License: MIT Imports: 24 Imported by: 1

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ServeSSE added in v1.0.1

func ServeSSE(srv *Server, addr string, basePath string) error

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

func ServeStdio(srv *Server) error

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).

func ServeWebSocket added in v1.0.1

func ServeWebSocket(srv *Server, addr string, path string) error

ServeWebSocket runs the MCP server, handling connections via WebSockets. It listens on the specified network address (e.g., ":8080") and handles upgrade requests at the given path (e.g., "/mcp").

Types

type NotificationHandlerFunc

type NotificationHandlerFunc func(ctx context.Context, params interface{}) error

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

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

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 (s *Server) NotifyResourceUpdated(resource protocol.Resource)

func (*Server) RegisterNotificationHandler

func (s *Server) RegisterNotificationHandler(method string, handler NotificationHandlerFunc) error

func (*Server) RegisterPrompt

func (s *Server) RegisterPrompt(prompt protocol.Prompt) error

func (*Server) RegisterResource

func (s *Server) RegisterResource(resource protocol.Resource) error

func (*Server) RegisterSession

func (s *Server) RegisterSession(session types.ClientSession) error

func (*Server) RegisterTool

func (s *Server) RegisterTool(tool protocol.Tool, handler hooks.FinalToolHandler) error

func (*Server) ResourceRegistry

func (s *Server) ResourceRegistry() map[string]protocol.Resource

func (*Server) SendProgress

func (s *Server) SendProgress(sessionID string, params protocol.ProgressParams) error

func (*Server) SendPromptsListChanged

func (s *Server) SendPromptsListChanged() error

func (*Server) SendResourcesListChanged

func (s *Server) SendResourcesListChanged() error

func (*Server) SendToolsListChanged

func (s *Server) SendToolsListChanged() error

func (*Server) UnregisterPrompt

func (s *Server) UnregisterPrompt(uri string) error

func (*Server) UnregisterResource

func (s *Server) UnregisterResource(uri string) error

func (*Server) UnregisterSession

func (s *Server) UnregisterSession(sessionID string)

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.

Jump to

Keyboard shortcuts

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