Documentation
¶
Overview ¶
Package discovery provides tool discovery functionality for MCP servers. This package allows you to register tools that are hidden from the main tools/list response but can be discovered via search and executed through a wrapper tool.
This is useful when you have many tools and want to reduce context window usage. Instead of sending all tool definitions to the LLM upfront, you can: 1. Register essential tools normally with the MCP server 2. Register specialized tools with a ToolRegistry 3. Attach the registry to the server - it registers tool_search and execute_tool
The workflow for LLMs becomes:
- tool_search(query="email") -> finds tools with full schemas
- execute_tool(name="send_email", arguments={...}) -> executes the tool
Index ¶
- Variables
- func WithRequestProviders(ctx context.Context, providers ...ToolProvider) context.Context
- type SearchResult
- type ToolMetadata
- type ToolProvider
- type ToolRegistry
- func (r *ToolRegistry) AddProvider(provider ToolProvider)
- func (r *ToolRegistry) Attach(server *mcp.Server)
- func (r *ToolRegistry) CallTool(ctx context.Context, name string, args map[string]interface{}) (*mcp.ToolResponse, error)
- func (r *ToolRegistry) GetTool(ctx context.Context, name string) (*mcp.MCPTool, error)
- func (r *ToolRegistry) ListToolMetadata(ctx context.Context) ([]ToolMetadata, error)
- func (r *ToolRegistry) RegisterMCPTool(tool *mcp.MCPTool, handler mcp.ToolHandler, keywords ...string)
- func (r *ToolRegistry) RegisterTool(tool *mcp.ToolBuilder, handler mcp.ToolHandler, keywords ...string)
- func (r *ToolRegistry) RemoveProvider(provider ToolProvider)
- func (r *ToolRegistry) Search(ctx context.Context, query string, maxResults int) []SearchResult
Constants ¶
This section is empty.
Variables ¶
var ErrToolNotFound = mcp.ErrUnknownTool
ErrToolNotFound is returned when a tool is not found
Functions ¶
func WithRequestProviders ¶
func WithRequestProviders(ctx context.Context, providers ...ToolProvider) context.Context
WithRequestProviders adds request-scoped tool providers to the context. These providers are only available for the duration of the request. Use this for per-user or per-tenant tool providers.
Types ¶
type SearchResult ¶
type SearchResult struct {
Name string `json:"name"`
Description string `json:"description"`
Score float64 `json:"score"`
InputSchema interface{} `json:"inputSchema,omitempty"`
}
SearchResult represents a matched tool from a search
type ToolMetadata ¶
type ToolMetadata struct {
Name string `json:"name"`
Description string `json:"description"`
Keywords []string `json:"keywords,omitempty"`
}
ToolMetadata contains searchable information about a tool
type ToolProvider ¶
type ToolProvider interface {
// ListToolMetadata returns metadata for all searchable tools from this provider
ListToolMetadata(ctx context.Context) ([]ToolMetadata, error)
// GetTool returns the full tool definition for a specific tool by name
// Returns nil, nil if the tool doesn't exist in this provider
GetTool(ctx context.Context, name string) (*mcp.MCPTool, error)
// CallTool executes a tool by name with the given arguments
// Returns ErrToolNotFound if the tool doesn't exist in this provider
CallTool(ctx context.Context, name string, args map[string]interface{}) (*mcp.ToolResponse, error)
}
ToolProvider allows external tool sources (scripts, plugins, databases, etc.)
type ToolRegistry ¶
type ToolRegistry struct {
// contains filtered or unexported fields
}
ToolRegistry manages searchable tools and provides discovery functionality. Tools registered here are hidden from tools/list but can be discovered via search. Create one instance and attach it to your MCP server.
func NewToolRegistry ¶
func NewToolRegistry() *ToolRegistry
NewToolRegistry creates a new tool registry for searchable tools
func (*ToolRegistry) AddProvider ¶
func (r *ToolRegistry) AddProvider(provider ToolProvider)
AddProvider adds a dynamic tool provider
func (*ToolRegistry) Attach ¶
func (r *ToolRegistry) Attach(server *mcp.Server)
Attach registers the discovery tools (tool_search, execute_tool) with the MCP server
func (*ToolRegistry) CallTool ¶
func (r *ToolRegistry) CallTool(ctx context.Context, name string, args map[string]interface{}) (*mcp.ToolResponse, error)
CallTool attempts to call a registered or dynamic tool
func (*ToolRegistry) ListToolMetadata ¶
func (r *ToolRegistry) ListToolMetadata(ctx context.Context) ([]ToolMetadata, error)
ListToolMetadata returns metadata for all tools registered in this registry. This implements the ToolProvider interface, allowing a ToolRegistry to be used as a request-scoped provider via WithRequestProviders.
func (*ToolRegistry) RegisterMCPTool ¶ added in v0.6.12
func (r *ToolRegistry) RegisterMCPTool(tool *mcp.MCPTool, handler mcp.ToolHandler, keywords ...string)
RegisterMCPTool registers a searchable tool from an already-built MCPTool. This is useful for registering tools from remote servers where you already have the MCPTool. Keywords are used for fuzzy search matching.
func (*ToolRegistry) RegisterTool ¶
func (r *ToolRegistry) RegisterTool(tool *mcp.ToolBuilder, handler mcp.ToolHandler, keywords ...string)
RegisterTool registers a searchable tool that won't appear in ListTools but can be discovered and called. Keywords are used for fuzzy search matching.
func (*ToolRegistry) RemoveProvider ¶
func (r *ToolRegistry) RemoveProvider(provider ToolProvider)
RemoveProvider removes a dynamic tool provider
func (*ToolRegistry) Search ¶
func (r *ToolRegistry) Search(ctx context.Context, query string, maxResults int) []SearchResult
Search performs fuzzy search across all registered and dynamic tools. If query is empty, returns all tools.