0Pricing
MCP Academy · Lekcja

Narzędzie kalkulatora od początku do końca

Zbuduj małe, ale kompletne narzędzie do działań arytmetycznych.

Narzędzie kalkulatora od początku do końca to bezpłatna lekcja MCP Academy na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej MCP Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs MCP Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Build One Complete Tool

Let's combine everything into one small calculator tool, from a clear name to a clean result the model can trust. 🧮

Start with the Server

First create a FastMCP instance to hold the tool. Give it a name so clients can identify your server.

from mcp.server.fastmcp import FastMCP
mcp = FastMCP("calculator")

Name the Operation

Pick a precise name. We will add two numbers, so the function add reads clearly to both you and the model.

Declare Typed Parameters

Use type hints so the model sends numbers, not text. We accept two floats to allow decimals as well as whole values.

def add(a: float, b: float) -> float:
    ...

Write the Docstring

The docstring becomes the description, so state plainly what the tool does and what it returns.

def add(a: float, b: float) -> float:
    """Add two numbers and return the sum."""

Decorate It as a Tool

Add the tool decorator so FastMCP registers the function and advertises it to any connected client.

@mcp.tool()
def add(a: float, b: float) -> float:
    """Add two numbers."""
    return a + b

Return a Clear Result

Returning the sum works, but a labeled result reads better to the model and to the user who sees the answer.

    return a + b

Add a Second Tool

One tool per job, so create a separate divide tool rather than packing modes into add. Each stays simple to call.

@mcp.tool()
def divide(a: float, b: float) -> float:
    return a / b

Guard Against Bad Input

Division by zero would crash, so check first and return a clear error message the model can read and explain.

    if b == 0:
        return "Error: cannot divide by zero."

Run the Server

Finish with the run call so the server starts and listens for a client over stdio when you launch the file.

if __name__ == "__main__":
    mcp.run()

Test the Whole Tool

Launch it and let a client list your tools. Seeing add and divide with their descriptions confirms the wiring is correct.

Quick Check

Why give divide its own tool instead of a mode flag on add?

Recap

You built a full calculator tool: named clearly, typed parameters, docstring description, guarded errors, and a clean result the model trusts. ✅

Często zadawane pytania

Czy lekcja „Narzędzie kalkulatora od początku do końca” jest bezpłatna?

Tak — pełny tekst „Narzędzie kalkulatora od początku do końca” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu MCP Academy, przejdź na CoddyKit PRO. Kurs MCP Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Narzędzie kalkulatora od początku do końca”?

Zbuduj małe, ale kompletne narzędzie do działań arytmetycznych. Ćwiczysz MCP Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć MCP Academy?

Nie wymagamy żadnego doświadczenia. MCP Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Narzędzie kalkulatora od początku do końca”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji MCP Academy?

Tak. Każda lekcja MCP Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Dobre nazywanie i opisywanie narzędzi
  2. Deklarowanie parametrów za pomocą adnotacji typów
  3. Zwracanie użytecznych wyników
  4. Narzędzie kalkulatora od początku do końca
← Powrót do MCP Academy