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

Function _connect_inproc

src/mcp/client/client.py:72–90  ·  view source on GitHub ↗

Connector for an in-process ``Server``: legacy mode drives the stream loop via ``InMemoryTransport``; any other mode drives the modern per-request path through a ``DirectDispatcher`` peer pair (no streams, no JSON-RPC framing, no initialize handshake).

(server: Server[Any])

Source from the content-addressed store, hash-verified

70
71
72def _connect_inproc(server: Server[Any]) -> _Connector:
73 """Connector for an in-process ``Server``: legacy mode drives the stream loop via
74 ``InMemoryTransport``; any other mode drives the modern per-request path through a
75 ``DirectDispatcher`` peer pair (no streams, no JSON-RPC framing, no initialize handshake)."""
76
77 async def connect(exit_stack: AsyncExitStack, mode: ConnectMode, raise_exceptions: bool) -> Dispatcher[Any]:
78 if mode == "legacy":
79 transport = InMemoryTransport(server, raise_exceptions=raise_exceptions)
80 read_stream, write_stream = await exit_stack.enter_async_context(transport)
81 return JSONRPCDispatcher(read_stream, write_stream)
82 lifespan_state = await exit_stack.enter_async_context(server.lifespan(server))
83 client_disp, server_disp = create_direct_dispatcher_pair(raise_handler_exceptions=raise_exceptions)
84 tg = await exit_stack.enter_async_context(anyio.create_task_group())
85 exit_stack.callback(server_disp.close)
86 on_request = modern_on_request(server, lifespan_state)
87 await tg.start(server_disp.run, on_request, _no_inbound_client_notifications)
88 return client_disp
89
90 return connect
91
92
93def _connected(value: _T | None) -> _T:

Callers 1

__post_init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected