Skip to main content
The MCP interceptor system allows you to mock tool responses, modify requests, and simulate various conditions without calling the actual server implementation.

When to Use Mocking

Use MCP-level mocking when you want to:
  • Skip expensive tool operations in tests
  • Simulate error conditions
  • Test client behavior without server dependencies
  • Add latency to test timeout handling
  • Verify request parameters without side effects
For mocking external HTTP calls made by your tools, use HTTP Mocking instead.

Mock Registry

The mcp.mock API provides a registry for mocking MCP responses.

Basic Usage

Mock Options


Convenience Methods

Mocking Tools

Mocking Resources

Clearing Mocks


Request Interceptors

Intercept and modify outgoing requests before they reach the server.

Logging Requests

Modifying Requests

Returning Mock Response

Failing Requests

Interceptor Actions


Response Interceptors

Intercept and modify responses after they’re received from the server.

Logging Responses

Modifying Responses


Convenience Helpers

Adding Latency

Failing Specific Methods


Call Tracking

Track how mocks are used:

One-Time Mocks

Create mocks that only match a specific number of times:

mockResponse Helpers

Pre-built response creators for common scenarios:

Best Practices

Do:
  • Mock expensive operations (external APIs, database calls)
  • Use times: 1 to test retry logic
  • Clear mocks between tests if needed
  • Use convenience methods for simple cases
Don’t:
  • Mock everything - some tests should hit real code
  • Forget to remove interceptors after tests
  • Use mocking to hide bugs in your code
  • Over-engineer mock setups