server

package
v0.0.0-...-93b92dd Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2025 License: MIT Imports: 12 Imported by: 0

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

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 Resource

type Resource struct {
	Handler     interface{}
	Description string
	Pattern     string
}

Resource represents a data source that can be accessed by the LLM

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

func (s *Server) AddAsyncTool(name string, handler interface{}, description string) error

AddAsyncTool adds an asynchronous tool to the server

func (*Server) AddPrompt

func (s *Server) AddPrompt(name string, handler interface{}, description string) error

AddPrompt adds a prompt to the server

func (*Server) AddResource

func (s *Server) AddResource(pattern string, handler interface{}, description string) error

AddResource adds a resource to the server

func (*Server) AddTool

func (s *Server) AddTool(name string, handler interface{}, description string) error

AddTool adds a tool 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

func NewSession(ctx context.Context, server *Server) *Session

NewSession creates a new session for a client connection

func (*Session) Close

func (s *Session) Close() error

Close ends the session

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

type Tool

type Tool struct {
	Handler     interface{}
	Description string
	IsAsync     bool
}

Tool represents a function that can be called by the LLM

Jump to

Keyboard shortcuts

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