slashben/example-go-mcp-server
If you are the rightful owner of example-go-mcp-server 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 is a simple Model Context Protocol (MCP) server that provides basic mathematical operations using the mark3labs MCP Go library and Kubescape MCP implementation pattern.
Example Go MCP Server
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.
Features
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)
Project Structure
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
Installation
- 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
Usage
Running the MCP Server
Start the MCP server:
./example-go-mcp-server mcpserver
Or run directly with Go:
go run main.go mcpserver
Available Tools
The server provides the following tools that can be called via MCP:
Addition (add
)
- Parameters:
a
(required): First number to addb
(required): Second number to add
- Example Response:
{
"operation": "addition",
"a": 5,
"b": 3,
"result": 8
}
Subtraction (sub
)
- Parameters:
a
(required): Number to subtract fromb
(required): Number to subtract
- Example Response:
{
"operation": "subtraction",
"a": 10,
"b": 4,
"result": 6
}
Multiplication (mul
)
- Parameters:
a
(required): First number to multiplyb
(required): Second number to multiply
- Example Response:
{
"operation": "multiplication",
"a": 6,
"b": 7,
"result": 42
}
Division (div
)
- 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
}
Development
Adding New Tools
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)
Error Handling
The server includes comprehensive error handling:
- Parameter validation
- Type conversion safety
- Division by zero protection
- JSON marshaling error handling
Logging
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
Dependencies
github.com/mark3labs/mcp-go
: MCP protocol implementationgithub.com/spf13/cobra
: CLI framework
License
This project is open source and available under the MIT License.
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
References
This implementation is based on the Kubescape MCP server pattern: