UNPKG

mcp-server-tester-sse-http-stdio

Version:

MCP Server Tester with SSE support - Test MCP servers using HTTP, SSE, and STDIO transports

113 lines (112 loc) 4.69 kB
/** * Initialization flow tests for MCP server lifecycle */ import { DiagnosticTest } from '../DiagnosticTest.js'; import { TEST_SEVERITY } from '../types.js'; export class InitializationTests extends DiagnosticTest { name = 'Lifecycle: Initialization (initialize request/response)'; description = 'Tests MCP server initialization sequence and responses'; category = 'lifecycle'; feature = 'initialization'; severity = TEST_SEVERITY.CRITICAL; mcpSpecSection = 'Lifecycle - Initialization'; async execute(client, _config) { try { const results = await Promise.allSettled([ this.testInitializeRequest(client), this.testInitializeResponse(client), this.testInitializedNotification(client), this.testInitializationErrors(client), ]); const issues = []; const details = {}; // Process test results results.forEach((result, index) => { const testNames = [ 'InitializeRequest', 'InitializeResponse', 'InitializedNotification', 'InitializationErrors', ]; if (result.status === 'rejected') { issues.push(`${testNames[index]}: ${result.reason}`); } else { details[testNames[index]] = result.value; } }); if (issues.length > 0) { return this.createResult(false, `Initialization flow has ${issues.length} issue(s)`, { issues, details }, [ 'Verify server implements proper MCP initialization sequence', 'Check server response to InitializeRequest follows MCP specification', 'Ensure server sends proper InitializedNotification acknowledgment', ]); } return this.createResult(true, 'Initialization flow completed successfully', details); } catch (error) { return this.createResult(false, 'Initialization flow test failed', { error: error instanceof Error ? error.message : String(error) }, ['Check server connection and MCP protocol implementation']); } } async testInitializeRequest(client) { // Test basic connectivity which implicitly tests initialization try { // If we have a connected client, initialization succeeded const tools = await client.sdk.listTools(); return { status: 'passed', message: 'Server responds to requests after initialization', toolCount: tools.tools?.length || 0, }; } catch (error) { throw new Error(`InitializeRequest handling failed: ${error}`); } } async testInitializeResponse(client) { // Test that server provides valid initialization response try { // Access server capabilities and version from the connected client const capabilities = client.sdk.getServerCapabilities(); const version = client.sdk.getServerVersion(); return { status: 'passed', message: 'Server provides valid initialization response', capabilities, version, }; } catch (error) { throw new Error(`InitializeResponse validation failed: ${error}`); } } async testInitializedNotification(client) { // Test that server properly acknowledges initialization try { // If we can make any request, initialization was properly acknowledged await client.sdk.listTools(); return { status: 'passed', message: 'Server acknowledges initialization and accepts requests', }; } catch (error) { throw new Error(`InitializedNotification acknowledgment failed: ${error}`); } } async testInitializationErrors(client) { // Test error handling during initialization (this is more of a validation) try { // Verify we have a working connection, which means error handling worked const version = client.sdk.getServerVersion(); return { status: 'passed', message: 'Server handled initialization without errors', version, }; } catch (error) { throw new Error(`Initialization error handling test failed: ${error}`); } } }