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 BridgeOptions
- type BridgedSkill
- func (s *BridgedSkill) Close() error
- func (s *BridgedSkill) Description() string
- func (s *BridgedSkill) Init(ctx context.Context) error
- func (s *BridgedSkill) Name() string
- func (s *BridgedSkill) Refresh(ctx context.Context) error
- func (s *BridgedSkill) Session() *Session
- func (s *BridgedSkill) Tools() []skill.Tool
- func (s *BridgedSkill) Version() string
- 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 BridgeOptions ¶ added in v0.11.0
type BridgeOptions struct {
// Name overrides the skill name (default: derived from server info).
Name string
// Description overrides the skill description.
Description string
// ToolPrefix adds a prefix to all tool names (e.g., "remote_").
ToolPrefix string
}
BridgeOptions configures how a remote MCP server is bridged.
type BridgedSkill ¶ added in v0.11.0
type BridgedSkill struct {
// contains filtered or unexported fields
}
BridgedSkill wraps a remote MCP server's tools as a local skill.
This allows tools from remote MCP servers to be registered in a local registry and used transparently alongside local skills.
func Bridge ¶ added in v0.11.0
func Bridge(ctx context.Context, session *Session, opts *BridgeOptions) (*BridgedSkill, error)
Bridge creates a BridgedSkill from an MCP session.
The bridged skill exposes all tools from the remote server as local omniskill tools. Tool calls are forwarded to the remote server.
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
}
bridged, err := client.Bridge(ctx, session, nil)
if err != nil {
return err
}
reg := registry.New()
reg.Register(bridged)
func (*BridgedSkill) Close ¶ added in v0.11.0
func (s *BridgedSkill) Close() error
Close closes the underlying MCP session.
func (*BridgedSkill) Description ¶ added in v0.11.0
func (s *BridgedSkill) Description() string
Description returns the skill description.
func (*BridgedSkill) Init ¶ added in v0.11.0
func (s *BridgedSkill) Init(ctx context.Context) error
Init is a no-op (session is already initialized).
func (*BridgedSkill) Name ¶ added in v0.11.0
func (s *BridgedSkill) Name() string
Name returns the skill name.
func (*BridgedSkill) Refresh ¶ added in v0.11.0
func (s *BridgedSkill) Refresh(ctx context.Context) error
Refresh re-fetches tools from the remote server.
Call this if the remote server's tools may have changed.
func (*BridgedSkill) Session ¶ added in v0.11.0
func (s *BridgedSkill) Session() *Session
Session returns the underlying MCP session.
func (*BridgedSkill) Tools ¶ added in v0.11.0
func (s *BridgedSkill) Tools() []skill.Tool
Tools returns the bridged tools.
func (*BridgedSkill) Version ¶ added in v0.11.0
func (s *BridgedSkill) Version() string
Version returns empty string (remote servers don't expose version).
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.
func (*SessionSkill) Version ¶ added in v0.11.0
func (s *SessionSkill) Version() string
Version returns the skill version (empty for MCP session skills).
type SkillOption ¶
type SkillOption func(*SessionSkill)
SkillOption configures a SessionSkill.
func WithSkillDescription ¶
func WithSkillDescription(desc string) SkillOption
WithSkillDescription sets the skill description.