The MCP client provides a simple and intuitive way to interact with MCP servers. This guide will walk you through initializing the client, connecting to a server, and using various MCP features.
Here’s a simple example of creating and initializing an MCP client:
Copy
import ( mcp "github.com/metoro-io/mcp-golang" "github.com/metoro-io/mcp-golang/transport/stdio")// Create a transport (stdio in this example)transport := stdio.NewStdioServerTransportWithIO(stdout, stdin)// Create a new clientclient := mcp.NewClient(transport)// Initialize the clientresponse, err := client.Initialize(context.Background())if err != nil { log.Fatalf("Failed to initialize client: %v", err)}
Both ListTools and ListPrompts support pagination. You can pass a cursor to get the next page of results:
Copy
var cursor *stringfor { tools, err := client.ListTools(context.Background(), cursor) if err != nil { log.Fatalf("Failed to list tools: %v", err) } // Process tools... if tools.NextCursor == nil { break // No more pages } cursor = tools.NextCursor}
For web-based tools that communicate over HTTP/HTTPS:
Copy
transport := http.NewHTTPClientTransport("/mcp")transport.WithBaseURL("http://localhost:8080")client := mcp.NewClient(transport)
Note that the HTTP transport is stateless and does not support bidirectional features like notifications. Each request-response cycle is independent, making it suitable for simple tool invocations but not for scenarios requiring real-time updates or persistent connections.