client

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 6 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) AsSkill

func (s *Session) AsSkill(opts ...SkillOption) *SessionSkill

AsSkill creates a skill.Skill from this session.

The skill name defaults to "mcp" but can be overridden with options. Tools are discovered lazily on first access or when Init() is called.

Example:

skill := session.AsSkill(
    client.WithSkillName("github"),
    client.WithSkillDescription("GitHub 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.

type SessionSkill

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

SessionSkill wraps an MCP session as a skill.Skill.

Tools are discovered from the MCP server and cached. Tool calls are proxied to the underlying MCP session.

func (*SessionSkill) Close

func (s *SessionSkill) Close() error

Close closes the underlying MCP session.

func (*SessionSkill) Description

func (s *SessionSkill) Description() string

Description returns the skill description.

func (*SessionSkill) Init

func (s *SessionSkill) Init(ctx context.Context) error

Init discovers tools from the MCP session.

This fetches the tool list from the server and caches them. Subsequent calls to Tools() will return the cached list.

func (*SessionSkill) Name

func (s *SessionSkill) Name() string

Name returns the skill name.

func (*SessionSkill) Tools

func (s *SessionSkill) Tools() []skill.Tool

Tools returns all tools from the MCP session.

If Init() has not been called, this will call Init() with a background context to discover tools.

type SkillOption

type SkillOption func(*SessionSkill)

SkillOption configures a SessionSkill.

func WithSkillDescription

func WithSkillDescription(desc string) SkillOption

WithSkillDescription sets the skill description.

func WithSkillName

func WithSkillName(name string) SkillOption

WithSkillName sets the skill name.

Jump to

Keyboard shortcuts

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