@@ -371,54 +371,54 @@ def get_context(self) -> Context[ServerSession, LifespanResultT, Request]:
371371 a Context parameter in your tool/resource functions. Use this method only
372372 when you need context access from code that isn't directly called by FastMCP.
373373
374- ## When to use this method
374+ You might call this method directly in:
375375
376- **Helper functions**: When context is needed in utility functions:
376+ - **Helper functions**
377377
378- ```python
379- mcp = FastMCP(name="example")
378+ ```python
379+ mcp = FastMCP(name="example")
380380
381- async def log_operation(operation: str):
382- # Get context when it's not injected
383- ctx = mcp.get_context()
384- await ctx.info(f"Performing operation: {operation}")
381+ async def log_operation(operation: str):
382+ # Get context when it's not injected
383+ ctx = mcp.get_context()
384+ await ctx.info(f"Performing operation: {operation}")
385385
386- @mcp.tool()
387- async def main_tool(data: str) -> str:
388- await log_operation("data_processing") # Helper needs context
389- return process_data(data)
390- ```
386+ @mcp.tool()
387+ async def main_tool(data: str) -> str:
388+ await log_operation("data_processing") # Helper needs context
389+ return process_data(data)
390+ ```
391391
392- **Callbacks and event handlers**: When context is needed in async callbacks:
392+ - **Callbacks** and ** event handlers** when context is needed in async callbacks
393393
394- ```python
395- async def progress_callback(current: int, total: int):
396- ctx = mcp.get_context() # Access context in callback
397- await ctx.report_progress(current, total)
394+ ```python
395+ async def progress_callback(current: int, total: int):
396+ ctx = mcp.get_context() # Access context in callback
397+ await ctx.report_progress(current, total)
398398
399- @mcp.tool()
400- async def long_operation(data: str) -> str:
401- return await process_with_callback(data, progress_callback)
402- ```
399+ @mcp.tool()
400+ async def long_operation(data: str) -> str:
401+ return await process_with_callback(data, progress_callback)
402+ ```
403403
404- **Class methods**: When context is needed in class-based code:
404+ - **Class methods** when context is needed in class-based code
405405
406- ```python
407- class DataProcessor:
408- def __init__(self, mcp_server: FastMCP):
409- self.mcp = mcp_server
410-
411- async def process_chunk(self, chunk: str) -> str:
412- ctx = self.mcp.get_context() # Get context in method
413- await ctx.debug(f"Processing chunk of size {len(chunk)}")
414- return processed_chunk
406+ ```python
407+ class DataProcessor:
408+ def __init__(self, mcp_server: FastMCP):
409+ self.mcp = mcp_server
415410
416- processor = DataProcessor(mcp)
411+ async def process_chunk(self, chunk: str) -> str:
412+ ctx = self.mcp.get_context() # Get context in method
413+ await ctx.debug(f"Processing chunk of size {len(chunk)}")
414+ return processed_chunk
417415
418- @mcp.tool()
419- async def process_data(data: str) -> str:
420- return await processor.process_chunk(data)
421- ```
416+ processor = DataProcessor(mcp)
417+
418+ @mcp.tool()
419+ async def process_data(data: str) -> str:
420+ return await processor.process_chunk(data)
421+ ```
422422
423423 Returns:
424424 [`Context`][mcp.server.fastmcp.Context] object for the current request
@@ -1247,7 +1247,7 @@ async def simple_tool(data: str) -> str:
12471247 # No context needed
12481248 return f"Processed: {data}"
12491249
1250- @mcp.tool()
1250+ @mcp.tool()
12511251 async def advanced_tool(data: str, ctx: Context) -> str:
12521252 # Context automatically injected
12531253 await ctx.info("Starting processing")
@@ -1271,7 +1271,7 @@ async def advanced_tool(data: str, ctx: Context) -> str:
12711271 ```python
12721272 await ctx.debug("Detailed debug information")
12731273 await ctx.info("General status updates")
1274- await ctx.warning("Important warnings")
1274+ await ctx.warning("Important warnings")
12751275 await ctx.error("Error conditions")
12761276 ```
12771277
@@ -1289,7 +1289,7 @@ async def advanced_tool(data: str, ctx: Context) -> str:
12891289 class UserPrefs(BaseModel):
12901290 format: str
12911291 detailed: bool
1292-
1292+
12931293 result = await ctx.elicit("How should I format the output?", UserPrefs)
12941294 if result.action == "accept":
12951295 format_data(data, result.data.format)
@@ -1325,12 +1325,12 @@ class ProcessingOptions(BaseModel):
13251325
13261326 @mcp.tool()
13271327 async def process_data(
1328- data: str,
1328+ data: str,
13291329 ctx: Context,
13301330 auto_format: bool = False
13311331 ) -> str:
13321332 await ctx.info(f"Starting to process {len(data)} characters")
1333-
1333+
13341334 # Get user preferences if not auto-formatting
13351335 if not auto_format:
13361336 if ctx.session.check_client_capability(
@@ -1348,18 +1348,18 @@ async def process_data(
13481348 format_type = "standard"
13491349 include_meta = False
13501350 else:
1351- format_type = "standard"
1351+ format_type = "standard"
13521352 include_meta = False
13531353 else:
13541354 format_type = "auto"
13551355 include_meta = True
1356-
1356+
13571357 # Process with progress updates
13581358 for i in range(0, len(data), 100):
13591359 chunk = data[i:i+100]
13601360 await ctx.report_progress(i, len(data), f"Processing chunk {i//100 + 1}")
13611361 # ... process chunk
1362-
1362+
13631363 await ctx.info(f"Processing complete with format: {format_type}")
13641364 return processed_data
13651365 ```
@@ -1418,10 +1418,10 @@ def request_context(
14181418 async def advanced_tool(data: str, ctx: Context) -> str:
14191419 # Access lifespan context directly
14201420 db = ctx.request_context.lifespan_context["database"]
1421-
1421+
14221422 # Access request metadata
14231423 progress_token = ctx.request_context.meta.progressToken if ctx.request_context.meta else None
1424-
1424+
14251425 return processed_data
14261426 ```
14271427 """
@@ -1470,19 +1470,19 @@ async def elicit(
14701470
14711471 This method enables interactive data collection from clients during tool processing.
14721472 The client may display the message to the user and collect a response according to
1473- the provided Pydantic schema, or if the client is an agent, it may automatically
1473+ the provided Pydantic schema, or if the client is an agent, it may automatically
14741474 generate an appropriate response. This is useful for gathering additional parameters,
14751475 user preferences, or confirmation before proceeding with operations.
14761476
1477- You typically access this method through the [`Context`][mcp.server.fastmcp.Context]
1477+ You typically access this method through the [`Context`][mcp.server.fastmcp.Context]
14781478 object injected into your FastMCP tool functions. Always check that the client
14791479 supports elicitation using [`check_client_capability`][mcp.server.session.ServerSession.check_client_capability]
14801480 before calling this method.
14811481
14821482 Args:
14831483 message: The prompt or question to present to the user. Should clearly explain
14841484 what information is being requested and why it's needed.
1485- schema: A Pydantic model class defining the expected response structure.
1485+ schema: A Pydantic model class defining the expected response structure.
14861486 According to the MCP specification, only primitive types (str, int, float, bool)
14871487 and simple containers (list, dict) are allowed - no complex nested objects.
14881488
@@ -1519,13 +1519,13 @@ async def process_data(data: str, ctx: Context) -> str:
15191519 ):
15201520 # Fall back to default processing
15211521 return process_with_defaults(data)
1522-
1522+
15231523 # Ask user for processing preferences
15241524 result = await ctx.elicit(
15251525 "How would you like me to process this data?",
15261526 ProcessingOptions
15271527 )
1528-
1528+
15291529 if result.action == "accept":
15301530 options = result.data
15311531 await ctx.info(f"Processing with format: {options.format}")
@@ -1546,12 +1546,12 @@ class ConfirmDelete(BaseModel):
15461546 @mcp.tool()
15471547 async def delete_files(pattern: str, ctx: Context) -> str:
15481548 files = find_matching_files(pattern)
1549-
1549+
15501550 result = await ctx.elicit(
15511551 f"About to delete {len(files)} files matching '{pattern}'. Continue?",
15521552 ConfirmDelete
15531553 )
1554-
1554+
15551555 if result.action == "accept" and result.data.confirm:
15561556 await ctx.info(f"Deletion confirmed: {result.data.reason}")
15571557 return delete_files(files)
@@ -1572,7 +1572,7 @@ async def configure_system(ctx: Context) -> str:
15721572 "How should I configure the system?",
15731573 UserChoice
15741574 )
1575-
1575+
15761576 match result.action:
15771577 case "accept":
15781578 choice = result.data
@@ -1642,10 +1642,10 @@ def request_id(self) -> str:
16421642 async def traceable_tool(data: str, ctx: Context) -> str:
16431643 # Log with request ID for traceability
16441644 print(f"Processing request {ctx.request_id}")
1645-
1645+
16461646 # Request ID is automatically included in Context methods
16471647 await ctx.info("Starting processing") # Links to this request
1648-
1648+
16491649 return processed_data
16501650 ```
16511651 """
@@ -1664,7 +1664,7 @@ def session(self) -> ServerSession:
16641664 which internally use this session with appropriate request linking.
16651665
16661666 Returns:
1667- [`ServerSession`][mcp.server.session.ServerSession]: The session for
1667+ [`ServerSession`][mcp.server.session.ServerSession]: The session for
16681668 communicating with the client and accessing advanced MCP features.
16691669
16701670 Examples:
@@ -1693,7 +1693,7 @@ async def advanced_tool(data: str, ctx: Context) -> str:
16931693 @mcp.tool()
16941694 async def update_resource(uri: str, ctx: Context) -> str:
16951695 # ... update the resource ...
1696-
1696+
16971697 # Notify client of resource changes
16981698 await ctx.session.send_resource_updated(AnyUrl(uri))
16991699 return "Resource updated"
0 commit comments