docs: add code fences to Example: docstring blocks (#2104)
This commit is contained in:
parent
c0328540c9
commit
cb07adeca3
7 changed files with 83 additions and 18 deletions
|
|
@ -160,6 +160,7 @@ class Experimental:
|
|||
RuntimeError: If task support is not enabled or task_metadata is missing
|
||||
|
||||
Example:
|
||||
```python
|
||||
async def handle_tool(ctx: RequestContext, params: CallToolRequestParams) -> CallToolResult:
|
||||
async def work(task: ServerTaskContext) -> CallToolResult:
|
||||
result = await task.elicit(
|
||||
|
|
@ -170,6 +171,7 @@ class Experimental:
|
|||
return CallToolResult(content=[TextContent(text="Done" if confirmed else "Cancelled")])
|
||||
|
||||
return await ctx.experimental.run_task(work)
|
||||
```
|
||||
|
||||
WARNING: This API is experimental and may change without notice.
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -56,6 +56,7 @@ class ServerTaskContext:
|
|||
- Status notifications via the session
|
||||
|
||||
Example:
|
||||
```python
|
||||
async def my_task_work(task: ServerTaskContext) -> CallToolResult:
|
||||
await task.update_status("Starting...")
|
||||
|
||||
|
|
@ -68,6 +69,7 @@ class ServerTaskContext:
|
|||
return CallToolResult(content=[TextContent(text="Done!")])
|
||||
else:
|
||||
return CallToolResult(content=[TextContent(text="Cancelled")])
|
||||
```
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
|
|
|
|||
|
|
@ -31,14 +31,20 @@ class TaskSupport:
|
|||
- Manages a task group for background task execution
|
||||
|
||||
Example:
|
||||
# Simple in-memory setup
|
||||
server.experimental.enable_tasks()
|
||||
Simple in-memory setup:
|
||||
|
||||
# Custom store/queue for distributed systems
|
||||
```python
|
||||
server.experimental.enable_tasks()
|
||||
```
|
||||
|
||||
Custom store/queue for distributed systems:
|
||||
|
||||
```python
|
||||
server.experimental.enable_tasks(
|
||||
store=RedisTaskStore(redis_url),
|
||||
queue=RedisTaskMessageQueue(redis_url),
|
||||
)
|
||||
```
|
||||
"""
|
||||
|
||||
store: TaskStore
|
||||
|
|
|
|||
|
|
@ -118,14 +118,20 @@ class ExperimentalHandlers(Generic[LifespanResultT]):
|
|||
The TaskSupport configuration object
|
||||
|
||||
Example:
|
||||
# Simple in-memory setup
|
||||
server.experimental.enable_tasks()
|
||||
Simple in-memory setup:
|
||||
|
||||
# Custom store/queue for distributed systems
|
||||
```python
|
||||
server.experimental.enable_tasks()
|
||||
```
|
||||
|
||||
Custom store/queue for distributed systems:
|
||||
|
||||
```python
|
||||
server.experimental.enable_tasks(
|
||||
store=RedisTaskStore(redis_url),
|
||||
queue=RedisTaskMessageQueue(redis_url),
|
||||
)
|
||||
```
|
||||
|
||||
WARNING: This API is experimental and may change without notice.
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -535,19 +535,25 @@ class MCPServer(Generic[LifespanResultT]):
|
|||
- If False, unconditionally creates an unstructured tool
|
||||
|
||||
Example:
|
||||
```python
|
||||
@server.tool()
|
||||
def my_tool(x: int) -> str:
|
||||
return str(x)
|
||||
```
|
||||
|
||||
```python
|
||||
@server.tool()
|
||||
async def tool_with_context(x: int, ctx: Context) -> str:
|
||||
await ctx.info(f"Processing {x}")
|
||||
return str(x)
|
||||
```
|
||||
|
||||
```python
|
||||
@server.tool()
|
||||
async def async_tool(x: int, context: Context) -> str:
|
||||
await context.report_progress(50, 100)
|
||||
return str(x)
|
||||
```
|
||||
"""
|
||||
# Check if user passed function directly instead of calling decorator
|
||||
if callable(name):
|
||||
|
|
@ -579,12 +585,14 @@ class MCPServer(Generic[LifespanResultT]):
|
|||
- context: Optional CompletionContext with previously resolved arguments
|
||||
|
||||
Example:
|
||||
```python
|
||||
@mcp.completion()
|
||||
async def handle_completion(ref, argument, context):
|
||||
if isinstance(ref, ResourceTemplateReference):
|
||||
# Return completions based on ref, argument, and context
|
||||
return Completion(values=["option1", "option2"])
|
||||
return None
|
||||
```
|
||||
"""
|
||||
|
||||
def decorator(func: _CallableT) -> _CallableT:
|
||||
|
|
@ -647,6 +655,7 @@ class MCPServer(Generic[LifespanResultT]):
|
|||
meta: Optional metadata dictionary for the resource
|
||||
|
||||
Example:
|
||||
```python
|
||||
@server.resource("resource://my-resource")
|
||||
def get_data() -> str:
|
||||
return "Hello, world!"
|
||||
|
|
@ -664,6 +673,7 @@ class MCPServer(Generic[LifespanResultT]):
|
|||
async def get_weather(city: str) -> str:
|
||||
data = await fetch_weather(city)
|
||||
return f"Weather for {city}: {data}"
|
||||
```
|
||||
"""
|
||||
# Check if user passed function directly instead of calling decorator
|
||||
if callable(uri):
|
||||
|
|
@ -747,6 +757,7 @@ class MCPServer(Generic[LifespanResultT]):
|
|||
icons: Optional list of icons for the prompt
|
||||
|
||||
Example:
|
||||
```python
|
||||
@server.prompt()
|
||||
def analyze_table(table_name: str) -> list[Message]:
|
||||
schema = read_table_schema(table_name)
|
||||
|
|
@ -772,6 +783,7 @@ class MCPServer(Generic[LifespanResultT]):
|
|||
}
|
||||
}
|
||||
]
|
||||
```
|
||||
"""
|
||||
# Check if user passed function directly instead of calling decorator
|
||||
if callable(name):
|
||||
|
|
@ -813,9 +825,11 @@ class MCPServer(Generic[LifespanResultT]):
|
|||
include_in_schema: Whether to include in OpenAPI schema, defaults to True
|
||||
|
||||
Example:
|
||||
```python
|
||||
@server.custom_route("/health", methods=["GET"])
|
||||
async def health_check(request: Request) -> Response:
|
||||
return JSONResponse({"status": "ok"})
|
||||
```
|
||||
"""
|
||||
|
||||
def decorator( # pragma: no cover
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
This module implements a Server-Sent Events (SSE) transport layer for MCP servers.
|
||||
|
||||
Example usage:
|
||||
```
|
||||
Example:
|
||||
```python
|
||||
# Create an SSE transport at an endpoint
|
||||
sse = SseServerTransport("/messages/")
|
||||
|
||||
|
|
@ -27,7 +27,7 @@ Example usage:
|
|||
# Create and run Starlette app
|
||||
starlette_app = Starlette(routes=routes)
|
||||
uvicorn.run(starlette_app, host="127.0.0.1", port=port)
|
||||
```
|
||||
```
|
||||
|
||||
Note: The handle_sse function must return a Response to avoid a
|
||||
"TypeError: 'NoneType' object is not callable" error when client disconnects. The example above returns
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ This module provides functionality for creating an stdio-based transport layer
|
|||
that can be used to communicate with an MCP client through standard input/output
|
||||
streams.
|
||||
|
||||
Example usage:
|
||||
```
|
||||
Example:
|
||||
```python
|
||||
async def run_server():
|
||||
async with stdio_server() as (read_stream, write_stream):
|
||||
# read_stream contains incoming JSONRPCMessages from stdin
|
||||
|
|
@ -14,7 +14,7 @@ Example usage:
|
|||
await server.run(read_stream, write_stream, init_options)
|
||||
|
||||
anyio.run(run_server)
|
||||
```
|
||||
```
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
|
|
|||
Loading…
Reference in a new issue