MCPcopy Index your code
hub / github.com/modelcontextprotocol/python-sdk / modern_on_request

Function modern_on_request

src/mcp/server/runner.py:499–520  ·  view source on GitHub ↗

Return an `OnRequest` callback that serves each call via `serve_one` with a fresh per-request `Connection`. Wire this into the server side of a `DirectDispatcher` peer-pair to drive an in-process server on the modern per-request-envelope path (each request carries protocol version, clie

(server: Server[LifespanT], lifespan_state: LifespanT)

Source from the content-addressed store, hash-verified

497
498
499def modern_on_request(server: Server[LifespanT], lifespan_state: LifespanT) -> OnRequest:
500 """Return an `OnRequest` callback that serves each call via `serve_one` with a fresh per-request `Connection`.
501
502 Wire this into the server side of a `DirectDispatcher` peer-pair to drive an
503 in-process server on the modern per-request-envelope path (each request
504 carries protocol version, client info, and capabilities in `params._meta`;
505 no `initialize` handshake). Like `serve_one`, this raises whatever the
506 handler chain raises - the dispatcher owns the exception-to-error mapping.
507 """
508
509 async def handle(
510 dctx: DispatchContext[TransportContext], method: str, params: Mapping[str, Any] | None
511 ) -> dict[str, Any]:
512 meta = (params or {}).get("_meta", {})
513 connection = Connection.from_envelope(
514 meta.get(PROTOCOL_VERSION_META_KEY, LATEST_MODERN_VERSION),
515 meta.get(CLIENT_INFO_META_KEY),
516 meta.get(CLIENT_CAPABILITIES_META_KEY),
517 )
518 return await serve_one(server, dctx, method, params, connection=connection, lifespan_state=lifespan_state)
519
520 return handle

Callers 1

connectFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected