mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-21 21:14:17 +02:00
get_starlette_request → get_http_request
This commit is contained in:
parent
b672468d4f
commit
bb7d41a53d
2 changed files with 11 additions and 5 deletions
|
|
@ -253,7 +253,9 @@ async def request_info(ctx: Context) -> dict:
|
|||
|
||||
### Advanced Access
|
||||
|
||||
For advanced use cases, you can access the underlying MCP session and FastMCP server.
|
||||
For advanced use cases, you can access the underlying MCP session, FastMCP server, and HTTP requests.
|
||||
|
||||
#### Accessing FastMCP and Sessions
|
||||
|
||||
```python
|
||||
@mcp.tool()
|
||||
|
|
@ -269,13 +271,17 @@ async def advanced_tool(ctx: Context) -> str:
|
|||
return f"Server: {server_name}"
|
||||
```
|
||||
|
||||
#### Accessing HTTP Requests
|
||||
|
||||
<VersionBadge version="2.2.7" />
|
||||
|
||||
For web applications, you can access the underlying HTTP request:
|
||||
|
||||
```python
|
||||
@mcp.tool()
|
||||
async def handle_web_request(ctx: Context) -> dict:
|
||||
"""Access HTTP request information from the Starlette request."""
|
||||
request = ctx.get_starlette_request()
|
||||
request = ctx.get_http_request()
|
||||
|
||||
# Access HTTP headers, query parameters, etc.
|
||||
user_agent = request.headers.get("user-agent", "Unknown")
|
||||
|
|
@ -288,12 +294,12 @@ async def handle_web_request(ctx: Context) -> dict:
|
|||
}
|
||||
```
|
||||
|
||||
**Advanced Properties:**
|
||||
#### Advanced Properties Reference
|
||||
|
||||
- **`ctx.fastmcp -> FastMCP`**: Access the server instance the context belongs to
|
||||
- **`ctx.session`**: Access the raw `mcp.server.session.ServerSession` object
|
||||
- **`ctx.request_context`**: Access the raw `mcp.shared.context.RequestContext` object
|
||||
- **`ctx.get_starlette_request() -> Request`**: Access the active Starlette request object (when running with a web server)
|
||||
- **`ctx.get_http_request() -> Request`**: Access the active Starlette request object (when running with a web server)
|
||||
|
||||
<Warning>
|
||||
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.
|
||||
|
|
|
|||
|
|
@ -227,7 +227,7 @@ class Context(BaseModel, Generic[ServerSessionT, LifespanContextT]):
|
|||
|
||||
return result.content
|
||||
|
||||
def get_starlette_request(self) -> Request:
|
||||
def get_http_request(self) -> Request:
|
||||
"""Get the active starlette request."""
|
||||
request = get_current_starlette_request()
|
||||
if request is None:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue