A simple Model Context Protocol (MCP) server that provides basic mathematical operations. This project is based on the mark3labs MCP Go library and Kubescape MCP implementation pattern and demonstrates how to create custom MCP tools.
The server exposes four mathematical operation tools:
- add: Adds two numbers together
- sub: Subtracts the second number from the first
- mul: Multiplies two numbers together
- div: Divides the first number by the second (with zero division protection)
example-go-mcp-server/
├── cmd/
│ ├── root.go # Root command using Cobra
│ └── mcpserver.go # MCP server command
├── internal/
│ └── mcpserver/
│ └── server.go # Main MCP server implementation
├── main.go # Application entry point
├── go.mod # Go module file
└── README.md # This file
- Clone the repository:
git clone <repository-url>
cd example-go-mcp-server
- Install dependencies:
go mod tidy
- Build the application:
go build -o example-go-mcp-server
Start the MCP server:
./example-go-mcp-server mcpserver
Or run directly with Go:
go run main.go mcpserver
The server provides the following tools that can be called via MCP:
- Parameters:
a
(required): First number to addb
(required): Second number to add
- Example Response:
{
"operation": "addition",
"a": 5,
"b": 3,
"result": 8
}
- Parameters:
a
(required): Number to subtract fromb
(required): Number to subtract
- Example Response:
{
"operation": "subtraction",
"a": 10,
"b": 4,
"result": 6
}
- Parameters:
a
(required): First number to multiplyb
(required): Second number to multiply
- Example Response:
{
"operation": "multiplication",
"a": 6,
"b": 7,
"result": 42
}
- Parameters:
a
(required): Number to be divided (dividend)b
(required): Number to divide by (divisor)
- Example Response:
{
"operation": "division",
"a": 15,
"b": 3,
"result": 5
}
To add a new mathematical operation:
- Add a new tool definition in
createMathTools()
function - Add a case in the
CallTool()
switch statement - Implement the handler function (e.g.,
handlePow()
for power operation)
The server includes comprehensive error handling:
- Parameter validation
- Type conversion safety
- Division by zero protection
- JSON marshaling error handling
The server logs all MCP tool inputs and responses to a single file in the temp directory:
- Location:
{temp_dir}/example-go-mcp-server/mcp_server.log
- Format: JSON lines with timestamp, operation, input, output, and error information
- Purpose: Debugging, monitoring, and audit trail
- Append Mode: All new log entries are appended to the same file
github.com/mark3labs/mcp-go
: MCP protocol implementationgithub.com/spf13/cobra
: CLI framework
This project is open source and available under the MIT License.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This implementation is based on the Kubescape MCP server pattern: