Skip to content

Commit 81d58bd

Browse files
committed
refactor: follow Outbound.send_raw_request rename in PeerMixin/BaseContext
PeerMixin methods and Peer/BaseContext now call/expose send_raw_request. The typed send_request lands on Connection/Context in the next commit.
1 parent de81288 commit 81d58bd

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

src/mcp/shared/context.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""`BaseContext` — the user-facing per-request context.
22
33
Composition over a `DispatchContext`: forwards the transport metadata, the
4-
back-channel (`send_request`/`notify`), progress reporting, and the cancel
4+
back-channel (`send_raw_request`/`notify`), progress reporting, and the cancel
55
event. Adds `meta` (the inbound request's `_meta` field).
66
77
Satisfies `Outbound`, so `PeerMixin` works on it (the server-side `Context`
@@ -56,7 +56,7 @@ def meta(self) -> RequestParamsMeta | None:
5656
"""The inbound request's ``_meta`` field, if present."""
5757
return self._meta
5858

59-
async def send_request(
59+
async def send_raw_request(
6060
self,
6161
method: str,
6262
params: Mapping[str, Any] | None,
@@ -68,7 +68,7 @@ async def send_request(
6868
MCPError: The peer responded with an error.
6969
NoBackChannelError: ``can_send_request`` is ``False``.
7070
"""
71-
return await self._dctx.send_request(method, params, opts)
71+
return await self._dctx.send_raw_request(method, params, opts)
7272

7373
async def notify(self, method: str, params: Mapping[str, Any] | None) -> None:
7474
"""Send a notification to the peer on the back-channel."""

src/mcp/shared/peer.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""Typed MCP request sugar over an `Outbound`.
22
33
`PeerMixin` defines the server-to-client request methods (sampling, elicitation,
4-
roots, ping) once. Any class that satisfies `Outbound` (i.e. has `send_request`
4+
roots, ping) once. Any class that satisfies `Outbound` (i.e. has `send_raw_request`
55
and `notify`) can mix it in and get the typed methods for free — `Context`,
66
`Connection`, `Client`, or the bare `Peer` wrapper below.
77
88
The mixin does no capability gating: it builds the params, calls
9-
``self.send_request(method, params)``, and parses the result into the typed
10-
model. Gating (and `NoBackChannelError`) is the host's `send_request`'s job.
9+
``self.send_raw_request(method, params)``, and parses the result into the typed
10+
model. Gating (and `NoBackChannelError`) is the host's `send_raw_request`'s job.
1111
"""
1212

1313
from collections.abc import Mapping
@@ -43,7 +43,7 @@ class PeerMixin:
4343
"""Typed server-to-client request methods.
4444
4545
Each method constrains ``self`` to `Outbound` so the mixin can be applied
46-
to anything with ``send_request``/``notify`` — pyright checks the host
46+
to anything with ``send_raw_request``/``notify`` — pyright checks the host
4747
class structurally at the call site.
4848
"""
4949

@@ -113,7 +113,7 @@ async def sample(
113113
tools=tools,
114114
tool_choice=tool_choice,
115115
)
116-
result = await self.send_request("sampling/createMessage", _dump(params), opts)
116+
result = await self.send_raw_request("sampling/createMessage", _dump(params), opts)
117117
if tools is not None:
118118
return CreateMessageResultWithTools.model_validate(result)
119119
return CreateMessageResult.model_validate(result)
@@ -131,7 +131,7 @@ async def elicit_form(
131131
NoBackChannelError: No back-channel for server-initiated requests.
132132
"""
133133
params = ElicitRequestFormParams(message=message, requested_schema=requested_schema)
134-
result = await self.send_request("elicitation/create", _dump(params), opts)
134+
result = await self.send_raw_request("elicitation/create", _dump(params), opts)
135135
return ElicitResult.model_validate(result)
136136

137137
async def elicit_url(
@@ -148,7 +148,7 @@ async def elicit_url(
148148
NoBackChannelError: No back-channel for server-initiated requests.
149149
"""
150150
params = ElicitRequestURLParams(message=message, url=url, elicitation_id=elicitation_id)
151-
result = await self.send_request("elicitation/create", _dump(params), opts)
151+
result = await self.send_raw_request("elicitation/create", _dump(params), opts)
152152
return ElicitResult.model_validate(result)
153153

154154
async def list_roots(self: Outbound, opts: CallOptions | None = None) -> ListRootsResult:
@@ -158,7 +158,7 @@ async def list_roots(self: Outbound, opts: CallOptions | None = None) -> ListRoo
158158
MCPError: The peer responded with an error.
159159
NoBackChannelError: No back-channel for server-initiated requests.
160160
"""
161-
result = await self.send_request("roots/list", None, opts)
161+
result = await self.send_raw_request("roots/list", None, opts)
162162
return ListRootsResult.model_validate(result)
163163

164164
async def ping(self: Outbound, opts: CallOptions | None = None) -> None:
@@ -168,7 +168,7 @@ async def ping(self: Outbound, opts: CallOptions | None = None) -> None:
168168
MCPError: The peer responded with an error.
169169
NoBackChannelError: No back-channel for server-initiated requests.
170170
"""
171-
await self.send_request("ping", None, opts)
171+
await self.send_raw_request("ping", None, opts)
172172

173173

174174
class Peer(PeerMixin):
@@ -182,13 +182,13 @@ class Peer(PeerMixin):
182182
def __init__(self, outbound: Outbound) -> None:
183183
self._outbound = outbound
184184

185-
async def send_request(
185+
async def send_raw_request(
186186
self,
187187
method: str,
188188
params: Mapping[str, Any] | None,
189189
opts: CallOptions | None = None,
190190
) -> dict[str, Any]:
191-
return await self._outbound.send_request(method, params, opts)
191+
return await self._outbound.send_raw_request(method, params, opts)
192192

193193
async def notify(self, method: str, params: Mapping[str, Any] | None) -> None:
194194
await self._outbound.notify(method, params)

tests/shared/test_context.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Tests for `BaseContext`.
22
33
`BaseContext` is composition over a `DispatchContext` — it forwards
4-
``transport``/``cancel_requested``/``send_request``/``notify``/``progress``
4+
``transport``/``cancel_requested``/``send_raw_request``/``notify``/``progress``
55
and adds ``meta``. It must satisfy `Outbound` so `PeerMixin` works on it.
66
"""
77

@@ -33,7 +33,7 @@ async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] |
3333

3434
async with running_pair(direct_pair, server_on_request=server_on_request) as (client, *_):
3535
with anyio.fail_after(5):
36-
await client.send_request("t", None)
36+
await client.send_raw_request("t", None)
3737
bctx = captured[0]
3838
assert bctx.transport.kind == "direct"
3939
assert isinstance(bctx.cancel_requested, anyio.Event)
@@ -42,13 +42,13 @@ async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] |
4242

4343

4444
@pytest.mark.anyio
45-
async def test_base_context_send_request_and_notify_forward_to_dispatch_context():
45+
async def test_base_context_send_raw_request_and_notify_forward_to_dispatch_context():
4646
crec = Recorder()
4747
c_req, c_notify = echo_handlers(crec)
4848

4949
async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] | None) -> dict[str, Any]:
5050
bctx = BaseContext(ctx)
51-
sample = await bctx.send_request("sampling/createMessage", {"x": 1})
51+
sample = await bctx.send_raw_request("sampling/createMessage", {"x": 1})
5252
await bctx.notify("notifications/message", {"level": "info"})
5353
return {"sample": sample}
5454

@@ -59,7 +59,7 @@ async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] |
5959
client_on_notify=c_notify,
6060
) as (client, *_):
6161
with anyio.fail_after(5):
62-
result = await client.send_request("tools/call", None)
62+
result = await client.send_raw_request("tools/call", None)
6363
await crec.notified.wait()
6464
assert crec.requests == [("sampling/createMessage", {"x": 1})]
6565
assert crec.notifications == [("notifications/message", {"level": "info"})]
@@ -80,7 +80,7 @@ async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] |
8080

8181
async with running_pair(direct_pair, server_on_request=server_on_request) as (client, *_):
8282
with anyio.fail_after(5):
83-
await client.send_request("t", None, {"on_progress": on_progress})
83+
await client.send_raw_request("t", None, {"on_progress": on_progress})
8484
assert received == [(0.5, 1.0, "halfway")]
8585

8686

@@ -99,7 +99,7 @@ async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] |
9999
direct_pair, server_on_request=server_on_request, client_on_request=c_req, client_on_notify=c_notify
100100
) as (client, *_):
101101
with anyio.fail_after(5):
102-
await client.send_request("t", None)
102+
await client.send_raw_request("t", None)
103103
assert crec.requests == [("ping", None)]
104104

105105

@@ -112,4 +112,4 @@ async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] |
112112

113113
async with running_pair(direct_pair, server_on_request=server_on_request) as (client, *_):
114114
with anyio.fail_after(5):
115-
await client.send_request("t", None)
115+
await client.send_raw_request("t", None)

0 commit comments

Comments
 (0)