Fire in da houseTop Tip:Paying $100+ per month for Perplexity, MidJourney, Runway, ChatGPT and other tools is crazy - get all your AI tools in one site starting at $15 per month with Galaxy AI Fire in da houseCheck it out free

go-mcp

MCP.Pizza Chef: MegaGrindStone

Go-MCP is a Go language implementation of the Model Context Protocol (MCP), an open standard designed to facilitate smooth integration between large language model (LLM) applications and external data sources or tools. This server enables developers to expose structured, real-time context to LLMs, allowing them to interact with their environment safely and effectively. Go-MCP supports modular, scalable, and secure interactions, making it ideal for building AI-enhanced workflows, agents, and copilots. It follows semantic versioning and encourages using stable tagged releases for production use, ensuring reliability and forward compatibility.

Use This MCP server To

Integrate LLMs with external APIs and databases Expose real-time data streams to language models Build AI agents that interact with file systems Enable secure, scoped model interactions Create modular AI-enhanced workflows Serve as a backend for LLM context providers

README

go-mcp

Go Reference CI Go Report Card codecov

A Go implementation of the Model Context Protocol (MCP) - an open protocol that enables seamless integration between LLM applications and external data sources and tools.

⚠️ Warning: The main branch contains unreleased changes and may be unstable. We recommend using the latest tagged release for stability. This library follows semantic versioning - breaking changes may be introduced with minor version bumps (0.x.0) until v1.0.0 is released. After v1.0.0, the API will be stable and breaking changes will only occur in major version updates. We recommend pinning your dependency to a specific version and reviewing the changelog before upgrading.

Overview

This repository provides a Go library implementing the Model Context Protocol (MCP) following the official specification.

Features

Core Protocol

  • Complete MCP protocol implementation with JSON-RPC 2.0 messaging
  • Pluggable transport system supporting SSE and Standard IO
  • Session-based client-server communication
  • Comprehensive error handling and progress tracking

Server Features

  • Modular server implementation with optional capabilities
  • Support for prompts, resources, and tools
  • Real-time notifications and updates
  • Built-in logging system
  • Resource subscription management

Client Features

  • Flexible client configuration with optional capabilities
  • Automatic session management and health monitoring
  • Support for streaming and pagination
  • Progress tracking and cancellation support
  • Configurable timeouts and retry logic

Transport Options

  • Server-Sent Events (SSE) for web-based real-time updates
  • Standard IO for command-line tool integration

Installation

go get github.com/MegaGrindStone/go-mcp

Usage

Server Implementation

There are two main steps to implementing an MCP server:

1. Create a Server Implementation

Create a server implementation that provides the capabilities you need:

// Example implementing a server with tool support
type MyToolServer struct{}

func (s *MyToolServer) ListTools(ctx context.Context, params mcp.ListToolsParams, 
    progress mcp.ProgressReporter, requestClient mcp.RequestClientFunc) (mcp.ListToolsResult, error) {
    // Return available tools
    return mcp.ListToolsResult{
        Tools: []mcp.Tool{
            {
                Name: "example-tool",
                Description: "An example tool",
                // Additional tool properties...
            },
        },
    }, nil
}

func (s *MyToolServer) CallTool(ctx context.Context, params mcp.CallToolParams, 
    progress mcp.ProgressReporter, requestClient mcp.RequestClientFunc) (mcp.CallToolResult, error) {
    // Implement tool functionality
    return mcp.CallToolResult{
        Content: []mcp.Content{
            {
                Type: mcp.ContentTypeText,
                Text: "Tool result",
            },
        },
    }, nil
}
2. Initialize and Serve

Create and configure the server with your implementation and chosen transport:

// Create server with your implementation
toolServer := &MyToolServer{}

// Choose a transport method
// Option 1: Server-Sent Events (SSE)
sseSrv := mcp.NewSSEServer("/message")
srv := mcp.NewServer(mcp.Info{
    Name:    "my-mcp-server",
    Version: "1.0",
}, sseSrv, 
    mcp.WithToolServer(toolServer),
    mcp.WithServerPingInterval(30*time.Second),
    // Add other capabilities as needed
)

// Set up HTTP handlers for SSE
http.Handle("/sse", sseSrv.HandleSSE())
http.Handle("/message", sseSrv.HandleMessage())
go http.ListenAndServe(":8080", nil)

// Option 2: Standard IO
srvIO := mcp.NewStdIO(os.Stdin, os.Stdout)
srv := mcp.NewServer(mcp.Info{
    Name:    "my-mcp-server", 
    Version: "1.0",
}, srvIO,
    mcp.WithToolServer(toolServer),
    // Add other capabilities as needed
)

// Start the server - this blocks until shutdown
go srv.Serve()

// To shutdown gracefully
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
srv.Shutdown(ctx)
Available Server Options

Configure your server with additional capabilities:

// Prompt capabilities
mcp.WithPromptServer(promptServer)
mcp.WithPromptListUpdater(promptListUpdater)

// Resource capabilities
mcp.WithResourceServer(resourceServer)
mcp.WithResourceListUpdater(resourceListUpdater)
mcp.WithResourceSubscriptionHandler(subscriptionHandler)

// Tool capabilities
mcp.WithToolServer(toolServer)
mcp.WithToolListUpdater(toolListUpdater)

// Roots and logging capabilities
mcp.WithRootsListWatcher(rootsListWatcher)
mcp.WithLogHandler(logHandler)

// Server behavior configuration
mcp.WithServerPingInterval(interval)
mcp.WithServerPingTimeout(timeout)
mcp.WithServerPingTimeoutThreshold(threshold)
mcp.WithServerSendTimeout(timeout)
mcp.WithInstructions(instructions)

// Event callbacks
mcp.WithServerOnClientConnected(func(id string, info mcp.Info) {
    fmt.Printf("Client connected: %s\n", id)
})
mcp.WithServerOnClientDisconnected(func(id string) {
    fmt.Printf("Client disconnected: %s\n", id)
})

Client Implementation

The client implementation involves creating a client with transport options and capabilities, connecting to a server, and executing MCP operations.

Creating and Connecting a Client
// Create client info
info := mcp.Info{
    Name:    "my-mcp-client",
    Version: "1.0",
}

// Create a context for connection and operations
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Choose transport layer - SSE or Standard IO
// Option 1: Server-Sent Events (SSE)
sseClient := mcp.NewSSEClient("http://localhost:8080/sse", http.DefaultClient)
cli := mcp.NewClient(info, sseClient,
    // Optional client configurations
    mcp.WithClientPingInterval(30*time.Second),
    mcp.WithProgressListener(progressListener),
    mcp.WithLogReceiver(logReceiver),
)

// Option 2: Standard IO
srvReader, srvWriter := io.Pipe()
cliReader, cliWriter := io.Pipe()
cliIO := mcp.NewStdIO(cliReader, srvWriter)
srvIO := mcp.NewStdIO(srvReader, cliWriter)
cli := mcp.NewClient(info, cliIO)

// Connect client (requires context)
if err := cli.Connect(ctx); err != nil {
    log.Fatal(err)
}
// Ensure proper cleanup
defer func() {
    disconnectCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()
    cli.Disconnect(disconnectCtx)
}()
Making Requests
// List available tools
tools, err := cli.ListTools(ctx, mcp.ListToolsParams{})
if err != nil {
    log.Fatal(err)
}

// Call a tool (with proper argument structure)
args := map[string]string{"message": "Hello MCP!"}
argsBs, _ := json.Marshal(args)

result, err := cli.CallTool(ctx, mcp.CallToolParams{
    Name:      "echo",
    Arguments: argsBs,
})
if err != nil {
    log.Fatal(err)
}

// Work with resources
resources, err := cli.ListResources(ctx, mcp.ListResourcesParams{})
if err != nil {
    log.Fatal(err)
}

// Subscribe to resource updates
err = cli.SubscribeResource(ctx, mcp.SubscribeResourceParams{
    URI: "resource-uri",
})
if err != nil {
    log.Fatal(err)
}

// Work with prompts
prompts, err := cli.ListPrompts(ctx, mcp.ListPromptsParams{})
if err != nil {
    log.Fatal(err)
}

prompt, err := cli.GetPrompt(ctx, mcp.GetPromptParams{
    Name: "my-prompt",
})
if err != nil {
    log.Fatal(err)
}
Implementing Handlers for Client Capabilities
// Implement required interfaces for client capabilities
type myClient struct {
    // ...client fields
}

// For sampling capability
func (c *myClient) CreateSampleMessage(ctx context.Context, params mcp.SamplingParams) (mcp.SamplingResult, error) {
    // Generate sample LLM output
    return mcp.SamplingResult{
        Role: mcp.RoleAssistant,
        Content: mcp.SamplingContent{
            Type: mcp.ContentTypeText,
            Text: "Sample response text",
        },
        Model: "my-llm-model",
    }, nil
}

// For resource subscription notifications
func (c *myClient) OnResourceSubscribedChanged(uri string) {
    fmt.Printf("Resource %s was updated\n", uri)
}

// For progress tracking
func (c *myClient) OnProgress(params mcp.ProgressParams) {
    fmt.Printf("Progress: %.2f/%.2f\n", params.Progress, params.Total)
}

// Pass these handlers when creating the client
cli := mcp.NewClient(info, transport,
    mcp.WithSamplingHandler(client),
    mcp.WithResourceSubscribedWatcher(client),
    mcp.WithProgressListener(client),
)

Complete Examples

For complete working examples:

  • See example/everything/ for a comprehensive server and client implementation with all features
  • See example/filesystem/ for a focused example of file operations using Standard IO transport

These examples demonstrate:

  • Server and client lifecycle management
  • Transport layer setup
  • Error handling
  • Tool implementation
  • Resource management
  • Progress tracking
  • Logging integration

For more details, check the example directory in the repository.

Server Packages

The servers directory contains reference server implementations that mirror those found in the official modelcontextprotocol/servers repository.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the MIT License - see the LICENSE file for details.

go-mcp FAQ

How do I install go-mcp?
You can install go-mcp by running 'go get github.com/MegaGrindStone/go-mcp' and importing it into your Go project. Refer to the GitHub repository for detailed setup instructions.
Is go-mcp stable for production use?
The main branch may contain unstable, unreleased changes. It is recommended to use the latest tagged release for production to ensure stability.
Does go-mcp support semantic versioning?
Yes, go-mcp follows semantic versioning. Breaking changes may occur in minor versions until v1.0.0, after which the API will be stable.
Can go-mcp be used to expose custom data sources to LLMs?
Yes, go-mcp is designed to expose structured data from various external sources, enabling LLMs to access and interact with them.
How does go-mcp ensure secure interactions?
Go-mcp supports scoped and observable model interactions, allowing secure and controlled access to external data and tools.
What programming language is go-mcp implemented in?
Go-mcp is implemented entirely in Go, making it efficient and easy to integrate into Go-based projects.
Where can I find documentation for go-mcp?
Documentation and usage examples are available on the GitHub repository and the Go package documentation site.
Can go-mcp be integrated with multiple LLM providers?
Yes, go-mcp is provider-agnostic and can work with various LLMs like OpenAI, Anthropic Claude, and Google Gemini.