Skip to content

[MCP caching]: pool MCP backends in thread manager - #15258

Closed
jgershen-oai wants to merge 2 commits into
codex/jgershen-mcp-backend-session-boundaryfrom
codex/jgershen-mcp-threadmanager-pool
Closed

jgershen-oai wants to merge 2 commits into
codex/jgershen-mcp-backend-session-boundaryfrom
codex/jgershen-mcp-threadmanager-pool

Conversation

@jgershen-oai

@jgershen-oai jgershen-oai commented Mar 20, 2026 •

Copy link
Copy Markdown
Contributor

Depends on #15256.

Summary

This is the PR that actually makes MCP backend reuse happen across loaded threads.

  • add a ThreadManager-owned pool of shared MCP backends for enabled Stdio MCP servers
  • keep StreamableHttp MCPs, including codex_apps, per-thread/local
  • force-rebuild the pooled stdio half on refresh, and reacquire it when sandbox state changes
  • add stdio-focused app-server lifecycle coverage for unsubscribe, archive, and archive -> unarchive -> resume

Why

The concrete bug here is duplicate local MCP processes when multiple loaded threads use the same stdio MCP configuration. Pooling only stdio backends fixes that without turning codex_apps into a shared HTTP bottleneck or introducing cross-thread elicitation routing.

Testing

  • cargo check -p codex-core
  • cargo test -p codex-core mcp_connection_manager::tests::
  • cargo test -p codex-core pooled_new_turn_keeps_local_mcp_startup_token_when_sandbox_state_changes
  • cargo test -p codex-app-server thread_mcp_pool
  • cargo test -p codex-app-server mcp_server_elicitation

@jgershen-oai jgershen-oai changed the title feat(core): pool MCP backends in thread manager [MCP caching]: pool MCP backends in thread manager Mar 20, 2026
@etraut-openai etraut-openai added the oai PRs contributed by OpenAI employees label Mar 20, 2026
@jgershen-oai
jgershen-oai force-pushed the codex/jgershen-mcp-threadmanager-pool branch from 2eb5d61 to a97ee12 Compare March 20, 2026 07:11
@jgershen-oai
jgershen-oai force-pushed the codex/jgershen-mcp-elicitation-router branch from 1b23646 to 4a4b5d1 Compare March 20, 2026 07:11
@jgershen-oai
jgershen-oai marked this pull request as ready for review March 20, 2026 07:41
@jgershen-oai
jgershen-oai requested a review from pakrym-oai March 20, 2026 07:41
@jgershen-oai
jgershen-oai changed the base branch from codex/jgershen-mcp-elicitation-router to codex/jgershen-mcp-backend-session-boundary March 20, 2026 18:53
@jgershen-oai
jgershen-oai force-pushed the codex/jgershen-mcp-threadmanager-pool branch from 15a0803 to 05c88b6 Compare March 20, 2026 18:54
store_mode: OAuthCredentialsStoreMode,
auth_entries: HashMap<String, McpAuthStatusEntry>,
tx_event: Sender<Event>,
initial_sandbox_state: SandboxState,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is all sandboxing stuff purely for codex/sandbox-state/update? If so the shell mcp is going to get removed today/tomorrow so we can shed the sandboxing complexity.

impl SharedMcpBackendCacheKey {
pub(crate) fn new(
mcp_servers: &HashMap<String, McpServerConfig>,
store_mode: OAuthCredentialsStoreMode,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this ever change mid session?

pub(crate) struct SessionServices {
pub(crate) mcp_connection_manager: Arc<RwLock<McpConnectionManager>>,
pub(crate) mcp_startup_cancellation_token: Mutex<CancellationToken>,
pub(crate) shared_mcp_backend_pool: Option<Arc<SharedMcpBackendPool>>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Having both mcp_manager and shared_mcp_backend_pool here feels like bad API that leaks implementation details.

let (mcp_connection_manager, cancel_token) = async {
match sess.services.shared_mcp_backend_pool.as_ref() {
Some(shared_mcp_backend_pool) => {
McpConnectionManager::new_with_pool(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this logic shouldn't be in codex. Pooling decisions should be hidden by McpConnectionManager or SharedMcpBackendPool


impl SharedMcpBackendCacheKey {
pub(crate) fn new(
mcp_servers: &HashMap<String, McpServerConfig>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd expect a key to be per server not per group of them, what am I missing?


struct SharedMcpBackendLeaseInner {
backend: Arc<SharedMcpBackend>,
cancel_token: CancellationToken,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is the startup cancellation, right? how do you drop the lease/free the server?

@github-actions

github-actions Bot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

Closing this pull request because it has had no updates for more than 14 days. If you plan to continue working on it, feel free to reopen or open a new PR.

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

Labels

oai PRs contributed by OpenAI employees

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants