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 ¶
- type Client
- type Options
- type Session
- func (s *Session) AsSkill(opts ...SkillOption) *SessionSkill
- func (s *Session) CallTool(ctx context.Context, name string, args map[string]any) (*mcp.CallToolResult, error)
- func (s *Session) Close() error
- func (s *Session) GetPrompt(ctx context.Context, name string, args map[string]string) (*mcp.GetPromptResult, error)
- func (s *Session) ID() string
- func (s *Session) InitializeResult() *mcp.InitializeResult
- func (s *Session) ListPrompts(ctx context.Context) ([]*mcp.Prompt, error)
- func (s *Session) ListResources(ctx context.Context) ([]*mcp.Resource, error)
- func (s *Session) ListTools(ctx context.Context) ([]*mcp.Tool, error)
- func (s *Session) MCPSession() *mcp.ClientSession
- func (s *Session) ReadResource(ctx context.Context, uri string) (*mcp.ReadResourceResult, error)
- func (s *Session) Wait() error
- type SessionSkill
- type SkillOption
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 ¶
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)
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 ¶
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) 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 ¶
ListPrompts returns all prompts available on the server.
func (*Session) ListResources ¶
ListResources returns all resources available on the server.
func (*Session) ListTools ¶
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 ¶
ReadResource reads a resource by URI.
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) 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.