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) 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
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) 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.