MCP Client Usage Guide
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.Installation
Add the MCP Golang package to your project:Basic Usage
Here’s a simple example of creating and initializing an MCP client:Working with Tools
Listing Available Tools
Calling a Tool
Working with Prompts
Listing Available Prompts
Using a Prompt
Working with Resources
Listing Resources
Reading a Resource
Pagination
BothListTools
and ListPrompts
support pagination. You can pass a cursor to get the next page of results:
Error Handling
The client includes comprehensive error handling. All methods return an error as their second return value:Best Practices
- Always initialize the client before making any calls
- Use appropriate context management for timeouts and cancellation
- Handle errors appropriately for your use case
- Close or clean up resources when done
- Define type-safe structs for tool and prompt arguments
- Use struct tags to ensure correct JSON field names
Complete Example
For a complete working example, check out our example client implementation.Transport Options
The MCP client supports multiple transport options:Standard I/O Transport
For command-line tools that communicate through stdin/stdout:HTTP Transport
For web-based tools that communicate over HTTP/HTTPS:Context Support
All client operations now support context propagation:- Set timeouts for operations
- Cancel long-running operations
- Pass request-scoped values
- Implement tracing and monitoring