lucasgdb/mcp-server-go-example
If you are the rightful owner of mcp-server-go-example and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
This document provides a structured summary of a Model Context Protocol (MCP) server implemented in Go, showcasing its features, tools, resources, and usage.
Hello World - MCP Server in Go
Introduction
This example demonstrates how to build a minimal Model Context Protocol (MCP) server in Go, using the mcp-go SDK. MCP is a lightweight JSONâRPCâbased protocol that lets language model clients discover and invoke external tools in a structured way.
With just a few lines of Go code, you can:
- Define an MCP server with a humanâreadable name and version.
- Declare one or more tools (commands) with typed parameters and descriptions.
- Register handler functions that execute your custom logic when a client calls a tool.
- Expose the server over standard I/O (stdin/stdout) so any MCPâcompatible client (like Cursor, Claude Desktop, or other IDE extensions) can launch and communicate with it without extra setup.
How It Works
-
Server Initialization
mcpServer := server.NewMCPServer( "Hello World MCP Server", "1.0.0", server.WithToolCapabilities(false), )
- Creates an MCP server named âHello World MCP Serverâ at version 1.0.0.
- Disables automatic tool capability announcements (so only your explicit tools appear).
-
Tool Definition
helloWorldTool := mcp.NewTool("hello_world", mcp.WithDescription("Greet someone"), mcp.WithString("name", mcp.Required(), mcp.Description("Personâs name"), ), )
- Defines a
hello_world
tool that accepts a single required string parametername
. - Descriptions help clients display usage information and generate prompts.
- Defines a
-
Handler Registration
mcpServer.AddTool(helloWorldTool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { name, ok := req.Params.Arguments["name"].(string) if !ok { return nil, errors.New("name must be a string") } return mcp.NewToolResultText(fmt.Sprintf("Hello, %s!", name)), nil })
- Associates the
hello_world
tool with a Go function. - Extracts the
name
argument, validates it, and returns a greeting. - Errors are surfaced back to the client as JSONâRPC errors.
- Associates the
-
Serving Over Stdio
if err := server.ServeStdio(mcpServer); err != nil { fmt.Printf("Server error: %v\n", err) }
- Starts an event loop reading JSONâRPC requests from stdin and writing responses to stdout.
- Allows any MCP client to launch the binary directly and speak MCP over pipes.
Getting Started
-
Install Dependencies
go mod tidy
-
Build the Server
go build -o hello-mcp ./src/main.go
-
Connect from an MCP Client
- In your clientâs configuration, point to the
hello-mcp
executable. - The client will automatically discover the
hello_world
tool schema and let you invoke it.
Example:
{ "mcpServers": { "demo-go-mcp": { "command": "path-to-server/hello-mcp" } } }
or, use my MCP client made in Go
- In your clientâs configuration, point to the
Enjoy building more complex workflows by adding additional tools, parameters, and transports (e.g., HTTP/SSE) as needed!