Repository navigation
perf: share one SSL context across MCP calls - #263
Merged
Merged
Conversation
Every MCP exchange built a new httpx.AsyncClient, and each one built its own SSL context, loading the whole CA bundle: about 7ms per tool call, listing or search. One context built on first use is shared instead. httpx builds it the same way (create_ssl_context with trust_env), so SSL_CERT_FILE and SSL_CERT_DIR are still honoured, read once rather than per call. Against the local MCP mock, the median tool.execute() drops from about 20ms to about 12ms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Guillaume Lebedel <10488548+glebedel@users.noreply.github.com>
…figure The test now clears the cache and requires one CA-bundle load for a listing and two calls, so it fails if the shared context is not used (it also passed with zero loads before). The docstring no longer quotes a timing that varies by machine. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Guillaume Lebedel <10488548+glebedel@users.noreply.github.com>
GET /accounts built its own SSL context, so discovery still loaded the CA bundle a second time. The test now discovers accounts too and clears the cached context when it finishes, so later tests start fresh. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Guillaume Lebedel <10488548+glebedel@users.noreply.github.com>
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.
Summary
tool.execute(), listing and search) built a newhttpx.AsyncClient, and each one built its own SSL context, loading the whole CA bundle. That took about 7ms per call.httpx.create_ssl_context(), the same factory and defaults httpx uses forverify=True) and passes it to every MCP client and to theGET /accountsclient.Why, with evidence
Measured against the local MCP mock (
tests/mocks/serve.ts), 100 calls oftool.execute({})after a warm-up:httpx.AsyncClient()takes 7.4 ms to construct by default and 0.03 ms when it is given an existing context (httpx 0.28.1).I also tried keeping a persistent event loop with pooled clients so that TCP and TLS connections could be reused across calls. It gave the same gain, but the connection trace showed every request still opening a new connection. The
mcpclient stops reading each SSE response before the closing chunk, so httpcore drops the connection. The gain came entirely from not rebuilding the SSL context, so this PR keeps only that. Reusing connections would also need themcpclient to drain SSE responses, which is out of scope here.Trade-offs to know about
SSL_CERT_FILE/SSL_CERT_DIRand the CA bundle are read once per process instead of once per call. A CA removed from the bundle at runtime stays trusted until the process restarts, which is how a long-livedhttpx.Clientorrequests.Sessionalready behaves.How verified
test_calls_share_one_ssl_contextcountsSSLContext.load_verify_locationsacross account discovery, a listing and two real calls to the mock. It fails on main and passes here with exactly one load.uv run ruff check .,uv run ruff format --check .anduv run ty check stackone_ai examplesall pass.uv run pytest: 872 passed.Gates (head fb4976c)
SSL_CERT_FILEare read once per process (disclosed above); clean on fb4976c (job 3d0eeb35):CERT_REQUIREDand hostname checks stay on for both clients, proxy env handling unchangedGET /accountsnow uses the shared context too (the test discovers accounts and failed with 2 CA loads before the fix), and the test clears the cache on teardown. Declined 5: reading the CA bundle once per process is the disclosed trade-off, httpcore sets ALPN to the same value on every connection, client reuse was measured with no gain because the MCP client abandons each SSE response, the first-call race costs only duplicate work, and counting CA loads tests the intent directlyRequested by guillaume@stackone.com in Slack (repo audit thread).
🤖 Generated with Claude Code
Summary by cubic
Shares one SSL context across all MCP calls so the CA bundle loads once per process instead of once per call, cutting median tool execution from about 20ms to 12ms.
The new test clears the context cache, then expects exactly one CA bundle load across a listing and two calls; it fails on the old per-call behavior.
Written for commit 96d729d. Summary will update on new commits.