Skip to content

Commit cb07ade

Browse files
docs: add code fences to Example: docstring blocks (#2104)
1 parent c032854 commit cb07ade

File tree

18 files changed

+83
-18
lines changed

18 files changed

+83
-18
lines changed

src/mcp/client/experimental/task_handlers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,13 @@ class ExperimentalTaskHandlers:
187187
WARNING: These APIs are experimental and may change without notice.
188188
189189
Example:
190+
```python
190191
handlers = ExperimentalTaskHandlers(
191192
get_task=my_get_task_handler,
192193
list_tasks=my_list_tasks_handler,
193194
)
194195
session = ClientSession(..., experimental_task_handlers=handlers)
196+
```
195197
"""
196198

197199
# Pure task request handlers

src/mcp/client/experimental/tasks.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
WARNING: These APIs are experimental and may change without notice.
66
77
Example:
8+
```python
89
# Call a tool as a task
910
result = await session.experimental.call_tool_as_task("tool_name", {"arg": "value"})
1011
task_id = result.task.task_id
@@ -21,6 +22,7 @@
2122
2223
# Cancel a task
2324
await session.experimental.cancel_task(task_id)
25+
```
2426
"""
2527

2628
from collections.abc import AsyncIterator
@@ -72,6 +74,7 @@ async def call_tool_as_task(
7274
CreateTaskResult containing the task reference
7375
7476
Example:
77+
```python
7578
# Create task
7679
result = await session.experimental.call_tool_as_task(
7780
"long_running_tool", {"input": "data"}
@@ -87,6 +90,7 @@ async def call_tool_as_task(
8790
8891
# Get result
8992
final = await session.experimental.get_task_result(task_id, CallToolResult)
93+
```
9094
"""
9195
return await self._session.send_request(
9296
types.CallToolRequest(
@@ -189,6 +193,7 @@ async def poll_task(self, task_id: str) -> AsyncIterator[types.GetTaskResult]:
189193
GetTaskResult for each poll
190194
191195
Example:
196+
```python
192197
async for status in session.experimental.poll_task(task_id):
193198
print(f"Status: {status.status}")
194199
if status.status == "input_required":
@@ -197,6 +202,7 @@ async def poll_task(self, task_id: str) -> AsyncIterator[types.GetTaskResult]:
197202
198203
# Task is now terminal, get the result
199204
result = await session.experimental.get_task_result(task_id, CallToolResult)
205+
```
200206
"""
201207
async for status in poll_until_terminal(self.get_task, task_id):
202208
yield status

src/mcp/client/session.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,10 @@ def experimental(self) -> ExperimentalClientFeatures:
206206
These APIs are experimental and may change without notice.
207207
208208
Example:
209+
```python
209210
status = await session.experimental.get_task(task_id)
210211
result = await session.experimental.get_task_result(task_id, CallToolResult)
212+
```
211213
"""
212214
if self._experimental_features is None:
213215
self._experimental_features = ExperimentalClientFeatures(self)

src/mcp/client/session_group.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,14 @@ class ClientSessionGroup:
9191
For auxiliary handlers, such as resource subscription, this is delegated to
9292
the client and can be accessed via the session.
9393
94-
Example Usage:
94+
Example:
95+
```python
9596
name_fn = lambda name, server_info: f"{(server_info.name)}_{name}"
9697
async with ClientSessionGroup(component_name_hook=name_fn) as group:
9798
for server_param in server_params:
9899
await group.connect_to_server(server_param)
99100
...
100-
101+
```
101102
"""
102103

103104
class _ComponentNames(BaseModel):

src/mcp/server/experimental/request_context.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ async def run_task(
160160
RuntimeError: If task support is not enabled or task_metadata is missing
161161
162162
Example:
163+
```python
163164
async def handle_tool(ctx: RequestContext, params: CallToolRequestParams) -> CallToolResult:
164165
async def work(task: ServerTaskContext) -> CallToolResult:
165166
result = await task.elicit(
@@ -170,6 +171,7 @@ async def work(task: ServerTaskContext) -> CallToolResult:
170171
return CallToolResult(content=[TextContent(text="Done" if confirmed else "Cancelled")])
171172
172173
return await ctx.experimental.run_task(work)
174+
```
173175
174176
WARNING: This API is experimental and may change without notice.
175177
"""

src/mcp/server/experimental/task_context.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class ServerTaskContext:
5656
- Status notifications via the session
5757
5858
Example:
59+
```python
5960
async def my_task_work(task: ServerTaskContext) -> CallToolResult:
6061
await task.update_status("Starting...")
6162
@@ -68,6 +69,7 @@ async def my_task_work(task: ServerTaskContext) -> CallToolResult:
6869
return CallToolResult(content=[TextContent(text="Done!")])
6970
else:
7071
return CallToolResult(content=[TextContent(text="Cancelled")])
72+
```
7173
"""
7274

7375
def __init__(

src/mcp/server/experimental/task_support.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,20 @@ class TaskSupport:
3131
- Manages a task group for background task execution
3232
3333
Example:
34-
# Simple in-memory setup
34+
Simple in-memory setup:
35+
36+
```python
3537
server.experimental.enable_tasks()
38+
```
39+
40+
Custom store/queue for distributed systems:
3641
37-
# Custom store/queue for distributed systems
42+
```python
3843
server.experimental.enable_tasks(
3944
store=RedisTaskStore(redis_url),
4045
queue=RedisTaskMessageQueue(redis_url),
4146
)
47+
```
4248
"""
4349

4450
store: TaskStore

src/mcp/server/lowlevel/experimental.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,20 @@ def enable_tasks(
118118
The TaskSupport configuration object
119119
120120
Example:
121-
# Simple in-memory setup
121+
Simple in-memory setup:
122+
123+
```python
122124
server.experimental.enable_tasks()
125+
```
126+
127+
Custom store/queue for distributed systems:
123128
124-
# Custom store/queue for distributed systems
129+
```python
125130
server.experimental.enable_tasks(
126131
store=RedisTaskStore(redis_url),
127132
queue=RedisTaskMessageQueue(redis_url),
128133
)
134+
```
129135
130136
WARNING: This API is experimental and may change without notice.
131137
"""

src/mcp/server/mcpserver/server.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,19 +535,25 @@ def tool(
535535
- If False, unconditionally creates an unstructured tool
536536
537537
Example:
538+
```python
538539
@server.tool()
539540
def my_tool(x: int) -> str:
540541
return str(x)
542+
```
541543
544+
```python
542545
@server.tool()
543546
async def tool_with_context(x: int, ctx: Context) -> str:
544547
await ctx.info(f"Processing {x}")
545548
return str(x)
549+
```
546550
551+
```python
547552
@server.tool()
548553
async def async_tool(x: int, context: Context) -> str:
549554
await context.report_progress(50, 100)
550555
return str(x)
556+
```
551557
"""
552558
# Check if user passed function directly instead of calling decorator
553559
if callable(name):
@@ -579,12 +585,14 @@ def completion(self):
579585
- context: Optional CompletionContext with previously resolved arguments
580586
581587
Example:
588+
```python
582589
@mcp.completion()
583590
async def handle_completion(ref, argument, context):
584591
if isinstance(ref, ResourceTemplateReference):
585592
# Return completions based on ref, argument, and context
586593
return Completion(values=["option1", "option2"])
587594
return None
595+
```
588596
"""
589597

590598
def decorator(func: _CallableT) -> _CallableT:
@@ -647,6 +655,7 @@ def resource(
647655
meta: Optional metadata dictionary for the resource
648656
649657
Example:
658+
```python
650659
@server.resource("resource://my-resource")
651660
def get_data() -> str:
652661
return "Hello, world!"
@@ -664,6 +673,7 @@ def get_weather(city: str) -> str:
664673
async def get_weather(city: str) -> str:
665674
data = await fetch_weather(city)
666675
return f"Weather for {city}: {data}"
676+
```
667677
"""
668678
# Check if user passed function directly instead of calling decorator
669679
if callable(uri):
@@ -747,6 +757,7 @@ def prompt(
747757
icons: Optional list of icons for the prompt
748758
749759
Example:
760+
```python
750761
@server.prompt()
751762
def analyze_table(table_name: str) -> list[Message]:
752763
schema = read_table_schema(table_name)
@@ -772,6 +783,7 @@ async def analyze_file(path: str) -> list[Message]:
772783
}
773784
}
774785
]
786+
```
775787
"""
776788
# Check if user passed function directly instead of calling decorator
777789
if callable(name):
@@ -813,9 +825,11 @@ def custom_route(
813825
include_in_schema: Whether to include in OpenAPI schema, defaults to True
814826
815827
Example:
828+
```python
816829
@server.custom_route("/health", methods=["GET"])
817830
async def health_check(request: Request) -> Response:
818831
return JSONResponse({"status": "ok"})
832+
```
819833
"""
820834

821835
def decorator( # pragma: no cover

src/mcp/server/sse.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
This module implements a Server-Sent Events (SSE) transport layer for MCP servers.
44
5-
Example usage:
6-
```
5+
Example:
6+
```python
77
# Create an SSE transport at an endpoint
88
sse = SseServerTransport("/messages/")
99
@@ -27,7 +27,7 @@ async def handle_sse(request):
2727
# Create and run Starlette app
2828
starlette_app = Starlette(routes=routes)
2929
uvicorn.run(starlette_app, host="127.0.0.1", port=port)
30-
```
30+
```
3131
3232
Note: The handle_sse function must return a Response to avoid a
3333
"TypeError: 'NoneType' object is not callable" error when client disconnects. The example above returns

0 commit comments

Comments
 (0)