or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

auth.mdindex.mdjsonrpc.mdmcp-client.mdmcp-protocol.mdmcp-server.mdmcp-transports.mdmcp-types.mdoauthex.md
tile.json

mcp-client.mddocs/

MCP Client

The MCP client implementation enables connecting to MCP servers, calling tools, accessing prompts and resources, and handling server notifications.

Client Construction

NewClient

Creates a new MCP client.

import "github.com/modelcontextprotocol/go-sdk/mcp"

func NewClient(impl *Implementation, opts *ClientOptions) *Client

Parameters:

  • impl: Implementation information (must not be nil)
  • opts: Optional client configuration

Example:

client := mcp.NewClient(&mcp.Implementation{
    Name:    "my-client",
    Version: "1.0.0",
}, nil)

Implementation

Describes the name and version of an MCP implementation.

type Implementation struct {
    Name    string `json:"name"`
    Title   string `json:"title,omitempty"`
    Version string `json:"version"`
}

Fields:

  • Name: Programmatic identifier for the implementation
  • Title: Human-readable title for UI contexts
  • Version: Version string

ClientOptions

Configures client behavior and capabilities.

type ClientOptions struct {
    CreateMessageHandler            func(context.Context, *CreateMessageRequest) (*CreateMessageResult, error)
    ElicitationHandler              func(context.Context, *ElicitRequest) (*ElicitResult, error)
    ToolListChangedHandler          func(context.Context, *ToolListChangedRequest)
    PromptListChangedHandler        func(context.Context, *PromptListChangedRequest)
    ResourceListChangedHandler      func(context.Context, *ResourceListChangedRequest)
    ResourceUpdatedHandler          func(context.Context, *ResourceUpdatedNotificationRequest)
    LoggingMessageHandler           func(context.Context, *LoggingMessageRequest)
    ProgressNotificationHandler     func(context.Context, *ProgressNotificationClientRequest)
    KeepAlive                       time.Duration
}

Fields:

  • CreateMessageHandler: Handles sampling/createMessage requests (advertises sampling capability when set)
  • ElicitationHandler: Handles elicitation/create requests (advertises elicitation capability when set)
  • ToolListChangedHandler: Called when server tool list changes
  • PromptListChangedHandler: Called when server prompt list changes
  • ResourceListChangedHandler: Called when server resource list changes
  • ResourceUpdatedHandler: Called when a subscribed resource is updated
  • LoggingMessageHandler: Called when server sends log messages
  • ProgressNotificationHandler: Called for progress notifications
  • KeepAlive: Interval for automatic ping requests (0 disables)

Client Methods

Connect

Connects the client to a server using a transport.

func (c *Client) Connect(
    ctx context.Context,
    t Transport,
    _ *ClientSessionOptions,
) (cs *ClientSession, err error)

Parameters:

  • ctx: Context for the connection operation
  • t: Transport to use for communication
  • _: Reserved for future options (pass nil)

Returns: A new ClientSession for interacting with the server

Example:

transport := &mcp.CommandTransport{Command: exec.Command("myserver")}
session, err := client.Connect(ctx, transport, nil)
if err != nil {
    log.Fatal(err)
}
defer session.Close()

AddRoots

Adds roots to the client and notifies connected servers.

func (c *Client) AddRoots(roots ...*Root)

Parameters:

  • roots: One or more Root objects to add

Example:

client.AddRoots(&mcp.Root{
    URI:  "file:///home/user/project",
    Name: "My Project",
})

RemoveRoots

Removes roots by URI and notifies connected servers.

func (c *Client) RemoveRoots(uris ...string)

Parameters:

  • uris: URIs of roots to remove

AddSendingMiddleware

Wraps the sending method handler with middleware.

func (c *Client) AddSendingMiddleware(middleware ...Middleware)

AddReceivingMiddleware

Wraps the receiving method handler with middleware.

func (c *Client) AddReceivingMiddleware(middleware ...Middleware)

ClientSession

A ClientSession represents a logical connection with an MCP server. It provides methods to send requests and notifications to the server.

Tool Operations

CallTool

Calls a tool on the server.

func (cs *ClientSession) CallTool(
    ctx context.Context,
    params *CallToolParams,
) (*CallToolResult, error)

Parameters:

  • ctx: Context for the operation
  • params: Tool call parameters

Example:

result, err := session.CallTool(ctx, &mcp.CallToolParams{
    Name:      "greet",
    Arguments: map[string]any{"name": "Alice"},
})

ListTools

Lists all available tools on the server.

func (cs *ClientSession) ListTools(
    ctx context.Context,
    params *ListToolsParams,
) (*ListToolsResult, error)

Tools

Returns an iterator over all tools (handles pagination automatically).

func (cs *ClientSession) Tools(
    ctx context.Context,
    params *ListToolsParams,
) iter.Seq2[*Tool, error]

Example:

for tool, err := range session.Tools(ctx, &mcp.ListToolsParams{}) {
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Tool: %s - %s\n", tool.Name, tool.Description)
}

Prompt Operations

GetPrompt

Gets a specific prompt from the server.

func (cs *ClientSession) GetPrompt(
    ctx context.Context,
    params *GetPromptParams,
) (*GetPromptResult, error)

Example:

result, err := session.GetPrompt(ctx, &mcp.GetPromptParams{
    Name: "greeting",
    Arguments: map[string]string{"language": "spanish"},
})

ListPrompts

Lists all available prompts on the server.

func (cs *ClientSession) ListPrompts(
    ctx context.Context,
    params *ListPromptsParams,
) (*ListPromptsResult, error)

Prompts

Returns an iterator over all prompts (handles pagination automatically).

func (cs *ClientSession) Prompts(
    ctx context.Context,
    params *ListPromptsParams,
) iter.Seq2[*Prompt, error]

Resource Operations

ReadResource

Reads a resource from the server.

func (cs *ClientSession) ReadResource(
    ctx context.Context,
    params *ReadResourceParams,
) (*ReadResourceResult, error)

Example:

result, err := session.ReadResource(ctx, &mcp.ReadResourceParams{
    URI: "file:///path/to/file.txt",
})

ListResources

Lists all available resources on the server.

func (cs *ClientSession) ListResources(
    ctx context.Context,
    params *ListResourcesParams,
) (*ListResourcesResult, error)

Resources

Returns an iterator over all resources (handles pagination automatically).

func (cs *ClientSession) Resources(
    ctx context.Context,
    params *ListResourcesParams,
) iter.Seq2[*Resource, error]

ListResourceTemplates

Lists resource templates available on the server.

func (cs *ClientSession) ListResourceTemplates(
    ctx context.Context,
    params *ListResourceTemplatesParams,
) (*ListResourceTemplatesResult, error)

ResourceTemplates

Returns an iterator over all resource templates (handles pagination automatically).

func (cs *ClientSession) ResourceTemplates(
    ctx context.Context,
    params *ListResourceTemplatesParams,
) iter.Seq2[*ResourceTemplate, error]

Subscribe

Subscribes to updates for a specific resource.

func (cs *ClientSession) Subscribe(
    ctx context.Context,
    params *SubscribeParams,
) error

Unsubscribe

Unsubscribes from updates for a specific resource.

func (cs *ClientSession) Unsubscribe(
    ctx context.Context,
    params *UnsubscribeParams,
) error

Completion Operations

Complete

Requests completion suggestions from the server.

func (cs *ClientSession) Complete(
    ctx context.Context,
    params *CompleteParams,
) (*CompleteResult, error)

Logging Operations

SetLoggingLevel

Sets the logging level for server messages.

func (cs *ClientSession) SetLoggingLevel(
    ctx context.Context,
    params *SetLoggingLevelParams,
) error

Progress Notifications

NotifyProgress

Sends a progress notification from the client to the server.

func (cs *ClientSession) NotifyProgress(
    ctx context.Context,
    params *ProgressNotificationParams,
) error

Connection Management

Close

Performs a graceful close of the connection.

func (cs *ClientSession) Close() error

Wait

Waits for the connection to be closed by the server.

func (cs *ClientSession) Wait() error

Ping

Makes an MCP "ping" request to the server.

func (cs *ClientSession) Ping(ctx context.Context, params *PingParams) error

Session Information

ID

Returns the session ID.

func (cs *ClientSession) ID() string

InitializeResult

Returns the initialize result from the server.

func (cs *ClientSession) InitializeResult() *InitializeResult

Client Capabilities

ClientCapabilities

Describes capabilities that a client may support.

type ClientCapabilities struct {
    Experimental map[string]any          `json:"experimental,omitempty"`
    Roots        struct { ListChanged bool } `json:"roots,omitempty"`
    Sampling     *SamplingCapabilities   `json:"sampling,omitempty"`
    Elicitation  *ElicitationCapabilities `json:"elicitation,omitempty"`
}

Fields:

  • Experimental: Non-standard capabilities
  • Roots: Present if client supports listing roots
  • Sampling: Present if client supports LLM sampling
  • Elicitation: Present if client supports elicitation

SamplingCapabilities

Indicates the client supports sampling from an LLM.

type SamplingCapabilities struct{}

ElicitationCapabilities

Indicates the client supports elicitation from the server.

type ElicitationCapabilities struct{}

Root Management

Root

Represents a root directory or file that the server can operate on.

type Root struct {
    Meta `json:"_meta,omitempty"`
    Name string `json:"name,omitempty"`
    URI  string `json:"uri"`
}

Fields:

  • Meta: Reserved metadata field
  • Name: Optional name for the root
  • URI: URI identifying the root (must start with file://)

Middleware

Middleware

A function type for wrapping method handlers.

type Middleware func(MethodHandler) MethodHandler

MethodHandler

A function that handles MCP messages.

type MethodHandler func(
    ctx context.Context,
    method string,
    req Request,
) (result Result, err error)

Session State

ClientSessionOptions

Reserved for future use when connecting.

type ClientSessionOptions struct{}