91 lines
3.6 KiB
Markdown
91 lines
3.6 KiB
Markdown
|
|
# 📘 Assignment Solution: Extending Your Calculator MCP Server with a Square Root Tool
|
|
|
|
## Overview
|
|
In this assignment, you enhanced your calculator MCP server by adding a new tool that calculates the square root of a number. This addition allows your AI agent to handle more advanced mathematical queries, such as "What is the square root of 16?" or "Calculate √49," using natural language prompts.
|
|
|
|
## 🛠️ Implementing the Square Root Tool
|
|
To add this functionality, you defined a new tool function in your server.py file. Here's the implementation:
|
|
|
|
```python
|
|
"""
|
|
Sample MCP Calculator Server implementation in Python.
|
|
|
|
This module demonstrates how to create a simple MCP server with calculator tools
|
|
that can perform basic arithmetic operations (add, subtract, multiply, divide).
|
|
"""
|
|
|
|
from mcp.server.fastmcp import FastMCP
|
|
import math
|
|
|
|
server = FastMCP("calculator")
|
|
|
|
@server.tool()
|
|
def add(a: float, b: float) -> float:
|
|
"""Add two numbers together and return the result."""
|
|
return a + b
|
|
|
|
@server.tool()
|
|
def subtract(a: float, b: float) -> float:
|
|
"""Subtract b from a and return the result."""
|
|
return a - b
|
|
|
|
@server.tool()
|
|
def multiply(a: float, b: float) -> float:
|
|
"""Multiply two numbers together and return the result."""
|
|
return a * b
|
|
|
|
@server.tool()
|
|
def divide(a: float, b: float) -> float:
|
|
"""
|
|
Divide a by b and return the result.
|
|
|
|
Raises:
|
|
ValueError: If b is zero
|
|
"""
|
|
if b == 0:
|
|
raise ValueError("Cannot divide by zero")
|
|
return a / b
|
|
|
|
@server.tool()
|
|
def sqrt(a: float) -> float:
|
|
"""
|
|
Return the square root of a.
|
|
|
|
Raises:
|
|
ValueError: If a is negative.
|
|
"""
|
|
if a < 0:
|
|
raise ValueError("Cannot compute the square root of a negative number.")
|
|
return math.sqrt(a)
|
|
```
|
|
|
|
## 🔍 How It Works
|
|
|
|
- **Import the `math` module**: To perform mathematical operations beyond basic arithmetic, Python provides the built-in `math` module. This module includes a variety of mathematical functions and constants. By importing it using `import math`, you gain access to functions like `math.sqrt()`, which computes the square root of a number.
|
|
- **Function Definition**: The `@server.tool()` decorator registers the `sqrt` function as a tool accessible by your AI agent.
|
|
- **Input Parameter**: The function accepts a single argument `a` of type `float`.
|
|
- **Error Handling**: If `a` is negative, the function raises a `ValueError` to prevent computing the square root of a negative number, which is not supported by the `math.sqrt()` function.
|
|
- **Return Value**: For non-negative inputs, the function returns the square root of `a` using Python's built-in `math.sqrt()` method.
|
|
|
|
## 🔄 Restarting the Server
|
|
After adding the new `sqrt` tool, it's essential to restart your MCP server to ensure the agent recognizes and can utilize the newly added functionality.
|
|
|
|
## 💬 Example Prompts to Test the New Tool
|
|
Here are some natural language prompts you can use to test the square root functionality:
|
|
|
|
- "What is the square root of 25?"
|
|
- "Calculate the square root of 81."
|
|
- "Find the square root of 0."
|
|
- "What is the square root of 2.25?"
|
|
|
|
These prompts should trigger the agent to invoke the `sqrt` tool and return the correct results.
|
|
|
|
## ✅ Summary
|
|
By completing this assignment, you've:
|
|
|
|
- Extended your calculator MCP server with a new `sqrt` tool.
|
|
- Enabled your AI agent to handle square root calculations through natural language prompts.
|
|
- Practiced adding new tools and restarting the server to integrate additional functionalities.
|
|
|
|
Feel free to experiment further by adding more mathematical tools, such as exponentiation or logarithmic functions, to continue enhancing your agent's capabilities! |