mcptest

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package mcptest checks an MCP server against what agentsafe-mcp relies on. OneEffect checks any tool that changes something: one call makes exactly one effect. A server that does it twice inside one call (two tickets, two charges) can't be protected by any proxy: the proxy sees one call. A policy with key "meta" or "argument" is the operator's claim that the server deduplicates on that key: after a crash or a timeout, the proxy retries with it. SameKey checks the claim. Run it against a sandbox of the real server (a test account, a scratch database), not a mock: the point is to catch the real server's races.

func TestChargeHonoursItsKey(t *testing.T) {
	policy := mcp.Policy{Identity: []string{"ticket_id"}, Key: mcp.KeyArgument, KeyArgument: "idempotency_key"}
	mcptest.SameKey(t, sandbox, "charge", policy, json.RawMessage(`{"ticket_id":"T-1","amount":"1.00"}`),
		func() int { return sandboxChargeCount() })
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckOneEffect added in v0.4.0

func CheckOneEffect(ctx context.Context, upstream *sdk.ClientSession, tool string, policy mcp.Policy, args json.RawMessage, effects func() int) error

CheckOneEffect is OneEffect returning an error instead of failing a test.

func CheckSameKey

func CheckSameKey(ctx context.Context, upstream *sdk.ClientSession, tool string, policy mcp.Policy, args json.RawMessage, effects func() int) error

CheckSameKey is SameKey returning an error instead of failing a test.

func OneEffect added in v0.4.0

func OneEffect(t testing.TB, upstream *sdk.ClientSession, tool string, policy mcp.Policy, args json.RawMessage, effects func() int)

OneEffect fails t unless one call to the upstream's tool, made as agentsafe-mcp makes it under policy (with a fresh key where the policy sends one), makes exactly one effect. It's the check for every tool that changes something, keyed or not: none (the call did nothing, or effects counts the wrong thing) or two (the server repeats the effect inside one call) are both caught. effects counts the real effects; args are the arguments of one operation, as the agent sends them (the proxy adds the key).

func SameKey

func SameKey(t testing.TB, upstream *sdk.ClientSession, tool string, policy mcp.Policy, args json.RawMessage, effects func() int)

SameKey fails t unless the upstream's tool, called the way agentsafe-mcp calls it under policy, performs its effect at most once per key: one call after another (a retry after a crash) or many at the same moment (a retry after a timeout while the first is still running), every caller getting the same answer. effects counts the real effects (charges made, rows written); args are the arguments of one operation.

Types

This section is empty.

Jump to

Keyboard shortcuts

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