Documentation
¶
Overview ¶
Package server provides MCP (Model Control Protocol) server implementations.
Index ¶
- func CreateErrorResponse(id interface{}, code int, message string) mcp.JSONRPCMessage
- func NewTestServer(server *MCPServer) *httptest.Server
- func ServeStdio(server *MCPServer) error
- type AuthChecker
- type MCPServer
- func (s *MCPServer) AddAuthorizer(f AuthChecker)
- func (s *MCPServer) AddNotificationHandler(method string, handler NotificationHandlerFunc)
- func (s *MCPServer) AddPrompt(prompt mcp.Prompt, handler PromptHandlerFunc)
- func (s *MCPServer) AddResource(resource mcp.Resource, handler ResourceHandlerFunc)
- func (s *MCPServer) AddResourceTemplate(template mcp.ResourceTemplate, handler ResourceTemplateHandlerFunc)
- func (s *MCPServer) AddTool(tool mcp.Tool, handler ToolHandlerFunc)
- func (s *MCPServer) AddToolMiddleware(f ToolMiddlewareFunc)
- func (s *MCPServer) AddTools(tools ...ServerTool)
- func (s *MCPServer) DeleteTools(names ...string)
- func (s *MCPServer) HandleMessage(ctx context.Context, message json.RawMessage) mcp.JSONRPCMessage
- func (s *MCPServer) NeedAuth(r *http.Request) bool
- func (s *MCPServer) Notifications() <-chan ServerNotification
- func (s *MCPServer) SendNotificationToClient(method string, params map[string]interface{}) error
- func (s *MCPServer) SetTools(tools ...ServerTool)
- func (s *MCPServer) WithContext(ctx context.Context, notifCtx NotificationContext) context.Context
- type NotificationContext
- type NotificationHandlerFunc
- type PromptHandlerFunc
- type ResourceHandlerFunc
- type ResourceTemplateHandlerFunc
- type SSEServer
- type ServerNotification
- type ServerOption
- type ServerTool
- type StdioServer
- type ToolHandlerFunc
- type ToolMiddlewareFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateErrorResponse ¶ added in v0.0.10
func CreateErrorResponse( id interface{}, code int, message string, ) mcp.JSONRPCMessage
func NewTestServer ¶
NewTestServer creates a test server for testing purposes
func ServeStdio ¶
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 AuthChecker ¶ added in v0.2.9
AuthChecker verify sse request
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 ¶
func NewMCPServer( name, version string, opts ...ServerOption, ) *MCPServer
NewMCPServer creates a new MCP server instance with the given name, version and options
func ServerFromContext ¶
ServerFromContext retrieves the MCPServer instance from a context
func (*MCPServer) AddAuthorizer ¶ added in v0.2.9
func (s *MCPServer) AddAuthorizer(f AuthChecker)
AddAuthorizer include auth checker to server
func (*MCPServer) AddNotificationHandler ¶
func (s *MCPServer) AddNotificationHandler( method string, handler NotificationHandlerFunc, )
AddNotificationHandler registers a new handler for incoming notifications
func (*MCPServer) AddPrompt ¶
func (s *MCPServer) AddPrompt(prompt mcp.Prompt, handler PromptHandlerFunc)
AddPrompt registers a new prompt handler with the given name
func (*MCPServer) AddResource ¶
func (s *MCPServer) AddResource( resource mcp.Resource, handler ResourceHandlerFunc, )
AddResource registers a new resource and its handler
func (*MCPServer) AddResourceTemplate ¶
func (s *MCPServer) AddResourceTemplate( template mcp.ResourceTemplate, handler ResourceTemplateHandlerFunc, )
AddResourceTemplate registers a new resource template and its handler
func (*MCPServer) AddTool ¶
func (s *MCPServer) AddTool(tool mcp.Tool, handler ToolHandlerFunc)
AddTool registers a new tool and its handler
func (*MCPServer) AddToolMiddleware ¶ added in v0.2.7
func (s *MCPServer) AddToolMiddleware(f ToolMiddlewareFunc)
func (*MCPServer) AddTools ¶
func (s *MCPServer) AddTools(tools ...ServerTool)
AddTools registers multiple tools at once
func (*MCPServer) DeleteTools ¶
DeleteTools removes a tool from the server
func (*MCPServer) HandleMessage ¶
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) Notifications ¶ added in v0.0.10
func (s *MCPServer) Notifications() <-chan ServerNotification
func (*MCPServer) SendNotificationToClient ¶
SendNotificationToClient sends a notification to the current client
func (*MCPServer) SetTools ¶
func (s *MCPServer) SetTools(tools ...ServerTool)
SetTools replaces all existing tools with the provided list
func (*MCPServer) WithContext ¶
func (s *MCPServer) WithContext( ctx context.Context, notifCtx NotificationContext, ) context.Context
WithContext sets the current client context and returns the provided context
type NotificationContext ¶
NotificationContext provides client identification for notifications
type NotificationHandlerFunc ¶
type NotificationHandlerFunc func(ctx context.Context, notification mcp.JSONRPCNotification)
NotificationHandlerFunc handles incoming notifications.
type PromptHandlerFunc ¶
type PromptHandlerFunc func(ctx context.Context, request mcp.GetPromptRequest) (*mcp.GetPromptResult, error)
PromptHandlerFunc handles prompt requests with given arguments.
type ResourceHandlerFunc ¶
type ResourceHandlerFunc func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error)
ResourceHandlerFunc is a function that returns resource contents.
type ResourceTemplateHandlerFunc ¶
type ResourceTemplateHandlerFunc func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error)
ResourceTemplateHandlerFunc is a function that returns a resource template.
type SSEServer ¶
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 ¶
NewSSEServer creates a new SSE server instance with the given MCP server and base URL.
func (*SSEServer) SendEventToSession ¶
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 ¶
func (s *SSEServer) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface.
type ServerNotification ¶
type ServerNotification struct { Context NotificationContext Notification mcp.JSONRPCNotification }
ServerNotification combines the notification with client context
type ServerOption ¶
type ServerOption func(*MCPServer)
ServerOption is a function that configures an MCPServer.
func WithInstructions ¶ added in v0.0.9
func WithInstructions(instructions string) ServerOption
func WithLogging ¶
func WithLogging() ServerOption
WithLogging enables logging capabilities for the server
func WithPromptCapabilities ¶
func WithPromptCapabilities(listChanged bool) ServerOption
WithPromptCapabilities configures prompt-related server capabilities
func WithResourceCapabilities ¶
func WithResourceCapabilities(subscribe, listChanged bool) ServerOption
WithResourceCapabilities configures resource-related server capabilities
type ServerTool ¶
type ServerTool struct { Tool mcp.Tool Handler ToolHandlerFunc }
ServerTool combines a Tool with its ToolHandlerFunc.
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 ¶
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 ¶
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) SetErrorLogger ¶
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 ¶
type ToolHandlerFunc func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error)
ToolHandlerFunc handles tool calls with given arguments.
type ToolMiddlewareFunc ¶ added in v0.2.7
type ToolMiddlewareFunc func(ctx context.Context, tool ServerTool, request mcp.CallToolRequest) (*mcp.CallToolResult, error)
ToolMiddlewareFunc add a middleware interceptor for tool