mcp

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2026 License: MIT Imports: 11 Imported by: 0

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:

  1. Client connects to MCP server
  2. Client requests available tools (list_tools)
  3. Server responds with tool schemas
  4. Client executes tool (call_tool)
  5. 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

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 NewClient

func NewClient(server *Server) *Client

NewClient creates a new MCP client for the given server.

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.

func (*Client) GetServer

func (c *Client) GetServer() *Server

GetServer returns the server configuration.

func (*Client) ListTools

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

ListTools retrieves all available tools from the MCP server.

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping checks if the MCP server is reachable.

type ConfigManager added in v0.1.9

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

ConfigManager handles reading and writing MCP configuration files

func NewConfigManager added in v0.1.9

func NewConfigManager(configPath string) *ConfigManager

NewConfigManager creates a new config manager

func (*ConfigManager) AddServer added in v0.1.9

func (cm *ConfigManager) AddServer(name string, serverConfig ServerConfig) error

AddServer adds or updates a server in the configuration

func (*ConfigManager) GetConfigPath added in v0.1.9

func (cm *ConfigManager) GetConfigPath() string

GetConfigPath returns the path to the configuration file

func (*ConfigManager) GetServer added in v0.1.9

func (cm *ConfigManager) GetServer(name string) (*ServerConfig, error)

GetServer retrieves a server configuration by name

func (*ConfigManager) ListServers added in v0.1.9

func (cm *ConfigManager) ListServers() ([]string, error)

ListServers returns all server names in the configuration

func (*ConfigManager) LoadConfig added in v0.1.9

func (cm *ConfigManager) LoadConfig() (*MCPConfig, error)

LoadConfig loads the MCP configuration from disk

func (*ConfigManager) RemoveServer added in v0.1.9

func (cm *ConfigManager) RemoveServer(name string) error

RemoveServer removes a server from the configuration

func (*ConfigManager) SaveConfig added in v0.1.9

func (cm *ConfigManager) SaveConfig(config *MCPConfig) error

SaveConfig saves the MCP configuration to disk

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 MCPConfig added in v0.1.9

type MCPConfig struct {
	MCPServers map[string]ServerConfig `json:"mcpServers"`
}

MCPConfig represents the structure of .mcp.json file

type RPCError

type RPCError struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

RPCError represents a JSON-RPC error.

func (*RPCError) Error

func (e *RPCError) Error() string

Error implements the error interface for RPCError.

type Registry

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

Registry manages multiple MCP servers and their tools.

func NewRegistry

func NewRegistry(checkInterval time.Duration) *Registry

NewRegistry creates a new MCP server registry.

func (*Registry) AddServer

func (r *Registry) AddServer(server *Server) error

AddServer adds an MCP server to the 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

func (r *Registry) DiscoverTools(ctx context.Context) error

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) GetServer

func (r *Registry) GetServer(name string) (*Client, error)

GetServer returns a server client by name.

func (*Registry) GetTool

func (r *Registry) GetTool(name string) (*ToolInfo, error)

GetTool returns a tool by its fully qualified name (server.tool).

func (*Registry) ListServers

func (r *Registry) ListServers() []string

ListServers returns all registered server names.

func (*Registry) ListTools

func (r *Registry) ListTools() map[string]*ToolInfo

ListTools returns all discovered tools.

func (*Registry) RemoveServer

func (r *Registry) RemoveServer(name string) error

RemoveServer removes an MCP server from the registry.

func (*Registry) SetHealthStatus added in v0.1.4

func (r *Registry) SetHealthStatus(name string, status *HealthStatus)

SetHealthStatus manually sets the health status for a server. This is primarily used for testing purposes.

func (*Registry) StartHealthChecks

func (r *Registry) StartHealthChecks(ctx context.Context)

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 ServerConfig added in v0.1.9

type ServerConfig struct {
	Command string            `json:"command,omitempty"`
	Args    []string          `json:"args,omitempty"`
	Env     map[string]string `json:"env,omitempty"`
	// For HTTP-based servers (our internal format)
	URL         string            `json:"url,omitempty"`
	Timeout     string            `json:"timeout,omitempty"`
	Headers     map[string]string `json:"headers,omitempty"`
	Enabled     *bool             `json:"enabled,omitempty"`
	Description string            `json:"description,omitempty"`
}

ServerConfig represents a single server configuration in .mcp.json This matches the Claude Desktop format for 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 ToolInfo

type ToolInfo struct {
	Tool       Tool
	ServerName string
}

ToolInfo contains information about a tool and its source server.

type ToolResult

type ToolResult struct {
	Content []ResultContent `json:"content"`
	IsError bool            `json:"isError,omitempty"`
}

ToolResult represents the result of a tool execution.

Jump to

Keyboard shortcuts

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