mcptest

package
v0.56.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 11 Imported by: 1

Documentation

Overview

Package mcptest implements helper functions for testing MCP servers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

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

Server encapsulates an MCP server and manages resources like pipes and context.

func NewServer

func NewServer(t *testing.T, tools ...server.ServerTool) (*Server, error)

NewServer starts a new MCP server with the provided tools and returns the server instance. The server's lifetime is managed by Close(), not by the test context, so it can be safely created in setup helpers and used across multiple subtests.

func NewUnstartedServer

func NewUnstartedServer(t *testing.T) *Server

NewUnstartedServer creates a new MCP server instance with the given name, but does not start the server. Useful for tests where you need to add tools before starting the server.

func (*Server) AddPrompt added in v0.32.0

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

AddPrompt adds a prompt to an unstarted server.

func (*Server) AddPrompts added in v0.32.0

func (s *Server) AddPrompts(prompts ...server.ServerPrompt)

AddPrompts adds multiple prompts to an unstarted server.

func (*Server) AddResource added in v0.32.0

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

AddResource adds a resource to an unstarted server.

func (*Server) AddResourceTemplate added in v0.33.0

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

AddResourceTemplate adds a resource template to an unstarted server.

func (*Server) AddResourceTemplates added in v0.33.0

func (s *Server) AddResourceTemplates(templates ...server.ServerResourceTemplate)

AddResourceTemplates adds multiple resource templates to an unstarted server.

func (*Server) AddResources added in v0.32.0

func (s *Server) AddResources(resources ...server.ServerResource)

AddResources adds multiple resources to an unstarted server.

func (*Server) AddServerOptions added in v0.48.0

func (s *Server) AddServerOptions(opts ...server.ServerOption)

AddServerOptions adds server options to an unstarted server. These options are passed to server.NewMCPServer when the server is started, allowing configuration of hooks, middleware, tool filters, and other server settings.

func (*Server) AddTool

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

AddTool adds a tool to an unstarted server.

func (*Server) AddTools

func (s *Server) AddTools(tools ...server.ServerTool)

AddTools adds multiple tools to an unstarted server.

func (*Server) Client

func (s *Server) Client() *client.Client

Client returns an MCP client connected to the server. The client is already initialized, i.e. you do _not_ need to call Client.Initialize().

func (*Server) Close

func (s *Server) Close()

Close stops the server and cleans up resources like temporary directories.

func (*Server) SetClientInfo added in v0.44.0

func (s *Server) SetClientInfo(info mcp.Implementation)

SetClientInfo sets the client info for the test client.

func (*Server) SetElicitationHandler added in v0.55.0

func (s *Server) SetElicitationHandler(h client.ElicitationHandler)

SetElicitationHandler registers a handler that responds to elicitation requests (server.RequestElicitation) made by tools under test. Must be called before Start(). The test client will advertise the elicitation capability during initialization so that the server is allowed to issue elicitation requests.

func (*Server) SetSamplingHandler added in v0.55.0

func (s *Server) SetSamplingHandler(h client.SamplingHandler)

SetSamplingHandler registers a handler that responds to sampling requests (server.RequestSampling) made by tools under test. Must be called before Start(). The test client will advertise the sampling capability during initialization so that the server is allowed to issue sampling requests.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start starts the server in a goroutine. Make sure to defer Close() after Start(). When using NewServer(), the returned server is already started.

Jump to

Keyboard shortcuts

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