mcpgateway

package
v0.264.3 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCallToolProxy

func NewCallToolProxy(gw *Gateway) tools.BaseTool

NewCallToolProxy returns a BaseTool that proxies arbitrary MCP tool calls.

func NewCatalogTool

func NewCatalogTool(gw *Gateway) tools.BaseTool

NewCatalogTool returns a BaseTool that wraps the catalog search capability.

Types

type CallToolProxy

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

CallToolProxy implements tools.BaseTool as "mcp_call_tool". It executes any MCP tool by name, using the gateway to route the call.

func (*CallToolProxy) Info

func (t *CallToolProxy) Info() tools.ToolInfo

func (*CallToolProxy) Run

type CatalogTool

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

CatalogTool implements tools.BaseTool as "mcp_query_catalog". It searches the MCP tool registry by description/task keyword.

func (*CatalogTool) Info

func (t *CatalogTool) Info() tools.ToolInfo

func (*CatalogTool) Run

type FavoriteConfig

type FavoriteConfig struct {
	Threshold    int // minimum calls to become a favorite (default 5)
	MaxFavorites int // maximum directly-exposed tools (default 15)
	WindowDays   int // look-back window in days (default 30)
	DecayDays    int // inactivity days before removal from favorites (default 14)
}

FavoriteConfig controls how favorites are computed from usage statistics.

func DefaultFavoriteConfig

func DefaultFavoriteConfig() FavoriteConfig

DefaultFavoriteConfig returns FavoriteConfig with sensible defaults.

type Gateway

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

Gateway is the main MCP gateway orchestrator, combining the tool registry and usage statistics subsystems.

func NewGateway

func NewGateway(db *sql.DB, cfg FavoriteConfig) *Gateway

NewGateway creates a Gateway with the given database connection and favorites config.

func (*Gateway) CallTool

func (g *Gateway) CallTool(ctx context.Context, toolID string, params map[string]interface{}, sessionID string) (interface{}, error)

CallTool executes a registered MCP tool by its ID ("server/tool" or tool_name). It records usage statistics after the call.

func (*Gateway) Close

func (g *Gateway) Close()

Close releases all pooled MCP client connections.

func (*Gateway) DeleteServerData added in v0.140.0

func (g *Gateway) DeleteServerData(ctx context.Context, serverName string) error

DeleteServerData removes registry and usage data associated with one MCP server.

func (*Gateway) GetAllTools

func (g *Gateway) GetAllTools(ctx context.Context) ([]RegisteredTool, error)

GetAllTools returns all tools in the registry.

func (*Gateway) GetFavorites

func (g *Gateway) GetFavorites(ctx context.Context) ([]RegisteredTool, error)

GetFavorites returns the current favorite tools based on usage statistics.

func (*Gateway) Initialize

func (g *Gateway) Initialize(ctx context.Context, mcpServers map[string]config.MCPServer) error

Initialize discovers all MCP server tools and populates the registry.

func (*Gateway) RecordUsage

func (g *Gateway) RecordUsage(ctx context.Context, toolID, sessionID string, durationMs int64, success bool)

RecordUsage records a tool usage event asynchronously (errors are logged, not returned).

func (*Gateway) SearchTools

func (g *Gateway) SearchTools(ctx context.Context, query string, maxResults int) ([]RegisteredTool, error)

SearchTools performs a keyword search over the registry.

type MCPClientPool

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

MCPClientPool manages long-lived MCP client connections to avoid creating a new subprocess / HTTP client on every tool call. Clients are keyed by server name and are initialized (handshake done) on first use. A client is evicted when an error occurs, so the next caller will transparently reconnect.

func NewClientPool

func NewClientPool() *MCPClientPool

NewClientPool creates an empty MCPClientPool.

func (*MCPClientPool) Evict

func (p *MCPClientPool) Evict(serverName string)

Evict closes and removes the cached client for a server. Called after a call error so that the next invocation creates a fresh connection.

func (*MCPClientPool) GetOrCreate

func (p *MCPClientPool) GetOrCreate(ctx context.Context, serverName string, srv config.MCPServer) (pooledMCPClient, error)

GetOrCreate returns a cached and already-initialized MCP client for the given server, creating and initializing one if none exists. The caller must NOT close the returned client — the pool owns its lifecycle.

func (*MCPClientPool) StopAll

func (p *MCPClientPool) StopAll()

StopAll closes all pooled clients and resets the pool.

type RegisteredTool

type RegisteredTool struct {
	ID             string
	ServerName     string
	ToolName       string
	Description    string
	InputSchema    map[string]interface{}
	LastDiscovered time.Time
}

RegisteredTool represents a tool stored in the MCP tool registry.

type Registry

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

Registry manages the MCP tool catalog in SQLite.

func NewRegistry

func NewRegistry(db *sql.DB) *Registry

NewRegistry creates a new Registry backed by the given database connection.

func (*Registry) DeleteServer added in v0.140.0

func (r *Registry) DeleteServer(ctx context.Context, serverName string) error

DeleteServer removes all registry rows for a server; usage rows are cascade-deleted by FK.

func (*Registry) DiscoverAll

func (r *Registry) DiscoverAll(ctx context.Context, mcpServers map[string]config.MCPServer) error

DiscoverAll iterates the configured MCP servers, calls ListTools on each, and upserts every discovered tool into mcp_tool_registry.

func (*Registry) GetAllTools

func (r *Registry) GetAllTools(ctx context.Context) ([]RegisteredTool, error)

GetAllTools returns all registered tools.

func (*Registry) GetTool

func (r *Registry) GetTool(ctx context.Context, toolID string) (*RegisteredTool, error)

GetTool retrieves a tool by its composite ID ("server/tool") or by tool_name.

func (*Registry) GetToolsByIDs

func (r *Registry) GetToolsByIDs(ctx context.Context, ids []string) ([]RegisteredTool, error)

GetToolsByIDs returns tools matching the given IDs.

func (*Registry) SearchTools

func (r *Registry) SearchTools(ctx context.Context, query string, maxResults int) ([]RegisteredTool, error)

SearchTools performs a keyword search over tool_name and description.

func (*Registry) UpsertTool

func (r *Registry) UpsertTool(ctx context.Context, serverName, toolName, description string, inputSchema map[string]interface{}) error

UpsertTool inserts or updates a tool record in mcp_tool_registry.

type Stats

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

Stats manages usage statistics and favorite computation.

func NewStats

func NewStats(db *sql.DB, cfg FavoriteConfig) *Stats

NewStats creates a new Stats instance with the given database and configuration.

func (*Stats) GetFavorites

func (s *Stats) GetFavorites(ctx context.Context) ([]RegisteredTool, error)

GetFavorites returns tools that meet the favorite criteria:

  • called at least Threshold times in the last WindowDays days
  • active within the last DecayDays days
  • capped at MaxFavorites entries

It joins with mcp_tool_registry to return full RegisteredTool records.

func (*Stats) GetTopTools

func (s *Stats) GetTopTools(ctx context.Context, limit int) ([]string, error)

GetTopTools returns the tool IDs ordered by call count within the configured window.

func (*Stats) RecordUsage

func (s *Stats) RecordUsage(ctx context.Context, toolID, sessionID string, durationMs int64, success bool) error

RecordUsage inserts a single invocation record into mcp_tool_usage_stats.

type UsageStat

type UsageStat struct {
	ToolID     string
	SessionID  string
	DurationMs int64
	Success    bool
}

UsageStat holds data for a single tool invocation record.

Jump to

Keyboard shortcuts

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