MCPcopy
hub / github.com/modelcontextprotocol/python-sdk / send_request

Method send_request

src/mcp/client/session.py:275–319  ·  view source on GitHub ↗

Send a request and wait for its typed result. Args: metadata: Streamable HTTP resumption hints. Raises: MCPError: Error response, read timeout, or connection closed. RuntimeError: Called before entering the context manager.

(
        self,
        request: types.ClientRequest,
        result_type: type[ReceiveResultT] | TypeAdapter[ReceiveResultT],
        request_read_timeout_seconds: float | None = None,
        metadata: ClientMessageMetadata | None = None,
        progress_callback: ProgressFnT | None = None,
    )

Source from the content-addressed store, hash-verified

273 return result
274
275 async def send_request(
276 self,
277 request: types.ClientRequest,
278 result_type: type[ReceiveResultT] | TypeAdapter[ReceiveResultT],
279 request_read_timeout_seconds: float | None = None,
280 metadata: ClientMessageMetadata | None = None,
281 progress_callback: ProgressFnT | None = None,
282 ) -> ReceiveResultT:
283 """Send a request and wait for its typed result.
284
285 Args:
286 metadata: Streamable HTTP resumption hints.
287
288 Raises:
289 MCPError: Error response, read timeout, or connection closed.
290 RuntimeError: Called before entering the context manager.
291 """
292 data = request.model_dump(by_alias=True, mode="json", exclude_none=True)
293 method: str = data["method"]
294 opts: CallOptions = {}
295 self._stamp(data, opts)
296 timeout = (
297 request_read_timeout_seconds
298 if request_read_timeout_seconds is not None
299 else self._session_read_timeout_seconds
300 )
301 if timeout is not None:
302 opts["timeout"] = timeout
303 if progress_callback is not None:
304 opts["on_progress"] = progress_callback
305 if metadata is not None:
306 if metadata.resumption_token is not None:
307 opts["resumption_token"] = metadata.resumption_token
308 if metadata.on_resumption_token_update is not None:
309 opts["on_resumption_token"] = metadata.on_resumption_token_update
310 raw = await self._dispatcher.send_raw_request(method, data.get("params"), opts)
311 # Literal fallback covers pre-handshake and stateless; matches runner.py.
312 version = self._negotiated_version or "2025-11-25"
313 try:
314 _methods.validate_server_result(method, version, raw)
315 except KeyError:
316 pass
317 if isinstance(result_type, TypeAdapter):
318 return result_type.validate_python(raw, by_name=False)
319 return result_type.model_validate(raw, by_name=False)
320
321 async def send_notification(self, notification: types.ClientNotification) -> None:
322 """Send a one-way notification. Usable before entering the context manager.

Callers 15

initializeMethod · 0.95
send_pingMethod · 0.95
set_logging_levelMethod · 0.95
list_resourcesMethod · 0.95
read_resourceMethod · 0.95
subscribe_resourceMethod · 0.95
unsubscribe_resourceMethod · 0.95
call_toolMethod · 0.95
list_promptsMethod · 0.95
get_promptMethod · 0.95
completeMethod · 0.95

Calls 1

send_raw_requestMethod · 0.45