Enviar progresso para tarefas longas
Transmita ao cliente atualizações sobre o percentual concluído.
Enviar progresso para tarefas longas é uma aula grátis de MCP Academy no CoddyKit. Esta é a aula 1 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.
Why Progress Matters
When a tool takes seconds or minutes, the client should not just hang. MCP lets your server send progress updates so the user sees movement.
The Progress Token
The client opts in by attaching a progressToken to the request. Without that token, your server simply skips sending updates.
Reach the Context Object
In the Python SDK, a tool receives a Context object. It is your handle for talking back to the client mid-call, including progress.
from mcp.server.fastmcp import Context
@mcp.tool()
async def crunch(n: int, ctx: Context) -> str:
return "done"Call report_progress
To push an update, you call ctx.report_progress. Each call tells the client how far along the work has come.
await ctx.report_progress(progress=3, total=10)Progress and Total
The total argument is the finish line. The client can divide progress by total to draw a percent-done bar for the user.
await ctx.report_progress(progress=5, total=10) # 50%Update Inside a Loop
The natural place to report is inside your work loop. After each chunk finishes, send one update so the bar climbs steadily.
for i, item in enumerate(items):
process(item)
await ctx.report_progress(i + 1, len(items))Progress Without a Total
Sometimes you cannot know the total upfront. You may omit it; the client then shows an indeterminate spinner instead of a precise bar.
await ctx.report_progress(progress=42) # total unknownAdd an Optional Message
Many SDK versions let you attach a short message to a progress call, like "Indexing files", giving the user friendly human context.
await ctx.report_progress(2, 5, message="Fetching pages")It Is a Notification
Progress travels as a JSON-RPC notification, so it has no reply and never blocks. Your tool keeps working while updates flow out.
Keep Updates Reasonable
Do not flood the client with thousands of tiny updates. Report at meaningful steps so the stream stays useful and lightweight.
Progress Plus a Result
Progress notifications are separate from your tool's return value. You stream updates as you go, then hand back the final result at the end.
await ctx.report_progress(10, 10)
return "index built"Quick Check
Test your grasp of how progress reporting begins.
Recap: Progress
You learned to call ctx.report_progress with progress and total, loop your updates, and let the client draw a live bar. Nice work!
Perguntas Frequentes
A aula “Enviar progresso para tarefas longas” é grátis?
Sim — o texto completo de “Enviar progresso para tarefas longas” é 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 “Enviar progresso para tarefas longas”?
Transmita ao cliente atualizações sobre o percentual concluído. 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 1 de 4.
Quanto tempo leva a aula “Enviar progresso para tarefas longas”?
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
- Enviar progresso para tarefas longas
- Lidar com solicitações de cancelamento
- Registro estruturado para o cliente
- Definir níveis de registro em tempo de execução