Add HTTP and stdio MCP Client Implementations with Unified Interface
Summary
This PR introduces comprehensive client implementations for the Model Context Protocol (MCP), supporting both HTTP and stdio-based communication methods. It refactors the codebase to define a unified MCPClient interface, allowing flexible selection of client types based on configuration. Additionally, it enhances connection management, tool listing, and command execution functionalities, facilitating reliable and efficient interactions with MCP servers.
Affected Modules
pkg/mcp/interfaces.go: Defines theMCPClientinterface and client configuration structures.pkg/mcp/stdio_client.go: Implements stdio-based MCP client with connection handling, tool listing, and command execution.- Client factory functions (
NewMCPClient,NewStdioClient,NewHTTPClient) — facilitating client instantiation based on configuration. pkg/mcp/manager.go: Manages client lifecycle and ensures proper connection setup with environment variable translation.
Key Details
- Client Interface:
MCPClientwith methods for connection management, tool listing, command invocation, and cleanup. - Client Implementations:
stdioClient: Communicates via command-line processes, with support for connection verification.httpClient: Uses streaming HTTP connections with support for OAuth and header customization.
- Configuration Flexibility: The
ClientConfigstruct supports both HTTP and stdio clients, with fields for command, environment, URL, authentication, and timeout. - Connection Handling:
Connect(),initializeConnection(),verifyConnection()ensure a reliable setup before tool operations.Close()andcleanup()are designed to gracefully terminate client sessions.
- Tool Operations:
ListTools()fetches available tool list.CallTool()executes specified tools and processes their responses.
Potential Impacts
- Enables plug-and-play support for multiple communication protocols with MCP servers.
- Simplifies client management and reduces connection errors through explicit verification.
- Facilitates testing and future extensions, including potential support for additional transport mechanisms.
with
Servers:
- Args:
- -y
- '@modelcontextprotocol/server-sequential-thinking'
Command: npx
Env: null
Name: sequential-thinking
- Name: cloudflare-documentation
URL: https://docs.mcp.cloudflare.com/mcp
@tuannvm one of the presubmits are failing.
@droot ✅ Curious if we can configure the CI to auto-run so that contributors can catch CI bugs automatically?
Curious if we can configure the CI to auto-run so that contributors can catch CI bugs automatically?
Yes, we want to do this. We need to figure out a way to do that only the PR does not involve modifying the presubmit jobs itself (because that introduces security attack vector). I haven't spent much time figuring this out, but if you have a recipe from other projects, pl. share.