Skip to content

Defer MCP imports for agents without MCP tools - #6216

Closed
markstuart-oai wants to merge 1 commit into
mainfrom
perf/defer-mcp-imports
Closed

markstuart-oai wants to merge 1 commit into
mainfrom
perf/defer-mcp-imports

Conversation

@markstuart-oai

Copy link
Copy Markdown
Contributor

Summary

This pull request has been shortening cold starts for agents without MCP tools. Importing Agent has been loading the Model Context Protocol (MCP) package through agents.mcp.util, even for local function tools.

With this change, the SDK has been loading the compatibility helpers on demand during MCP tool conversion, invocation or custom data extraction. One tradeoff for MCP users — a potential pause on the event loop at first use.

Benchmarks

Startup. From launch to the first checked answer with a local tool, the PR has been saving a median 179 ms per pair (95% interval 137–243 ms), or 15.9%. Peak process memory has been falling by 19.2 MiB.

  • With MCPServer imported at launch on both sides, a smaller 4.6% saving [4.2%, 5.8%].
  • With MCPServerStdio imported immediately after the ordinary SDK imports, 225 ms more for that step. No clear change in total import time across 16 separate pairs.

Tradeoffs. Two typed tool-output workloads have been exceeding the 3% margin on both runs. Both have been following large integer-list outputs in the same process. Results from 64 fresh pairs below.

Elapsed time Main PR Paired change
Typed outputs, median of 3 ordinary calls 6.56 ms 6.85 ms +5.37%
Typed outputs, median of 3 streamed calls 8.11 ms 8.87 ms +9.06%
First typed stream 8.09 ms 108.50 ms +1,220%
All 18 timed calls in the tool-output sequence 1,713 ms 1,729 ms +1.00%
Second batch of 32 concurrent streams 370.64 ms 453.19 ms +21.53%
All three batches of 32 streams 1,334 ms 1,348 ms +0.94%

Garbage collection. Three separate traces per version. A full collection on the first typed stream in 3/3 PR processes and 0/3 on main. In a separate 20-block test, releasing completed stream results has been moving that pause to a later call. Individual requests have still been incurring it.

SQLite. The large write has been taking 4.64% longer [1.55%, 7.40%] on repeat. Uncertain against the 3% margin.

Empty server. Same timed code and output checks. After timing, the harness has been checking for MCP on main and its absence on this PR. The separate MCPServer check above has been requiring the import on both sides.

All 83 comparisons with Codex off. Offline Python SDK workloads on one Linux host with CPython 3.12.16. Each comparison has been measuring this change alone against main at 4e162c79, without network or model latency.

95% interval versus a 3% slowdown Within margin Uncertain Above margin
Initial, all 83 78 3 2
Independent repeat, 7 selected 4 1 2
Latest result per workload 80 1 2
Timings and 95% intervals for all 83 comparisons
  • Initial — 32 matched pairs of fresh processes, balanced by run order.
  • Repeat — 64 new pairs for every initial upper bound above +3%, both SQLite writes and 32 concurrent streams. No pooling.
  • Main / PR — median elapsed time in milliseconds, from the repeat where available. For repeated calls, the median across process medians. For 32 streams, the median of three timed batches.
  • Change — median within-pair percentage change, with 95% bounds from 5,000 resamples of whole pairs. Negative for faster. Calculated from individual pairs before taking the median.
  • Margin — within for an upper bound ≤ +3%, above for a lower bound > +3%, otherwise uncertain. Individual intervals only, without correction for multiple comparisons.
Workload Main, ms PR, ms Initial % [95% interval] Repeat % [95% interval]
Fresh-agent control, normal 42.482 38.414 -11.24 [-18.41, -4.82] —
Fresh-agent control, streaming 287.85 188.47 -34.27 [-37.05, -30.63] —
Integer-list output, 17 calls, 32769 initial entries, normal 240.24 240.37 -0.47 [-1.13, +1.49] —
Integer-list output, 17 calls, 32769 initial entries, streaming 266.84 268.12 -0.01 [-0.93, +2.01] —
Integer-list output, 3 calls, 8193 initial entries, normal 13.341 13.18 -0.81 [-1.99, +1.34] —
Integer-list output, 3 calls, 8193 initial entries, streaming 16.529 15.949 -2.70 [-5.30, -0.10] —
Reused-agent control, normal 38.342 37.697 -1.01 [-2.19, +1.26] —
Reused-agent control, streaming 39.868 40.413 +1.15 [-0.37, +1.79] —
Typed text-list output, 3 calls, 257 initial entries, normal 6.5632 6.8494 +9.01 [+4.72, +11.40] +5.37 [+3.81, +6.12]
Typed text-list output, 3 calls, 257 initial entries, streaming 8.1054 8.8734 +9.74 [+6.43, +12.58] +9.06 [+8.04, +10.83]
Dynamic tool addition 1.72 1.7121 -0.62 [-1.44, +0.06] —
Agent with 12 tools, dictionary settings, construction only 0.051197 0.051347 +0.05 [-0.28, +0.84] —
Agent with 12 tools, default settings, construction only 0.070817 0.070854 -0.12 [-0.43, +1.07] —
Agent with 12 tools, typed settings, construction only 0.038272 0.038405 +0.33 [-0.09, +1.10] —
Concurrent runs, 1 tasks 0.50901 0.49066 -2.85 [-4.66, -2.08] —
Concurrent runs, 32 tasks 11.933 12.007 +1.77 [+0.90, +3.12] +0.44 [-0.53, +1.30]
Concurrent runs, 8 tasks 2.9273 2.8852 -1.50 [-2.40, -0.65] —
32 concurrent runs with GC observation 12.283 12.146 -0.96 [-1.85, +0.17] —
Stream completion, 128 history messages 1.2701 1.247 -2.60 [-2.96, -0.90] —
0 available handoffs 0.49793 0.49123 -1.63 [-3.25, -0.80] —
12 available handoffs 0.70059 0.70006 -0.05 [-1.47, +0.88] —
Input history, 120 messages 0.93409 0.9089 -2.58 [-3.20, -2.27] —
Input history, 2000 messages 6.2317 6.2097 +0.49 [-1.09, +0.82] —
Input history, 720 messages 2.6617 2.6289 -1.01 [-1.95, +0.06] —
6 handoff routes, mixed agent names 0.7246 0.71449 -2.25 [-2.97, -1.25] —
Structured streamed response, 67 fields, 16 tool turns 75.734 75.544 +0.34 [-1.05, +1.91] —
8 handoff routes, repeated agent names 1.0027 0.99494 -0.86 [-1.16, -0.44] —
Retained history, 16384 existing + 1 new message 0.41068 0.41233 +0.63 [-0.36, +1.89] —
Retained history, 2048 existing + 1 new message 0.053516 0.05347 +0.02 [-0.55, +0.43] —
Agent with 6 tools, dictionary settings, construction + run 0.7781 0.77084 -1.19 [-1.60, -0.21] —
Agent with 6 tools, default settings, construction + run 0.83435 0.82257 -1.24 [-1.91, -0.73] —
Agent with 6 tools, typed settings, construction + run 0.74607 0.72999 -2.07 [-2.93, -1.43] —
24 available tool schemas 0.85309 0.84618 -0.48 [-1.42, +0.12] —
Server-managed conversation, 1 history messages 1.8354 1.8046 -1.61 [-2.38, -0.77] —
Server-managed conversation, 1024 history messages 35.132 34.871 -0.98 [-2.50, +0.34] —
Server-managed conversation, 128 history messages 4.7434 4.7194 -0.54 [-1.71, +0.45] —
Server-managed conversation, 512 history messages 15.352 15.188 -1.37 [-2.70, +0.20] —
0 handoff routes, distinct agent names 0.49744 0.48972 -1.89 [-3.23, -0.03] —
12 handoff routes, distinct agent names 0.58673 0.58255 -1.05 [-1.63, +0.33] —
Streamed final response 0.79314 0.76267 -3.22 [-3.81, -3.01] —
32 sustained concurrent runs 11.994 11.829 -1.57 [-1.99, -0.58] —
SQLite committed batch, 1024 items × 4096 payload characters 57.234 59.417 +1.66 [-3.58, +5.44] +4.64 [+1.55, +7.40]
Retained history, 16384 existing + 2 new messages 0.80529 0.80534 +0.00 [-0.44, +0.64] —
Retained history, 2048 existing + 2 new messages 0.10526 0.10454 -0.39 [-1.29, +0.07] —
SQLite committed batch, 2 items × 256 payload characters 7.7296 7.4892 -0.93 [-8.21, +2.10] -3.83 [-7.38, +0.51]
Retained history, 4096 existing + 8 new messages 0.83657 0.82959 -0.63 [-1.92, +0.49] —
Tool loop, 1 turns 1.6351 1.5967 -1.94 [-3.00, -1.42] —
Tool loop, 16 turns 19.543 19.361 -0.70 [-1.34, -0.38] —
Tool loop, 4 turns 4.6676 4.6085 -1.83 [-2.38, -0.19] —
Warm final response 0.48567 0.48252 -0.44 [-1.38, -0.18] —
Batched tool turn, 1 calls, 1024 history messages 35.661 34.977 -1.47 [-2.96, -0.62] —
Batched tool turn, 2 calls, 32 history messages 3.8369 3.7574 -1.83 [-2.83, -0.85] —
Tool turn, 12 calls, 64 history messages 16.979 16.778 -1.31 [-1.89, -0.78] —
Tool turn, 1 calls, 1024 history messages 35.205 34.87 -0.67 [-1.90, -0.14] —
Tool turn, 1 calls, 2048 history messages 94.068 94.173 -0.08 [-0.69, +0.78] —
Tool turn, 1 calls, 256 history messages 7.8606 7.7502 -1.83 [-2.92, -0.43] —
Structured tool workload, 4 calls, 1024 history messages 58.624 58.424 +0.70 [-0.38, +3.93] -0.06 [-1.04, +1.11]
Structured tool workload, 8 calls, 128 history messages 38.639 37.912 -1.22 [-2.31, +1.63] —
32 concurrent streams, 2 tool calls, 128 history messages 473.54 447.94 -5.49 [-6.65, -4.20] -5.36 [-6.16, -4.57]
First completed run, 1 tool turn 1182.3 976.17 -15.93 [-21.28, -11.39] —
First completed run with empty tool server, 1 tool turn 1181.5 932.71 -19.41 [-22.03, -14.51] —
List output, 1 calls × 1024 entries 5.9264 5.8368 -0.61 [-2.08, +0.31] —
List output, 8 calls × 8192 entries 59.004 58.482 +0.26 [-1.19, +1.04] —
Provider-response conversion, 1 calls, 2048 history messages 318.72 317.73 -0.60 [-1.48, +0.29] —
Provider-response conversion, 1 calls, 4096 history messages 742.89 747.35 +1.08 [-0.47, +1.87] —
Line-item calculation, 1 calls × 1 records 1.7448 1.7123 -1.52 [-2.17, -1.08] —
Line-item calculation, 1 calls × 64 records 2.6386 2.6084 -1.18 [-1.51, -0.51] —
Line-item calculation, 8 calls × 16 records 11.743 11.697 -0.72 [-1.28, +0.38] —
Line-item calculation, 8 calls × 256 records 37.58 37.438 -0.61 [-1.22, +0.09] —
Line-item calculation, 8 calls × 64 records 17.172 16.966 -1.92 [-2.39, -0.82] —
Structured response, 12 tool calls, 96 fields, normal 35.863 35.646 -0.61 [-2.32, +1.32] —
Structured response, 12 tool calls, 96 fields, stream 58.133 58.259 +0.38 [-0.69, +1.22] —
Structured response, 4 tool calls, 12 fields, stream 11.917 11.872 +0.50 [-1.62, +1.04] —
Structured response, 8 tool calls, 0 fields, stream 14.773 14.56 -1.01 [-2.40, -0.34] —
Sustained structured tool workload, 4 calls, 1024 history messages 59.579 59.054 -0.83 [-1.22, +0.13] —
Integer list, 1 calls × 1 entries 1.7122 1.6897 -1.38 [-2.08, -0.96] —
Integer list, 8 calls × 8192 entries 33.426 32.736 -2.12 [-2.72, +0.09] —
Mixed list, 4 calls × 512 entries 10.325 10.285 +0.16 [-1.24, +0.62] —
Record list, 8 calls × 512 entries 13.163 12.885 -2.28 [-2.65, -1.84] —
Typed text list, 16 calls × 1024 entries 81.322 80.236 -0.89 [-2.10, +0.81] —
Typed text list, 4 calls × 128 entries 7.1335 7.0746 -0.90 [-1.29, -0.52] —
Warm tool control, 1 calls, 0 history messages 1.7428 1.7173 -1.38 [-2.02, -0.83] —
Warm tool control, 1 calls, 4096 history messages 35.738 35.937 +0.51 [-0.72, +2.08] —

Test plan and checks

  • Fresh-process regression with MCP imports blocked, a local function tool and an empty custom server. Existing MCP conversion, invocation and error tests.
  • Full repository verifier (.agents/skills/code-change-verification/scripts/run.sh) including formatting, lint, mypy, Pyright and both pytest phases. 12,865 passed, 30 skipped.
  • Independent review of the complete diff. Identical Git tree for the local checks and PR head.

Credential-redaction tests have been matching the fixture username user in the original checkout path. All 136 tests in the two affected files and the full suite have been passing from a neutral path with identical source and assertions.

@markstuart-oai
markstuart-oai requested review from a team, rm-openai and seratch as code owners November 25, 2026 00:59
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T01:00:42.896409Z dcf37eb PR opened
🔒 Security Review ✅ Completed 2026-11-25T01:01:25.167283Z dcf37eb PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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.

1 participant