In dieser Aufgabe hast du deinen Calculator MCP Server erweitert, indem du ein neues Tool hinzugefügt hast, das die Quadratwurzel einer Zahl berechnet. Dadurch kann dein KI-Agent komplexere mathematische Anfragen verarbeiten, wie zum Beispiel „Wie lautet die Quadratwurzel von 16?“ oder „Berechne √49“ – und zwar über natürliche Sprachbefehle.
Um diese Funktion hinzuzufügen, hast du eine neue Tool-Funktion in deiner server.py Datei definiert. Hier ist die Umsetzung:
"""
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)- Importiere das
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. - Ermögliche deinem KI-Agenten, Quadratwurzel-Berechnungen über natürliche Sprachbefehle durchzuführen.
- Übe das Hinzufügen neuer Tools und das Neustarten des Servers, um zusätzliche Funktionen zu integrieren.
Probiere gerne aus, weitere mathematische Werkzeuge hinzuzufügen, wie Potenz- oder Logarithmusfunktionen, um die Fähigkeiten deines Agenten weiter zu verbessern!
Haftungsausschluss:
Dieses Dokument wurde mit dem KI-Übersetzungsdienst Co-op Translator übersetzt. Obwohl wir uns um Genauigkeit bemühen, beachten Sie bitte, dass automatisierte Übersetzungen Fehler oder Ungenauigkeiten enthalten können. Das Originaldokument in seiner Ursprungssprache ist als maßgebliche Quelle zu betrachten. Für wichtige Informationen wird eine professionelle menschliche Übersetzung empfohlen. Wir übernehmen keine Haftung für Missverständnisse oder Fehlinterpretationen, die durch die Nutzung dieser Übersetzung entstehen.