mcphub

package
v0.8.8 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package mcphub manages multiple MCP server connections and provides unified tool discovery and dispatch for the dscli tool framework.

Architecture

Agent tool call      toolcall.HandleToolCall
       │                      │
       ▼                      ▼
┌──────────────────────────────────────┐
│              mcphub.Dispatch         │
│                                      │
│  "lightpanda_markdown" → parse       │
│    server="lightpanda"               │
│    tool="markdown"                   │
│       │                              │
│       ▼                              │
│  lightpanda MCPClient.CallTool       │
└──────────────────────────────────────┘

Configuration

MCP server definitions are in the `mcp-servers` block of config.dscli:

mcp-servers {
  server-id {
    name = lightpanda
    type = local
    command = lightpanda
    args = [mcp]
    enabled = true
  }
}

Built-in servers:

  • lightpanda: web page interaction via LightPanda MCP

User-defined servers are defined in the mcp-servers config block.

Package mcphub manages multiple MCP server connections and provides unified tool discovery and dispatch for the dscli tool framework.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dispatch

func Dispatch(ctx context.Context, toolName, argsRaw string) (result, warning string, err error)

Dispatch routes a tool call to the correct MCP server based on the tool name prefix. The tool name format is "serverName_toolName".

func Init

func Init(ctx context.Context) error

Init initializes the global hub with built-in and user-configured servers. Called during startup from the web tool package init.

func SwitchServerTransport

func SwitchServerTransport(ctx context.Context, name, target string) error

SwitchServerTransport switches a server's transport between local (stdio) and cloud (SSE) at runtime. The new connection is validated before replacing the old one.

Types

type Hub

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

Hub manages multiple MCP server connections.

func (*Hub) SwitchServerTransport

func (h *Hub) SwitchServerTransport(ctx context.Context, name, target string) error

SwitchServerTransport replaces a server's connection with a new one that matches the specified target transport type ("local" or "cloud"). This allows switching between stdio and SSE transports at runtime without restarting the application.

The new connection is validated by listing tools before replacing the old one. If validation fails, the old connection is preserved.

type MCPClient

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

MCPClient wraps an MCP session. It manages the lifecycle of an MCP server subprocess, providing a simple interface for tool calls.

The client is safe for concurrent use (calls are serialized through an internal mutex because stdio transport cannot safely multiplex writes).

func NewMCPClient

func NewMCPClient(ctx context.Context, command string, args []string) (*MCPClient, error)

NewMCPClient starts an MCP server subprocess and connects to it. command is the executable path, args are the command-line arguments. The caller must call Close when done.

func NewSSEMCPClient

func NewSSEMCPClient(ctx context.Context, endpoint string) (*MCPClient, error)

NewSSEMCPClient connects to an MCP server over SSE. The caller must call Close when done.

func (*MCPClient) CallTool

func (c *MCPClient) CallTool(ctx context.Context, name string, args map[string]any) (string, error)

CallTool calls a tool by name with the given arguments. The caller is responsible for calling Close after finishing.

func (*MCPClient) Close

func (c *MCPClient) Close() error

Close shuts down the MCP session and kills the subprocess.

func (*MCPClient) ListTools

func (c *MCPClient) ListTools(ctx context.Context) ([]*mcp.Tool, error)

ListTools returns the list of tools available on the MCP server.

type MCPToolError

type MCPToolError struct {
	Tool    string        // name of the tool that failed
	Content []mcp.Content // original response content, preserved for debugging
}

MCPToolError is returned when an MCP tool call completes with IsError=true. Unlike a transport-level error (which would be returned as a Go error directly), this indicates the tool ran but its operation failed (e.g., URL unreachable).

func (*MCPToolError) Error

func (e *MCPToolError) Error() string

type ServerConfig

type ServerConfig struct {
	Name    string   // logical server name; set from map key if empty
	Type    string   // "local" (stdio) or "cloud" (SSE); default "local"
	Command string   // executable (stdio) or URL (http/https)
	Args    []string // command-line args (stdio) or query key=value pairs (SSE)
	Enabled bool
}

ServerConfig defines an MCP server connection.

func (ServerConfig) IsSSE

func (s ServerConfig) IsSSE() bool

IsSSE reports whether this server uses SSE transport. Transport type is determined by the Type field:

  • "cloud" → SSE transport
  • otherwise → stdio transport (subprocess)

Jump to

Keyboard shortcuts

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