0Pricing
MCP Academy · Aula

Uma ferramenta de calculadora, do início ao fim

Crie uma ferramenta de aritmética pequena, mas completa.

Uma ferramenta de calculadora, do início ao fim é uma aula grátis de MCP Academy no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de MCP Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de MCP Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

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. ✅

Perguntas Frequentes

A aula “Uma ferramenta de calculadora, do início ao fim” é grátis?

Sim — o texto completo de “Uma ferramenta de calculadora, do início ao fim” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de MCP Academy, atualize para CoddyKit PRO. O curso de MCP Academy inclui 4 aulas no total.

O que vou aprender em “Uma ferramenta de calculadora, do início ao fim”?

Crie uma ferramenta de aritmética pequena, mas completa. Você pratica MCP Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar MCP Academy?

Nenhuma experiência prévia é necessária. MCP Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.

Quanto tempo leva a aula “Uma ferramenta de calculadora, do início ao fim”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de MCP Academy?

Sim. Cada aula de MCP Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Dê um bom nome e uma boa descrição à ferramenta
  2. Declarar parâmetros com dicas de tipo
  3. Retornar resultados úteis
  4. Uma ferramenta de calculadora, do início ao fim
← Voltar para MCP Academy