Skip to content

Conversation

qhhonx
Copy link

@qhhonx qhhonx commented Jul 3, 2025

This PR introduces socket transport as an alternative to the existing stdio transport, offering similar process management capabilities but with enhanced flexibility through TCP socket communication.

Relationship with stdio Transport

The socket transport implementation follows similar patterns to the existing stdio transport:

Shared Patterns:

  • Process management and lifecycle handling
  • Stream-based communication model
  • Resource cleanup mechanisms
  • Error handling approaches
  • FastMCP integration model

Key Differences:

  1. Communication Channel:

    • stdio: Uses stdin/stdout pipes
    • socket: Uses bidirectional TCP sockets
  2. Connection Management:

    • stdio: Immediate pipe connection
    • socket: Supports connection retry and timeout
  3. Deployment Flexibility:

    • stdio: Limited to local process communication
    • socket: Supports both local and network communication
  4. Configuration Options:

    • stdio: Fixed to system pipes
    • socket: Configurable host/port settings
  5. Error Handling:

    • stdio: Basic pipe error handling
    • socket: Advanced connection and network error handling

Implementation Details

The implementation maintains consistency with stdio transport while adding socket-specific features:

  1. Process Management:
# stdio transport
async with stdio_client(server_params) as (read_stream, write_stream):
    # Use streams...

# socket transport (similar pattern)
async with socket_client(server_params) as (read_stream, write_stream):
    # Use streams...
  1. Server Integration:
# stdio server
server.run(transport='stdio')

# socket server (consistent API)
server.run(transport='socket')
  1. Configuration:
# stdio parameters
StdioServerParameters(
    command='python',
    args=['-m', 'server'],
)

# socket parameters (extended but similar)
SocketServerParameters(
    command='python',
    args=['-m', 'server'],
    host='127.0.0.1',  # Additional socket-specific options
    port=8000,
)

Enhanced Capabilities

The socket transport adds several capabilities not possible with stdio:

  1. Network Communication:

    • Support for remote process communication
    • Configurable network parameters
    • Better support for distributed systems
  2. Connection Management:

    • Retry mechanisms for reliability
    • Timeout handling for better error recovery
    • More robust error handling
  3. Deployment Options:

    • Flexible host/port configuration
    • Support for various network topologies
    • Better scalability options

Migration and Compatibility

The socket transport is designed to be a drop-in alternative to stdio transport:

# Existing stdio usage
server = FastMCP(name='Server')
server.run(transport='stdio')

# New socket transport usage
server = FastMCP(
    name='Server',
    socket_host='127.0.0.1',
    socket_port=8000,
)
server.run(transport='socket')

Testing Strategy

Tests are designed to verify both shared behavior with stdio and socket-specific features:

  1. Shared Behavior Tests:

    • Process lifecycle management
    • Stream communication patterns
    • Resource cleanup
    • Basic error handling
  2. Socket-Specific Tests:

    • Network connection handling
    • Timeout and retry mechanisms
    • Host/port configuration
    • Network error scenarios

Future Considerations

  1. Potential Enhancements:

    • TLS/SSL support for secure communication
    • Unix domain socket support
    • Performance optimizations
    • Advanced network configurations
  2. Migration Tools:

    • Consider adding utilities to help migrate from stdio to socket transport
    • Add documentation for choosing between transports

This commit introduces socket-based transport as an alternative to stdio transport, offering similar process management but with bidirectional TCP socket communication:

Core Features:
- Implement socket transport following stdio transport's process management pattern
- Add direct TCP socket communication instead of stdin/stdout pipes
- Support custom encoding and error handling like stdio transport
- Add connection retry and timeout handling (not available in stdio)
- Implement proper process cleanup similar to stdio transport

Key Differences from stdio:
- Bidirectional TCP socket vs stdin/stdout pipes
- Support for custom host/port vs fixed pipe streams
- Connection retry and timeout mechanisms vs immediate pipe connection
- More flexible process-to-process communication patterns
- Better support for distributed deployment scenarios

Implementation Details:
- Add SocketServerParameters extending stdio's parameter pattern
- Support auto-port assignment for flexible deployment
- Handle connection timeouts and retries (new capability)
- Implement proper resource cleanup following stdio pattern
- Add comprehensive test coverage comparing with stdio behavior

FastMCP Integration:
- Add socket transport alongside stdio transport
- Support socket configuration in Settings
- Maintain consistent API patterns with stdio transport
@qhhonx qhhonx force-pushed the feat/socket-transport-impl branch from dd7fe38 to 98ede50 Compare July 3, 2025 04:50
@qhhonx qhhonx changed the title feat: add socket transport implementation feat: Add socket transport as an alternative to stdio transport Jul 3, 2025
- Add graceful cancellation handling for socket reader/writer
- Implement timeout-based cleanup mechanism (5s timeout)
- Add force process termination for hanging cleanup
- Improve server-side resource cleanup
- Add comprehensive tests for cancellation and cleanup scenarios
- Fix Python interpreter path resolution in tests
@Kludex
Copy link
Member

Kludex commented Jul 4, 2025

I don't think we want to keep including transports into this package that are not in the MCP specification.

I think the best approach for now would be to create a mcp-socket package (or something similar).


That said, I think this transport should support fds and uds as well.

@Kludex Kludex closed this Jul 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants