From 7ede47729b6d2aeb4e90c0daf8624f4631656330 Mon Sep 17 00:00:00 2001 From: "marvin-context-protocol[bot]" <225465937+marvin-context-protocol[bot]@users.noreply.github.com> Date: Wed, 10 Sep 2025 23:21:59 +0000 Subject: [PATCH 1/2] Fix Context docstring examples to use async syntax Updates both main docstring example and session_id property example to use async functions with proper await syntax for Context methods. Co-authored-by: William Easton --- src/fastmcp/server/context.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/fastmcp/server/context.py b/src/fastmcp/server/context.py index 65af5076b..97ca70dc3 100644 --- a/src/fastmcp/server/context.py +++ b/src/fastmcp/server/context.py @@ -85,18 +85,18 @@ class Context: ```python @server.tool - def my_tool(x: int, ctx: Context) -> str: + async def my_tool(x: int, ctx: Context) -> str: # Log messages to the client - ctx.info(f"Processing {x}") - ctx.debug("Debug info") - ctx.warning("Warning message") - ctx.error("Error message") + await ctx.info(f"Processing {x}") + await ctx.debug("Debug info") + await ctx.warning("Warning message") + await ctx.error("Error message") # Report progress - ctx.report_progress(50, 100, "Processing") + await ctx.report_progress(50, 100, "Processing") # Access resources - data = ctx.read_resource("resource://data") + data = await ctx.read_resource("resource://data") # Get request info request_id = ctx.request_id @@ -261,7 +261,7 @@ class Context: Example: ```python @server.tool - def store_data(data: dict, ctx: Context) -> str: + async def store_data(data: dict, ctx: Context) -> str: session_id = ctx.session_id redis_client.set(f"session:{session_id}:data", json.dumps(data)) return f"Data stored for session {session_id}" From 0f80cbc7228f8568c1b0b0b1270faea5a84a6bc2 Mon Sep 17 00:00:00 2001 From: William Easton Date: Thu, 11 Sep 2025 09:35:23 -0500 Subject: [PATCH 2/2] Fix sync/async docstrings --- src/fastmcp/server/context.py | 2 +- src/fastmcp/server/server.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/fastmcp/server/context.py b/src/fastmcp/server/context.py index 97ca70dc3..40fac923a 100644 --- a/src/fastmcp/server/context.py +++ b/src/fastmcp/server/context.py @@ -261,7 +261,7 @@ class Context: Example: ```python @server.tool - async def store_data(data: dict, ctx: Context) -> str: + def store_data(data: dict, ctx: Context) -> str: session_id = ctx.session_id redis_client.set(f"session:{session_id}:data", json.dumps(data)) return f"Data stored for session {session_id}" diff --git a/src/fastmcp/server/server.py b/src/fastmcp/server/server.py index 0d77d26a5..170f5d10e 100644 --- a/src/fastmcp/server/server.py +++ b/src/fastmcp/server/server.py @@ -1210,8 +1210,8 @@ class FastMCP(Generic[LifespanResultT]): return f"Weather for {city}" @server.resource("resource://{city}/weather") - def get_weather_with_context(city: str, ctx: Context) -> str: - ctx.info(f"Fetching weather for {city}") + async def get_weather_with_context(city: str, ctx: Context) -> str: + await ctx.info(f"Fetching weather for {city}") return f"Weather for {city}" @server.resource("resource://{city}/weather") @@ -1386,8 +1386,8 @@ class FastMCP(Generic[LifespanResultT]): ] @server.prompt() - def analyze_with_context(table_name: str, ctx: Context) -> list[Message]: - ctx.info(f"Analyzing table {table_name}") + async def analyze_with_context(table_name: str, ctx: Context) -> list[Message]: + await ctx.info(f"Analyzing table {table_name}") schema = read_table_schema(table_name) return [ { @@ -1397,7 +1397,7 @@ class FastMCP(Generic[LifespanResultT]): ] @server.prompt("custom_name") - def analyze_file(path: str) -> list[Message]: + async def analyze_file(path: str) -> list[Message]: content = await read_file(path) return [ {