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

Function serve_connection

src/mcp/server/runner.py:417–437  ·  view source on GitHub ↗

Drive ``dispatcher`` until the underlying channel closes. The loop-mode driver: builds the kernel, hands `on_request`/`on_notify` to `dispatcher.run()`, and tears down `connection.exit_stack` (shielded) on the way out. The entry constructs the `Connection`; this only consumes it.

(
    server: Server[LifespanT],
    dispatcher: Dispatcher[Any],
    *,
    connection: Connection,
    lifespan_state: LifespanT,
    init_options: InitializationOptions | None = None,
    task_status: anyio.abc.TaskStatus[None] = anyio.TASK_STATUS_IGNORED,
)

Source from the content-addressed store, hash-verified

415
416
417async def serve_connection(
418 server: Server[LifespanT],
419 dispatcher: Dispatcher[Any],
420 *,
421 connection: Connection,
422 lifespan_state: LifespanT,
423 init_options: InitializationOptions | None = None,
424 task_status: anyio.abc.TaskStatus[None] = anyio.TASK_STATUS_IGNORED,
425) -> None:
426 """Drive ``dispatcher`` until the underlying channel closes.
427
428 The loop-mode driver: builds the kernel, hands `on_request`/`on_notify`
429 to `dispatcher.run()`, and tears down `connection.exit_stack` (shielded)
430 on the way out. The entry constructs the `Connection`; this only consumes
431 it.
432 """
433 runner = ServerRunner(server, connection, lifespan_state, init_options=init_options)
434 try:
435 await dispatcher.run(runner.on_request, runner.on_notify, task_status=task_status)
436 finally:
437 await aclose_shielded(connection)
438
439
440async def serve_loop(

Callers 2

run_stateless_serverMethod · 0.90
serve_loopFunction · 0.85

Calls 3

ServerRunnerClass · 0.85
aclose_shieldedFunction · 0.85
runMethod · 0.45

Tested by

no test coverage detected