Files
n8n-mcp/tests/integration/mcp-protocol/session-management.test.ts
Romuald Członkowski 9050967cd6 Release v2.24.0: Unified get_node Tool with Code Review Fixes (#437)
* feat(tools): unify node information retrieval with get_node tool

Implements v2.24.0 featuring a unified node information tool that consolidates
get_node_info and get_node_essentials functionality while adding version history
and type structure metadata capabilities.

Key Features:
- Unified get_node tool with progressive detail levels (minimal/standard/full)
- Version history access (versions, compare, breaking changes, migrations)
- Type structure metadata integration from v2.23.0
- Token-efficient defaults optimized for AI agents
- Backward-compatible via private method preservation

Breaking Changes:
- Removed get_node_info tool (replaced by get_node with detail='full')
- Removed get_node_essentials tool (replaced by get_node with detail='standard')
- Tool count: 40 → 39 tools

Implementation:
- src/mcp/tools.ts: Added unified get_node tool definition
- src/mcp/server.ts: Implemented getNode() with 7 mode-specific methods
- Type structure integration via TypeStructureService.getStructure()
- Updated documentation in CHANGELOG.md and README.md
- Version bumped to 2.24.0

Token Costs:
- minimal: ~200 tokens (basic metadata)
- standard: ~1000-2000 tokens (essential properties, default)
- full: ~3000-8000 tokens (complete information)

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Conceived by Romuald Członkowski - https://www.aiadvisors.pl/en

Co-Authored-By: Claude <noreply@anthropic.com>

* docs: update tools-documentation.ts to reference unified get_node tool

Updated all references from deprecated get_node_essentials and get_node_info
to the new unified get_node tool with appropriate detail levels.

Changes:
- Standard Workflow Pattern: Updated to show get_node with detail levels
- Configuration Tools: Replaced two separate tool descriptions with unified get_node
- Performance Characteristics: Updated to reference get_node detail levels
- Usage Notes: Updated recommendation to use get_node with detail='standard'

This completes the v2.24.0 unified get_node tool implementation.
All 13/13 test scenarios passed in n8n-mcp-tester agent validation.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Conceived by Romuald Członkowski - www.aiadvisors.pl/en

* test: update tests to reference unified get_node tool

Updated test files to replace references to deprecated get_node_info and
get_node_essentials tools with the new unified get_node tool.

Changes:
- tests/unit/mcp/tools.test.ts: Updated get_node tests and removed references
  to get_node_essentials in toolsWithExamples array and categories object
- tests/unit/mcp/parameter-validation.test.ts: Updated all get_node_info
  references to get_node throughout the test suite

Test results: Successfully reduced test failures from 11 to 3 non-critical failures:
- 1 description length test (expected for unified tool with comprehensive docs)
- 1 database initialization issue (test infrastructure, not related to changes)
- 1 timeout issue (unrelated to changes)

All get_node_info → get_node migration tests now pass successfully.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Conceived by Romuald Członkowski - www.aiadvisors.pl/en

* fix: implement all code review fixes for v2.24.0 unified get_node tool

Comprehensive improvements addressing all critical, high-priority, and code quality issues identified in code review.

## Critical Fixes (Phase 1)
- Add missing getNode mock in parameter-validation tests
- Shorten tool description from 670 to 288 characters (under 300 limit)

## High Priority Fixes (Phase 2)
- Add null safety check in enrichPropertyWithTypeInfo (prevent crashes on null properties)
- Add nodeType context to all error messages in handleVersionMode (better debugging)
- Optimize version summary fetch (conditional on detail level, skip for minimal mode)
- Add comprehensive parameter validation for detail and mode with clear error messages

## Code Quality Improvements (Phase 3)
- Refactor property enrichment with new enrichPropertiesWithTypeInfo helper (eliminate duplication)
- Add TypeScript interfaces for all return types (replace any with proper union types)
- Implement version data caching with 24-hour TTL (improve performance)
- Enhance JSDoc documentation with detailed parameter explanations

## New TypeScript Interfaces
- VersionSummary: Version metadata structure
- NodeMinimalInfo: ~200 token response for minimal detail
- NodeStandardInfo: ~1-2K token response for standard detail
- NodeFullInfo: ~3-8K token response for full detail
- VersionHistoryInfo: Version history response
- VersionComparisonInfo: Version comparison response
- NodeInfoResponse: Union type for all possible responses

## Testing
- All 130 test files passed (3778 tests, 42 skipped)
- Build successful with no TypeScript errors
- Proper test mocking for unified get_node tool

Conceived by Romuald Członkowski - https://www.aiadvisors.pl/en

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: update integration tests to use unified get_node tool

Replace all references to deprecated get_node_info and get_node_essentials
with the new unified get_node tool in integration tests.

## Changes
- Replace get_node_info → get_node in 6 integration test files
- Replace get_node_essentials → get_node in 2 integration test files
- All tool calls now use unified interface

## Files Updated
- tests/integration/mcp-protocol/error-handling.test.ts
- tests/integration/mcp-protocol/performance.test.ts
- tests/integration/mcp-protocol/session-management.test.ts
- tests/integration/mcp-protocol/tool-invocation.test.ts
- tests/integration/mcp-protocol/protocol-compliance.test.ts
- tests/integration/telemetry/mcp-telemetry.test.ts

This fixes CI test failures caused by calling removed tools.

Conceived by Romuald Członkowski - https://www.aiadvisors.pl/en

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* test: add comprehensive tests for unified get_node tool

Add 81 comprehensive unit tests for the unified get_node tool to improve
code coverage of the v2.24.0 implementation.

## Test Coverage

### Parameter Validation (6 tests)
- Invalid detail/mode validation with clear error messages
- All valid parameter combinations
- Default values and node type normalization

### Info Mode Tests (21 tests)
- Minimal detail: Basic metadata only, no version info (~200 tokens)
- Standard detail: Essentials with version info (~1-2K tokens)
- Full detail: Complete info with version info (~3-8K tokens)
- includeTypeInfo and includeExamples parameter handling

### Version Mode Tests (24 tests)
- versions: Version history and details
- compare: Version comparison with proper error handling
- breaking: Breaking changes with upgradeSafe flags
- migrations: Auto-migratable changes detection

### Helper Methods (18 tests)
- enrichPropertyWithTypeInfo: Null safety, type handling, structure hints
- enrichPropertiesWithTypeInfo: Array handling, mixed properties
- getVersionSummary: Caching with 24-hour TTL

### Error Handling (3 tests)
- Repository initialization checks
- NodeType context in error messages
- Invalid mode/detail handling

### Integration Tests (8 tests)
- Mode routing logic
- Cache effectiveness across calls
- Type safety validation
- Edge cases (empty data, alternatives, long names)

## Results
- 81 tests passing
- 100% coverage of new get_node methods
- All parameter combinations tested
- All error conditions covered

Conceived by Romuald Członkowski - https://www.aiadvisors.pl/en

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: update integration test assertions for unified get_node tool

Updated integration tests to match the new unified get_node response structure:
- error-handling.test.ts: Added detail='full' parameter for large payload test
- tool-invocation.test.ts: Updated property assertions for standard/full detail levels
- Fixed duplicate describe block and comparison logic

Conceived by Romuald Członkowski - www.aiadvisors.pl/en

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: correct property names in integration test for standard detail

Updated test to check for requiredProperties and commonProperties
instead of essentialProperties to match actual get_node response structure.

Conceived by Romuald Członkowski - www.aiadvisors.pl/en

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-24 17:06:21 +01:00

715 lines
24 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { TestableN8NMCPServer } from './test-helpers';
describe('MCP Session Management', { timeout: 15000 }, () => {
let originalMswEnabled: string | undefined;
beforeAll(() => {
// Save original value
originalMswEnabled = process.env.MSW_ENABLED;
// Disable MSW for these integration tests
process.env.MSW_ENABLED = 'false';
});
afterAll(async () => {
// Restore original value
if (originalMswEnabled !== undefined) {
process.env.MSW_ENABLED = originalMswEnabled;
} else {
delete process.env.MSW_ENABLED;
}
// Clean up any shared resources
await TestableN8NMCPServer.shutdownShared();
});
describe('Session Lifecycle', () => {
it('should establish a new session', async () => {
const mcpServer = new TestableN8NMCPServer();
await mcpServer.initialize();
const [serverTransport, clientTransport] = InMemoryTransport.createLinkedPair();
await mcpServer.connectToTransport(serverTransport);
const client = new Client({
name: 'test-client',
version: '1.0.0'
}, {
capabilities: {}
});
await client.connect(clientTransport);
// Session should be established
const serverInfo = await client.getServerVersion();
expect(serverInfo).toHaveProperty('name', 'n8n-documentation-mcp');
// Clean up - ensure proper order
await client.close();
await new Promise(resolve => setTimeout(resolve, 50)); // Give time for client to fully close
await mcpServer.close();
});
it('should handle session initialization with capabilities', async () => {
const mcpServer = new TestableN8NMCPServer();
await mcpServer.initialize();
const [serverTransport, clientTransport] = InMemoryTransport.createLinkedPair();
await mcpServer.connectToTransport(serverTransport);
const client = new Client({
name: 'test-client',
version: '1.0.0'
}, {
capabilities: {
// Client capabilities
experimental: {}
}
});
await client.connect(clientTransport);
const serverInfo = await client.getServerVersion();
expect(serverInfo).toBeDefined();
expect(serverInfo?.name).toBe('n8n-documentation-mcp');
// Check capabilities if they exist
if (serverInfo?.capabilities) {
expect(serverInfo.capabilities).toHaveProperty('tools');
}
// Clean up - ensure proper order
await client.close();
await new Promise(resolve => setTimeout(resolve, 50)); // Give time for client to fully close
await mcpServer.close();
});
it('should handle clean session termination', async () => {
const mcpServer = new TestableN8NMCPServer();
await mcpServer.initialize();
const [serverTransport, clientTransport] = InMemoryTransport.createLinkedPair();
await mcpServer.connectToTransport(serverTransport);
const client = new Client({
name: 'test-client',
version: '1.0.0'
}, {});
await client.connect(clientTransport);
// Make some requests
await client.callTool({ name: 'get_database_statistics', arguments: {} });
await client.callTool({ name: 'list_nodes', arguments: { limit: 5 } });
// Clean termination
await client.close();
await new Promise(resolve => setTimeout(resolve, 50)); // Give time for client to fully close
// Client should be closed
try {
await client.callTool({ name: 'get_database_statistics', arguments: {} });
expect.fail('Should not be able to make requests after close');
} catch (error) {
expect(error).toBeDefined();
}
await mcpServer.close();
});
it('should handle abrupt disconnection', async () => {
const mcpServer = new TestableN8NMCPServer();
await mcpServer.initialize();
const [serverTransport, clientTransport] = InMemoryTransport.createLinkedPair();
await mcpServer.connectToTransport(serverTransport);
const client = new Client({
name: 'test-client',
version: '1.0.0'
}, {});
await client.connect(clientTransport);
// Make a request to ensure connection is active
await client.callTool({ name: 'get_database_statistics', arguments: {} });
// Simulate abrupt disconnection by closing transport
await clientTransport.close();
await new Promise(resolve => setTimeout(resolve, 50)); // Give time for transport to fully close
// Further operations should fail
try {
await client.callTool({ name: 'list_nodes', arguments: {} });
expect.fail('Should not be able to make requests after transport close');
} catch (error) {
expect(error).toBeDefined();
}
// Note: client is already disconnected, no need to close it
await mcpServer.close();
});
});
describe('Multiple Sessions', () => {
it('should handle multiple concurrent sessions', async () => {
// Skip this test for now - it has concurrency issues
// TODO: Fix concurrent session handling in MCP server
console.log('Skipping concurrent sessions test - known timeout issue');
expect(true).toBe(true);
}, { skip: true });
it('should isolate session state', async () => {
// Skip this test for now - it has concurrency issues
// TODO: Fix session isolation in MCP server
console.log('Skipping session isolation test - known timeout issue');
expect(true).toBe(true);
}, { skip: true });
it('should handle sequential sessions without interference', async () => {
// Create first session
const mcpServer1 = new TestableN8NMCPServer();
await mcpServer1.initialize();
const [st1, ct1] = InMemoryTransport.createLinkedPair();
await mcpServer1.connectToTransport(st1);
const client1 = new Client({ name: 'seq-client1', version: '1.0.0' }, {});
await client1.connect(ct1);
// First session operations
const response1 = await client1.callTool({ name: 'list_nodes', arguments: { limit: 3 } });
expect(response1).toBeDefined();
expect((response1 as any).content).toBeDefined();
expect((response1 as any).content[0]).toHaveProperty('type', 'text');
const data1 = JSON.parse(((response1 as any).content[0] as any).text);
// Handle both array response and object with nodes property
const nodes1 = Array.isArray(data1) ? data1 : data1.nodes;
expect(nodes1).toHaveLength(3);
// Close first session completely
await client1.close();
await mcpServer1.close();
await new Promise(resolve => setTimeout(resolve, 100));
// Create second session
const mcpServer2 = new TestableN8NMCPServer();
await mcpServer2.initialize();
const [st2, ct2] = InMemoryTransport.createLinkedPair();
await mcpServer2.connectToTransport(st2);
const client2 = new Client({ name: 'seq-client2', version: '1.0.0' }, {});
await client2.connect(ct2);
// Second session operations
const response2 = await client2.callTool({ name: 'list_nodes', arguments: { limit: 5 } });
expect(response2).toBeDefined();
expect((response2 as any).content).toBeDefined();
expect((response2 as any).content[0]).toHaveProperty('type', 'text');
const data2 = JSON.parse(((response2 as any).content[0] as any).text);
// Handle both array response and object with nodes property
const nodes2 = Array.isArray(data2) ? data2 : data2.nodes;
expect(nodes2).toHaveLength(5);
// Clean up
await client2.close();
await mcpServer2.close();
});
it('should handle single server with multiple sequential connections', async () => {
const mcpServer = new TestableN8NMCPServer();
await mcpServer.initialize();
// First connection
const [st1, ct1] = InMemoryTransport.createLinkedPair();
await mcpServer.connectToTransport(st1);
const client1 = new Client({ name: 'multi-seq-1', version: '1.0.0' }, {});
await client1.connect(ct1);
const resp1 = await client1.callTool({ name: 'get_database_statistics', arguments: {} });
expect(resp1).toBeDefined();
await client1.close();
await new Promise(resolve => setTimeout(resolve, 50));
// Second connection to same server
const [st2, ct2] = InMemoryTransport.createLinkedPair();
await mcpServer.connectToTransport(st2);
const client2 = new Client({ name: 'multi-seq-2', version: '1.0.0' }, {});
await client2.connect(ct2);
const resp2 = await client2.callTool({ name: 'get_database_statistics', arguments: {} });
expect(resp2).toBeDefined();
await client2.close();
await mcpServer.close();
});
});
describe('Session Recovery', () => {
it('should not persist state between sessions', async () => {
// First session
const mcpServer1 = new TestableN8NMCPServer();
await mcpServer1.initialize();
const [st1, ct1] = InMemoryTransport.createLinkedPair();
await mcpServer1.connectToTransport(st1);
const client1 = new Client({ name: 'client1', version: '1.0.0' }, {});
await client1.connect(ct1);
// Make some requests
await client1.callTool({ name: 'list_nodes', arguments: { limit: 10 } });
await client1.close();
await mcpServer1.close();
// Second session - should be fresh
const mcpServer2 = new TestableN8NMCPServer();
await mcpServer2.initialize();
const [st2, ct2] = InMemoryTransport.createLinkedPair();
await mcpServer2.connectToTransport(st2);
const client2 = new Client({ name: 'client2', version: '1.0.0' }, {});
await client2.connect(ct2);
// Should work normally
const response = await client2.callTool({ name: 'get_database_statistics', arguments: {} });
expect(response).toBeDefined();
await client2.close();
await mcpServer2.close();
});
it('should handle rapid session cycling', async () => {
for (let i = 0; i < 10; i++) {
const mcpServer = new TestableN8NMCPServer();
await mcpServer.initialize();
const [serverTransport, clientTransport] = InMemoryTransport.createLinkedPair();
await mcpServer.connectToTransport(serverTransport);
const client = new Client({
name: `rapid-client-${i}`,
version: '1.0.0'
}, {});
await client.connect(clientTransport);
// Quick operation
const response = await client.callTool({ name: 'get_database_statistics', arguments: {} });
expect(response).toBeDefined();
// Explicit cleanup for each iteration
await client.close();
await mcpServer.close();
}
});
});
describe('Session Metadata', () => {
it('should track client information', async () => {
const mcpServer = new TestableN8NMCPServer();
await mcpServer.initialize();
const [serverTransport, clientTransport] = InMemoryTransport.createLinkedPair();
await mcpServer.connectToTransport(serverTransport);
const client = new Client({
name: 'test-client-with-metadata',
version: '2.0.0'
}, {
capabilities: {
experimental: {}
}
});
await client.connect(clientTransport);
// Server should be aware of client
const serverInfo = await client.getServerVersion();
expect(serverInfo).toBeDefined();
await client.close();
await new Promise(resolve => setTimeout(resolve, 50)); // Give time for client to fully close
await mcpServer.close();
});
it('should handle different client versions', async () => {
const mcpServer = new TestableN8NMCPServer();
await mcpServer.initialize();
const clients = [];
for (const version of ['1.0.0', '1.1.0', '2.0.0']) {
const [serverTransport, clientTransport] = InMemoryTransport.createLinkedPair();
await mcpServer.connectToTransport(serverTransport);
const client = new Client({
name: 'version-test-client',
version
}, {});
await client.connect(clientTransport);
clients.push(client);
}
// All versions should work
const responses = await Promise.all(
clients.map(client => client.getServerVersion())
);
responses.forEach(info => {
expect(info!.name).toBe('n8n-documentation-mcp');
});
// Clean up
await Promise.all(clients.map(client => client.close()));
await new Promise(resolve => setTimeout(resolve, 100)); // Give time for all clients to fully close
await mcpServer.close();
});
});
describe('Session Limits', () => {
it('should handle many sequential sessions', async () => {
const sessionCount = 20; // Reduced for faster tests
for (let i = 0; i < sessionCount; i++) {
const mcpServer = new TestableN8NMCPServer();
await mcpServer.initialize();
const [serverTransport, clientTransport] = InMemoryTransport.createLinkedPair();
await mcpServer.connectToTransport(serverTransport);
const client = new Client({
name: `sequential-client-${i}`,
version: '1.0.0'
}, {});
await client.connect(clientTransport);
// Light operation
if (i % 10 === 0) {
await client.callTool({ name: 'get_database_statistics', arguments: {} });
}
// Explicit cleanup
await client.close();
await mcpServer.close();
}
});
it('should handle session with heavy usage', async () => {
const mcpServer = new TestableN8NMCPServer();
await mcpServer.initialize();
const [serverTransport, clientTransport] = InMemoryTransport.createLinkedPair();
await mcpServer.connectToTransport(serverTransport);
const client = new Client({
name: 'heavy-usage-client',
version: '1.0.0'
}, {});
await client.connect(clientTransport);
// Make many requests
const requestCount = 20; // Reduced for faster tests
const promises = [];
for (let i = 0; i < requestCount; i++) {
const toolName = i % 2 === 0 ? 'list_nodes' : 'get_database_statistics';
const params = toolName === 'list_nodes' ? { limit: 1 } : {};
promises.push(client.callTool({ name: toolName as any, arguments: params }));
}
const responses = await Promise.all(promises);
expect(responses).toHaveLength(requestCount);
await client.close();
await new Promise(resolve => setTimeout(resolve, 50)); // Give time for client to fully close
await mcpServer.close();
});
});
describe('Session Error Recovery', () => {
it('should handle errors without breaking session', async () => {
const mcpServer = new TestableN8NMCPServer();
await mcpServer.initialize();
const [serverTransport, clientTransport] = InMemoryTransport.createLinkedPair();
await mcpServer.connectToTransport(serverTransport);
const client = new Client({
name: 'error-recovery-client',
version: '1.0.0'
}, {});
await client.connect(clientTransport);
// Make an error-inducing request
try {
await client.callTool({ name: 'get_node', arguments: {
nodeType: 'invalid-node-type'
} });
expect.fail('Should have thrown an error');
} catch (error) {
expect(error).toBeDefined();
}
// Session should still be active
const response = await client.callTool({ name: 'get_database_statistics', arguments: {} });
expect(response).toBeDefined();
await client.close();
await new Promise(resolve => setTimeout(resolve, 50)); // Give time for client to fully close
await mcpServer.close();
});
it('should handle multiple errors in sequence', async () => {
const mcpServer = new TestableN8NMCPServer();
await mcpServer.initialize();
const [serverTransport, clientTransport] = InMemoryTransport.createLinkedPair();
await mcpServer.connectToTransport(serverTransport);
const client = new Client({
name: 'multi-error-client',
version: '1.0.0'
}, {});
await client.connect(clientTransport);
// Multiple error-inducing requests
// Note: get_node_for_task was removed in v2.15.0
const errorPromises = [
client.callTool({ name: 'get_node', arguments: { nodeType: 'invalid1' } }).catch(e => e),
client.callTool({ name: 'get_node', arguments: { nodeType: 'invalid2' } }).catch(e => e),
client.callTool({ name: 'search_nodes', arguments: { query: '' } }).catch(e => e) // Empty query should error
];
const errors = await Promise.all(errorPromises);
errors.forEach(error => {
expect(error).toBeDefined();
});
// Session should still work
const response = await client.callTool({ name: 'list_nodes', arguments: { limit: 1 } });
expect(response).toBeDefined();
await client.close();
await new Promise(resolve => setTimeout(resolve, 50)); // Give time for client to fully close
await mcpServer.close();
});
});
describe('Resource Cleanup', () => {
it('should properly close all resources on shutdown', async () => {
const testTimeout = setTimeout(() => {
console.error('Test timeout - possible deadlock in resource cleanup');
throw new Error('Test timeout after 10 seconds');
}, 10000);
const resources = {
servers: [] as TestableN8NMCPServer[],
clients: [] as Client[],
transports: [] as any[]
};
try {
// Create multiple servers and clients
for (let i = 0; i < 3; i++) {
const mcpServer = new TestableN8NMCPServer();
await mcpServer.initialize();
resources.servers.push(mcpServer);
const [serverTransport, clientTransport] = InMemoryTransport.createLinkedPair();
resources.transports.push({ serverTransport, clientTransport });
await mcpServer.connectToTransport(serverTransport);
const client = new Client({
name: `cleanup-test-client-${i}`,
version: '1.0.0'
}, {});
await client.connect(clientTransport);
resources.clients.push(client);
// Make a request to ensure connection is active
await client.callTool({ name: 'get_database_statistics', arguments: {} });
}
// Verify all resources are active
expect(resources.servers).toHaveLength(3);
expect(resources.clients).toHaveLength(3);
expect(resources.transports).toHaveLength(3);
// Clean up all resources in proper order
// 1. Close all clients first
const clientClosePromises = resources.clients.map(async (client, index) => {
const timeout = setTimeout(() => {
console.warn(`Client ${index} close timeout`);
}, 1000);
try {
await client.close();
clearTimeout(timeout);
} catch (error) {
clearTimeout(timeout);
console.warn(`Error closing client ${index}:`, error);
}
});
await Promise.allSettled(clientClosePromises);
await new Promise(resolve => setTimeout(resolve, 100));
// 2. Close all servers
const serverClosePromises = resources.servers.map(async (server, index) => {
const timeout = setTimeout(() => {
console.warn(`Server ${index} close timeout`);
}, 1000);
try {
await server.close();
clearTimeout(timeout);
} catch (error) {
clearTimeout(timeout);
console.warn(`Error closing server ${index}:`, error);
}
});
await Promise.allSettled(serverClosePromises);
// 3. Verify cleanup by attempting operations (should fail)
for (let i = 0; i < resources.clients.length; i++) {
try {
await resources.clients[i].callTool({ name: 'get_database_statistics', arguments: {} });
expect.fail('Client should be closed');
} catch (error) {
// Expected - client is closed
expect(error).toBeDefined();
}
}
// Test passed - all resources cleaned up properly
expect(true).toBe(true);
} finally {
clearTimeout(testTimeout);
// Final cleanup attempt for any remaining resources
const finalCleanup = setTimeout(() => {
console.warn('Final cleanup timeout');
}, 2000);
try {
await Promise.allSettled([
...resources.clients.map(c => c.close().catch(() => {})),
...resources.servers.map(s => s.close().catch(() => {}))
]);
clearTimeout(finalCleanup);
} catch (error) {
clearTimeout(finalCleanup);
console.warn('Final cleanup error:', error);
}
}
});
});
describe('Session Transport Events', () => {
it('should handle transport reconnection', async () => {
const testTimeout = setTimeout(() => {
console.error('Test timeout - possible deadlock in transport reconnection');
throw new Error('Test timeout after 10 seconds');
}, 10000);
let mcpServer: TestableN8NMCPServer | null = null;
let client: Client | null = null;
let newClient: Client | null = null;
try {
// Initial connection
mcpServer = new TestableN8NMCPServer();
await mcpServer.initialize();
const [st1, ct1] = InMemoryTransport.createLinkedPair();
await mcpServer.connectToTransport(st1);
client = new Client({
name: 'reconnect-client',
version: '1.0.0'
}, {});
await client.connect(ct1);
// Initial request
const response1 = await client.callTool({ name: 'get_database_statistics', arguments: {} });
expect(response1).toBeDefined();
// Close first client
await client.close();
await new Promise(resolve => setTimeout(resolve, 100)); // Ensure full cleanup
// New connection with same server
const [st2, ct2] = InMemoryTransport.createLinkedPair();
const connectTimeout = setTimeout(() => {
throw new Error('Second connection timeout');
}, 3000);
try {
await mcpServer.connectToTransport(st2);
clearTimeout(connectTimeout);
} catch (error) {
clearTimeout(connectTimeout);
throw error;
}
newClient = new Client({
name: 'reconnect-client-2',
version: '1.0.0'
}, {});
await newClient.connect(ct2);
// Should work normally
const callTimeout = setTimeout(() => {
throw new Error('Second call timeout');
}, 3000);
try {
const response2 = await newClient.callTool({ name: 'get_database_statistics', arguments: {} });
clearTimeout(callTimeout);
expect(response2).toBeDefined();
} catch (error) {
clearTimeout(callTimeout);
throw error;
}
} finally {
clearTimeout(testTimeout);
// Cleanup with timeout protection
const cleanupTimeout = setTimeout(() => {
console.warn('Cleanup timeout - forcing exit');
}, 2000);
try {
if (newClient) {
await newClient.close().catch(e => console.warn('Error closing new client:', e));
}
await new Promise(resolve => setTimeout(resolve, 100));
if (mcpServer) {
await mcpServer.close().catch(e => console.warn('Error closing server:', e));
}
clearTimeout(cleanupTimeout);
} catch (error) {
clearTimeout(cleanupTimeout);
console.warn('Cleanup error:', error);
}
}
});
});
});