Skip to content

Latest commit

 

History

History
102 lines (79 loc) · 4.59 KB

File metadata and controls

102 lines (79 loc) · 4.59 KB

📘 Lösung der Aufgabe: Erweiterung deines Calculator MCP Servers mit einem Quadratwurzel-Tool

Überblick

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.

🛠️ Implementierung des Quadratwurzel-Tools

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)

🔍 So funktioniert es

  • Importiere das 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.
  • 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.