mockmcpserver

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExampleMockMCPServer_receivedRequests

func ExampleMockMCPServer_receivedRequests()

ExampleMockMCPServer_receivedRequests demonstrates inspecting the recorded tool invocations after the client finishes calling the MCP server.

func ExampleNewMockMCPServer_withCalls

func ExampleNewMockMCPServer_withCalls()

ExampleNewMockMCPServer_withCalls shows how to queue scripted tool calls when constructing the mock server.

func StdioServerCommand

func StdioServerCommand(t testing.TB) []string

StdioServerCommand returns the command args to launch the stdio MCP server script. It fails the test if the script cannot be prepared.

Types

type APIKey

type APIKey struct {
	Key    string
	UserID string
	Scopes []string
}

APIKey is a static API key record for auth verification in tests.

type Call

type Call struct {
	Tool            string
	Result          json.RawMessage
	DynamicResult   func(json.RawMessage) (json.RawMessage, error)
	Error           *CallError
	Progress        []ProgressUpdate
	ResponseHeaders http.Header
}

Call defines a scripted tool invocation.

type CallError

type CallError struct {
	Message string
}

CallError describes the error payload returned by the tool.

type IncomingHTTPRequest

type IncomingHTTPRequest struct {
	Method  string
	Path    string
	Headers http.Header
	Body    []byte
}

IncomingHTTPRequest captures an HTTP request received by the mock server.

type IncomingRequest

type IncomingRequest struct {
	Tool      string
	Arguments json.RawMessage
	Headers   http.Header
}

IncomingRequest captures the tool arguments and headers that reached the mock.

type MockMCPServer

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

func NewMockMCPServer

func NewMockMCPServer(opts ...Option) *MockMCPServer

NewMockMCPServer constructs an empty mock server configured by optional options.

func (*MockMCPServer) BaseURL

func (m *MockMCPServer) BaseURL() *url.URL

BaseURL returns the HTTP endpoint the MCP client should connect to.

func (*MockMCPServer) Close

func (m *MockMCPServer) Close()

Close shuts down the server and asserts all scripted calls were consumed.

func (*MockMCPServer) ReceivedHTTPRequests

func (m *MockMCPServer) ReceivedHTTPRequests() []IncomingHTTPRequest

ReceivedHTTPRequests returns all HTTP requests observed by the mock server.

func (*MockMCPServer) ReceivedRequests

func (m *MockMCPServer) ReceivedRequests() []IncomingRequest

ReceivedRequests returns the recorded tool requests in order.

func (*MockMCPServer) Start

func (m *MockMCPServer) Start(t testing.TB)

Start launches the mock MCP server and registers cleanup with t.

func (*MockMCPServer) StartInMemory

func (m *MockMCPServer) StartInMemory(t testing.TB) *mcp.InMemoryTransport

StartInMemory launches the mock MCP server on an in-memory transport.

func (*MockMCPServer) StartStdio

func (m *MockMCPServer) StartStdio(t testing.TB) *mcp.IOTransport

StartStdio launches the mock MCP server over stdio and returns a client transport. Note that stdio is process-wide, so this method should not be used concurrently.

func (*MockMCPServer) TLSCertPEM

func (m *MockMCPServer) TLSCertPEM() ([]byte, error)

TLSCertPEM returns the server certificate PEM for TLS-enabled servers.

func (*MockMCPServer) WaitForRequests

func (m *MockMCPServer) WaitForRequests(ctx context.Context, n int) error

WaitForRequests blocks until at least n tool calls have completed or ctx expires.

type Option

type Option func(*MockMCPServer)

MockMCPServer hosts a Streamable HTTP MCP server backed by scripted tool handlers.

func WithCalls

func WithCalls(calls ...Call) Option

WithCalls seeds the mock with scripted tool invocations.

func WithHostHandler

func WithHostHandler(host string, handler http.Handler) Option

WithHostHandler serves requests for host with handler before the default MCP routes.

func WithKeepalivePings

func WithKeepalivePings() Option

WithKeepalivePings injects SSE keepalive ping events ahead of streamable responses.

func WithOAuthDiscoveryResources

func WithOAuthDiscoveryResources() Option

WithOAuthDiscoveryResources enables the well-known OAuth ProtectedResourceMetaData endpoint.

func WithOAuthProtection

func WithOAuthProtection() Option

WithOAuthProtection enables OAuth discovery endpoints and protects the MCP server with auth.RequireBearerToken using a static in-memory API key set.

func WithProtectedResourceMetadata

func WithProtectedResourceMetadata(meta oauthex.ProtectedResourceMetadata) Option

WithProtectedResourceMetadata sets the metadata returned by the well-known OAuth Protected Resource Metadata endpoint.

func WithRequiredHeader

func WithRequiredHeader(name, value string) Option

WithRequiredHeader requires every HTTP request to carry name: value.

func WithRequiredHeaderFor

func WithRequiredHeaderFor(name, value string, predicate func(*http.Request) bool) Option

WithRequiredHeaderFor requires matching HTTP requests to carry name: value.

func WithTLSServer

func WithTLSServer() Option

WithTLSServer starts the mock MCP server with TLS enabled.

func WithToolListChangedNotificationsDisabled

func WithToolListChangedNotificationsDisabled() Option

WithToolListChangedNotificationsDisabled disables tool list changed notifications.

func WithUnixSocketPath

func WithUnixSocketPath(path string) Option

WithUnixSocketPath makes the mock serve HTTP over the provided Unix-domain socket.

func WithWWWAuthenticateProbe

func WithWWWAuthenticateProbe() Option

WithWWWAuthenticateProbe enables a 401 response with WWW-Authenticate header for empty POST probes.

type ProgressUpdate

type ProgressUpdate struct {
	Percentage float64
	Message    string
}

ProgressUpdate emits a notifications/progress event during a streaming response.

Jump to

Keyboard shortcuts

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