Testing

Comprehensive guide to testing your MCP server and tools.

Testing Strategy

A complete testing strategy includes:

  1. Unit Tests - Test individual tools in isolation

  2. Integration Tests - Test tools with real AIDP API

  3. End-to-End Tests - Test complete MCP server workflows

  4. Performance Tests - Test under load

  5. Security Tests - Test authentication and authorization


Unit Testing

Setup

npm install --save-dev jest @types/jest

Test Structure

// tools/__tests__/search-businesses.test.ts
import { searchBusinesses } from '../search-businesses';
import { AIDPClient } from '@aidp/sdk';

// Mock AIDP client
jest.mock('@aidp/sdk');

describe('search_businesses tool', () => {
  let mockClient: jest.Mocked<AIDPClient>;

  beforeEach(() => {
    mockClient = new AIDPClient({ apiKey: 'test' }) as jest.Mocked<AIDPClient>;
  });

  afterEach(() => {
    jest.clearAllMocks();
  });

  it('should search for businesses successfully', async () => {
    // Arrange
    const mockResponse = {
      success: true,
      data: {
        businesses: [{ id: 'biz_1', name: 'Test Business' }],
        total: 1,
      },
    };
    mockClient.search.mockResolvedValue(mockResponse);

    // Act
    const result = await searchBusinesses.handler({
      query: 'coffee shops',
      location: { lat: 45.5231, lon: -122.6765 },
    });

    // Assert
    expect(result.success).toBe(true);
    expect(result.data.businesses).toHaveLength(1);
    expect(mockClient.search).toHaveBeenCalledWith({
      query: 'coffee shops',
      location: { lat: 45.5231, lon: -122.6765, distance: '10km' },
      limit: 10,
    });
  });

  it('should handle missing parameters', async () => {
    // Act
    const result = await searchBusinesses.handler({});

    // Assert
    expect(result.success).toBe(false);
    expect(result.error.code).toBe('INVALID_PARAMETERS');
  });

  it('should handle API errors', async () => {
    // Arrange
    mockClient.search.mockRejectedValue(new Error('API Error'));

    // Act
    const result = await searchBusinesses.handler({
      query: 'coffee',
      location: { lat: 45.5231, lon: -122.6765 },
    });

    // Assert
    expect(result.success).toBe(false);
    expect(result.error.code).toBe('SEARCH_FAILED');
  });
});

Running Unit Tests


Integration Testing

Test tools with real AIDP API using sandbox environment.

Setup

Running Integration Tests


End-to-End Testing

Test complete MCP server workflows.

Setup


Performance Testing

Test server performance under load.

Using Artillery

Install Artillery:

Create artillery-config.yml:

Run performance test:

Performance Metrics

Monitor these metrics:

  • Response Time: p50, p95, p99

  • Throughput: Requests per second

  • Error Rate: Failed requests percentage

  • Resource Usage: CPU, memory, network


Security Testing

Authentication Tests


Test Coverage

Measuring Coverage

Coverage Goals

Aim for:

  • Statements: > 80%

  • Branches: > 75%

  • Functions: > 80%

  • Lines: > 80%

Coverage Report


Continuous Integration

GitHub Actions

Create .github/workflows/test.yml:


Manual Testing

Using cURL

Using Postman

  1. Import MCP server collection

  2. Set environment variables

  3. Run test suite

  4. Review results


Debugging Tests

Enable Debug Logging

Use Test Debugger


Best Practices

1. Test Isolation

Each test should be independent:

2. Use Test Fixtures

Create reusable test data:

3. Test Edge Cases

4. Test Timeouts


Troubleshooting

Tests Failing Intermittently

Issue: Flaky tests

Solution: Add retries for integration tests:

Slow Tests

Issue: Tests take too long

Solution: Run tests in parallel:

Memory Leaks

Issue: Tests consume too much memory

Solution: Clear mocks and reset state:


Next Steps


Support

Last updated