Last active
February 18, 2025 16:50
-
-
Save up1/994608015be1bc2a1e9f4c345ac7861d to your computer and use it in GitHub Desktop.
MCP Server with Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$export ANTHROPIC_API_KEY='your-api-key' | |
$mcphost --config ./mcp-server.json | |
2025/02/18 23:50:07 INFO Model loaded provider=anthropic model=claude-3-5-sonnet-latest | |
2025/02/18 23:50:07 INFO Initializing server... name=mcp-calculator-with-docker | |
2025/02/18 23:50:09 INFO Server connected name=mcp-calculator-with-docker | |
2025/02/18 23:50:09 INFO Tools loaded server=mcp-calculator-with-docker count=1 | |
┃ Enter your prompt (Type /help for commands, Ctrl+C to quit) | |
┃ | |
┃ | |
┃ | |
┃ | |
┃ | |
┃ | |
alt+enter / ctrl+j new line • ctrl+e open editor • enter submit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM golang:1.23.6-alpine3.21 AS builder | |
WORKDIR /app | |
COPY . . | |
RUN go mod tidy | |
RUN go build -o calculator | |
FROM alpine:3.21 | |
WORKDIR /app | |
RUN apk add --no-cache ca-certificates | |
COPY --from=builder /app/calculator . | |
ENTRYPOINT ["./calculator"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"github.com/mark3labs/mcp-go/mcp" | |
"github.com/mark3labs/mcp-go/server" | |
) | |
func main() { | |
// Create a new MCP server | |
s := server.NewMCPServer( | |
"demo", | |
"1.0.0", | |
server.WithResourceCapabilities(true, true), | |
server.WithLogging(), | |
) | |
// Add a calculator tool | |
calculatorTool := mcp.NewTool("calculate", | |
mcp.WithDescription("Perform basic arithmetic operations"), | |
mcp.WithString("operation", | |
mcp.Required(), | |
mcp.Description("The operation to perform (add, subtract, multiply, divide)"), | |
mcp.Enum("add", "subtract", "multiply", "divide"), | |
), | |
mcp.WithNumber("x", | |
mcp.Required(), | |
mcp.Description("First number"), | |
), | |
mcp.WithNumber("y", | |
mcp.Required(), | |
mcp.Description("Second number"), | |
), | |
) | |
// Add the calculator handler | |
s.AddTool(calculatorTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | |
op := request.Params.Arguments["operation"].(string) | |
x := request.Params.Arguments["x"].(float64) | |
y := request.Params.Arguments["y"].(float64) | |
var result float64 | |
switch op { | |
case "add": | |
result = x + y | |
case "subtract": | |
result = x - y | |
case "multiply": | |
result = x * y | |
case "divide": | |
if y == 0 { | |
return mcp.NewToolResultError("Cannot divide by zero"), nil | |
} | |
result = x / y | |
} | |
return mcp.NewToolResultText(fmt.Sprintf("%.2f", result)), nil | |
}) | |
// Start the server | |
if err := server.ServeStdio(s); err != nil { | |
fmt.Printf("Server error: %v\n", err) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"mcpServers": { | |
"mcp-calculator-with-docker" :{ | |
"command": "docker", | |
"args": [ | |
"run", | |
"--rm", | |
"-i", | |
"calculator" | |
] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment