Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
f64894e
1
Parent(s): 0d33002
get_starlette_request → get_http_request
Browse files- docs/servers/context.mdx +10 -4
- src/fastmcp/server/context.py +1 -1
docs/servers/context.mdx
CHANGED
|
@@ -253,7 +253,9 @@ async def request_info(ctx: Context) -> dict:
|
|
| 253 |
|
| 254 |
### Advanced Access
|
| 255 |
|
| 256 |
-
For advanced use cases, you can access the underlying MCP session
|
|
|
|
|
|
|
| 257 |
|
| 258 |
```python
|
| 259 |
@mcp.tool()
|
|
@@ -269,13 +271,17 @@ async def advanced_tool(ctx: Context) -> str:
|
|
| 269 |
return f"Server: {server_name}"
|
| 270 |
```
|
| 271 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
For web applications, you can access the underlying HTTP request:
|
| 273 |
|
| 274 |
```python
|
| 275 |
@mcp.tool()
|
| 276 |
async def handle_web_request(ctx: Context) -> dict:
|
| 277 |
"""Access HTTP request information from the Starlette request."""
|
| 278 |
-
request = ctx.
|
| 279 |
|
| 280 |
# Access HTTP headers, query parameters, etc.
|
| 281 |
user_agent = request.headers.get("user-agent", "Unknown")
|
|
@@ -288,12 +294,12 @@ async def handle_web_request(ctx: Context) -> dict:
|
|
| 288 |
}
|
| 289 |
```
|
| 290 |
|
| 291 |
-
|
| 292 |
|
| 293 |
- **`ctx.fastmcp -> FastMCP`**: Access the server instance the context belongs to
|
| 294 |
- **`ctx.session`**: Access the raw `mcp.server.session.ServerSession` object
|
| 295 |
- **`ctx.request_context`**: Access the raw `mcp.shared.context.RequestContext` object
|
| 296 |
-
- **`ctx.
|
| 297 |
|
| 298 |
<Warning>
|
| 299 |
Direct use of `session` or `request_context` requires understanding the low-level MCP Python SDK and may be less stable than using the methods provided directly on the `Context` object.
|
|
|
|
| 253 |
|
| 254 |
### Advanced Access
|
| 255 |
|
| 256 |
+
For advanced use cases, you can access the underlying MCP session, FastMCP server, and HTTP requests.
|
| 257 |
+
|
| 258 |
+
#### Accessing FastMCP and Sessions
|
| 259 |
|
| 260 |
```python
|
| 261 |
@mcp.tool()
|
|
|
|
| 271 |
return f"Server: {server_name}"
|
| 272 |
```
|
| 273 |
|
| 274 |
+
#### Accessing HTTP Requests
|
| 275 |
+
|
| 276 |
+
<VersionBadge version="2.2.7" />
|
| 277 |
+
|
| 278 |
For web applications, you can access the underlying HTTP request:
|
| 279 |
|
| 280 |
```python
|
| 281 |
@mcp.tool()
|
| 282 |
async def handle_web_request(ctx: Context) -> dict:
|
| 283 |
"""Access HTTP request information from the Starlette request."""
|
| 284 |
+
request = ctx.get_http_request()
|
| 285 |
|
| 286 |
# Access HTTP headers, query parameters, etc.
|
| 287 |
user_agent = request.headers.get("user-agent", "Unknown")
|
|
|
|
| 294 |
}
|
| 295 |
```
|
| 296 |
|
| 297 |
+
#### Advanced Properties Reference
|
| 298 |
|
| 299 |
- **`ctx.fastmcp -> FastMCP`**: Access the server instance the context belongs to
|
| 300 |
- **`ctx.session`**: Access the raw `mcp.server.session.ServerSession` object
|
| 301 |
- **`ctx.request_context`**: Access the raw `mcp.shared.context.RequestContext` object
|
| 302 |
+
- **`ctx.get_http_request() -> Request`**: Access the active Starlette request object (when running with a web server)
|
| 303 |
|
| 304 |
<Warning>
|
| 305 |
Direct use of `session` or `request_context` requires understanding the low-level MCP Python SDK and may be less stable than using the methods provided directly on the `Context` object.
|
src/fastmcp/server/context.py
CHANGED
|
@@ -227,7 +227,7 @@ class Context(BaseModel, Generic[ServerSessionT, LifespanContextT]):
|
|
| 227 |
|
| 228 |
return result.content
|
| 229 |
|
| 230 |
-
def
|
| 231 |
"""Get the active starlette request."""
|
| 232 |
request = get_current_starlette_request()
|
| 233 |
if request is None:
|
|
|
|
| 227 |
|
| 228 |
return result.content
|
| 229 |
|
| 230 |
+
def get_http_request(self) -> Request:
|
| 231 |
"""Get the active starlette request."""
|
| 232 |
request = get_current_starlette_request()
|
| 233 |
if request is None:
|