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

Function serve_one

src/mcp/server/runner.py:473–496  ·  view source on GitHub ↗

Handle a single request ``(method, params)`` and return its result dict. The single-exchange driver: builds the kernel, runs `on_request` once under `dctx`, and tears down `connection.exit_stack` (shielded) on the way out. The entry constructs the (born-ready) `Connection` and the `dctx

(
    server: Server[LifespanT],
    dctx: DispatchContext[TransportContext],
    method: str,
    params: Mapping[str, Any] | None,
    *,
    connection: Connection,
    lifespan_state: LifespanT,
)

Source from the content-addressed store, hash-verified

471
472
473async def serve_one(
474 server: Server[LifespanT],
475 dctx: DispatchContext[TransportContext],
476 method: str,
477 params: Mapping[str, Any] | None,
478 *,
479 connection: Connection,
480 lifespan_state: LifespanT,
481) -> dict[str, Any]:
482 """Handle a single request ``(method, params)`` and return its result dict.
483
484 The single-exchange driver: builds the kernel, runs `on_request` once under
485 `dctx`, and tears down `connection.exit_stack` (shielded) on the way out.
486 The entry constructs the (born-ready) `Connection` and the `dctx`; this
487 only consumes them.
488
489 Raises whatever the handler chain raises (`MCPError` / `ValidationError` /
490 unmapped); callers own the exception-to-wire mapping.
491 """
492 runner = ServerRunner(server, connection, lifespan_state)
493 try:
494 return await runner.on_request(dctx, method, params)
495 finally:
496 await aclose_shielded(connection)
497
498
499def modern_on_request(server: Server[LifespanT], lifespan_state: LifespanT) -> OnRequest:

Calls 3

on_requestMethod · 0.95
ServerRunnerClass · 0.85
aclose_shieldedFunction · 0.85