Skip to content

Commit 2e327f3

Browse files
committed
add streamable-http transport
1 parent 4b4c105 commit 2e327f3

File tree

4 files changed

+287
-155
lines changed

4 files changed

+287
-155
lines changed

Dockerfile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
FROM golang:1.23-alpine AS builder
2+
3+
RUN apk add --no-cache git ca-certificates
4+
5+
WORKDIR /app
6+
7+
COPY go.mod go.sum ./
8+
RUN go mod download
9+
10+
COPY . .
11+
RUN GOOS=linux go build -o mcp-calculator-server ./server.go
12+
13+
FROM alpine:3.21
14+
15+
RUN apk --no-cache add ca-certificates wget
16+
17+
RUN addgroup -g 1001 -S mcp && \
18+
adduser -S -D -H -u 1001 -s /sbin/nologin -G mcp mcp
19+
20+
WORKDIR /app
21+
22+
COPY --from=builder /app/mcp-calculator-server .
23+
24+
RUN chown mcp:mcp mcp-calculator-server
25+
26+
USER mcp
27+
28+
EXPOSE 8080
29+
30+
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
31+
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1
32+
33+
CMD ["./mcp-calculator-server"]

README.md

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,87 @@
11
# MCP Calculator Server
22

3-
A Model Context Protocol (MCP) server that provides calculator functionality for MCP clients.
3+
A Model Context Protocol (MCP) server that provides calculator functionality with support for mathematical expressions.
44

55
## Features
66

7-
- **Mathematical Operations**: Supports addition (+), subtraction (-), multiplication (*), and division (/)
8-
- **MCP Protocol Compliance**: Fully compatible with Claude Desktop and other MCP clients
7+
- **Mathematical Operations**: Addition (+), subtraction (-), multiplication (*), division (/)
8+
- **Expression Parsing**: Proper operator precedence and parentheses support
9+
- **Dual Transport Modes**:
10+
- **stdio** for local development and MCP Inspector
11+
- **streamable-http** for web deployments and containers
12+
- **Health Check Endpoint**: Health monitoring for HTTP deployments
13+
14+
## Transport Configuration
15+
16+
Simple environment-based configuration:
17+
18+
```bash
19+
TRANSPORT=stdio # For local development/debugging
20+
TRANSPORT=streamable-http # For web deployments (default)
21+
```
22+
23+
Default Behavior
24+
25+
- **Default**: `streamable-http` (if no TRANSPORT is set)
26+
- **Port**: 8080 (configurable via PORT environment variable)
927

1028
## Installation
1129

30+
Build the server:
31+
32+
```bash
33+
go build -o mcp-calculator-server server.go
34+
```
35+
36+
## Usage
37+
38+
### Local Development (stdio)
39+
1240
```bash
13-
go install github.com/yinebebt/mcp-calculator-server
41+
TRANSPORT=stdio ./mcp-calculator-server
1442
```
1543

16-
## Configuration
44+
### Web Deployment (streamable-http, default)
1745

18-
### Claude Desktop Configuration
46+
```bash
47+
./mcp-calculator-server
48+
# or explicitly:
49+
TRANSPORT=streamable-http PORT=8080 ./mcp-calculator-server
50+
```
1951

20-
Add this server to your Claude Desktop configuration file:
52+
### MCP Inspector Configuration
2153

22-
```json
23-
{
24-
"mcpServers": {
25-
"calculator": {
26-
"command": "/path/to/mcp-calculator-server"
27-
}
28-
}
29-
}
54+
```bash
55+
Command: /path/to/calculator-server
56+
Environment: TRANSPORT=stdio
3057
```
3158

32-
### Restart Claude Desktop
59+
### Testing with Client
3360

34-
After updating the configuration, restart Claude Desktop to load the new server.
61+
```bash
62+
go run -tags=client client.go ./calculator-server
63+
```
3564

36-
## Usage
65+
## HTTP Endpoints (streamable-http mode)
3766

38-
Once configured, you can ask Claude to perform calculations like "Can you calculate 2 + 3?"
67+
- **MCP Endpoint**: `http://localhost:8080/mcp`
68+
- **Health Check**: `http://localhost:8080/health`
3969

4070
## Tool Reference
4171

4272
### calculate
4373

44-
Performs basic mathematical operations.
74+
Performs mathematical operations with proper operator precedence and implicit multiplication.
4575

4676
**Parameters:**
47-
- `expression` (string, required): A mathematical expression to evaluate
4877

49-
**Supported operations:**
78+
- `expression` (string, required): Mathematical expression to evaluate
79+
80+
**Supported Operations:**
81+
5082
- Addition: `+`
5183
- Subtraction: `-`
5284
- Multiplication: `*`
5385
- Division: `/`
5486
- Parentheses: `()` for grouping
55-
- Negative numbers: `-5`, `(-3 + 2)`
87+
- Negative numbers: `-5`, `(-3 + 2)`

client.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type Client struct {
4545
func main() {
4646
if len(os.Args) < 2 {
4747
fmt.Println("Usage: go run -tags=client client.go <your-mcp-server-binary> [args...]")
48-
fmt.Println("Example: go run -tags=client client.go ./mcp-calculator-server")
48+
fmt.Println("Example: go run -tags=client client.go ./calculator-server")
4949
os.Exit(1)
5050
}
5151

@@ -55,6 +55,9 @@ func main() {
5555
args := os.Args[2:] // Skip program name and server binary
5656
cmd := exec.Command(os.Args[1], args...)
5757

58+
// Set environment to force stdio transport mode
59+
cmd.Env = append(os.Environ(), "TRANSPORT=stdio")
60+
5861
stdin, err := cmd.StdinPipe()
5962
if err != nil {
6063
fmt.Printf("Error creating stdin pipe: %v\n", err)
@@ -262,8 +265,9 @@ func (c *Client) printMenu() {
262265

263266
func (c *Client) testBasicCalculation() {
264267
fmt.Println("\nRunning calculation test...")
268+
265269
expr := "((2 + 3) * -4) / 2"
266-
result, err := c.callCalculator("((2 + 3) * -4) / 2")
270+
result, err := c.callCalculator(expr)
267271
if err != nil {
268272
fmt.Printf(" %s = ERROR: %v\n", expr, err)
269273
} else {

0 commit comments

Comments
 (0)