server

package
v1.0.7 Latest Latest
Warning

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

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

Documentation

Overview

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.

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

type ToolHandlerFunc

type ToolHandlerFunc func(ctx context.Context, progressToken interface{}, arguments any) (content []protocol.Content, isError bool)

ToolHandlerFunc defines the signature for functions that handle tool execution. ToolHandlerFunc defines the signature for functions that handle tool execution. progressToken is interface{} to accept string or number per spec.

Jump to

Keyboard shortcuts

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