You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Tools can optionally receive a Context object by including a parameter with the `Context` type annotation. This context is automatically injected by the FastMCP framework and provides access to MCP capabilities:
The Context object gives your tools and resources access to MCP capabilities:
528
+
The Context object is automatically injected into tool and resource functions that request it via type hints. It provides access to MCP capabilities like logging, progress reporting, resource reading, user interaction, and request metadata.
529
+
530
+
#### Getting Context in Functions
531
+
532
+
To use context in a tool or resource function, add a parameter with the `Context` type annotation:
533
+
534
+
```python
535
+
from mcp.server.fastmcp import Context, FastMCP
536
+
537
+
mcp = FastMCP(name="Context Example")
538
+
539
+
540
+
@mcp.tool()
541
+
asyncdefmy_tool(x: int, ctx: Context) -> str:
542
+
"""Tool that uses context capabilities."""
543
+
# The context parameter can have any name as long as it's type-annotated
544
+
returnawait process_with_context(x, ctx)
545
+
```
546
+
547
+
#### Context Properties and Methods
548
+
549
+
The Context object provides the following capabilities:
550
+
551
+
**Request Information:**
552
+
553
+
-`ctx.request_id` - Unique ID for the current request
554
+
-`ctx.client_id` - Client ID if available
555
+
-`ctx.session` - Access to the underlying session for advanced usage
0 commit comments