Skip to content

fix(#80): keep MCP server alive until client disconnects#81

Merged
randomm merged 2 commits intomainfrom
feature/issue-80-fix-mcp-server-lifecycle
Apr 12, 2026
Merged

fix(#80): keep MCP server alive until client disconnects#81
randomm merged 2 commits intomainfrom
feature/issue-80-fix-mcp-server-lifecycle

Conversation

@randomm
Copy link
Copy Markdown
Owner

@randomm randomm commented Apr 12, 2026

Root Cause

rmcp::serve_server().await only handles the MCP initialization handshake and returns a RunningService handle. The actual message serving (tools/list, store_memory, etc.) runs as a background tokio task. The previous code dropped the handle immediately, cancelling the background task before any tool calls could be served.

Fix

Add service.waiting().await to keep block_on alive until the client disconnects:

let service = rmcp::serve_server(handler, (stdin, stdout))
    .await
    .map_err(|e| Error::Config(format!("MCP server error: {}", e)))?;
service.waiting().await
    .map_err(|e| Error::Config(format!("MCP server task error: {}", e)))?;

This matches the pattern used by reference MCP server implementations with rmcp v1.2.

Changes

  • src/mcp/server.rs: Add service.waiting().await — core lifecycle fix
  • src/mcp/tools.rs: Make StoreWrapper and methods pub(crate) for test access
  • src/mcp/tests.rs: Add #[tokio::test] exercising ingest/search/list data path
  • Cargo.toml: Add tokio to [dev-dependencies] for #[tokio::test]

Quality Gates

  • cargo fmt --check
  • cargo clippy -- -D warnings
  • cargo test ✅ (162 passed)
  • cargo test --no-default-features ✅ (154 passed)
  • cargo clippy --no-default-features -- -D warnings

Fixes #80
Part of #72

randomm added 2 commits April 12, 2026 20:09
- Add service.waiting().await after serve_server() to block until
  the client disconnects, preventing RunningService from being dropped
  immediately after initialization
- Add tokio to dev-dependencies for #[tokio::test] support
- Add StoreWrapper::new() pub(crate) constructor for test access
- Add test exercising ToolHandler ingest/search/list data path
- Make StoreWrapper and methods pub(crate) for crate-internal access

Fixes #80
@randomm randomm merged commit 49d60fe into main Apr 12, 2026
6 checks passed
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.

Bug: MCP server exits immediately after initialization handshake

1 participant