Documentation
¶
Overview ¶
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 ToolHandlerFunc) 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 WithInstructions(instructions string) ServerOption
- func WithLogger(logger types.Logger) ServerOption
- func WithPromptCapabilities(listChanged bool) ServerOption
- func WithResourceCapabilities(subscribe, listChanged bool) ServerOption
- func WithServerCapabilities(caps protocol.ServerCapabilities) ServerOption
- func WithToolCapabilities(listChanged bool) ServerOption
- type ToolHandlerFunc
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.
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 (s *Server) RegisterTool(tool protocol.Tool, handler ToolHandlerFunc) error
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 WithInstructions ¶ added in v1.0.1
func WithInstructions(instructions string) ServerOption
WithInstructions sets the server instructions string returned during initialization.
func WithLogger ¶ added in v1.0.1
func WithLogger(logger types.Logger) ServerOption
WithLogger provides an option to set a custom logger.
func WithPromptCapabilities ¶ added in v1.0.1
func WithPromptCapabilities(listChanged bool) ServerOption
WithPromptCapabilities sets specific prompt-related capabilities. It ensures the Prompts capability struct is initialized.
func WithResourceCapabilities ¶ added in v1.0.1
func WithResourceCapabilities(subscribe, listChanged bool) ServerOption
WithResourceCapabilities sets specific resource-related capabilities. It ensures the Resources capability struct is initialized.
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 WithToolCapabilities ¶ added in v1.0.1
func WithToolCapabilities(listChanged bool) ServerOption
WithToolCapabilities sets specific tool-related capabilities. It ensures the Tools capability struct is initialized.
type ToolHandlerFunc ¶
type ToolHandlerFunc func(ctx context.Context, progressToken *protocol.ProgressToken, arguments any) (content []protocol.Content, isError bool)
ToolHandlerFunc defines the signature for functions that handle tool execution.