client

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package client provides an MCP client wrapper for connecting to MCP servers.

This package simplifies connecting to MCP servers and discovering/invoking their tools. It wraps the official MCP SDK client with convenience methods.

Example

client := client.New("my-client", "v1.0.0", nil)

cmd := exec.Command("npx", "-y", "@modelcontextprotocol/server-github")
session, err := client.ConnectCommand(ctx, cmd, nil)
if err != nil {
	return err
}
defer session.Close()

tools, err := session.ListTools(ctx)
// ...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client wraps mcp.Client with convenience methods.

func New

func New(name, version string, opts *Options) *Client

New creates a new MCP client with the given identity.

The name and version identify this client to MCP servers. The opts parameter may be nil to use defaults.

func (*Client) Connect

func (c *Client) Connect(ctx context.Context, transport mcp.Transport, opts *mcp.ClientSessionOptions) (*Session, error)

Connect establishes a session with an MCP server via the given transport.

The transport determines how the client communicates with the server. Common transports include mcp.CommandTransport for spawning a subprocess.

The returned Session must be closed when done.

func (*Client) ConnectCommand

func (c *Client) ConnectCommand(ctx context.Context, cmd *exec.Cmd, opts *mcp.ClientSessionOptions) (*Session, error)

ConnectCommand spawns a command and connects to it via stdio.

This is a convenience method for the common case of spawning an MCP server as a subprocess. The command's stdin/stdout are used for MCP communication.

Environment variables can be set on the command before calling this method.

Example:

cmd := exec.Command("npx", "-y", "@modelcontextprotocol/server-github")
cmd.Env = append(os.Environ(), "GITHUB_TOKEN=xxx")
session, err := client.ConnectCommand(ctx, cmd, nil)

func (*Client) MCPClient

func (c *Client) MCPClient() *mcp.Client

MCPClient returns the underlying mcp.Client for advanced use cases.

type Options

type Options struct {
	// ClientOptions are passed to the underlying mcp.Client.
	ClientOptions *mcp.ClientOptions
}

Options configures a Client.

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session wraps mcp.ClientSession with tool operations.

func (*Session) CallTool

func (s *Session) CallTool(ctx context.Context, name string, args map[string]any) (*mcp.CallToolResult, error)

CallTool invokes a tool by name with the given arguments.

The args map should match the tool's input schema. The result contains the tool's output content.

func (*Session) Close

func (s *Session) Close() error

Close closes the session and releases resources.

This should be called when the session is no longer needed. After Close, no other methods should be called on the session.

func (*Session) GetPrompt

func (s *Session) GetPrompt(ctx context.Context, name string, args map[string]string) (*mcp.GetPromptResult, error)

GetPrompt retrieves a prompt by name with the given arguments.

func (*Session) ID

func (s *Session) ID() string

ID returns the session identifier.

func (*Session) InitializeResult

func (s *Session) InitializeResult() *mcp.InitializeResult

InitializeResult returns the server's initialization response.

This includes the server's capabilities and implementation info.

func (*Session) ListPrompts

func (s *Session) ListPrompts(ctx context.Context) ([]*mcp.Prompt, error)

ListPrompts returns all prompts available on the server.

func (*Session) ListResources

func (s *Session) ListResources(ctx context.Context) ([]*mcp.Resource, error)

ListResources returns all resources available on the server.

func (*Session) ListTools

func (s *Session) ListTools(ctx context.Context) ([]*mcp.Tool, error)

ListTools returns all tools available on the server.

This fetches the complete list of tools from the MCP server. The tools include their names, descriptions, and input schemas.

func (*Session) MCPSession

func (s *Session) MCPSession() *mcp.ClientSession

MCPSession returns the underlying mcp.ClientSession for advanced use.

func (*Session) ReadResource

func (s *Session) ReadResource(ctx context.Context, uri string) (*mcp.ReadResourceResult, error)

ReadResource reads a resource by URI.

func (*Session) Wait

func (s *Session) Wait() error

Wait blocks until the session is closed by the server.

This is useful for long-running sessions where you want to wait for the server to terminate.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL