From 5ab3a37e71354427325859a7415d1c74fb33d99a Mon Sep 17 00:00:00 2001 From: Steve Billings Date: Wed, 13 Aug 2025 15:22:02 -0400 Subject: [PATCH 1/3] Added a FastMCP example that shows how to send log messages and progress reports back to the client during tool execution. --- examples/fastmcp/logging_and_progress.py | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 examples/fastmcp/logging_and_progress.py diff --git a/examples/fastmcp/logging_and_progress.py b/examples/fastmcp/logging_and_progress.py new file mode 100644 index 000000000..c055d177f --- /dev/null +++ b/examples/fastmcp/logging_and_progress.py @@ -0,0 +1,28 @@ +""" +FastMCP Echo Server +""" + +import asyncio + +from mcp.server.fastmcp import Context, FastMCP + +# Create server +mcp = FastMCP("Echo Server") + + +@mcp.tool() +async def echo(text: str, ctx: Context) -> str: + """Echo the input text. Send log messages and progress updates.""" + await ctx.report_progress(progress=0, total=100) + await ctx.info("Starting to process echo for input: " + text) + + await asyncio.sleep(2) + + await ctx.info("Halfway through processing echo for input: " + text) + await ctx.report_progress(progress=50, total=100) + + await asyncio.sleep(2) + + await ctx.info("Finished processing echo for input: " + text) + await ctx.report_progress(progress=100, total=100) + return text From 8daf7f0c30be9d1c64a83d93e0464b5604dd9c61 Mon Sep 17 00:00:00 2001 From: Steve Billings Date: Wed, 13 Aug 2025 16:41:39 -0400 Subject: [PATCH 2/3] added brief sleep at the end of the tool function to highlight the potential race condition affecting notifications sent near the end of a tool function. --- examples/fastmcp/logging_and_progress.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/fastmcp/logging_and_progress.py b/examples/fastmcp/logging_and_progress.py index c055d177f..f12f10ebf 100644 --- a/examples/fastmcp/logging_and_progress.py +++ b/examples/fastmcp/logging_and_progress.py @@ -25,4 +25,9 @@ async def echo(text: str, ctx: Context) -> str: await ctx.info("Finished processing echo for input: " + text) await ctx.report_progress(progress=100, total=100) + + # Progress notifications are process asynchronously by the client. + # A small delay here helps ensure the last notification is processed by the client. + await asyncio.sleep(0.1) + return text From cd8bdbc2f49ce2357d563b2e0d5eff158b1b6748 Mon Sep 17 00:00:00 2001 From: Steve Billings Date: Wed, 13 Aug 2025 16:42:12 -0400 Subject: [PATCH 3/3] improved server name and comments --- examples/fastmcp/logging_and_progress.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/fastmcp/logging_and_progress.py b/examples/fastmcp/logging_and_progress.py index f12f10ebf..91c2b806d 100644 --- a/examples/fastmcp/logging_and_progress.py +++ b/examples/fastmcp/logging_and_progress.py @@ -1,5 +1,5 @@ """ -FastMCP Echo Server +FastMCP Echo Server that sends log messages and progress updates to the client """ import asyncio @@ -7,12 +7,12 @@ from mcp.server.fastmcp import Context, FastMCP # Create server -mcp = FastMCP("Echo Server") +mcp = FastMCP("Echo Server with logging and progress updates") @mcp.tool() async def echo(text: str, ctx: Context) -> str: - """Echo the input text. Send log messages and progress updates.""" + """Echo the input text sending log messages and progress updates during processing.""" await ctx.report_progress(progress=0, total=100) await ctx.info("Starting to process echo for input: " + text)