Sponsored by Deepsite.site

MCP Go SDK

Created By
SetiabudiResearch10 months ago
This SDK provides a Go implementation of the Machine Control Protocol (MCP), enabling bidirectional communication between clients and servers for tool execution, resource access, and prompt handling. Based on
Content

MCP Go SDK

This SDK provides a Go implementation of the Model Context Protocol (MCP), enabling bidirectional communication between clients and servers for tool execution, resource access, and prompt handling.

Features

  • Transport Layer

    • Multiple transport options (stdio, SSE, WebSocket)
    • Bidirectional communication
    • Configurable endpoints and settings
  • Server Implementation

    • Tool registration and execution
    • Resource pattern matching and access
    • Prompt template rendering
    • Session management
    • Reflection-based handler invocation
  • Core Protocol Types

    • JSON-RPC message handling
    • MCP-specific types (tools, resources, prompts)
    • Capabilities and initialization

Installation

go get github.com/SetiabudiResearch/mcp-go-sdk

Quick Start

Here's a simple example of creating an MCP server:

package main

import (
    "context"
    "log"
    "github.com/SetiabudiResearch/mcp-go-sdk/pkg/mcp/server"
    "github.com/SetiabudiResearch/mcp-go-sdk/pkg/mcp/transport"
)

func main() {
    // Create a new server
    srv := server.NewServer("Example Server")

    // Add a tool
    srv.AddTool("greet", func(name string) string {
        return "Hello, " + name + "!"
    }, "Greet a person")

    // Create a session
    session := server.NewSession(context.Background(), srv)

    // Create and start a transport
    t := transport.NewStdioTransport(session)
    if err := t.Start(); err != nil {
        log.Fatal(err)
    }
}

Usage Guide

Creating a Server

// Create a new server with a name
srv := server.NewServer("My Server")

// Optionally configure server capabilities
srv.WithCapabilities(protocol.ServerCapabilities{
    SupportsAsync: true,
})

Adding Tools

// Add a synchronous tool
srv.AddTool("myTool", func(arg1 string, arg2 int) (string, error) {
    return fmt.Sprintf("Processed %s with %d", arg1, arg2), nil
}, "Tool description")

// Add an asynchronous tool
srv.AddAsyncTool("longRunningTool", func(params string) error {
    // Long-running operation
    return nil
}, "Async tool description")

Adding Resources

// Add a resource with pattern matching
srv.AddResource("files/{path}", func(path string) ([]byte, error) {
    return ioutil.ReadFile(path)
}, "Access files")

// Resource patterns support multiple parameters
srv.AddResource("api/{version}/{endpoint}", func(version, endpoint string) (interface{}, error) {
    return callAPI(version, endpoint)
}, "API access")

Adding Prompts

// Add a simple text prompt
srv.AddPrompt("confirm", func(action string) string {
    return fmt.Sprintf("Are you sure you want to %s?", action)
}, "Confirmation prompt")

// Add a multi-message prompt
srv.AddPrompt("chat", func(context string) []protocol.PromptMessage {
    return []protocol.PromptMessage{
        {
            Role: protocol.RoleAssistant,
            Content: protocol.TextContent{
                Type: "text",
                Text: "How can I help you with " + context + "?",
            },
        },
    }
}, "Chat prompt")

Transport Configuration

// Create a session
session := server.NewSession(context.Background(), srv)

// Stdio transport (for CLI applications)
t := transport.NewStdioTransport(session)

// WebSocket transport (for web applications)
t := transport.NewWebSocketTransport(session, transport.WithAddress(":8080"))

// Server-Sent Events transport (for web browsers)
t := transport.NewSSETransport(session, transport.WithAddress(":8080"))

// Start the transport
if err := t.Start(); err != nil {
    log.Fatal(err)
}

Example Applications

See the examples directory for complete example applications:

  • File Server: A complete file server implementation using MCP
  • Calculator: A simple calculator service
  • Chat Bot: An example chat bot using prompts

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

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

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
CursorThe AI Code Editor
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
ChatWiseThe second fastest AI chatbot™
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Tavily Mcp
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Playwright McpPlaywright MCP server
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
DeepChatYour AI Partner on Desktop
Serper MCP ServerA Serper MCP Server
WindsurfThe new purpose-built IDE to harness magic
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Amap Maps高德地图官方 MCP Server
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.