mcp

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2025 License: MIT Imports: 16 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/andmang/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/andmang/agent-sdk-go/pkg/agent"
    "github.com/andmang/agent-sdk-go/pkg/interfaces"
    "github.com/andmang/agent-sdk-go/pkg/llm/openai"
    "github.com/andmang/agent-sdk-go/pkg/mcp"
    "github.com/andmang/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-go 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 using the official SDK

func NewLazyMCPTool

func NewLazyMCPTool(name, description string, schema interface{}, config LazyMCPServerConfig) interfaces.Tool

NewLazyMCPTool creates a new lazy MCP tool

func NewMCPServer

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

NewMCPServer creates a new MCPServer with the given transport using the official SDK

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 using the official SDK

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
	ProtocolType ServerProtocolType
}

HTTPServerConfig holds configuration for an HTTP MCP server

type LazyMCPServerCache

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

LazyMCPServerCache manages shared MCP server instances

type LazyMCPServerConfig

type LazyMCPServerConfig struct {
	Name    string
	Type    string // "stdio" or "http"
	Command string
	Args    []string
	Env     []string
	URL     string
}

LazyMCPServerConfig holds configuration for creating an MCP server on demand

type LazyMCPTool

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

LazyMCPTool is a tool that initializes its MCP server on first use

func (*LazyMCPTool) Description

func (t *LazyMCPTool) Description() string

Description returns a description of what the tool does

func (*LazyMCPTool) DisplayName

func (t *LazyMCPTool) DisplayName() string

DisplayName implements interfaces.ToolWithDisplayName.DisplayName

func (*LazyMCPTool) Execute

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

Execute executes the tool with the given arguments

func (*LazyMCPTool) Internal

func (t *LazyMCPTool) Internal() bool

Internal implements interfaces.InternalTool.Internal

func (*LazyMCPTool) Name

func (t *LazyMCPTool) Name() string

Name returns the name of the tool

func (*LazyMCPTool) Parameters

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

Parameters returns the parameters that the tool accepts

func (*LazyMCPTool) Run

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

Run executes the tool with the given input

type MCPServerImpl

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

MCPServerImpl is the implementation of interfaces.MCPServer using the official SDK

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

func (t *MCPTool) DisplayName() string

DisplayName implements interfaces.ToolWithDisplayName.DisplayName

func (*MCPTool) Execute

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

Execute executes the tool with the given arguments

func (*MCPTool) Internal

func (t *MCPTool) Internal() bool

Internal implements interfaces.InternalTool.Internal

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

func (a *MCPToolAdapter) DisplayName() string

DisplayName implements interfaces.ToolWithDisplayName.DisplayName

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

func (a *MCPToolAdapter) Internal() bool

Internal implements interfaces.InternalTool.Internal

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 ServerProtocolType

type ServerProtocolType string

ServerProtocolType defines the protocol type for the MCP server communication Supported types are "streamable" and "sse"

const (
	StreamableHTTP ServerProtocolType = "streamable"
	SSE            ServerProtocolType = "sse"
)

type StdioServerConfig

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

StdioServerConfig holds configuration for a stdio MCP server

Jump to

Keyboard shortcuts

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