Skip to content

Commit be4b869

Browse files
committed
feat: add stdio tool retry and improve readme
1 parent d160e0c commit be4b869

8 files changed

Lines changed: 326 additions & 292 deletions

README.md

Lines changed: 100 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,27 +174,110 @@ Example `config/tool_config.json`:
174174
export TOOLS_FOLDER=/srv/mcp_tools
175175
```
176176
177-
### 4. Proxy Behavior Configuration (Error Handling & Retries)
178-
While some general proxy behaviors might be configured here in the future, the primary retry and error handling settings are now managed via **Environment Variables** for easier deployment-specific overrides. Values in `config/mcp_server.json` for these specific settings will be overridden by environment variables if set.
177+
- **`RETRY_SSE_TOOL_CALL_ON_DISCONNECT`**: (Optional) Controls whether to automatically reconnect and retry on SSE tool call failures. Set to `"true"` to enable, `"false"` to disable. Default: `true`. See the "Enhanced Reliability Features" section for details.
178+
```bash
179+
export RETRY_SSE_TOOL_CALL_ON_DISCONNECT="true"
180+
```
181+
- **`RETRY_HTTP_TOOL_CALL`**: (Optional) Controls whether to retry on HTTP tool call connection errors. Set to `"true"` to enable, `"false"` to disable. Default: `true`. See the "Enhanced Reliability Features" section for details.
182+
```bash
183+
export RETRY_HTTP_TOOL_CALL="true"
184+
```
185+
- **`HTTP_TOOL_CALL_MAX_RETRIES`**: (Optional) Maximum number of retry attempts for HTTP tool calls (after the initial failure). Default: `2`. See the "Enhanced Reliability Features" section for details.
186+
```bash
187+
export HTTP_TOOL_CALL_MAX_RETRIES="3"
188+
```
189+
- **`HTTP_TOOL_CALL_RETRY_DELAY_BASE_MS`**: (Optional) Base delay in milliseconds for HTTP tool call retries, used in exponential backoff. Default: `300`. See the "Enhanced Reliability Features" section for details.
190+
```bash
191+
export HTTP_TOOL_CALL_RETRY_DELAY_BASE_MS="500"
192+
```
193+
- **`RETRY_STDIO_TOOL_CALL`**: (Optional) Controls whether to retry on Stdio tool call connection errors (attempts to restart the process). Set to `"true"` to enable, `"false"` to disable. Default: `true`. See the "Enhanced Reliability Features" section for details.
194+
```bash
195+
export RETRY_STDIO_TOOL_CALL="true"
196+
```
197+
- **`STDIO_TOOL_CALL_MAX_RETRIES`**: (Optional) Maximum number of retry attempts for Stdio tool calls (after the initial failure). Default: `2`. See the "Enhanced Reliability Features" section for details.
198+
```bash
199+
export STDIO_TOOL_CALL_MAX_RETRIES="5"
200+
```
201+
- **`STDIO_TOOL_CALL_RETRY_DELAY_BASE_MS`**: (Optional) Base delay in milliseconds for Stdio tool call retries, used in exponential backoff. Default: `300`. See the "Enhanced Reliability Features" section for details.
202+
```bash
203+
export STDIO_TOOL_CALL_RETRY_DELAY_BASE_MS="1000"
204+
```
179205
180-
Example `config/mcp_server.json` showing other potential proxy settings:
181-
```json
182-
{
183-
"mcpServers": {
184-
"...": "..."
185-
},
186-
"proxy": {
187-
"someOtherProxySettingNotOverriddenByEnv": "example_value"
188-
// Specific retry settings like retrySseToolCallOnDisconnect, retryHttpToolCall,
189-
// httpToolCallMaxRetries, and httpToolCallRetryDelayBaseMs are now
190-
// preferably set via environment variables (see below).
191-
}
192-
}
206+
## Enhanced Reliability Features
207+
208+
The MCP Proxy Server includes features to improve its resilience and the reliability of interactions with backend MCP services, ensuring smoother operations and more consistent tool execution.
209+
210+
### 1. Error Propagation
211+
The proxy server ensures that errors originating from backend MCP services are consistently propagated to the requesting client. These errors are formatted as standard JSON-RPC error responses, making it easier for clients to handle them uniformly.
212+
213+
### 2. SSE Connection Retry for Tool Calls
214+
When a `tools/call` operation is made to an SSE-based backend server, and the underlying connection is lost or experiences an error, the proxy server will automatically attempt to:
215+
1. Re-establish the connection to the SSE backend.
216+
2. If reconnection is successful, it will retry the original `tools/call` request **once**.
217+
218+
This behavior helps mitigate transient network issues that might temporarily disrupt SSE connections.
219+
220+
**Configuration:**
221+
This feature is primarily controlled by the **`RETRY_SSE_TOOL_CALL_ON_DISCONNECT`** environment variable.
222+
- **`RETRY_SSE_TOOL_CALL_ON_DISCONNECT`** (environment variable):
223+
- Set to `"true"` to enable the automatic reconnect and retry.
224+
- Set to `"false"` to disable this feature.
225+
- **Default Behavior:** `true` (if the environment variable is not set, is empty, or is an invalid value).
226+
227+
**Example (Environment Variable):**
228+
```bash
229+
export RETRY_SSE_TOOL_CALL_ON_DISCONNECT="true"
193230
```
194-
See the "Enhanced Reliability Features" and "Environment Variables" sections for details on these options.
195231
232+
### 3. HTTP Request Retry for Tool Calls
233+
For `tools/call` operations directed to HTTP-based backend servers, the proxy implements a retry mechanism for connection errors (e.g., "failed to fetch", network timeouts).
234+
235+
**Retry Mechanism:**
236+
If an initial HTTP request fails due to a connection error, the proxy will retry the request using an exponential backoff strategy. This means the delay before each subsequent retry attempt increases exponentially, with a small amount of jitter (randomness) added to prevent thundering herd scenarios.
196237
197-
## Enhanced Reliability Features
238+
**Configuration:**
239+
These settings are primarily controlled by environment variables.
240+
241+
- **`RETRY_HTTP_TOOL_CALL`** (environment variable):
242+
- Set to `"true"` to enable retries for HTTP tool calls.
243+
- Set to `"false"` to disable this feature.
244+
- **Default Behavior:** `true` (if the environment variable is not set, is empty, or is an invalid value).
245+
246+
- **`HTTP_TOOL_CALL_MAX_RETRIES`** (environment variable):
247+
- Specifies the maximum number of retry attempts *after* the initial failed attempt. For example, if set to `"2"`,there will be one initial attempt and up to two retry attempts, totaling a maximum of three attempts.
248+
- **Default Behavior:** `2` (if the environment variable is not set, is empty, or is not a valid integer).
249+
250+
- **`HTTP_TOOL_CALL_RETRY_DELAY_BASE_MS`** (environment variable):
251+
- The base delay in milliseconds used in the exponential backoff calculation. The delay before the *n*-th retry (0-indexed) is roughly `HTTP_TOOL_CALL_RETRY_DELAY_BASE_MS * (2^n) + jitter`.
252+
- **Default Behavior:** `300` (milliseconds) (if the environment variable is not set, is empty, or is not a valid integer).
253+
254+
### 4. Stdio Connection Retry for Tool Calls
255+
For `tools/call` operations directed to Stdio-based backend servers, the proxy implements a retry mechanism for connection errors (e.g., process crash or unresponsiveness).
256+
257+
**Retry Mechanism:**
258+
If an initial Stdio connection or tool call fails, the proxy will attempt to restart the Stdio process and retry the request. This mechanism follows an exponential backoff strategy similar to HTTP retries.
259+
260+
**Configuration:**
261+
These settings are primarily controlled by environment variables.
262+
263+
- **`RETRY_STDIO_TOOL_CALL`** (environment variable):
264+
- Set to `"true"` to enable Stdio tool call retries.
265+
- Set to `"false"` to disable this feature.
266+
- **Default Behavior:** `true` (if the environment variable is not set, is empty, or is an invalid value).
267+
268+
- **`STDIO_TOOL_CALL_MAX_RETRIES`** (environment variable):
269+
- Specifies the maximum number of retry attempts *after* the initial failed attempt. For example, if set to `"2"`,there will be one initial attempt and up to two retry attempts, totaling a maximum of three attempts.
270+
- **Default Behavior:** `2` (if the environment variable is not set, is empty, or is not a valid integer).
271+
272+
- **`STDIO_TOOL_CALL_RETRY_DELAY_BASE_MS`** (environment variable):
273+
- The base delay in milliseconds used in the exponential backoff calculation. The delay before the *n*-th retry (0-indexed) is roughly `STDIO_TOOL_CALL_RETRY_DELAY_BASE_MS * (2^n) + jitter`.
274+
- **Default Behavior:** `300` (milliseconds) (if the environment variable is not set, is empty, or is not a valid integer).
275+
276+
**General Notes on Environment Variable Parsing:**
277+
- Boolean environment variables (`RETRY_SSE_TOOL_CALL_ON_DISCONNECT`, `RETRY_HTTP_TOOL_CALL`, `RETRY_STDIO_TOOL_CALL`) are considered `true` if their lowercase value is exactly `"true"`. Any other value (including empty or not set) results in the default being applied or `false` if the default is `false` (though for these specific variables, the default is `true`).
278+
- Numeric environment variables (`HTTP_TOOL_CALL_MAX_RETRIES`, `HTTP_TOOL_CALL_RETRY_DELAY_BASE_MS`, `STDIO_TOOL_CALL_MAX_RETRIES`, `STDIO_TOOL_CALL_RETRY_DELAY_BASE_MS`) are parsed as base-10 integers. If parsing fails (e.g., the value is not a number, or the variable is empty/not set), the default value is used.
279+
280+
## Development
198281
199282
The MCP Proxy Server includes features to improve its resilience and the reliability of interactions with backend MCP services, ensuring smoother operations and more consistent tool execution.
200283

README_ZH.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,109 @@
175175
export TOOLS_FOLDER=/srv/mcp_tools
176176
```
177177

178+
- **`RETRY_SSE_TOOL_CALL_ON_DISCONNECT`**: (可选) 控制 SSE 工具调用失败时是否自动重连并重试。设置为 `"true"` 启用,`"false"` 禁用。默认: `true`。有关详细信息,请参阅“增强的可靠性特性”部分。
179+
```bash
180+
export RETRY_SSE_TOOL_CALL_ON_DISCONNECT="true"
181+
```
182+
- **`RETRY_HTTP_TOOL_CALL`**: (可选) 控制 HTTP 工具调用连接错误时是否重试。设置为 `"true"` 启用,`"false"` 禁用。默认: `true`。有关详细信息,请参阅“增强的可靠性特性”部分。
183+
```bash
184+
export RETRY_HTTP_TOOL_CALL="true"
185+
```
186+
- **`HTTP_TOOL_CALL_MAX_RETRIES`**: (可选) HTTP 工具调用最大重试次数(在初始失败后)。默认: `2`。有关详细信息,请参阅“增强的可靠性特性”部分。
187+
```bash
188+
export HTTP_TOOL_CALL_MAX_RETRIES="3"
189+
```
190+
- **`HTTP_TOOL_CALL_RETRY_DELAY_BASE_MS`**: (可选) HTTP 工具调用重试延迟基准(毫秒),用于指数退避。默认: `300`。有关详细信息,请参阅“增强的可靠性特性”部分。
191+
```bash
192+
export HTTP_TOOL_CALL_RETRY_DELAY_BASE_MS="500"
193+
```
194+
- **`RETRY_STDIO_TOOL_CALL`**: (可选) 控制 Stdio 工具调用连接错误时是否重试(尝试重启进程)。设置为 `"true"` 启用,`"false"` 禁用。默认: `true`。有关详细信息,请参阅“增强的可靠性特性”部分。
195+
```bash
196+
export RETRY_STDIO_TOOL_CALL="true"
197+
```
198+
- **`STDIO_TOOL_CALL_MAX_RETRIES`**: (可选) Stdio 工具调用最大重试次数(在初始失败后)。默认: `2`。有关详细信息,请参阅“增强的可靠性特性”部分。
199+
```bash
200+
export STDIO_TOOL_CALL_MAX_RETRIES="5"
201+
```
202+
- **`STDIO_TOOL_CALL_RETRY_DELAY_BASE_MS`**: (可选) Stdio 工具调用重试延迟基准(毫秒),用于指数退避。默认: `300`。有关详细信息,请参阅“增强的可靠性特性”部分。
203+
```bash
204+
export STDIO_TOOL_CALL_RETRY_DELAY_BASE_MS="1000"
205+
```
206+
207+
## 增强的可靠性特性
208+
209+
MCP 代理服务器包含多项特性,用以提升其自身弹性以及与后端 MCP 服务交互的可靠性,确保更平稳的操作和更一致的工具执行。
210+
211+
### 1. 错误传播
212+
代理服务器确保从后端 MCP 服务产生的错误能够一致地传播给请求客户端。这些错误被格式化为标准的 JSON-RPC 错误响应,使客户端更容易统一处理它们。
213+
214+
### 2. SSE 工具调用的连接重试
215+
当对基于 SSE 的后端服务器执行 `tools/call` 操作时,如果底层连接丢失或遇到错误,代理服务器将自动尝试:
216+
1. 重新建立与 SSE 后端的连接。
217+
2. 如果重新连接成功,它将重试原始的 `tools/call` 请求**一次**
218+
219+
此行为有助于缓解可能暂时中断 SSE 连接的瞬时网络问题。
220+
221+
**配置:**
222+
此功能主要通过 **`RETRY_SSE_TOOL_CALL_ON_DISCONNECT`** 环境变量控制。
223+
- **`RETRY_SSE_TOOL_CALL_ON_DISCONNECT`** (环境变量):
224+
- 设置为 `"true"` 以启用自动重新连接和重试。
225+
- 设置为 `"false"` 以禁用此功能。
226+
- **默认行为:** `true` (如果环境变量未设置、为空或为无效值)。
227+
228+
**示例 (环境变量):**
229+
```bash
230+
export RETRY_SSE_TOOL_CALL_ON_DISCONNECT="true"
231+
```
232+
233+
### 3. HTTP 工具调用的请求重试
234+
对于定向到基于 HTTP 的后端服务器的 `tools/call` 操作,代理服务器为连接错误(例如,“failed to fetch”、网络超时)实现了一套重试机制。
235+
236+
**重试机制:**
237+
如果初始 HTTP 请求因连接错误而失败,代理将使用指数退避策略重试该请求。这意味着每次后续重试尝试之前的延迟会指数级增加,并加入少量抖动(随机性)以防止“惊群效应”。
238+
239+
**配置:**
240+
这些设置主要通过环境变量控制。
241+
242+
- **`RETRY_HTTP_TOOL_CALL`** (环境变量):
243+
- 设置为 `"true"` 以启用 HTTP 工具调用的重试。
244+
- 设置为 `"false"` 以禁用此功能。
245+
- **默认行为:** `true` (如果环境变量未设置、为空或为无效值)。
246+
247+
- **`HTTP_TOOL_CALL_MAX_RETRIES`** (环境变量):
248+
- 指定在初次失败尝试*之后*的最大重试次数。例如,如果设置为 `"2"`,则会有一次初始尝试和最多两次重试尝试,总共最多三次尝试。
249+
- **默认行为:** `2` (如果环境变量未设置、为空或不是一个有效的整数)。
250+
251+
- **`HTTP_TOOL_CALL_RETRY_DELAY_BASE_MS`** (环境变量):
252+
- 用于指数退避计算的基准延迟(以毫秒为单位)。第 *n* 次重试(0索引)之前的延迟大约是 `HTTP_TOOL_CALL_RETRY_DELAY_BASE_MS * (2^n) + 抖动`
253+
- **默认行为:** `300` (毫秒) (如果环境变量未设置、为空或不是一个有效的整数)。
254+
255+
### 4. Stdio 工具调用的连接重试
256+
对于指向基于 Stdio 的后端服务器的 `tools/call` 操作,代理实现了针对连接错误(例如,进程崩溃或无响应)的重试机制。
257+
258+
**重试机制:**
259+
如果初始 Stdio 连接或工具调用失败,代理将尝试重新启动 Stdio 进程并重试请求。此机制类似于 HTTP 重试,使用指数退避策略。
260+
261+
**配置:**
262+
这些设置主要由环境变量控制。
263+
264+
- **`RETRY_STDIO_TOOL_CALL`** (环境变量):
265+
- 设置为 `"true"` 以启用 Stdio 工具调用重试。
266+
- 设置为 `"false"` 以禁用此功能。
267+
- **默认行为:** `true` (如果环境变量未设置、为空或为无效值)。
268+
269+
- **`STDIO_TOOL_CALL_MAX_RETRIES`** (环境变量):
270+
- 指定在初次失败尝试*之后*的最大重试尝试次数。例如,如果设置为 `"2"`,则将有一次初始尝试和最多两次重试尝试,总共最多三次尝试。
271+
- **默认行为:** `2` (如果环境变量未设置、为空或不是一个有效的整数)。
272+
273+
- **`STDIO_TOOL_CALL_RETRY_DELAY_BASE_MS`** (环境变量):
274+
- 用于指数退避计算的基准延迟(以毫秒为单位)。第 *n* 次重试(从 0 开始索引)之前的延迟大约是 `STDIO_TOOL_CALL_RETRY_DELAY_BASE_MS * (2^n) + 抖动`
275+
- **默认行为:** `300` (毫秒) (如果环境变量未设置、为空或不是一个有效的整数)。
276+
277+
**环境变量解析通用说明:**
278+
- 布尔环境变量(`RETRY_SSE_TOOL_CALL_ON_DISCONNECT``RETRY_HTTP_TOOL_CALL``RETRY_STDIO_TOOL_CALL`)如果其小写值恰好是 `"true"`,则被视为 `true`。任何其他值(包括空或未设置)将应用默认值,或者如果默认值为 `false` 则为 `false`(尽管对于这些特定变量,默认值为 `true`)。
279+
- 数字环境变量(`HTTP_TOOL_CALL_MAX_RETRIES``HTTP_TOOL_CALL_RETRY_DELAY_BASE_MS``STDIO_TOOL_CALL_MAX_RETRIES``STDIO_TOOL_CALL_RETRY_DELAY_BASE_MS`)被解析为十进制整数。如果解析失败(例如,值不是数字,或变量为空/未设置),则使用默认值。
280+
178281
## 开发
179282

180283
安装依赖:

README_ZH.md.delta_env_var_config_translation.md

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)