Add AsyncKeeperHubMcpClient to the Python kernel - #7
Open
himanshu748 wants to merge 1 commit into
Open
Conversation
The Python kernel ships sync-only, on httpx.Client. Python agent frameworks (OpenAI Agents SDK, LangChain, anything on httpx.AsyncClient) run an asyncio loop, so today an integrator either blocks the event loop for every MCP round trip or writes their own transport. That is the first wall a Python builder hits before their first execution. AsyncKeeperHubMcpClient is a faithful async mirror of KeeperHubMcpClient: same session bootstrap, same 401 and session-404 re-init with the same one-retry cap, same single JSON-result unwrap, same error messages. get_async_client mirrors get_client's per-key caching, minus the close-on-replace, since aclose is a coroutine. The lock is created lazily on the running loop. On Python 3.9, asyncio.Lock() binds to whichever loop is current at construction time, so a client built at import time and awaited inside asyncio.run() would otherwise fail; the suite covers that case. Additive only: no change to the sync client, and no new dependencies. The async tests drive asyncio.run(), so pytest and httpx remain the only dev requirements and the 3.9/3.12 CI matrix is untouched. Tests: 15 pass (9 new async, 6 existing sync).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
The Python kernel ships sync-only, on
httpx.Client. Mainstream Python agent frameworks (OpenAI Agents SDK, LangChain, anything built onhttpx.AsyncClient) run an asyncio loop, so today a Python integrator either blocks the event loop for the whole duration of every MCP round trip, or writes their own transport before they get to their first execution.Since
python/README.mdpositions this package as the foundation Python adapters build on, the async client felt like it belonged in the kernel rather than in each adapter.What
AsyncKeeperHubMcpClient, a faithful async mirror ofKeeperHubMcpClient:mcp-session-idhandling401and session-404re-init, with the same one-retry capget_async_client/reset_async_client_for_testsmirroring the sync helpersOne deliberate difference, documented in its docstring and the README:
get_async_clientdoes not close a superseded client, becauseaclose()is a coroutine and the helper is sync. Callers rotating keys should awaitaclose()on the client they discard.Python 3.9 loop binding
On 3.9,
asyncio.Lock()binds to whichever loop is current at construction time, so a client constructed at import time and then awaited insideasyncio.run()would fail. The lock is therefore created lazily on the running loop, andtest_lock_binds_to_the_running_loopcovers exactly that shape.Testing
python/tests/test_async_client.pymirrorstest_client.pycase for case, plus two async-specific cases: concurrentcall_toolcalls sharing a single session bootstrap, and the loop-binding case above.The tests drive
asyncio.run()rather than pulling inpytest-asyncio, sopytestandhttpxremain the only dev requirements and the 3.9/3.12 CI matrix is unchanged.(9 new async, 6 existing sync.)
Scope
Additive only: 563 insertions, no deletions, no behaviour change to the sync client, no new dependencies.
Context
Found this while building an allowance-revocation agent on KeeperHub for the Agents Onchain hackathon: the agent is async end to end, so the sync kernel was the one thing I had to work around. The agent now runs on this client, so it is code the project depends on rather than a drive-by patch. Happy to adjust naming or structure to match your plans for the package.