Documentation
¶
Overview ¶
Package mcp implements the Model Context Protocol for custom tool registration.
The Model Context Protocol (MCP) is a JSON-RPC based protocol that enables external servers to register and provide custom tools to the AI assistant. This allows for extensibility and integration with external services.
Features:
- JSON-RPC 2.0 protocol implementation
- Tool discovery from MCP servers
- Dynamic tool schema parsing
- Tool execution delegation
- Error handling and recovery
- Server health monitoring
- Configuration management
Protocol Flow:
- Client connects to MCP server
- Client requests available tools (list_tools)
- Server responds with tool schemas
- Client executes tool (call_tool)
- Server executes and returns result
Example usage:
import "github.com/AINative-studio/ainative-code/internal/mcp"
// Create MCP client
client, err := mcp.NewClient("http://localhost:8080")
if err != nil {
log.Fatal(err)
}
// Discover tools
tools, err := client.ListTools(ctx)
if err != nil {
log.Fatal(err)
}
// Execute tool
result, err := client.CallTool(ctx, "my_tool", map[string]interface{}{
"param1": "value1",
})
Index ¶
- type CallToolParams
- type Client
- func (c *Client) CallTool(ctx context.Context, name string, arguments map[string]interface{}) (*ToolResult, error)
- func (c *Client) CheckHealth(ctx context.Context) *HealthStatus
- func (c *Client) GetServer() *Server
- func (c *Client) ListTools(ctx context.Context) ([]Tool, error)
- func (c *Client) Ping(ctx context.Context) error
- type HealthStatus
- type JSONRPCRequest
- type JSONRPCResponse
- type ListToolsParams
- type ListToolsResult
- type RPCError
- type Registry
- func (r *Registry) AddServer(server *Server) error
- func (r *Registry) CallTool(ctx context.Context, name string, arguments map[string]interface{}) (*ToolResult, error)
- func (r *Registry) DiscoverTools(ctx context.Context) error
- func (r *Registry) GetAllHealthStatus() map[string]*HealthStatus
- func (r *Registry) GetHealthStatus(name string) (*HealthStatus, error)
- func (r *Registry) GetServer(name string) (*Client, error)
- func (r *Registry) GetTool(name string) (*ToolInfo, error)
- func (r *Registry) ListServers() []string
- func (r *Registry) ListTools() map[string]*ToolInfo
- func (r *Registry) RemoveServer(name string) error
- func (r *Registry) StartHealthChecks(ctx context.Context)
- func (r *Registry) StopHealthChecks()
- type ResultContent
- type Server
- type Tool
- type ToolInfo
- type ToolResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CallToolParams ¶
type CallToolParams struct {
Name string `json:"name"`
Arguments map[string]interface{} `json:"arguments"`
}
CallToolParams represents parameters for call_tool request.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents an MCP protocol client.
func (*Client) CallTool ¶
func (c *Client) CallTool(ctx context.Context, name string, arguments map[string]interface{}) (*ToolResult, error)
CallTool executes a tool on the MCP server.
func (*Client) CheckHealth ¶
func (c *Client) CheckHealth(ctx context.Context) *HealthStatus
CheckHealth checks the health status of the MCP server.
type HealthStatus ¶
type HealthStatus struct {
Healthy bool `json:"healthy"`
LastChecked time.Time `json:"lastChecked"`
ResponseTime time.Duration `json:"responseTime"`
Error string `json:"error,omitempty"`
}
HealthStatus represents the health status of an MCP server.
type JSONRPCRequest ¶
type JSONRPCRequest struct {
JSONRPC string `json:"jsonrpc"`
ID interface{} `json:"id"`
Method string `json:"method"`
Params interface{} `json:"params,omitempty"`
}
JSONRPCRequest represents a JSON-RPC 2.0 request.
type JSONRPCResponse ¶
type JSONRPCResponse struct {
JSONRPC string `json:"jsonrpc"`
ID interface{} `json:"id"`
Result interface{} `json:"result,omitempty"`
Error *RPCError `json:"error,omitempty"`
}
JSONRPCResponse represents a JSON-RPC 2.0 response.
type ListToolsParams ¶
type ListToolsParams struct {
Cursor string `json:"cursor,omitempty"`
}
ListToolsParams represents parameters for list_tools request.
type ListToolsResult ¶
type ListToolsResult struct {
Tools []Tool `json:"tools"`
NextCursor string `json:"nextCursor,omitempty"`
}
ListToolsResult represents the result of list_tools request.
type RPCError ¶
type RPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
RPCError represents a JSON-RPC error.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry manages multiple MCP servers and their tools.
func NewRegistry ¶
NewRegistry creates a new MCP server registry.
func (*Registry) CallTool ¶
func (r *Registry) CallTool(ctx context.Context, name string, arguments map[string]interface{}) (*ToolResult, error)
CallTool executes a tool by its fully qualified name.
func (*Registry) DiscoverTools ¶
DiscoverTools discovers all tools from all registered servers.
func (*Registry) GetAllHealthStatus ¶
func (r *Registry) GetAllHealthStatus() map[string]*HealthStatus
GetAllHealthStatus returns health status for all servers.
func (*Registry) GetHealthStatus ¶
func (r *Registry) GetHealthStatus(name string) (*HealthStatus, error)
GetHealthStatus returns the health status of a server.
func (*Registry) ListServers ¶
ListServers returns all registered server names.
func (*Registry) RemoveServer ¶
RemoveServer removes an MCP server from the registry.
func (*Registry) StartHealthChecks ¶
StartHealthChecks starts periodic health checks for all servers.
func (*Registry) StopHealthChecks ¶
func (r *Registry) StopHealthChecks()
StopHealthChecks stops the health check background process.
type ResultContent ¶
type ResultContent struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
Data string `json:"data,omitempty"`
}
ResultContent represents content in a tool result.
type Server ¶
type Server struct {
Name string `json:"name"`
URL string `json:"url"`
Timeout time.Duration `json:"timeout"`
Headers map[string]string `json:"headers,omitempty"`
Enabled bool `json:"enabled"`
Description string `json:"description,omitempty"`
}
Server represents an MCP server configuration.
type Tool ¶
type Tool struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema map[string]interface{} `json:"inputSchema"`
}
Tool represents a tool available from an MCP server.
type ToolResult ¶
type ToolResult struct {
Content []ResultContent `json:"content"`
IsError bool `json:"isError,omitempty"`
}
ToolResult represents the result of a tool execution.