Skip to content

Commit 79b4c4a

Browse files
authored
Merge pull request #444 from microsoft/calfix
Calfix
2 parents c0da9ec + 86be25d commit 79b4c4a

File tree

4 files changed

+102
-47
lines changed

4 files changed

+102
-47
lines changed
Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,54 @@
1-
# Sample
2-
3-
This is a Python sample for an MCP Server
4-
5-
Here's what the calculator portion looks like:
6-
7-
```python
8-
@mcp.tool()
9-
def add(a: float, b: float) -> float:
10-
"""Add two numbers together and return the result."""
11-
return a + b
12-
13-
@mcp.tool()
14-
def subtract(a: float, b: float) -> float:
15-
"""Subtract b from a and return the result."""
16-
return a - b
17-
18-
@mcp.tool()
19-
def multiply(a: float, b: float) -> float:
20-
"""Multiply two numbers together and return the result."""
21-
return a * b
22-
23-
@mcp.tool()
24-
def divide(a: float, b: float) -> float:
25-
"""
26-
Divide a by b and return the result.
27-
28-
Raises:
29-
ValueError: If b is zero
30-
"""
31-
if b == 0:
32-
raise ValueError("Cannot divide by zero")
33-
return a / b
34-
```
1+
# MCP Calculator Server (Python)
2+
3+
4+
5+
A simple Model Context Protocol (MCP) server implementation in Python that provides basic calculator functionality.
356

36-
## Install
377

38-
Run the following command:
8+
## Installation
9+
10+
Install the required dependencies:
3911

4012
```bash
41-
pip install mcp
13+
pip install -r requirements.txt
4214
```
4315

44-
## Run
16+
Or install the MCP Python SDK directly:
17+
18+
```bash
19+
pip install mcp>=1.18.0
20+
```
21+
22+
## Usage
23+
24+
### Running the Server
25+
26+
The server is designed to be used by MCP clients (like Claude Desktop). To start the server:
4527

4628
```bash
4729
python mcp_calculator_server.py
48-
```
30+
```
31+
32+
**Note**: When run directly in a terminal, you'll see JSON-RPC validation errors. This is normal behavior - the server is waiting for properly formatted MCP client messages.
33+
34+
### Testing the Functions
35+
36+
To test that the calculator functions work correctly:
37+
38+
```bash
39+
python test_calculator.py
40+
```
41+
42+
## Troubleshooting
43+
44+
### Import Errors
45+
46+
If you see `ModuleNotFoundError: No module named 'mcp'`, install the MCP Python SDK:
47+
48+
```bash
49+
pip install mcp>=1.18.0
50+
```
51+
52+
### JSON-RPC Errors When Running Directly
53+
54+
Errors like "Invalid JSON: EOF while parsing a value" when running the server directly are expected. The server needs MCP client messages, not direct terminal input.

03-GettingStarted/samples/python/mcp_calculator_server.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@
66
that can perform basic arithmetic operations (add, subtract, multiply, divide).
77
"""
88

9-
import asyncio
109
from mcp.server.fastmcp import FastMCP
11-
from mcp.server.transports.stdio import serve_stdio
1210

1311
# Create a FastMCP server
14-
mcp = FastMCP(
15-
name="Calculator MCP Server",
16-
version="1.0.0"
17-
)
12+
mcp = FastMCP("Calculator MCP Server")
1813

1914
@mcp.tool()
2015
def add(a: float, b: float) -> float:
@@ -44,5 +39,5 @@ def divide(a: float, b: float) -> float:
4439
return a / b
4540

4641
if __name__ == "__main__":
47-
# Start the server with stdio transport
48-
asyncio.run(serve_stdio(mcp))
42+
# Start the server
43+
mcp.run()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mcp>=1.18.0
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test script for the MCP Calculator Server.
4+
5+
This script tests the calculator functions to ensure they work correctly
6+
before running the MCP server.
7+
"""
8+
9+
import sys
10+
import os
11+
12+
# Add the current directory to the path so we can import the calculator server
13+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
14+
15+
def test_calculator_functions():
16+
"""Test all calculator functions."""
17+
# Import the functions from the calculator server
18+
from mcp_calculator_server import add, subtract, multiply, divide
19+
20+
print("Testing calculator functions...")
21+
22+
# Test addition
23+
result = add(5, 3)
24+
assert result == 8, f"Expected 8, got {result}"
25+
print("✓ Addition test passed")
26+
27+
# Test subtraction
28+
result = subtract(10, 4)
29+
assert result == 6, f"Expected 6, got {result}"
30+
print("✓ Subtraction test passed")
31+
32+
# Test multiplication
33+
result = multiply(7, 6)
34+
assert result == 42, f"Expected 42, got {result}"
35+
print("✓ Multiplication test passed")
36+
37+
# Test division
38+
result = divide(15, 3)
39+
assert result == 5, f"Expected 5, got {result}"
40+
print("✓ Division test passed")
41+
42+
# Test division by zero
43+
try:
44+
divide(10, 0)
45+
assert False, "Expected ValueError for division by zero"
46+
except ValueError as e:
47+
assert str(e) == "Cannot divide by zero", f"Expected 'Cannot divide by zero', got '{str(e)}'"
48+
print("✓ Division by zero test passed")
49+
50+
print("\nAll calculator function tests passed! ✅")
51+
52+
if __name__ == "__main__":
53+
test_calculator_functions()

0 commit comments

Comments
 (0)