mcp

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2025 License: MIT Imports: 14 Imported by: 0

README

MCP Integration for Agent SDK Go

This package provides integration with the Model Context Protocol (MCP) for the Agent SDK Go. It allows agents to connect to MCP servers and use their tools.

Overview

The MCP integration allows agents to:

  1. Connect to MCP servers using different transports (stdio, HTTP)
  2. List and use tools provided by MCP servers
  3. Convert MCP tools to agent tools

Usage

Creating an MCP Server Connection
import (
    "context"
    "github.com/Ingenimax/agent-sdk-go/pkg/mcp"
)

// Create an HTTP-based MCP server client
httpServer, err := mcp.NewHTTPServer(context.Background(), mcp.HTTPServerConfig{
    BaseURL: "http://localhost:8083/mcp",
})
if err != nil {
    log.Printf("Failed to initialize HTTP MCP server: %v", err)
}

// Create a stdio-based MCP server
stdioServer, err := mcp.NewStdioServer(context.Background(), mcp.StdioServerConfig{
    Command: "go",
    Args:    []string{"run", "./server-stdio/main.go"},
})
if err != nil {
    log.Printf("Failed to initialize STDIO MCP server: %v", err)
}
Creating an Agent with MCP Servers
import (
    "github.com/Ingenimax/agent-sdk-go/pkg/agent"
    "github.com/Ingenimax/agent-sdk-go/pkg/interfaces"
    "github.com/Ingenimax/agent-sdk-go/pkg/llm/openai"
    "github.com/Ingenimax/agent-sdk-go/pkg/mcp"
    "github.com/Ingenimax/agent-sdk-go/pkg/memory"
)

// Create MCP servers
var mcpServers []interfaces.MCPServer
mcpServers = append(mcpServers, httpServer)
mcpServers = append(mcpServers, stdioServer)

// Create an LLM (e.g., OpenAI)
llm := openai.NewClient(apiKey, openai.WithModel("gpt-4o-mini"))

// Create the agent with MCP server support
myAgent, err := agent.NewAgent(
    agent.WithLLM(llm),
    agent.WithMCPServers(mcpServers),
    agent.WithMemory(memory.NewConversationBuffer()),
    agent.WithSystemPrompt("You are an AI assistant that can use tools from MCP servers."),
)
Listing MCP Tools
// List tools from an MCP server
tools, err := mcpServer.ListTools(context.Background())
if err != nil {
    log.Printf("Failed to list tools: %v", err)
    return
}

for _, tool := range tools {
    fmt.Printf("Tool: %s - %s\n", tool.Name, tool.Description)
}

Transports

The MCP integration supports different transports for connecting to MCP servers:

  • stdio: For local MCP servers that communicate over standard input/output
  • HTTP: For remote MCP servers that communicate over HTTP

Implementation Details

The MCP integration is built on top of the mcp-golang library, which provides a Go implementation of the Model Context Protocol.

The integration consists of:

  1. MCPServer: An interface for interacting with MCP servers
  2. MCPTool: A struct that implements the interfaces.Tool interface for MCP tools
  3. Agent integration: The agent can use MCP tools alongside its regular tools

Example

See the MCP examples for complete examples of using the MCP integration:

  • client: A client that connects to both HTTP and stdio MCP servers and uses their tools
  • server-http: An example HTTP MCP server implementation with tools for time and drink recommendations
  • server-stdio: An example stdio MCP server implementation with a food recommendation tool

To run the examples:

  1. Start the HTTP server: go run cmd/examples/mcp/server-http/main.go
  2. Run the client, which will also start the stdio server: go run cmd/examples/mcp/client/main.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertMCPTools

func ConvertMCPTools(server interfaces.MCPServer, mcpTools []map[string]interface{}) ([]interfaces.Tool, error)

ConvertMCPTools converts a list of MCP tools to agent SDK tools

func FetchMCPToolsFromServer

func FetchMCPToolsFromServer(ctx context.Context, url string) ([]map[string]interface{}, error)

FetchMCPToolsFromServer fetches the list of tools from an MCP server

func NewHTTPServer

func NewHTTPServer(ctx context.Context, config HTTPServerConfig) (interfaces.MCPServer, error)

NewHTTPServer creates a new MCPServer that communicates over HTTP

func NewMCPServer

func NewMCPServer(ctx context.Context, transport transport.Transport) (interfaces.MCPServer, error)

NewMCPServer creates a new MCPServer with the given transport

func NewMCPTool

func NewMCPTool(name, description string, schema interface{}, server interfaces.MCPServer) interfaces.Tool

NewMCPTool creates a new MCPTool

func NewStdioServer

func NewStdioServer(ctx context.Context, config StdioServerConfig) (interfaces.MCPServer, error)

NewStdioServer creates a new MCPServer that communicates over stdio

func RunStdioToolCommand

func RunStdioToolCommand(cmd *exec.Cmd, payload []byte) ([]byte, error)

RunStdioToolCommand executes a command for an MCP stdio server

Types

type HTTPServerConfig

type HTTPServerConfig struct {
	BaseURL string
	Path    string
	Token   string
}

HTTPServerConfig holds configuration for an HTTP MCP server

type MCPServer

type MCPServer interface {
	// Initialize initializes the connection to the MCP server
	Initialize(ctx context.Context) error

	// ListTools lists the tools available on the MCP server
	ListTools(ctx context.Context) ([]interfaces.MCPTool, error)

	// CallTool calls a tool on the MCP server
	CallTool(ctx context.Context, name string, args interface{}) (*interfaces.MCPToolResponse, error)

	// Close closes the connection to the MCP server
	Close() error
}

MCPServer represents a connection to an MCP server

type MCPServerImpl

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

MCPServerImpl is the implementation of interfaces.MCPServer

func (*MCPServerImpl) CallTool

func (s *MCPServerImpl) CallTool(ctx context.Context, name string, args interface{}) (*interfaces.MCPToolResponse, error)

CallTool calls a tool on the MCP server

func (*MCPServerImpl) Close

func (s *MCPServerImpl) Close() error

Close closes the connection to the MCP server

func (*MCPServerImpl) Initialize

func (s *MCPServerImpl) Initialize(ctx context.Context) error

Initialize initializes the connection to the MCP server

func (*MCPServerImpl) ListTools

func (s *MCPServerImpl) ListTools(ctx context.Context) ([]interfaces.MCPTool, error)

ListTools lists the tools available on the MCP server

type MCPTool

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

MCPTool implements interfaces.Tool for MCP tools

func (*MCPTool) Description

func (t *MCPTool) Description() string

Description returns a description of what the tool does

func (*MCPTool) Execute

func (t *MCPTool) Execute(ctx context.Context, args string) (string, error)

Execute executes the tool with the given arguments

func (*MCPTool) Name

func (t *MCPTool) Name() string

Name returns the name of the tool

func (*MCPTool) Parameters

func (t *MCPTool) Parameters() map[string]interfaces.ParameterSpec

Parameters returns the parameters that the tool accepts

func (*MCPTool) Run

func (t *MCPTool) Run(ctx context.Context, input string) (string, error)

Run executes the tool with the given input

type MCPToolAdapter

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

MCPToolAdapter converts MCP tools to the agent SDK tool interface

func NewMCPToolAdapter

func NewMCPToolAdapter(server interfaces.MCPServer, name, description string, params map[string]ParameterSpec) *MCPToolAdapter

NewMCPToolAdapter creates a new adapter for an MCP tool

func (*MCPToolAdapter) Description

func (a *MCPToolAdapter) Description() string

Description returns the description of the tool

func (*MCPToolAdapter) Execute

func (a *MCPToolAdapter) Execute(ctx context.Context, args string) (string, error)

Execute executes the tool with the given arguments string

func (*MCPToolAdapter) InputSchema

func (a *MCPToolAdapter) InputSchema() map[string]interfaces.ParameterSpec

InputSchema returns the schema for the tool's input

func (*MCPToolAdapter) Name

func (a *MCPToolAdapter) Name() string

Name returns the name of the tool

func (*MCPToolAdapter) Parameters

func (a *MCPToolAdapter) Parameters() map[string]interfaces.ParameterSpec

Parameters returns the parameters that the tool accepts

func (*MCPToolAdapter) Run

func (a *MCPToolAdapter) Run(ctx context.Context, input string) (string, error)

Run executes the tool with the given input

type MCPToolResponse

type MCPToolResponse struct {
	Content interface{} `json:"content"`
	Error   string      `json:"error,omitempty"`
	Text    string      `json:"text,omitempty"`
}

MCPToolResponse represents the response from an MCP tool call

type ParameterSpec

type ParameterSpec struct {
	Type        string `json:"type"`
	Description string `json:"description"`
	Required    bool   `json:"required"`
}

ParameterSpec represents a parameter specification for an MCP tool

type StdioServerConfig

type StdioServerConfig struct {
	Command string
	Args    []string
	Env     []string
}

StdioServerConfig holds configuration for a stdio MCP server

type Tool

type Tool struct {
	Name        string
	Description string
	Schema      interface{}
}

Tool represents a tool available on an MCP server

type ToolResponse

type ToolResponse struct {
	Content []*mcplib.Content
	IsError bool
}

ToolResponse represents a response from a tool call

Jump to

Keyboard shortcuts

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