Master the Expect API for powerful assertions on content, tools, performance, and more.
The Expect API is your Swiss Army knife for MCP testing. Use it to assert everything from simple content checks to complex path efficiency and LLM quality judgments.
# Verify a specific tool was calledawait session.assert_that( Expect.tools.was_called("calculator"), name="used_calculator")# Check tool wasn't called (negative assertion)await session.assert_that( Expect.tools.count("dangerous_tool", 0), name="safety_check")
# Verify exact sequence of toolsawait session.assert_that( Expect.tools.sequence(["auth", "fetch", "parse"]), name="correct_order")# Check tool was called with specific argumentsawait session.assert_that( Expect.tools.called_with( tool_name="fetch", expected_args={"url": "https://api.example.com"} ))# Verify success rate across all tool callsawait session.assert_that( Expect.tools.success_rate(min_rate=0.95), name="high_reliability")
# Maximum response time in millisecondsawait session.assert_that( Expect.performance.response_time_under(5000), name="fast_response")# Limit conversation iterationsawait session.assert_that( Expect.performance.max_iterations(3), name="efficient_solution")
# Basic quality checkawait session.assert_that( Expect.judge.llm( rubric=""" The response should: - Be professional and courteous - Provide accurate information - Be concise (under 100 words) """, min_score=0.8, include_input=True # Give judge full context ), response=response, name="quality_check")
# Use specific model for judgingjudge = Expect.judge.llm( rubric="Evaluate for technical accuracy", min_score=0.85, model="claude-3-opus-20240229")await session.assert_that(judge, response=response)
# Define the ideal execution pathawait session.assert_that( Expect.path.efficiency( expected_tool_sequence=["validate", "process", "save"], tool_usage_limits={ "validate": 1, # Should validate only once "process": 1, # Process only once "save": 1 # Save only once }, allow_extra_steps=0, penalize_backtracking=True ), name="golden_path")
# Allow some variation while ensuring key waypointsawait session.assert_that( # For waypoint/pattern checks, combine Expect.tools.sequence and # Expect.tools.count along with Expect.path.efficiency)
Start simple, then add complexity: Begin with basic content assertions, then layer on tool checks, performance requirements, and quality judgments as needed.
Avoid over-constraining: Too many strict assertions can make tests brittle. Focus on what truly matters for your use case.
Use descriptive names: Always provide a name parameter for your assertions. This makes debugging much easier when tests fail.