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
Themcp.mock API provides a registry for mocking MCP responses.
Basic Usage
Mock Options
| Option | Type | Description |
|---|---|---|
method | string | MCP method to match (e.g., 'tools/call') |
params | object | Parameters to match (partial match) |
response | object | JSON-RPC response to return |
times | number | Number of times to use this mock (default: unlimited) |
delay | number | Delay in ms before responding |
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
| Action | Description |
|---|---|
passthrough | Continue to server normally |
modify | Modify the request before sending |
mock | Return a mock response without calling server |
error | Throw an error |
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: 1to test retry logic - Clear mocks between tests if needed
- Use convenience methods for simple cases
- 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

