// Test script to verify OpenAPI schema support // Run this in the MCPHub project directory with: tsx test-openapi-schema.ts import { OpenAPIClient } from './src/clients/openapi.js'; import type { ServerConfig } from './src/types/index.js'; async function testOpenAPISchemaSupport() { console.log('๐Ÿงช Testing OpenAPI Schema Support...\n'); // Test 1: Schema-based OpenAPI client console.log('1๏ธโƒฃ Testing OpenAPI client with JSON schema...'); const sampleSchema = { openapi: '3.1.0', info: { title: 'Test API', version: '1.0.0', }, servers: [ { url: 'https://api.example.com', }, ], paths: { '/users': { get: { operationId: 'getUsers', summary: 'Get all users', responses: { '200': { description: 'List of users', content: { 'application/json': { schema: { type: 'array', items: { type: 'object', properties: { id: { type: 'integer' }, name: { type: 'string' }, }, }, }, }, }, }, }, }, }, '/users/{id}': { get: { operationId: 'getUserById', summary: 'Get user by ID', parameters: [ { name: 'id', in: 'path', required: true, schema: { type: 'integer' }, }, ], responses: { '200': { description: 'User details', content: { 'application/json': { schema: { type: 'object', properties: { id: { type: 'integer' }, name: { type: 'string' }, email: { type: 'string' }, }, }, }, }, }, }, }, }, }, }; try { const schemaConfig: ServerConfig = { type: 'openapi', openapi: { schema: sampleSchema, version: '3.1.0', security: { type: 'apiKey', apiKey: { name: 'X-API-Key', in: 'header', value: 'test-key', }, }, }, }; console.log(' Creating OpenAPI client with schema...'); const client = new OpenAPIClient(schemaConfig); console.log(' Initializing client...'); await client.initialize(); console.log(' Getting available tools...'); const tools = client.getTools(); console.log(` โœ… Schema-based client initialized successfully!`); console.log(` ๐Ÿ“‹ Found ${tools.length} tools:`); tools.forEach((tool) => { console.log(` - ${tool.name}: ${tool.description}`); }); // Test 2: Compare with URL-based client (if available) console.log('\n2๏ธโƒฃ Testing configuration validation...'); // Valid configurations const validConfigs = [ { name: 'URL-based config', config: { type: 'openapi' as const, openapi: { url: 'https://api.example.com/openapi.json', }, }, }, { name: 'Schema-based config', config: { type: 'openapi' as const, openapi: { schema: sampleSchema, }, }, }, { name: 'Both URL and schema (should prefer schema)', config: { type: 'openapi' as const, openapi: { url: 'https://api.example.com/openapi.json', schema: sampleSchema, }, }, }, ]; validConfigs.forEach(({ name, config }) => { try { const _client = new OpenAPIClient(config); console.log(` โœ… ${name}: Valid configuration`); } catch (error) { console.log(` โŒ ${name}: Invalid configuration - ${error}`); } }); // Invalid configurations console.log('\n3๏ธโƒฃ Testing invalid configurations...'); const invalidConfigs = [ { name: 'No URL or schema', config: { type: 'openapi' as const, openapi: { version: '3.1.0', }, }, }, { name: 'Empty openapi object', config: { type: 'openapi' as const, openapi: {}, }, }, ]; invalidConfigs.forEach(({ name, config }) => { try { const _client = new OpenAPIClient(config); console.log(` โŒ ${name}: Should have failed but didn't`); } catch (error) { console.log(` โœ… ${name}: Correctly rejected - ${(error as Error).message}`); } }); console.log('\n๐ŸŽ‰ All tests completed successfully!'); console.log('\n๐Ÿ“ Summary:'); console.log(' โœ… OpenAPI client supports JSON schema input'); console.log(' โœ… Schema parsing and tool extraction works'); console.log(' โœ… Configuration validation works correctly'); console.log(' โœ… Both URL and schema modes are supported'); } catch (error) { console.error('โŒ Test failed:', (error as Error).message); console.error(' Stack trace:', (error as Error).stack); } } // Handle uncaught errors gracefully process.on('uncaughtException', (error) => { console.error('Uncaught exception:', error); process.exit(1); }); process.on('unhandledRejection', (reason, promise) => { console.error('Unhandled rejection at:', promise, 'reason:', reason); process.exit(1); }); // Run the test testOpenAPISchemaSupport().catch(console.error);