server

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2025 License: MIT Imports: 18 Imported by: 725

Documentation

Overview

Package server provides MCP (Model Control Protocol) server implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTestServer added in v0.2.0

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

NewTestServer creates a test server for testing purposes

func ServeStdio

func ServeStdio(server *MCPServer, 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 ClientSession added in v0.14.0

type ClientSession interface {
	// NotificationChannel provides a channel suitable for sending notifications to client.
	NotificationChannel() chan<- mcp.JSONRPCNotification
	// SessionID is a unique identifier used to track user session.
	SessionID() string
}

ClientSession represents an active session that can be used by MCPServer to interact with client.

func ClientSessionFromContext added in v0.14.0

func ClientSessionFromContext(ctx context.Context) ClientSession

ClientSessionFromContext retrieves current client notification context from context.

type MCPServer

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

MCPServer implements a Model Control Protocol server that can handle various types of requests including resources, prompts, and tools.

func NewMCPServer added in v0.5.0

func NewMCPServer(
	name, version string,
	opts ...ServerOption,
) *MCPServer

NewMCPServer creates a new MCP server instance with the given name, version and options

func ServerFromContext added in v0.8.0

func ServerFromContext(ctx context.Context) *MCPServer

ServerFromContext retrieves the MCPServer instance from a context

func (*MCPServer) AddNotificationHandler added in v0.5.0

func (s *MCPServer) AddNotificationHandler(
	method string,
	handler NotificationHandlerFunc,
)

AddNotificationHandler registers a new handler for incoming notifications

func (*MCPServer) AddPrompt added in v0.5.0

func (s *MCPServer) AddPrompt(prompt mcp.Prompt, handler PromptHandlerFunc)

AddPrompt registers a new prompt handler with the given name

func (*MCPServer) AddResource added in v0.5.0

func (s *MCPServer) AddResource(
	resource mcp.Resource,
	handler ResourceHandlerFunc,
)

AddResource registers a new resource and its handler

func (*MCPServer) AddResourceTemplate added in v0.5.0

func (s *MCPServer) AddResourceTemplate(
	template mcp.ResourceTemplate,
	handler ResourceTemplateHandlerFunc,
)

AddResourceTemplate registers a new resource template and its handler

func (*MCPServer) AddTool added in v0.5.0

func (s *MCPServer) AddTool(tool mcp.Tool, handler ToolHandlerFunc)

AddTool registers a new tool and its handler

func (*MCPServer) AddTools added in v0.8.5

func (s *MCPServer) AddTools(tools ...ServerTool)

AddTools registers multiple tools at once

func (*MCPServer) DeleteTools added in v0.8.5

func (s *MCPServer) DeleteTools(names ...string)

DeleteTools removes a tool from the server

func (*MCPServer) HandleMessage added in v0.5.0

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

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

func (*MCPServer) RegisterSession added in v0.14.0

func (s *MCPServer) RegisterSession(
	session ClientSession,
) error

RegisterSession saves session that should be notified in case if some server attributes changed.

func (*MCPServer) SendNotificationToClient added in v0.8.0

func (s *MCPServer) SendNotificationToClient(
	ctx context.Context,
	method string,
	params map[string]any,
) error

SendNotificationToClient sends a notification to the current client

func (*MCPServer) SetTools added in v0.8.5

func (s *MCPServer) SetTools(tools ...ServerTool)

SetTools replaces all existing tools with the provided list

func (*MCPServer) UnregisterSession added in v0.14.0

func (s *MCPServer) UnregisterSession(
	sessionID string,
)

UnregisterSession removes from storage session that is shut down.

func (*MCPServer) WithContext added in v0.8.0

func (s *MCPServer) WithContext(
	ctx context.Context,
	session ClientSession,
) context.Context

WithContext sets the current client session and returns the provided context

type NotificationHandlerFunc added in v0.5.0

type NotificationHandlerFunc func(ctx context.Context, notification mcp.JSONRPCNotification)

NotificationHandlerFunc handles incoming notifications.

type PromptHandlerFunc added in v0.5.0

type PromptHandlerFunc func(ctx context.Context, request mcp.GetPromptRequest) (*mcp.GetPromptResult, error)

PromptHandlerFunc handles prompt requests with given arguments.

type ResourceHandlerFunc added in v0.5.0

type ResourceHandlerFunc func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error)

ResourceHandlerFunc is a function that returns resource contents.

type ResourceTemplateHandlerFunc added in v0.5.0

type ResourceTemplateHandlerFunc func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error)

ResourceTemplateHandlerFunc is a function that returns a resource template.

type SSEContextFunc added in v0.13.0

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 added in v0.13.0

type SSEOption func(*SSEServer)

SSEOption defines a function type for configuring SSEServer

func WithBasePath added in v0.12.0

func WithBasePath(basePath string) SSEOption

Add a new option for setting base path

func WithBaseURL added in v0.12.0

func WithBaseURL(baseURL string) SSEOption

WithBaseURL sets the base URL for the SSE server

func WithHTTPServer added in v0.12.0

func WithHTTPServer(srv *http.Server) SSEOption

WithHTTPServer sets the HTTP server instance

func WithMessageEndpoint added in v0.12.0

func WithMessageEndpoint(endpoint string) SSEOption

WithMessageEndpoint sets the message endpoint path

func WithSSEContextFunc added in v0.13.0

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 added in v0.12.0

func WithSSEEndpoint(endpoint string) SSEOption

WithSSEEndpoint sets the SSE endpoint path

type SSEServer added in v0.2.0

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 added in v0.2.0

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

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

func (*SSEServer) SendEventToSession added in v0.2.0

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 added in v0.10.0

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

ServeHTTP implements the http.Handler interface.

func (*SSEServer) Shutdown added in v0.2.0

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 added in v0.2.0

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 ServerOption added in v0.5.0

type ServerOption func(*MCPServer)

ServerOption is a function that configures an MCPServer.

func WithInstructions added in v0.12.0

func WithInstructions(instructions string) ServerOption

WithInstructions sets the server instructions for the client returned in the initialize response

func WithLogging added in v0.5.0

func WithLogging() ServerOption

WithLogging enables logging capabilities for the server

func WithPromptCapabilities added in v0.5.0

func WithPromptCapabilities(listChanged bool) ServerOption

WithPromptCapabilities configures prompt-related server capabilities

func WithResourceCapabilities added in v0.5.0

func WithResourceCapabilities(subscribe, listChanged bool) ServerOption

WithResourceCapabilities configures resource-related server capabilities

func WithToolCapabilities added in v0.5.0

func WithToolCapabilities(listChanged bool) ServerOption

WithToolCapabilities configures tool-related server capabilities

type ServerTool added in v0.8.5

type ServerTool struct {
	Tool    mcp.Tool
	Handler ToolHandlerFunc
}

ServerTool combines a Tool with its ToolHandlerFunc.

type StdioContextFunc added in v0.13.0

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 added in v0.13.0

type StdioOption func(*StdioServer)

StdioOption defines a function type for configuring StdioServer

func WithErrorLogger added in v0.13.0

func WithErrorLogger(logger *log.Logger) StdioOption

WithErrorLogger sets the error logger for the server

func WithStdioContextFunc added in v0.13.0

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 added in v0.5.5

func NewStdioServer(server *MCPServer) *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 added in v0.5.5

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 added in v0.13.0

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 added in v0.5.5

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 ToolHandlerFunc added in v0.5.0

type ToolHandlerFunc func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error)

ToolHandlerFunc handles tool calls with given arguments.

Jump to

Keyboard shortcuts

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