mcp-server.test.ts•5.66 kB
/**
* GEPA MCP Server Tests
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { GEPAMCPServer } from '../mcp/server';
import { TOOL_NAMES } from '../mcp/types';
describe('GEPA MCP Server', () => {
let server: GEPAMCPServer;
beforeEach(() => {
server = new GEPAMCPServer();
});
afterEach(async () => {
// Clean up any resources if needed
});
describe('Server Initialization', () => {
it('should create server instance successfully', () => {
expect(server).toBeDefined();
expect(server).toBeInstanceOf(GEPAMCPServer);
});
it('should export GEPAMCPServer class', () => {
expect(GEPAMCPServer).toBeDefined();
expect(typeof GEPAMCPServer).toBe('function');
});
});
describe('Tool Names', () => {
it('should define all required tool names', () => {
expect(TOOL_NAMES.EVOLVE_PROMPT).toBe('gepa_evolve_prompt');
expect(TOOL_NAMES.ANALYZE_PROMPT).toBe('gepa_analyze_prompt');
expect(TOOL_NAMES.OPTIMIZE_PROMPT).toBe('gepa_optimize_prompt');
expect(TOOL_NAMES.GENERATE_VARIANTS).toBe('gepa_generate_variants');
});
it('should have consistent tool name format', () => {
Object.values(TOOL_NAMES).forEach(toolName => {
expect(toolName).toMatch(/^gepa_[a-z_]+$/);
});
});
});
describe('MCP SDK Integration', () => {
it('should import MCP SDK types without errors', async () => {
// Test that MCP SDK types are properly imported
const { Server } = await import('@modelcontextprotocol/sdk/server/index.js');
const { StdioServerTransport } = await import('@modelcontextprotocol/sdk/server/stdio.js');
expect(Server).toBeDefined();
expect(StdioServerTransport).toBeDefined();
expect(typeof Server).toBe('function');
expect(typeof StdioServerTransport).toBe('function');
});
it('should import MCP schema types without errors', async () => {
const {
CallToolRequestSchema,
ListToolsRequestSchema,
} = await import('@modelcontextprotocol/sdk/types.js');
expect(CallToolRequestSchema).toBeDefined();
expect(ListToolsRequestSchema).toBeDefined();
expect(typeof CallToolRequestSchema).toBe('object');
expect(typeof ListToolsRequestSchema).toBe('object');
});
});
describe('GEPA Types', () => {
it('should define evolution parameters interface', async () => {
const types = await import('../mcp/types.js');
// Check that types module exports expected interfaces
expect(types.TOOL_NAMES).toBeDefined();
expect(types.GEPAError).toBeDefined();
expect(types.EvolutionError).toBeDefined();
expect(types.AnalysisError).toBeDefined();
expect(types.OptimizationError).toBeDefined();
});
it('should create custom error types correctly', async () => {
const { GEPAError, EvolutionError } = await import('../mcp/types.js');
const error = new GEPAError('Test error', 'TEST_CODE', { detail: 'test' });
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(GEPAError);
expect(error.message).toBe('Test error');
expect(error.code).toBe('TEST_CODE');
expect(error.details).toEqual({ detail: 'test' });
const evolutionError = new EvolutionError('Evolution failed');
expect(evolutionError).toBeInstanceOf(GEPAError);
expect(evolutionError.code).toBe('EVOLUTION_ERROR');
});
});
describe('Server Configuration', () => {
it('should have default configuration values', async () => {
const { DEFAULT_CONFIG } = await import('../mcp/index.js');
expect(DEFAULT_CONFIG.name).toBe('gepa-mcp-server');
expect(DEFAULT_CONFIG.version).toBe('1.0.0');
expect(typeof DEFAULT_CONFIG.default_population_size).toBe('number');
expect(typeof DEFAULT_CONFIG.default_generations).toBe('number');
expect(DEFAULT_CONFIG.default_population_size).toBeGreaterThan(0);
expect(DEFAULT_CONFIG.default_generations).toBeGreaterThan(0);
});
});
describe('Module Exports', () => {
it('should export main server class from index', async () => {
const { GEPAMCPServer: ExportedServer } = await import('../mcp/index.js');
expect(ExportedServer).toBeDefined();
expect(ExportedServer).toBe(GEPAMCPServer);
});
it('should re-export MCP SDK types', async () => {
const mcpIndex = await import('../mcp/index.js');
// Check that common MCP types are available through our index
expect(mcpIndex.DEFAULT_CONFIG).toBeDefined();
expect(mcpIndex.GEPAMCPServer).toBeDefined();
});
});
});
/**
* Integration test - requires actual server instance
* These tests verify that the MCP server can be instantiated
* and configured properly with the SDK.
*/
describe('MCP Server Integration', () => {
it('should handle server lifecycle without errors', async () => {
// This is a basic test to ensure the server can be created
// without throwing any import or initialization errors
expect(() => {
const testServer = new GEPAMCPServer();
expect(testServer).toBeDefined();
}).not.toThrow();
});
it('should validate MCP SDK version compatibility', async () => {
// Verify we can import the expected MCP SDK version
const packageJson = await import('../../package.json', { assert: { type: 'json' } });
const mcpSdkVersion = packageJson.default.dependencies['@modelcontextprotocol/sdk'];
expect(mcpSdkVersion).toBeDefined();
expect(mcpSdkVersion).toMatch(/^\^?0\.\d+\.\d+$/); // Should be version 0.x.x
});
});