Skip to content

Optimize MCP Server Detection to Eliminate Redundant Connections #432

@jolestar

Description

@jolestar

Problem

The current ServerDetector implementation creates an unnecessary additional MCP connection during server capability detection, leading to performance overhead and resource waste.

Current Flow

  1. ServerDetector.detectCapabilities() calls getMcpCapabilities()
  2. getMcpCapabilities() creates a temporary MCP connection to retrieve server capabilities
  3. The temporary connection is immediately closed after getting capabilities
  4. Later, UniversalMcpClient or PaymentChannelMcpClient creates another "official" connection
  5. Result: 2 connections total - 1 temporary + 1 official

Issues

  • Performance Impact: Extra network round-trips and connection overhead
  • Resource Waste: Temporary connections consume server resources unnecessarily
  • Latency: Additional connection establishment delays the overall process
  • Server Load: Unnecessary connection churn on MCP servers

Proposed Solution

Refactor the detection flow to eliminate the temporary connection by deferring MCP capability retrieval until after the official connection is established.

New Flow

  1. ServerDetector.detectCapabilities() only checks payment protocol support via well-known endpoint
  2. Official MCP connection is established by client (UniversalMcpClient/PaymentChannelMcpClient)
  3. MCP capabilities are retrieved from the established connection
  4. Payment info and MCP capabilities are merged to determine final server type
  5. Result: 1 connection total - only the official connection

Implementation Plan

Phase 1: Refactor ServerDetector

  • Remove getMcpCapabilities() method from ServerDetector
  • Modify detectCapabilities() to only return payment protocol detection results
  • Update ServerDetectionResult type to reflect partial detection (payment info only)
  • Adjust caching strategy for partial detection results

Phase 2: Move Capability Detection to Clients

  • Add capability retrieval logic to UniversalMcpClient.ensureInitialized()
  • Add capability retrieval logic to PaymentChannelMcpClient.ensureClient()
  • Create utility method to merge payment info with MCP capabilities
  • Update error handling for connection failures

Phase 3: Update Types and Interfaces

  • Create new types for partial vs complete detection results
  • Update method signatures to reflect the new flow
  • Ensure backward compatibility of public APIs

Phase 4: Testing and Validation

  • Write unit tests for the new detection flow
  • Add integration tests to verify single connection behavior
  • Performance testing to measure improvement
  • Ensure all existing functionality still works

Benefits

  • Performance: ~50% reduction in connection overhead during detection
  • Resource Efficiency: Eliminates temporary connections
  • Cleaner Architecture: Clearer separation between detection and connection phases
  • Backward Compatibility: Public APIs remain unchanged

Files to Modify

  • src/integrations/mcp/ServerDetector.ts - Core detection logic
  • src/integrations/mcp/UniversalMcpClient.ts - Universal client capability handling
  • src/integrations/mcp/PaymentChannelMcpClient.ts - Payment client capability handling
  • src/integrations/mcp/types.ts - Type definitions
  • Test files for validation

Acceptance Criteria

  • Server detection creates only one MCP connection (the official one)
  • All existing functionality continues to work
  • Performance improvement measurable in benchmarks
  • No breaking changes to public APIs
  • Comprehensive test coverage for new flow

Priority: Medium
Impact: Performance optimization, better resource utilization

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions