Skip to content

Add AsyncKeeperHubMcpClient to the Python kernel - #7

Open
himanshu748 wants to merge 1 commit into
KeeperHub:mainfrom
himanshu748:feat/python-async-client
Open

Add AsyncKeeperHubMcpClient to the Python kernel#7
himanshu748 wants to merge 1 commit into
KeeperHub:mainfrom
himanshu748:feat/python-async-client

Conversation

@himanshu748

Copy link
Copy Markdown

Why

The Python kernel ships sync-only, on httpx.Client. Mainstream Python agent frameworks (OpenAI Agents SDK, LangChain, anything built on httpx.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.md positions 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 of KeeperHubMcpClient:

  • same session bootstrap and mcp-session-id handling
  • same 401 and session-404 re-init, with the same one-retry cap
  • same single JSON-result unwrap and the same error messages
  • get_async_client / reset_async_client_for_tests mirroring the sync helpers

One deliberate difference, documented in its docstring and the README: get_async_client does not close a superseded client, because aclose() is a coroutine and the helper is sync. Callers rotating keys should await aclose() 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 inside asyncio.run() would fail. The lock is therefore created lazily on the running loop, and test_lock_binds_to_the_running_loop covers exactly that shape.

Testing

python/tests/test_async_client.py mirrors test_client.py case for case, plus two async-specific cases: concurrent call_tool calls sharing a single session bootstrap, and the loop-binding case above.

The tests drive asyncio.run() rather than pulling in pytest-asyncio, so pytest and httpx remain the only dev requirements and the 3.9/3.12 CI matrix is unchanged.

15 passed in 0.04s

(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.

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).
Copilot AI review requested due to automatic review settings July 27, 2026 11:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants