diff --git a/go-mcp-integration.go b/go-mcp-integration.go new file mode 100644 index 0000000..d725547 --- /dev/null +++ b/go-mcp-integration.go @@ -0,0 +1,76 @@ +package mcp + +import ( + "encoding/json" + "fmt" + "net" + "time" +) + +// MCPClient is a minimal client to establish a handshake with an MCP server. +type MCPClient struct { + Server string + Conn net.Conn + Timeout time.Duration +} + +// NewMCPClient creates a new MCPClient for the given server address. +func NewMCPClient(server string) *MCPClient { + return &MCPClient{Server: server, Timeout: 5 * time.Second} +} + +// Handshake performs a simple heartbeat with the MCP server. +func (c *MCPClient) Handshake() error { + conn, err := net.DialTimeout("tcp", c.Server, c.Timeout) + if err != nil { + return err + } + c.Conn = conn + payload := map[string]string{"type": "handshake", "version": "v1"} + b, _ := json.Marshal(payload) + if _, err := conn.Write(append(b, n)); err != nil { + return err + } + // read response + buf := make([]byte, 4096) + n, err := conn.Read(buf) + if err != nil { + return err + } + var resp map[string]interface{} + if err := json.Unmarshal(buf[:n], &resp); err != nil { + return err + } + if status, ok := resp["status"].(string); ok && status == "ok" { + return nil + } + return fmt.Errorf("handshake failed: %v", resp) +} + +// SendRequest sends a generic request to MCP server. +func (c *MCPClient) SendRequest(req interface{}) error { + if c.Conn == nil { + return fmt.Errorf("no MCP connection") + } + b, _ := json.Marshal(req) + _, err := c.Conn.Write(append(b, n)) + return err +} + +// Close closes the MCP connection. +func (c *MCPClient) Close() { + if c.Conn != nil { + c.Conn.Close() + c.Conn = nil + } +} + +// Example usage placeholder: initialize connection. +func MCPInit(server string) (*MCPClient, error) { + c := NewMCPClient(server) + if err := c.Handshake(); err != nil { + return nil, err + } + return c, nil +} +