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.
To add this functionality, you defined a new tool function in your server.py file. Here's the implementation:
"""
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)- Import the
mathmodule: To perform mathematical operations beyond basic arithmetic, Python provides the built-inmathmodule. This module includes a variety of mathematical functions and constants. By importing it usingimport math, you gain access to functions likemath.sqrt(), which computes the square root of a number. - Function Definition: The
@server.tool()decorator registers thesqrtfunction as a tool accessible by your AI agent. - Input Parameter: The function accepts a single argument
aof typefloat. - Error Handling: If
ais negative, the function raises aValueErrorto prevent computing the square root of a negative number, which is not supported by themath.sqrt()function. - Return Value: For non-negative inputs, the function returns the square root of
ausing Python's built-inmath.sqrt()method.
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.
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.
By completing this assignment, you've:
- Extended your calculator MCP server with a new
sqrttool. - 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!