testing

package
v0.0.0-...-7c75ef6 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package testing provides an httptest-backed fake MCP server for probe, dispatch, controller, and CLI tests.

The happy path is served by a REAL go-sdk Streamable-HTTP server, because the probe client speaks the full session handshake (initialize → Mcp-Session-Id → notifications/initialized) and would reject a stateless JSON-RPC responder with "invalid during session initialization". Fault-injection behaviors (Status / RawBody / JSONRPCError / SSEResponse) short-circuit that session with a raw response, so tests can still exercise how the client surfaces HTTP-status, malformed and JSON-RPC errors.

Because this package's name shadows the stdlib "testing" package, all consumers must import it with an alias:

import mcptest "github.com/authzed/openagentprimitives/pkg/tools/mcp/testing"

A session-keyed MCP server for tests that need to answer one question: does this caller reuse ONE MCP session across tool calls, or open a fresh session per call?

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCallServer

func NewCallServer(o CallServerOpts) *httptest.Server

NewCallServer stands up a REAL go-sdk Streamable-HTTP server scripting the tools/call path (unlike New, which focuses on tools/list). The caller must Close it (t.Cleanup(srv.Close)).

func NewSelectionServer

func NewSelectionServer() (srv *httptest.Server, sessionCount func() int)

NewSelectionServer starts a real go-sdk MCP server whose "select"/"selected" tools store and read a value keyed by the MCP session id (req.Session.ID()) — the same shape as the dedicated-mcp sidecar's per-session PermissionSystem selection. A value set by one call is visible to a later call ONLY if both calls ride the same MCP session, so calling select then selected is a direct behavioral probe for session reuse: "adobe" back means one shared session, IsError ("no selection for this session") means a fresh session per call.

sessionCount reports how many DISTINCT sessions the server has served, so a test can also assert reuse structurally rather than only by its side effect.

The caller owns the server: srv.Close (typically via t.Cleanup).

func TextResult

func TextResult(text string, isError bool) *mcp.CallToolResult

TextResult builds a single-text-block tools/call result.

func ToolNames

func ToolNames[T any](tools []T, name func(T) string) map[string]bool

ToolNames collects the names from a tools slice (via the given accessor) into a set, so tests can assert order-independent membership without repeating the extraction loop. The go-sdk client does not guarantee tools/list ordering, so assertions must be "contains", never positional.

Generic over the tool type (probe.Tool, a CLI result's tool, ...) so it stays in this leaf test-helper package without importing — and cycling with — the packages under test.

Types

type Annotations

type Annotations struct {
	DestructiveHint       bool            `json:"destructiveHint,omitempty"`
	ReadOnlyHint          bool            `json:"readOnlyHint,omitempty"`
	IdempotentHint        bool            `json:"idempotentHint,omitempty"`
	OpenWorldHint         bool            `json:"openWorldHint,omitempty"`
	Title                 string          `json:"title,omitempty"`
	MaliciousActivityHint bool            `json:"maliciousActivityHint,omitempty"`
	Attribution           []string        `json:"attribution,omitempty"`
	InputMetadata         json.RawMessage `json:"inputMetadata,omitempty"`
	ReturnMetadata        json.RawMessage `json:"returnMetadata,omitempty"`
}

Annotations mirrors tool annotation hints for the fake server.

type Behavior

type Behavior struct {
	// Tools returned on success. If a fault is set, Tools is ignored.
	Tools []Tool
	// ServerInfo, when non-nil, sets the go-sdk server's advertised identity.
	// When nil the server reports an empty implementation identity.
	ServerInfo *ServerInfo
	// Status overrides the HTTP status (default 200). A non-200 value faults the
	// session at the first (initialize) request.
	Status int
	// RawBody overrides the response body (used for malformed-JSON tests). Faults
	// the session.
	RawBody string
	// JSONRPCError, when non-empty, returns a JSON-RPC error envelope. Faults the
	// session.
	JSONRPCError string
	// SSEResponse, when non-empty, overrides the response body with the given raw
	// SSE bytes and sets Content-Type: text/event-stream. Highest-priority fault.
	SSEResponse string
	// RequireHeader, when set, 401s any request lacking that header value (checked
	// on the happy AND fault paths, before anything else).
	RequireHeaderName  string
	RequireHeaderValue string
}

Behavior controls per-test responses.

type CallServerOpts

type CallServerOpts struct {
	// Tools maps tool name → its call handler.
	Tools map[string]ToolHandler
	// RequireHeaderName/Value, when set, 401s any request lacking that header
	// value BEFORE the go-sdk session — for auth and reauth-retry tests. On a
	// reauth test the first (stale-token) request 401s, the client re-auths, and
	// the retried request carries the matching value.
	RequireHeaderName, RequireHeaderValue string
	// OnRequest, when set, is invoked with each request's Authorization header
	// (for tests asserting which credentials the server saw). Called for EVERY
	// request in the session (initialize, notifications, tools/call, delete), so
	// assert membership, not exact counts.
	OnRequest func(authorization string)
}

CallServerOpts configures NewCallServer.

type Server

type Server struct {
	*httptest.Server

	// LastHeader captures the last request's headers for test assertions.
	// NOTE: a go-sdk session makes several requests (initialize, notifications,
	// tools/list, and a DELETE on close), so this is the LAST of them — assert on
	// headers the client sets on every request (e.g. Authorization), not on a
	// per-method header like Content-Type. Safe to read after the request-making
	// call has fully returned. Concurrent in-flight requests are NOT safe — use
	// one Server per concurrent test.
	LastHeader http.Header
	// contains filtered or unexported fields
}

Server wraps an httptest.Server with mutable behavior.

func New

func New(b Behavior) *Server

New starts a fake server with the given initial behavior.

func (*Server) SetBehavior

func (s *Server) SetBehavior(b Behavior)

SetBehavior updates the server's behavior atomically (rebuilding the go-sdk handler so a subsequent session reflects the new tools/identity).

type ServerInfo

type ServerInfo struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

ServerInfo is the fake server's identity for the initialize handshake.

type Tool

type Tool struct {
	Name         string          `json:"name"`
	Description  string          `json:"description,omitempty"`
	InputSchema  json.RawMessage `json:"inputSchema,omitempty"`
	OutputSchema json.RawMessage `json:"outputSchema,omitempty"`
	Annotations  *Annotations    `json:"annotations,omitempty"`
}

Tool is the MCP tools/list result shape.

type ToolHandler

type ToolHandler = func(context.Context, *mcp.CallToolRequest) (*mcp.CallToolResult, error)

ToolHandler is one tool's tools/call implementation.

func StaticTool

func StaticTool(result *mcp.CallToolResult) ToolHandler

StaticTool returns a ToolHandler that always yields result.

Jump to

Keyboard shortcuts

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