This example demonstrates how to implement a basic Model Context Protocol (MCP) server in Go, supporting multiple transport modes (stdio and HTTP) and using the Gin web framework for HTTP routing.
The server.go file provides a minimal yet extensible MCP server that:
- Registers a common tool (see
operation.RegisterCommonTool) - Supports two transport types: stdio and HTTP
- Uses Gin for HTTP routing and middleware
- Demonstrates best practices for server setup, logging, and error handling
01-basic-mcp/
├── server.go
└── README.md ← (this file)Encapsulates the MCP server instance and provides a method to expose it as an HTTP handler.
type MCPServer struct {
server *server.MCPServer
}The NewMCPServer function creates and configures the MCP server, enabling tool capabilities, logging, and recovery middleware. It also registers a common tool for demonstration.
func NewMCPServer() *MCPServerThe server can be started in one of two modes, controlled by the -transport flag:
- stdio: Communicates via standard input/output (for CLI or local integration)
- http: Runs as a standard HTTP server using Gin
| Flag | Default | Description |
|---|---|---|
-addr |
:8080 |
Address to listen on (for HTTP mode) |
-transport |
stdio |
Transport type: stdio or http |
-t |
(none) | Alias for -transport |
- Go 1.24 or later
- Required dependencies (see
go.mod)
-
Clone the repository and navigate to the
01-basic-mcpdirectory. -
Build and run the server:
go run server.go -transport http -addr :8080go run server.gogo run server.go -transport http -addr :8080- POST /mcp
- GET /mcp
- DELETE /mcp
All handled by the MCP server via Gin.
- Server Initialization: Sets up the MCP server with middleware and registers tools.
- Transport Selection: Chooses the transport based on the
-transportflag. - HTTP Routing: Uses Gin to route
/mcprequests to the MCP server handler.
func main() {
// Parse flags
// Create MCPServer
// Switch on transport type:
// - stdio: server.ServeStdio
// - http: Gin router with /mcp endpoint
}- Register additional tools by calling their registration functions with the MCP server instance.
- Add custom middleware to the Gin router for authentication, logging, etc.
- Integrate with other transports as needed.
- mark3labs/mcp-go — MCP Go SDK
- gin-gonic/gin — Gin web framework
This example is provided under the MIT License.