Skip to content

Commit fda5736

Browse files
committed
refactor: move attribute docstrings out of constructor bodies
Connection's public attributes are now declared in the class body with their docstrings under the declarations; __init__ only assigns. The private _inline_methods docstring becomes a comment above the assignment.
1 parent dd2d770 commit fda5736

2 files changed

Lines changed: 38 additions & 25 deletions

File tree

src/mcp/server/connection.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,39 @@ class Connection(TypedServerRequestMixin):
4949
remains `None` (no handshake reaches a stateless connection).
5050
"""
5151

52+
has_standalone_channel: bool
53+
session_id: str | None
54+
55+
client_params: InitializeRequestParams | None
56+
"""The full `initialize` request params; `None` before initialization."""
57+
58+
protocol_version: str | None
59+
60+
initialized: anyio.Event
61+
"""Set when `notifications/initialized` arrives (matches TS `oninitialized`);
62+
the point from which the spec permits server-initiated requests beyond
63+
ping/logging. Pre-set on stateless connections."""
64+
65+
state: dict[str, Any]
66+
"""Per-connection scratch state; persists across requests on this connection."""
67+
68+
exit_stack: AsyncExitStack
69+
"""Per-connection teardown, unwound LIFO (shielded) when the connection
70+
closes. Push cleanup from handlers or middleware; exceptions are logged
71+
and swallowed."""
72+
5273
def __init__(self, outbound: Outbound, *, has_standalone_channel: bool, session_id: str | None = None) -> None:
5374
self._outbound = outbound
5475
self.has_standalone_channel = has_standalone_channel
55-
self.session_id: str | None = session_id
56-
57-
self.client_params: InitializeRequestParams | None = None
58-
"""The full `initialize` request params; `None` before initialization."""
59-
self.protocol_version: str | None = None
60-
self.initialized: anyio.Event = anyio.Event()
61-
"""Set when `notifications/initialized` arrives (matches TS `oninitialized`);
62-
the point from which the spec permits server-initiated requests beyond
63-
ping/logging. Pre-set on stateless connections."""
64-
65-
self.state: dict[str, Any] = {}
66-
"""Per-connection scratch state; persists across requests on this connection."""
67-
68-
self.exit_stack: AsyncExitStack = AsyncExitStack()
69-
"""Per-connection teardown, unwound LIFO (shielded) when the connection
70-
closes. Push cleanup from handlers or middleware; exceptions are logged
71-
and swallowed."""
76+
self.session_id = session_id
77+
78+
self.client_params = None
79+
self.protocol_version = None
80+
self.initialized = anyio.Event()
81+
82+
self.state = {}
83+
84+
self.exit_stack = AsyncExitStack()
7285

7386
@property
7487
def initialize_accepted(self) -> bool:

src/mcp/shared/jsonrpc_dispatcher.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,15 @@ def __init__(
259259
)
260260
self._peer_cancel_mode: PeerCancelMode = peer_cancel_mode
261261
self._raise_handler_exceptions = raise_handler_exceptions
262+
# Request methods handled inline in the read loop (awaited before the
263+
# next message is dequeued) instead of spawned concurrently. Use for
264+
# methods whose side effects must be observable to the next message,
265+
# e.g. `initialize`, so a pipelined follow-up sees the initialized state.
266+
# Only suitable for handlers that complete quickly, since inline handling
267+
# blocks dequeuing; a handler that awaits the peer (`send_raw_request`)
268+
# while inline will deadlock because the parked read loop cannot dequeue
269+
# the response.
262270
self._inline_methods = inline_methods
263-
"""Request methods handled inline in the read loop (awaited before the
264-
next message is dequeued) instead of spawned concurrently. Use for
265-
methods whose side effects must be observable to the next message,
266-
e.g. `initialize`, so a pipelined follow-up sees the initialized state.
267-
Only suitable for handlers that complete quickly, since inline handling
268-
blocks dequeuing; a handler that awaits the peer (`send_raw_request`)
269-
while inline will deadlock because the parked read loop cannot dequeue
270-
the response."""
271271

272272
self._next_id = 0
273273
self._pending: dict[RequestId, _Pending] = {}

0 commit comments

Comments
 (0)