mcp

package
v0.12.0 Latest Latest
Warning

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

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

Documentation

Overview

Package mcp provides deterministic MCP client simulation and assertion helpers for testing MCP tool implementations without deploying infrastructure.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertError

func AssertError(t testing.TB, resp *mcpruntime.Response, expectedCode int)

AssertError asserts that the JSON-RPC response contains an error with the expected error code.

func AssertHasTools

func AssertHasTools(t testing.TB, tools []mcpruntime.ToolDef, names ...string)

AssertHasTools asserts that the tool list contains tools with all the given names.

func AssertToolResult

func AssertToolResult(t testing.TB, resp *mcpruntime.Response, expectedText string)

AssertToolResult asserts that the JSON-RPC response contains a successful tool result with a text content block matching the expected text.

func CallToolRequest

func CallToolRequest(id any, name string, args any) (*mcpruntime.Request, error)

CallToolRequest builds a JSON-RPC tools/call request for the named tool.

func GetPromptRequest added in v0.11.0

func GetPromptRequest(id any, name string, args any) (*mcpruntime.Request, error)

GetPromptRequest builds a JSON-RPC prompts/get request for the given name and arguments.

func InitializeRequest

func InitializeRequest(id any) *mcpruntime.Request

InitializeRequest builds a JSON-RPC initialize request.

func ListPromptsRequest added in v0.11.0

func ListPromptsRequest(id any) *mcpruntime.Request

ListPromptsRequest builds a JSON-RPC prompts/list request.

func ListResourcesRequest added in v0.11.0

func ListResourcesRequest(id any) *mcpruntime.Request

ListResourcesRequest builds a JSON-RPC resources/list request.

func ListToolsRequest

func ListToolsRequest(id any) *mcpruntime.Request

ListToolsRequest builds a JSON-RPC tools/list request.

func ReadResourceRequest added in v0.11.0

func ReadResourceRequest(id any, uri string) (*mcpruntime.Request, error)

ReadResourceRequest builds a JSON-RPC resources/read request for the given URI.

Types

type Client

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

Client is a test MCP client that invokes an in-process MCP server.

func NewClient

func NewClient(server *mcpruntime.Server, env *testkit.Env) *Client

NewClient creates a test MCP client backed by the given MCP server and deterministic test environment.

func (*Client) CallTool

func (c *Client) CallTool(ctx context.Context, name string, args any) (*mcpruntime.ToolResult, error)

CallTool sends a tools/call request for the named tool with the given arguments.

func (*Client) GetPrompt added in v0.11.0

func (c *Client) GetPrompt(ctx context.Context, name string, args any) (*mcpruntime.PromptResult, error)

GetPrompt sends a prompts/get request for the named prompt with the given arguments.

func (*Client) Initialize

func (c *Client) Initialize(ctx context.Context) (*mcpruntime.Response, error)

Initialize sends an MCP initialize request and captures the returned session ID for use in subsequent requests.

func (*Client) ListPrompts added in v0.11.0

func (c *Client) ListPrompts(ctx context.Context) ([]mcpruntime.PromptDef, error)

ListPrompts sends a prompts/list request and returns the parsed prompt definitions.

func (*Client) ListResources added in v0.11.0

func (c *Client) ListResources(ctx context.Context) ([]mcpruntime.ResourceDef, error)

ListResources sends a resources/list request and returns the parsed resource definitions.

func (*Client) ListTools

func (c *Client) ListTools(ctx context.Context) ([]mcpruntime.ToolDef, error)

ListTools sends a tools/list request and returns the parsed tool definitions.

func (*Client) Raw

Raw sends an arbitrary JSON-RPC request to the MCP server and returns the parsed JSON-RPC response. It automatically includes the session ID header if one has been captured, and captures any new session ID from the response.

func (*Client) RawStream added in v0.11.1

func (c *Client) RawStream(ctx context.Context, req *mcpruntime.Request, extraHeaders map[string][]string) (*Stream, error)

RawStream sends a JSON-RPC request to POST /mcp with `Accept: text/event-stream` and returns a Stream for reading incremental SSE messages.

func (*Client) ReadResource added in v0.11.0

func (c *Client) ReadResource(ctx context.Context, uri string) ([]mcpruntime.ResourceContent, error)

ReadResource sends a resources/read request for the given URI and returns the parsed contents.

func (*Client) ResumeStream added in v0.11.1

func (c *Client) ResumeStream(ctx context.Context, lastEventID string, extraHeaders map[string][]string) (*Stream, error)

ResumeStream opens GET /mcp with Last-Event-ID and returns a Stream for replay/resume.

func (*Client) SessionID

func (c *Client) SessionID() string

SessionID returns the current session ID captured from the last response.

type SSEMessage added in v0.11.1

type SSEMessage struct {
	ID    string
	Event string
	Data  json.RawMessage
}

SSEMessage is a parsed Server-Sent Events frame.

For MCP Streamable HTTP, Data is typically a single JSON-RPC message.

func ReadSSEMessage added in v0.11.1

func ReadSSEMessage(r *bufio.Reader) (*SSEMessage, error)

ReadSSEMessage reads a single SSE message (one frame, terminated by a blank line).

type Stream added in v0.11.1

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

Stream represents an open SSE stream from an in-process MCP server.

Cancel simulates a client disconnect.

func (*Stream) Cancel added in v0.11.1

func (s *Stream) Cancel()

Cancel simulates a client disconnect by canceling the stream context.

func (*Stream) Next added in v0.11.1

func (s *Stream) Next() (*SSEMessage, error)

Next blocks until the next SSE message is available or the stream ends.

func (*Stream) ReadAll added in v0.11.1

func (s *Stream) ReadAll() ([]SSEMessage, error)

ReadAll reads all remaining SSE messages until EOF.

func (*Stream) Response added in v0.11.1

func (s *Stream) Response() apptheory.Response

Response returns the HTTP response metadata for the stream.

Jump to

Keyboard shortcuts

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