Documentation
¶
Overview ¶
Package server provides the core server implementation for the MCP protocol.
The server package handles:
- Server lifecycle management
- Session management
- Tool registration and execution
- Resource pattern matching and access
- Prompt template rendering
- Request and notification handling
Server Creation and Configuration:
// Create a new server srv := server.NewServer("My Server") // Configure server capabilities srv.WithCapabilities(protocol.ServerCapabilities{ SupportsAsync: true, })
Tool Registration:
// Add a synchronous tool srv.AddTool("myTool", func(arg1 string, arg2 int) (string, error) { return fmt.Sprintf("Processed %s with %d", arg1, arg2), nil }, "Tool description") // Add an asynchronous tool srv.AddAsyncTool("longRunningTool", func(params string) error { // Long-running operation return nil }, "Async tool description")
Resource Registration:
// Add a resource with pattern matching srv.AddResource("files/{path}", func(path string) ([]byte, error) { return ioutil.ReadFile(path) }, "Access files")
Prompt Registration:
// Add a prompt template srv.AddPrompt("confirm", func(action string) string { return fmt.Sprintf("Are you sure you want to %s?", action) }, "Confirmation prompt")
Session Management:
// Create a new session session := server.NewSession(context.Background(), srv) // Handle requests through the session response, err := session.HandleRequest(request)
The server package uses reflection to dynamically invoke handlers and convert parameters, making it easy to register any Go function as a tool, resource, or prompt handler.
Index ¶
- type Prompt
- type Resource
- type Server
- func (s *Server) AddAsyncTool(name string, handler interface{}, description string) error
- func (s *Server) AddPrompt(name string, handler interface{}, description string) error
- func (s *Server) AddResource(pattern string, handler interface{}, description string) error
- func (s *Server) AddTool(name string, handler interface{}, description string) error
- type ServerOption
- type Session
- type Tool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Prompt ¶
type Prompt struct { Handler interface{} Description string }
Prompt represents a template for LLM interactions
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents an MCP server instance
func NewServer ¶
func NewServer(name string, opts ...ServerOption) *Server
NewServer creates a new MCP server instance
func (*Server) AddAsyncTool ¶
AddAsyncTool adds an asynchronous tool to the server
func (*Server) AddResource ¶
AddResource adds a resource to the server
type ServerOption ¶
type ServerOption func(*Server)
ServerOption configures a Server
func WithCapabilities ¶
func WithCapabilities(caps protocol.ServerCapabilities) ServerOption
WithCapabilities sets the server capabilities
func WithDefaultCapabilities ¶
func WithDefaultCapabilities() ServerOption
WithDefaultCapabilities sets up the default server capabilities
func WithExperimentalCapabilities ¶
func WithExperimentalCapabilities(caps map[string]map[string]interface{}) ServerOption
WithExperimentalCapabilities adds experimental capabilities
func WithImplementation ¶
func WithImplementation(impl protocol.Implementation) ServerOption
WithImplementation sets the server implementation details
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session represents a connection between client and server
func NewSession ¶
NewSession creates a new session for a client connection
func (*Session) HandleNotification ¶
func (s *Session) HandleNotification(notif *protocol.JSONRPCNotification) error
HandleNotification processes an incoming JSON-RPC notification
func (*Session) HandleRequest ¶
func (s *Session) HandleRequest(req *protocol.JSONRPCRequest) (*protocol.JSONRPCResponse, error)
HandleRequest processes an incoming JSON-RPC request