Skip to main content

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

Both ListTools 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

  1. Always initialize the client before making any calls
  2. Use appropriate context management for timeouts and cancellation
  3. Handle errors appropriately for your use case
  4. Close or clean up resources when done
  5. Define type-safe structs for tool and prompt arguments
  6. 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:
This transport supports all MCP features including bidirectional communication and notifications.

HTTP Transport

For web-based tools that communicate over HTTP/HTTPS:
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.

Context Support

All client operations now support context propagation:
The context allows you to:
  • Set timeouts for operations
  • Cancel long-running operations
  • Pass request-scoped values
  • Implement tracing and monitoring