Documentation
¶
Overview ¶
Package mcp provides deterministic MCP client simulation and assertion helpers for testing MCP tool implementations without deploying infrastructure.
Index ¶
- func AssertError(t testing.TB, resp *mcpruntime.Response, expectedCode int)
- func AssertHasTools(t testing.TB, tools []mcpruntime.ToolDef, names ...string)
- func AssertToolResult(t testing.TB, resp *mcpruntime.Response, expectedText string)
- func CallToolRequest(id any, name string, args any) (*mcpruntime.Request, error)
- func GetPromptRequest(id any, name string, args any) (*mcpruntime.Request, error)
- func InitializeRequest(id any) *mcpruntime.Request
- func ListPromptsRequest(id any) *mcpruntime.Request
- func ListResourcesRequest(id any) *mcpruntime.Request
- func ListToolsRequest(id any) *mcpruntime.Request
- func ReadResourceRequest(id any, uri string) (*mcpruntime.Request, error)
- type Client
- func (c *Client) CallTool(ctx context.Context, name string, args any) (*mcpruntime.ToolResult, error)
- func (c *Client) GetPrompt(ctx context.Context, name string, args any) (*mcpruntime.PromptResult, error)
- func (c *Client) Initialize(ctx context.Context) (*mcpruntime.Response, error)
- func (c *Client) ListPrompts(ctx context.Context) ([]mcpruntime.PromptDef, error)
- func (c *Client) ListResources(ctx context.Context) ([]mcpruntime.ResourceDef, error)
- func (c *Client) ListTools(ctx context.Context) ([]mcpruntime.ToolDef, error)
- func (c *Client) Raw(ctx context.Context, req *mcpruntime.Request) (*mcpruntime.Response, error)
- func (c *Client) RawStream(ctx context.Context, req *mcpruntime.Request, extraHeaders map[string][]string) (*Stream, error)
- func (c *Client) ReadResource(ctx context.Context, uri string) ([]mcpruntime.ResourceContent, error)
- func (c *Client) ResumeStream(ctx context.Context, lastEventID string, extraHeaders map[string][]string) (*Stream, error)
- func (c *Client) SessionID() string
- type SSEMessage
- type Stream
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 ¶
CallToolRequest builds a JSON-RPC tools/call request for the named tool.
func GetPromptRequest ¶ added in v0.11.0
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 ¶
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
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 ¶
ListTools sends a tools/list request and returns the parsed tool definitions.
func (*Client) Raw ¶
func (c *Client) Raw(ctx context.Context, req *mcpruntime.Request) (*mcpruntime.Response, error)
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.
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.