Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 31, 2025

Some MCP servers (e.g., pdf-reader) are designed to handle one request and exit, causing "Connection closed" errors when MCPHub tries to maintain persistent connections.

Changes

Core behavior:

  • Added connectionMode field to server configuration: "persistent" (default) | "on-demand"
  • On-demand servers connect only during tool invocation, disconnect immediately after
  • During initialization, on-demand servers connect briefly to discover tools, then disconnect

Implementation details:

  • connectOnDemandServer() / disconnectOnDemandServer() helper functions manage lifecycle
  • Tool invocation wrapped in try-finally to guarantee cleanup even on errors
  • Smart routing updated to include disconnected on-demand servers in available tools
  • Comprehensive test coverage (9 new tests) for connection lifecycle and error cases

Usage

{
  "mcpServers": {
    "pdf-reader": {
      "command": "npx",
      "args": ["-y", "pdf-mcp-server"],
      "connectionMode": "on-demand"
    }
  }
}

Backward compatible—servers default to "persistent" mode.

Original prompt

This section details on the original issue you should resolve

<issue_title>MCP services are divided into long-term and short-term when they are added</issue_title>
<issue_description>I have an mcp service mcp/pdf-reader, docker runs, but this service does not support long-term running, it is one-time, and it will give an error when running:
Failed to connect: McpError: MCP error -32000: Connection closed
at Client._onclose (file:///app/node_modules/.pnpm/@modelcontextprotocol[email protected]/node_modules/@modelcontextprotocol/sdk/dist/esm/shared/protocol.js:109:23)
at _transport.onclose (file:///app/node_modules/.pnpm/@modelcontextprotocol[email protected]/node_modules/@modelcontextprotocol/sdk/dist/esm/shared/protocol.js:76:18)
at ChildProcess. (file:///app/node_modules/.pnpm/@modelcontextprotocol[email protected]/node_modules/@modelcontextprotocol/sdk/dist/esm/client/stdio.js:95:77)
at ChildProcess.emit (node:events:519:28)
at maybeClose (node:internal/child_process:1101:16)
at ChildProcess._handle.onexit (node:internal/child_process:304:5)

In principle, the popularity of this feature request is not that high, but it is only a situation that I have encountered when I use it personally. Hope there is this feature, but it is not necessary, thanks</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix MCP error for one-time pdf-reader service Add on-demand connection mode for ephemeral MCP servers Oct 31, 2025
Copilot AI requested a review from samanhappy October 31, 2025 15:28
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.

MCP services are divided into long-term and short-term when they are added

2 participants