docs: add code fences to Example: docstring blocks (#2104)
This commit is contained in:
parent
c0328540c9
commit
cb07adeca3
6 changed files with 83 additions and 18 deletions
|
|
@ -44,26 +44,38 @@ def create_mcp_http_client(
|
|||
The returned AsyncClient must be used as a context manager to ensure
|
||||
proper cleanup of connections.
|
||||
|
||||
Examples:
|
||||
# Basic usage with MCP defaults
|
||||
Example:
|
||||
Basic usage with MCP defaults:
|
||||
|
||||
```python
|
||||
async with create_mcp_http_client() as client:
|
||||
response = await client.get("https://api.example.com")
|
||||
```
|
||||
|
||||
# With custom headers
|
||||
With custom headers:
|
||||
|
||||
```python
|
||||
headers = {"Authorization": "Bearer token"}
|
||||
async with create_mcp_http_client(headers) as client:
|
||||
response = await client.get("/endpoint")
|
||||
```
|
||||
|
||||
# With both custom headers and timeout
|
||||
With both custom headers and timeout:
|
||||
|
||||
```python
|
||||
timeout = httpx.Timeout(60.0, read=300.0)
|
||||
async with create_mcp_http_client(headers, timeout) as client:
|
||||
response = await client.get("/long-request")
|
||||
```
|
||||
|
||||
# With authentication
|
||||
With authentication:
|
||||
|
||||
```python
|
||||
from httpx import BasicAuth
|
||||
auth = BasicAuth(username="user", password="pass")
|
||||
async with create_mcp_http_client(headers, timeout, auth) as client:
|
||||
response = await client.get("/protected-endpoint")
|
||||
```
|
||||
"""
|
||||
# Set MCP defaults
|
||||
kwargs: dict[str, Any] = {"follow_redirects": True}
|
||||
|
|
|
|||
|
|
@ -65,6 +65,7 @@ class UrlElicitationRequiredError(MCPError):
|
|||
must complete one or more URL elicitations before the request can be processed.
|
||||
|
||||
Example:
|
||||
```python
|
||||
raise UrlElicitationRequiredError([
|
||||
ElicitRequestURLParams(
|
||||
message="Authorization required for your files",
|
||||
|
|
@ -72,6 +73,7 @@ class UrlElicitationRequiredError(MCPError):
|
|||
elicitation_id="auth-001"
|
||||
)
|
||||
])
|
||||
```
|
||||
"""
|
||||
|
||||
def __init__(self, elicitations: list[ElicitRequestURLParams], message: str | None = None):
|
||||
|
|
|
|||
|
|
@ -72,8 +72,10 @@ async def cancel_task(
|
|||
- Task is already in a terminal state (completed, failed, cancelled)
|
||||
|
||||
Example:
|
||||
```python
|
||||
async def handle_cancel(ctx, params: CancelTaskRequestParams) -> CancelTaskResult:
|
||||
return await cancel_task(store, params.task_id)
|
||||
```
|
||||
"""
|
||||
task = await store.get_task(task_id)
|
||||
if task is None:
|
||||
|
|
|
|||
|
|
@ -18,11 +18,13 @@ def get_display_name(obj: Tool | Resource | Prompt | ResourceTemplate | Implemen
|
|||
For other objects: title > name
|
||||
|
||||
Example:
|
||||
```python
|
||||
# In a client displaying available tools
|
||||
tools = await session.list_tools()
|
||||
for tool in tools.tools:
|
||||
display_name = get_display_name(tool)
|
||||
print(f"Available tool: {display_name}")
|
||||
```
|
||||
|
||||
Args:
|
||||
obj: An MCP object with name and optional title fields
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ class ResponseRouter(Protocol):
|
|||
and deliver the response/error to the appropriate handler.
|
||||
|
||||
Example:
|
||||
```python
|
||||
class TaskResultHandler(ResponseRouter):
|
||||
def route_response(self, request_id, response):
|
||||
resolver = self._pending_requests.pop(request_id, None)
|
||||
|
|
@ -32,6 +33,7 @@ class ResponseRouter(Protocol):
|
|||
resolver.set_result(response)
|
||||
return True
|
||||
return False
|
||||
```
|
||||
"""
|
||||
|
||||
def route_response(self, request_id: RequestId, response: dict[str, Any]) -> bool:
|
||||
|
|
|
|||
|
|
@ -60,8 +60,10 @@ class RequestResponder(Generic[ReceiveRequestT, SendResultT]):
|
|||
cancellation handling:
|
||||
|
||||
Example:
|
||||
```python
|
||||
with request_responder as resp:
|
||||
await resp.respond(result)
|
||||
```
|
||||
|
||||
The context manager ensures:
|
||||
1. Proper cancellation scope setup and cleanup
|
||||
|
|
|
|||
Loading…
Reference in a new issue