@jadchene/mcp-gateway-service 0.11.1 → 0.11.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README_zh.md CHANGED
@@ -1,205 +1,209 @@
1
- [English](./README.md) | 简体中文
2
-
3
- # MCP Gateway
4
-
5
- ## 简介
6
-
7
- MCP Gateway 把多个 MCP 服务收拢到一个入口。Agent 连接网关后只会看到 6 个路由工具,具体的下游服务、工具和 schema 在需要时再查询。
8
-
9
- 项目使用官方 TypeScript SDK v2,需要 Node.js 24 或更高版本。
10
-
11
- > [!IMPORTANT]
12
- > MCP Gateway v0.6.2 及后续版本仅接受 MCP `2026-07-28`、`2025-11-25` 和 `2025-06-18`。传输方式仅支持换行分隔 stdio 和单端点 Streamable HTTP。不支持独立 HTTP+SSE(`/sse`)、`Content-Length` framing 和其他协议版本。
13
-
14
- ## 为什么使用
15
-
16
- - 即使配置了很多 MCP 服务,也不会一次性占用大量 Agent 上下文。
17
- - Agent 只需接入一次,后续增减下游服务只改网关配置。
18
- - 可以按名称或描述查找工具,需要时再读取 schema。
19
- - Stdio 和 Streamable HTTP 服务使用同一套调用方式。
20
- - 可按工具名模式要求用户确认,或隐藏并禁止调用指定工具。
21
- - 修改配置文件后自动重载,不必重启网关。
22
-
23
- ## 快速开始
24
-
25
- ### 安装
26
-
27
- ```bash
28
- npm install -g @jadchene/mcp-gateway-service
29
- ```
30
-
31
- 创建 `config.json`,将 `your-mcp-service` 替换为已经安装的 MCP 服务命令:
32
-
33
- ```json
34
- {
35
- "services": [
36
- {
37
- "serviceId": "tools",
38
- "name": "Tools",
39
- "transport": {
40
- "type": "stdio",
41
- "command": "your-mcp-service"
42
- }
43
- }
44
- ]
45
- }
46
- ```
47
-
48
- ### Stdio
49
-
50
- 网关进程由 Agent 启动,不需要单独运行启动命令。
51
-
52
- Codex `config.toml`:
53
-
54
- ```toml
55
- [mcp_servers.gateway]
56
- command = "mcp-gateway-service"
57
- args = ["--config", "./config.json"]
58
- ```
59
-
60
- Claude Code:
61
-
62
- ```bash
63
- claude mcp add gateway -- mcp-gateway-service --config ./config.json
64
- ```
65
-
66
- ### Streamable HTTP
67
-
68
- 启动网关:
69
-
70
- ```bash
71
- mcp-gateway-service --http --config ./config.json
72
- ```
73
-
74
- 默认地址为 `http://127.0.0.1:3000/mcp`。
75
- HTTP 默认隐藏 `gateway_manage_service`。监听非回环地址时,必须通过 `MCP_GATEWAY_AUTH_TOKEN` 配置 Bearer Token。
76
-
77
- Codex `config.toml`:
78
-
79
- ```toml
80
- [mcp_servers.gateway]
81
- url = "http://127.0.0.1:3000/mcp"
82
- ```
83
-
84
- Claude Code:
85
-
86
- ```bash
87
- claude mcp add --transport http gateway http://127.0.0.1:3000/mcp
88
- ```
89
-
90
- ## 配置与工具
91
-
92
- ### 启动参数
93
-
94
- | 参数 | 说明 | 默认值 |
95
- | --- | --- | --- |
96
- | `--config <path>` | 配置文件路径。 | `MCP_GATEWAY_CONFIG`,其次为 `./config.json` |
97
- | `--http` | 启用 Streamable HTTP。 | 不启用 |
98
- | `--host <host>` | HTTP 监听地址。 | `127.0.0.1` |
99
- | `--port <port>` | HTTP 监听端口。 | `3000` |
100
- | `--path <path>` | HTTP endpoint 路径。 | `/mcp` |
101
- | `--auth-token-env <名称>` | 保存 HTTP Bearer Token 的环境变量名。 | `MCP_GATEWAY_AUTH_TOKEN` |
102
- | `--http-admin-tools` | 在已认证 HTTP 入口暴露 `gateway_manage_service`。 | 不启用 |
103
- | `--max-concurrent-requests <数量>` | HTTP 并发请求上限。 | `64` |
104
- | `--max-request-body-bytes <字节数>` | HTTP 请求体字节上限,最大可配置为 64 MiB,超限返回 `413`。 | `16777216`(16 MiB) |
105
- | `--max-legacy-sessions <数量>` | 有状态旧版 HTTP 会话的保留上限。 | `256` |
106
- | `--version`、`-v` | 输出已安装版本。 | — |
107
-
108
- 启用 `--http` 后,stdio 入口仍然可用。
109
-
110
- ### 配置文件
111
-
112
- ```json
113
- {
114
- "logging": {
115
- "enable": false,
116
- "path": "./logs/mcp-gateway.log"
117
- },
118
- "services": [
119
- {
120
- "serviceId": "local-tools",
121
- "name": "Local Tools",
122
- "confirmationRequiredTools": ["delete_*", "deploy_?"],
123
- "disabledTools": ["internal_*", "legacy_tool"],
124
- "transport": {
125
- "type": "stdio",
126
- "command": "your-mcp-service",
127
- "args": []
128
- }
129
- },
130
- {
131
- "serviceId": "remote-tools",
132
- "name": "Remote Tools",
133
- "transport": {
134
- "type": "http",
135
- "url": "http://127.0.0.1:3200/mcp"
136
- }
137
- }
138
- ]
139
- }
140
- ```
141
-
142
- | 字段 | 说明 |
143
- | --- | --- |
144
- | `services` | 下游 MCP 服务列表。 |
145
- | `serviceId` | 必填,网关工具使用的唯一服务标识。 |
146
- | `name` | 必填,服务名称。 |
147
- | `description` | 可选,服务说明。 |
148
- | `enable` | 可选,默认为 `true`。 |
149
- | `callTimeoutMs` | 可选,下游工具调用超时;默认 120 秒。 |
150
- | `confirmationRequiredTools` | 可选,填写区分大小写的完整工具名 glob 模式;`*` 匹配零个或多个字符,`?` 匹配一个字符。数组项必须唯一且非空,网关在调用匹配的工具前会向上游用户发起表单 elicitation。 |
151
- | `disabledTools` | 可选,使用相同 glob 语法。匹配的工具不会出现在工具列表和 schema 查询中,调用也会在到达下游服务前被拒绝。 |
152
- | `transport.type` | `stdio` 或 `http`。 |
153
- | `transport.command` | stdio 服务必填,启动命令。 |
154
- | `transport.args` | stdio 服务的可选命令参数。 |
155
- | `transport.cwd` | stdio 服务的可选工作目录。 |
156
- | `transport.env` | stdio 服务的可选环境变量。 |
157
- | `transport.inheritEnv` | 是否继承网关完整环境;默认为 `false`。 |
158
- | `transport.envAllowlist` | 额外传递给 stdio 服务的进程环境变量名。 |
159
- | `transport.url` | HTTP 服务必填,Streamable HTTP 地址。 |
160
- | `transport.headers` | HTTP 服务的可选静态请求头。 |
161
- | `logging.enable` | 是否写入文件日志,默认为 `false`。 |
162
- | `logging.path` | 日志文件路径;相对路径按配置文件目录解析。 |
163
- | `logging.maxBytes` | 当前日志轮转前的大小上限;默认 10 MiB。 |
164
-
165
- 网关会监听配置文件变化。配置有误时不会替换当前配置,服务继续按最后一次有效配置运行。其他示例见 [config.example.json](./config.example.json)。
166
-
167
- ### 网关工具
168
-
169
- | 工具 | 说明 |
170
- | --- | --- |
171
- | `gateway_list_services` | 按服务标识或描述搜索已启用服务,并可按可用状态筛选。 |
172
- | `gateway_get_service` | 查看服务连接状态、协议版本、服务端信息和最近一次错误。 |
173
- | `gateway_list_tools` | 按名称或描述搜索服务中的工具,可同时返回 schema。 |
174
- | `gateway_get_tool_schema` | 获取准确工具名称对应的 schema。 |
175
- | `gateway_manage_service` | 重连服务,或在配置文件中启用、禁用服务。 |
176
- | `gateway_call_tool` | 调用一个下游工具并返回 MCP 结果。 |
177
-
178
- 通常按 `gateway_list_services` → `gateway_list_tools` → 按需调用 `gateway_get_tool_schema` → `gateway_call_tool` 的顺序使用。下游工具没有参数时,`gateway_call_tool` 的 `arguments` 传 `{}`。
179
-
180
- 匹配 `confirmationRequiredTools` 的调用会展示服务、工具和参数,须由当前 MCP 会话确认后才执行;不支持表单确认的客户端无法调用。状态不确定的失败调用不会自动重试。
181
-
182
- 当一个工具同时匹配 `disabledTools` 和 `confirmationRequiredTools` 时,`disabledTools` 优先:工具会被隐藏并直接拒绝,不会发起确认。
183
-
184
- 支持 Skills 的 Agent 可以使用仓库内置的 [MCP Gateway Skill](./skills/mcp-gateway/SKILL.md) 完成工具发现和调用。
185
-
186
- ## 开发
187
-
188
- ```bash
189
- git clone https://github.com/jadchene/mcp-gateway.git
190
- cd mcp-gateway
191
- npm install
192
- npm run verify
193
- npm run dev -- --config ./config.json
194
- ```
195
-
196
- 构建并运行编译产物:
197
-
198
- ```bash
199
- npm run build
200
- npm start -- --config ./config.json
201
- ```
202
-
203
- ## 版权声明
204
-
205
- [MIT](./LICENSE)
1
+ [English](./README.md) | 简体中文
2
+
3
+ # MCP Gateway
4
+
5
+ ## 简介
6
+
7
+ MCP Gateway 把多个 MCP 服务收拢到一个入口。Agent 连接网关后只会看到 6 个路由工具,具体的下游服务、工具和 schema 在需要时再查询。
8
+
9
+ 项目使用官方 TypeScript SDK v2,需要 Node.js 24 或更高版本。
10
+
11
+ > [!IMPORTANT]
12
+ > MCP Gateway v0.6.2 及后续版本仅接受 MCP `2026-07-28`、`2025-11-25` 和 `2025-06-18`。传输方式仅支持换行分隔 stdio 和单端点 Streamable HTTP。不支持独立 HTTP+SSE(`/sse`)、`Content-Length` framing 和其他协议版本。
13
+
14
+ ## 为什么使用
15
+
16
+ - 即使配置了很多 MCP 服务,也不会一次性占用大量 Agent 上下文。
17
+ - Agent 只需接入一次,后续增减下游服务只改网关配置。
18
+ - 可以按名称或描述查找工具,需要时再读取 schema。
19
+ - Stdio 和 Streamable HTTP 服务使用同一套调用方式。
20
+ - 可按工具名模式要求用户确认,或隐藏并禁止调用指定工具。
21
+ - 修改配置文件后自动重载,不必重启网关。
22
+
23
+ ## 快速开始
24
+
25
+ ### 安装
26
+
27
+ ```bash
28
+ npm install -g @jadchene/mcp-gateway-service
29
+ ```
30
+
31
+ 创建 `config.json`,将 `your-mcp-service` 替换为已经安装的 MCP 服务命令:
32
+
33
+ ```json
34
+ {
35
+ "services": [
36
+ {
37
+ "serviceId": "tools",
38
+ "name": "Tools",
39
+ "transport": {
40
+ "type": "stdio",
41
+ "command": "your-mcp-service"
42
+ }
43
+ }
44
+ ]
45
+ }
46
+ ```
47
+
48
+ ### Stdio
49
+
50
+ 网关进程由 Agent 启动,不需要单独运行启动命令。
51
+
52
+ Codex `config.toml`:
53
+
54
+ ```toml
55
+ [mcp_servers.gateway]
56
+ command = "mcp-gateway-service"
57
+ args = ["--config", "./config.json"]
58
+ ```
59
+
60
+ Claude Code:
61
+
62
+ ```bash
63
+ claude mcp add gateway -- mcp-gateway-service --config ./config.json
64
+ ```
65
+
66
+ ### Streamable HTTP
67
+
68
+ 启动网关:
69
+
70
+ ```bash
71
+ mcp-gateway-service --http --config ./config.json
72
+ ```
73
+
74
+ 默认地址为 `http://127.0.0.1:3000/mcp`。
75
+ HTTP 默认隐藏 `gateway_manage_service`。监听非回环地址时,必须通过 `MCP_GATEWAY_AUTH_TOKEN` 配置 Bearer Token。
76
+
77
+ Codex `config.toml`:
78
+
79
+ ```toml
80
+ [mcp_servers.gateway]
81
+ url = "http://127.0.0.1:3000/mcp"
82
+ ```
83
+
84
+ Claude Code:
85
+
86
+ ```bash
87
+ claude mcp add --transport http gateway http://127.0.0.1:3000/mcp
88
+ ```
89
+
90
+ ## 配置与工具
91
+
92
+ ### 启动参数
93
+
94
+ | 参数 | 说明 | 默认值 |
95
+ | --- | --- | --- |
96
+ | `--config <path>` | 配置文件路径。 | `MCP_GATEWAY_CONFIG`,其次为 `./config.json` |
97
+ | `--http` | 启用 Streamable HTTP。 | 不启用 |
98
+ | `--host <host>` | HTTP 监听地址。 | `127.0.0.1` |
99
+ | `--port <port>` | HTTP 监听端口。 | `3000` |
100
+ | `--path <path>` | HTTP endpoint 路径。 | `/mcp` |
101
+ | `--auth-token-env <名称>` | 保存 HTTP Bearer Token 的环境变量名。 | `MCP_GATEWAY_AUTH_TOKEN` |
102
+ | `--http-admin-tools` | 在已认证 HTTP 入口暴露 `gateway_manage_service`。 | 不启用 |
103
+ | `--max-concurrent-requests <数量>` | HTTP 并发请求上限。 | `64` |
104
+ | `--max-request-body-bytes <字节数>` | HTTP 请求体字节上限,最大可配置为 64 MiB,超限返回 `413`。 | `16777216`(16 MiB) |
105
+ | `--max-legacy-sessions <数量>` | 有状态旧版 HTTP 会话的保留上限。 | `256` |
106
+ | `--version`、`-v` | 输出已安装版本。 | — |
107
+
108
+ 启用 `--http` 后,stdio 入口仍然可用。
109
+
110
+ ### 配置文件
111
+
112
+ ```json
113
+ {
114
+ "logging": {
115
+ "enable": false,
116
+ "path": "./logs/mcp-gateway.log"
117
+ },
118
+ "services": [
119
+ {
120
+ "serviceId": "local-tools",
121
+ "name": "Local Tools",
122
+ "confirmationRequiredTools": ["delete_*", "deploy_?"],
123
+ "codexAutoReview": false,
124
+ "disabledTools": ["internal_*", "legacy_tool"],
125
+ "transport": {
126
+ "type": "stdio",
127
+ "command": "your-mcp-service",
128
+ "args": []
129
+ }
130
+ },
131
+ {
132
+ "serviceId": "remote-tools",
133
+ "name": "Remote Tools",
134
+ "transport": {
135
+ "type": "http",
136
+ "url": "http://127.0.0.1:3200/mcp"
137
+ }
138
+ }
139
+ ]
140
+ }
141
+ ```
142
+
143
+ | 字段 | 说明 |
144
+ | --- | --- |
145
+ | `services` | 下游 MCP 服务列表。 |
146
+ | `serviceId` | 必填,网关工具使用的唯一服务标识。 |
147
+ | `name` | 必填,服务名称。 |
148
+ | `description` | 可选,服务说明。 |
149
+ | `enable` | 可选,默认为 `true`。 |
150
+ | `callTimeoutMs` | 可选,下游工具调用超时;默认 120 秒。 |
151
+ | `confirmationRequiredTools` | 可选,填写区分大小写的完整工具名 glob 模式;`*` 匹配零个或多个字符,`?` 匹配一个字符。数组项必须唯一且非空,网关在调用匹配的工具前会向上游用户发起表单 elicitation。 |
152
+ | `codexAutoReview` | 可选 boolean,默认 `false`;设为 `true` 仅启用该服务的 `confirmationRequiredTools` 触发的网关自定义确认的 Codex 自动审批元数据。 |
153
+ | `disabledTools` | 可选,使用相同 glob 语法。匹配的工具不会出现在工具列表和 schema 查询中,调用也会在到达下游服务前被拒绝。 |
154
+ | `transport.type` | `stdio` 或 `http`。 |
155
+ | `transport.command` | stdio 服务必填,启动命令。 |
156
+ | `transport.args` | stdio 服务的可选命令参数。 |
157
+ | `transport.cwd` | stdio 服务的可选工作目录。 |
158
+ | `transport.env` | stdio 服务的可选环境变量。 |
159
+ | `transport.inheritEnv` | 是否继承网关完整环境;默认为 `false`。 |
160
+ | `transport.envAllowlist` | 额外传递给 stdio 服务的进程环境变量名。 |
161
+ | `transport.url` | HTTP 服务必填,Streamable HTTP 地址。 |
162
+ | `transport.headers` | HTTP 服务的可选静态请求头。 |
163
+ | `logging.enable` | 是否写入文件日志,默认为 `false`。 |
164
+ | `logging.path` | 日志文件路径;相对路径按配置文件目录解析。 |
165
+ | `logging.maxBytes` | 当前日志轮转前的大小上限;默认 10 MiB。 |
166
+
167
+ 网关会监听配置文件变化。配置有误时不会替换当前配置,服务继续按最后一次有效配置运行。其他示例见 [config.example.json](./config.example.json)。
168
+
169
+ ### 网关工具
170
+
171
+ | 工具 | 说明 |
172
+ | --- | --- |
173
+ | `gateway_list_services` | 按服务标识或描述搜索已启用服务,并可按可用状态筛选。 |
174
+ | `gateway_get_service` | 查看服务连接状态、协议版本、服务端信息和最近一次错误。 |
175
+ | `gateway_list_tools` | 按名称或描述搜索服务中的工具,可同时返回 schema。 |
176
+ | `gateway_get_tool_schema` | 获取准确工具名称对应的 schema。 |
177
+ | `gateway_manage_service` | 重连服务,或在配置文件中启用、禁用服务。 |
178
+ | `gateway_call_tool` | 调用一个下游工具并返回 MCP 结果。 |
179
+
180
+ 通常按 `gateway_list_services` → `gateway_list_tools` → 按需调用 `gateway_get_tool_schema` → `gateway_call_tool` 的顺序使用。下游工具没有参数时,`gateway_call_tool` 的 `arguments` 传 `{}`。
181
+
182
+ 匹配 `confirmationRequiredTools` 的调用会展示服务、工具和参数,须由当前 MCP 会话确认后才执行;不支持表单确认的客户端无法调用。状态不确定的失败调用不会自动重试。
183
+
184
+ Codex 自动审批扩展默认禁用;在指定服务的 `services[]` 配置块中显式设置 `"codexAutoReview": true`,仅为 `confirmationRequiredTools` 触发的网关自定义确认添加审批元数据。下游确认表单及其 `_meta` 原样透传,下游自动审批配置独立生效。Codex 开启 `approvals_reviewer = "auto_review"` 时自动审查,否则使用正常的 Accept / Decline / Cancel 确认;审批结果仍由 Codex 策略决定。
185
+
186
+ 当一个工具同时匹配 `disabledTools` 和 `confirmationRequiredTools` 时,`disabledTools` 优先:工具会被隐藏并直接拒绝,不会发起确认。
187
+
188
+ 支持 Skills 的 Agent 可以使用仓库内置的 [MCP Gateway Skill](./skills/mcp-gateway/SKILL.md) 完成工具发现和调用。
189
+
190
+ ## 开发
191
+
192
+ ```bash
193
+ git clone https://github.com/jadchene/mcp-gateway.git
194
+ cd mcp-gateway
195
+ npm install
196
+ npm run verify
197
+ npm run dev -- --config ./config.json
198
+ ```
199
+
200
+ 构建并运行编译产物:
201
+
202
+ ```bash
203
+ npm run build
204
+ npm start -- --config ./config.json
205
+ ```
206
+
207
+ ## 版权声明
208
+
209
+ [MIT](./LICENSE)
@@ -1,45 +1,47 @@
1
- {
2
- "logging": {
3
- "enable": false,
4
- "path": "./logs/mcp-gateway.log",
5
- "maxBytes": 10485760
6
- },
7
- "services": [
8
- {
9
- "serviceId": "demo-echo",
10
- "enable": false,
11
- "name": "Demo Echo Service",
12
- "description": "Sample echo MCP service.",
13
- "callTimeoutMs": 120000,
14
- "confirmationRequiredTools": [
15
- "echo*"
16
- ],
17
- "disabledTools": [
18
- "echo_debug*"
19
- ],
20
- "transport": {
21
- "type": "stdio",
22
- "command": "node",
23
- "args": [
24
- "--experimental-strip-types",
25
- "examples/echo-service.ts"
26
- ],
27
- "inheritEnv": false,
28
- "envAllowlist": []
29
- }
30
- },
31
- {
32
- "serviceId": "remote-http",
33
- "enable": false,
34
- "name": "Remote Streamable HTTP Service",
35
- "description": "Example downstream MCP service over Streamable HTTP.",
36
- "transport": {
37
- "type": "http",
38
- "url": "http://127.0.0.1:3200/mcp",
39
- "headers": {
40
- "Authorization": "Bearer example-token"
41
- }
42
- }
43
- }
44
- ]
45
- }
1
+ {
2
+ "logging": {
3
+ "enable": false,
4
+ "path": "./logs/mcp-gateway.log",
5
+ "maxBytes": 10485760
6
+ },
7
+ "services": [
8
+ {
9
+ "serviceId": "demo-echo",
10
+ "enable": false,
11
+ "name": "Demo Echo Service",
12
+ "description": "Sample echo MCP service.",
13
+ "callTimeoutMs": 120000,
14
+ "confirmationRequiredTools": [
15
+ "echo*"
16
+ ],
17
+ "disabledTools": [
18
+ "echo_debug*"
19
+ ],
20
+ "transport": {
21
+ "type": "stdio",
22
+ "command": "node",
23
+ "args": [
24
+ "--experimental-strip-types",
25
+ "examples/echo-service.ts"
26
+ ],
27
+ "inheritEnv": false,
28
+ "envAllowlist": []
29
+ },
30
+ "codexAutoReview": false
31
+ },
32
+ {
33
+ "serviceId": "remote-http",
34
+ "enable": false,
35
+ "name": "Remote Streamable HTTP Service",
36
+ "description": "Example downstream MCP service over Streamable HTTP.",
37
+ "transport": {
38
+ "type": "http",
39
+ "url": "http://127.0.0.1:3200/mcp",
40
+ "headers": {
41
+ "Authorization": "Bearer example-token"
42
+ }
43
+ },
44
+ "codexAutoReview": false
45
+ }
46
+ ]
47
+ }
package/dist/config.js CHANGED
@@ -85,6 +85,7 @@ function validateServiceConfig(input) {
85
85
  }
86
86
  const serviceId = requireNonEmptyString(input.serviceId, "service.serviceId");
87
87
  const enable = optionalBoolean(input.enable, `service '${serviceId}' enable`) ?? true;
88
+ const codexAutoReview = optionalBoolean(input.codexAutoReview, `service '${serviceId}' codexAutoReview`) ?? false;
88
89
  const name = requireNonEmptyString(input.name, `service '${serviceId}' name`);
89
90
  const description = optionalString(input.description, `service '${serviceId}' description`);
90
91
  const callTimeoutMs = optionalInteger(input.callTimeoutMs, `service '${serviceId}' callTimeoutMs`, 1, 24 * 60 * 60 * 1000);
@@ -93,6 +94,7 @@ function validateServiceConfig(input) {
93
94
  return {
94
95
  serviceId,
95
96
  enable,
97
+ codexAutoReview,
96
98
  name,
97
99
  description,
98
100
  transport: validateTransportConfig(serviceId, input.transport),
@@ -1,4 +1,5 @@
1
1
  import { ToolConfirmationInterceptor } from "./mcp/tool-confirmation.js";
2
+ import { withCodexApprovalMeta } from "./mcp/codex-approval.js";
2
3
  import { matchesAnyToolNamePattern } from "./tool-name-pattern.js";
3
4
  /**
4
5
  * Handles MCP JSON-RPC requests independently of the outer transport.
@@ -156,7 +157,7 @@ export class McpGatewayEngine {
156
157
  }
157
158
  if (this.toolConfirmation.handlesState(context.requestState)
158
159
  || matchesAnyToolNamePattern(toolName, snapshot?.config.confirmationRequiredTools)) {
159
- return this.toolConfirmation.execute(serviceId, toolName, toolArgs, context, invoke);
160
+ return this.toolConfirmation.execute(serviceId, toolName, toolArgs, context, invoke, (params) => withCodexApprovalMeta(params, context.clientInfo, toolName, toolArgs, serviceId, snapshot?.config.name, snapshot?.config.codexAutoReview === true));
160
161
  }
161
162
  return invoke(context);
162
163
  }
@@ -240,7 +241,7 @@ export function buildGatewayTools(options = {}) {
240
241
  },
241
242
  {
242
243
  name: "gateway_call_tool",
243
- description: "Calls one downstream tool and forwards its result. Side effects and confirmation requirements depend on that tool.",
244
+ description: "Calls one downstream tool and forwards its result.",
244
245
  inputSchema: objectSchema(["serviceId", "toolName", "arguments"], {
245
246
  serviceId: stringSchema("Exact, case-sensitive service ID from gateway_list_services."),
246
247
  toolName: stringSchema("Exact, case-sensitive tool name from gateway_list_tools."),
@@ -0,0 +1,44 @@
1
+ /**
2
+ * 按 Codex 0.160.0 的私有约定构造审批元数据,不改变服务端权限校验。
3
+ * @author chenjd
4
+ * @created 2026-10-02 22:15:29
5
+ */
6
+ export function buildCodexApprovalMeta(clientInfo, toolName, toolParams, description, sensitive = false) {
7
+ if (typeof clientInfo !== "object" || clientInfo === null || Array.isArray(clientInfo)) {
8
+ return undefined;
9
+ }
10
+ const name = clientInfo.name;
11
+ if (typeof name !== "string" || !/^codex(?:$|[-_])/i.test(name.trim())) {
12
+ return undefined;
13
+ }
14
+ return {
15
+ codex_request_type: "approval_request",
16
+ codex_approval_kind: "mcp_tool_call",
17
+ codex_strict_auto_review: true,
18
+ ...(sensitive ? { codex_sensitive_action: true } : {}),
19
+ tool_name: toolName,
20
+ tool_description: description,
21
+ tool_params: structuredClone(toolParams)
22
+ };
23
+ }
24
+ /** 仅为网关自行生成的空确认表单添加 Codex 扩展。 */
25
+ export function withCodexApprovalMeta(params, clientInfo, toolName, toolParams, serviceId, serviceName, enabled = false) {
26
+ const existing = params._meta ?? {};
27
+ if (!enabled || Object.keys(params.requestedSchema.properties).length > 0
28
+ || existing.codex_requires_user_input === true
29
+ || (existing.codex_approval_kind !== undefined && existing.codex_approval_kind !== "mcp_tool_call")) {
30
+ return params;
31
+ }
32
+ // 网关要求显式确认的调用统一同步审查,避免未知工具提前执行。
33
+ const approvalMeta = buildCodexApprovalMeta(clientInfo, toolName, toolParams, params.message, true);
34
+ if (!approvalMeta)
35
+ return params;
36
+ const meta = {
37
+ ...existing,
38
+ ...approvalMeta,
39
+ connector_id: serviceId,
40
+ connector_name: serviceName ?? serviceId
41
+ };
42
+ delete meta.persist;
43
+ return { ...params, _meta: meta };
44
+ }
@@ -1,4 +1,4 @@
1
- import { StreamableHTTPClientTransport } from "@modelcontextprotocol/client";
1
+ import { SdkHttpError, StreamableHTTPClientTransport } from "@modelcontextprotocol/client";
2
2
  import { SdkMcpClient } from "./sdk-client.js";
3
3
  /**
4
4
  * Connects to a downstream HTTP MCP service through the official SDK v2.
@@ -18,6 +18,35 @@ export class StreamableHttpClient extends SdkMcpClient {
18
18
  }
19
19
  }));
20
20
  }
21
+ /** 仅对已建立的旧版 HTTP 会话识别失效响应,不将认证或参数错误视为重连信号。 */
22
+ isSessionExpired(error, client) {
23
+ const transport = client?.transport;
24
+ if (!(error instanceof SdkHttpError)
25
+ || !(transport instanceof StreamableHTTPClientTransport)
26
+ || !transport.sessionId
27
+ || client?.getProtocolEra() !== "legacy") {
28
+ return false;
29
+ }
30
+ if (error.status === 404) {
31
+ return true;
32
+ }
33
+ if (error.status !== 400 || typeof error.data.text !== "string") {
34
+ return false;
35
+ }
36
+ try {
37
+ const response = JSON.parse(error.data.text);
38
+ // 微信开发者工具在旧会话不存在时返回通用 Bad Request,且响应不关联请求 ID。
39
+ return response?.error === "Invalid or missing session ID"
40
+ || (response?.jsonrpc === "2.0"
41
+ && response?.id === null
42
+ && response?.error?.code === -32000
43
+ && response?.error?.message === "Bad Request")
44
+ || response?.error?.message === "Session not found";
45
+ }
46
+ catch {
47
+ return false;
48
+ }
49
+ }
21
50
  }
22
51
  /**
23
52
  * Applies static configured headers without allowing them to replace per-request SDK headers.