Cover image
Try Now
2025-04-15

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.

3 years

Works with Finder

4

Github Watches

5

Github Forks

37

Github Stars

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.

相关推荐

  • https://maiplestudio.com
  • Find Exhibitors, Speakers and more

  • Yusuf Emre Yeşilyurt
  • I find academic articles and books for research and literature reviews.

  • Emmet Halm
  • Converts Figma frames into front-end code for various mobile frameworks.

  • https://suefel.com
  • Latest advice and best practices for custom GPT development.

  • Carlos Ferrin
  • Encuentra películas y series en plataformas de streaming.

  • Joshua Armstrong
  • Confidential guide on numerology and astrology, based of GG33 Public information

  • https://zenepic.net
  • Embark on a thrilling diplomatic quest across a galaxy on the brink of war. Navigate complex politics and alien cultures to forge peace and avert catastrophe in this immersive interstellar adventure.

  • Elijah Ng Shi Yi
  • Advanced software engineer GPT that excels through nailing the basics.

  • https://reddgr.com
  • Delivers concise Python code and interprets non-English comments

  • 林乔安妮
  • A fashion stylist GPT offering outfit suggestions for various scenarios.

  • 1Panel-dev
  • 💬 MaxKB is a ready-to-use AI chatbot that integrates Retrieval-Augmented Generation (RAG) pipelines, supports robust workflows, and provides advanced MCP tool-use capabilities.

  • ShrimpingIt
  • Micropython I2C-based manipulation of the MCP series GPIO expander, derived from Adafruit_MCP230xx

  • GLips
  • MCP server to provide Figma layout information to AI coding agents like Cursor

  • open-webui
  • User-friendly AI Interface (Supports Ollama, OpenAI API, ...)

  • Mintplex-Labs
  • The all-in-one Desktop & Docker AI application with built-in RAG, AI agents, No-code agent builder, MCP compatibility, and more.

  • Dhravya
  • Collection of apple-native tools for the model context protocol.

  • activepieces
  • AI Agents & MCPs & AI Workflow Automation • (280+ MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

    Reviews

    3 (1)
    Avatar
    user_33xpajrT
    2025-04-17

    I've been using go-mcp by MegaGrindStone and it's simply fantastic! The seamless integration and user-friendly interface make it a joy to work with. It's evident that a lot of thought has been put into its development, making it a must-have for anyone in need of a reliable and efficient MCP application. Highly recommended!