Repository navigation
Description
Goal
The local MCP listener stays responsive however a local client behaves on the TCP level: connections that never finish their request headers are closed after a short time, the number of open connections is bounded, and turning the MCP server off (or restarting it) always completes promptly.
Context
mcp_server::serve (crates/mcp-server/src/config.rs:63-98) binds a TcpListener and runs axum::serve(listener, router).with_graceful_shutdown(…). The bearer-token check is an axum middleware (require_bearer_token), so it runs only after a complete HTTP request head has arrived.
- No header read timeout, no connection limit. Every accepted connection gets a task and a file descriptor until the peer closes it. A local process that opens many connections and never completes the request head keeps all of them open without ever reaching the token check; once the process runs out of file descriptors, the app can no longer open files, sockets or SSH connections, and legitimate MCP clients are blocked too.
- Shutdown can hang.
McpServerHandle::shutdown(crates/mcp-server/src/config.rs:53-56) cancels the token and then awaits the server task. Graceful shutdown waits for every open connection, including one with a half-sent request, so the await may never return. The callerstop_server_if_running(crates/app-shell/src/mcp_settings.rs:231-237) holds thestate.mcp.runtimemutex (crates/app-logic/src/state.rs:240) across that await, so every later MCP settings command that takes this lock (build_dto,start_server_if_not_running,set_mcp_server_enabled, …) blocks as well.
Spec 0028 §8 (transport: Streamable HTTP, bound to 127.0.0.1, fixed port) and §11 (limits). Streamable HTTP keeps a long-lived response stream per MCP session, so a legitimately idle connection with a completed request must keep working.
Requirements
- Request heads must arrive within a fixed time (constant in
config.rs, e.g. 10 s). A connection that has not delivered a complete request head in that time is closed without a response body. Body reads and long-lived response streams of completed requests are not subject to this timeout. - The number of concurrently open connections is bounded by a constant (e.g. 64). Above the bound, new connections are accepted and closed immediately (or not accepted until a slot is free); existing connections keep working. The bound is well above what legitimate clients need for their sessions (one stream plus short requests per session; see the session limit from Limit MCP client name length and the number of MCP sessions per server #68).
McpServerHandle::shutdowncompletes within a bounded time (constant, e.g. 2 s): first graceful, then the server task and all connection tasks are aborted. After it returns, the port is free again.stop_server_if_runningtakes the handle out ofstate.mcp.runtimeand releases the lock before awaiting the shutdown, so no other MCP settings command waits on a shutdown in progress. Starting the server again right after stopping it either succeeds or reports the existing bind error; it never runs two listeners.- Each closed connection because of a timeout or the connection limit is logged once at
debugwithout request content; at most onewarnper minute when the connection limit is hit.
Acceptance Criteria
- Test (
crates/mcp-server/tests/listener_limits.rs): a TCP client that sends only part of a request head is disconnected after the header timeout (paused tokio clock or a short test value). - Test: with the connection limit set to a small test value N, N stalled connections are held, connection N+1 is closed without blocking the accept loop, and after the stalled connections reach the header timeout an authenticated MCP request succeeds.
- Test: a legitimate MCP session with an open response stream keeps working past the header timeout.
- Test:
shutdown()returns within the bounded time while a connection with a half-sent request is open, and the port can be bound again afterwards. - Test (
crates/app-shell): while a shutdown is in progress, reading the MCP settings does not wait for it (the runtime lock is not held across the shutdown await). - Existing MCP integration tests (
crates/mcp-server/tests/integration.rs) pass unchanged. - Relevant automated tests pass.
Technical Notes
axum::serveoffers no header read timeout. Options: run the accept loop yourself withhyper_util::server::conn::auto::Builder(http1().header_read_timeout(…)with a tokio timer) and serve the existing router as a service, or wrap the listener (axum 0.8Listenertrait) to enforce the connection bound with a semaphore.hyperandhyper-utilare already inCargo.lockthrough axum; add them as direct dependencies ofcrates/mcp-serveronly as far as needed.- Security-relevant area (MCP listener): the token check, the
Hostcheck against DNS rebinding, the loopback bind and the CORS behaviour must stay exactly as they are; only connection handling changes. Keep the existing tests for those. - The connection limit happens before authentication by design (nothing is known about the peer yet); it must not reveal whether a token is valid.
- No data model change.
Files: crates/mcp-server/src/config.rs, crates/mcp-server/Cargo.toml, crates/mcp-server/tests/listener_limits.rs, crates/app-shell/src/mcp_settings.rs, Cargo.lock, docs/specs/0028-mcp-server-integration.md
Spec impact: update 0028
Open questions
- Exact constants (header timeout, connection bound, shutdown grace period): the values above are recommendations; choose final values from what the integration tests and the session limit need, and document them in Spec 0028 §8.
Related Work
- Limit MCP client name length and the number of MCP sessions per server #68 (limit on MCP sessions per server)
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
fix(mcp-server): header read timeout, connection limit and bounded sh…
refactor(mcp-server): split accept loop helpers (#1715)
test(app-shell): drop timing-dependent assertion in shutdown lock test (
test(network-boundary): allow mcp-server's direct hyper server depend…
smart-ssh-coder commentedon Dec 4, 2026
Implementation ready for review. PR #1729. MCP listener now has its own hyper accept loop: 10 s header read timeout, 64-connection bound, 2 s bounded shutdown with abort, and the runtime lock is released before awaiting shutdown. Tests in listener_limits.rs and app-shell; CI green.
smart-ssh-coordinator commentedon Dec 4, 2026
Automation paused after 2 technical failures (last: crashed). Lift the block in the dashboard (Remove Block); removing the labels alone does not resume it.
smart-ssh-reviewer commentedon Dec 5, 2026
Review: The production change meets the issue, but three new tests stay green without the fix they guard. 1. crates/app-shell/src/mcp_settings.rs test_runtime_lock_is_free_while_shutdown_is_in_progress sends 'Bearer tok' while McpState::default() uses a random token: the request gets a 401, the shutdown is instant, and the test also passes with the lock held across the await. Use McpState { token: SharedToken::new("tok"), ..McpState::default() } and fix the comment at :390 (AC5). 2. listener_limits.rs test_connection_over_limit_is_closed_and_slots_free_after_timeout passes with the connection limit removed (N+1 is just closed by the 400 ms header timeout). Use a long header timeout for the limit phase and assert N+1 is closed well before it, or send a complete authenticated request on N+1 and assert no response (AC2). 3. listener_limits.rs test_open_response_stream_survives_header_timeout passes even when every connection is cut after the timeout, because the client reconnects. Assert on the open stream connection itself, e.g. a raw GET stream still open after the timeout (AC3). 4. mcp_settings.rs:199 comment points to nest_service in mcp_server::config::serve; it now lives in serve_with_limits. Non-blocking notes and two follow-ups (pre-auth slot holding; network-boundary hyper feature guard) are in the PR review.
test(mcp-server): make listener limit tests fail without the fix (#1715)
smart-ssh-coder commentedon Dec 5, 2026
Implementation ready for review. PR #1729. Fixed all four review points: shutdown-lock test now authenticates with a known token, limit test uses a long header timeout, open-stream test asserts on a raw SSE connection, stale comment updated. CI green on the pushed head.