Skip to content

slashben/example-go-mcp-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

  1. Clone the repository:
git clone <repository-url>
cd example-go-mcp-server
  1. Install dependencies:
go mod tidy
  1. 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 add
    • b (required): Second number to add
  • Example Response:
{
  "operation": "addition",
  "a": 5,
  "b": 3,
  "result": 8
}

Subtraction (sub)

  • Parameters:
    • a (required): Number to subtract from
    • b (required): Number to subtract
  • Example Response:
{
  "operation": "subtraction",
  "a": 10,
  "b": 4,
  "result": 6
}

Multiplication (mul)

  • Parameters:
    • a (required): First number to multiply
    • b (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:

  1. Add a new tool definition in createMathTools() function
  2. Add a case in the CallTool() switch statement
  3. 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 implementation
  • github.com/spf13/cobra: CLI framework

License

This project is open source and available under the MIT License.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

References

This implementation is based on the Kubescape MCP server pattern:

About

Example implementation of a calculator MCP server in Golang

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published