kubectl-ai icon indicating copy to clipboard operation
kubectl-ai copied to clipboard

Add HTTP and stdio MCP Client Implementations with Unified Interface

Open tuannvm opened this issue 7 months ago • 3 comments

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 the MCPClient interface 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: MCPClient with 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 ClientConfig struct 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() and cleanup() 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.

tuannvm avatar May 30 '25 12:05 tuannvm

image

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 avatar May 30 '25 17:05 tuannvm

@tuannvm one of the presubmits are failing.

droot avatar May 30 '25 22:05 droot

@droot ✅ Curious if we can configure the CI to auto-run so that contributors can catch CI bugs automatically?

tuannvm avatar May 31 '25 00:05 tuannvm

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.

droot avatar Jun 02 '25 18:06 droot