@jadchene/mcp-gateway-service 0.6.2 → 0.6.4
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.md +5 -3
- package/README_zh.md +5 -3
- package/dist/mcp/client.js +42 -5
- package/dist/mcp/form-elicitation-bridge.js +300 -0
- package/dist/mcp/sdk-client.js +19 -5
- package/dist/mcp/server.js +6 -1
- package/package.json +1 -1
- package/skills/mcp-gateway/SKILL.md +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ English | [简体中文](./README_zh.md)
|
|
|
4
4
|
|
|
5
5
|
MCP Gateway is a token-efficient Model Context Protocol gateway. It exposes six stable routing tools instead of flattening every downstream tool into each client's startup context.
|
|
6
6
|
|
|
7
|
-
MCP Gateway v0.6.
|
|
7
|
+
MCP Gateway v0.6.4 uses the official TypeScript SDK v2.
|
|
8
8
|
|
|
9
9
|
> [!IMPORTANT]
|
|
10
10
|
> MCP Gateway v0.6.2 and later accept only MCP `2026-07-28`, `2025-11-25`, and `2025-06-18`, using standard newline-delimited stdio or single-endpoint Streamable HTTP. Standalone HTTP+SSE (`/sse`), `Content-Length` framing, and other protocol revisions are not supported.
|
|
@@ -16,7 +16,7 @@ MCP Gateway v0.6.2 uses the official TypeScript SDK v2.
|
|
|
16
16
|
- Automatic negotiation among MCP `2026-07-28`, `2025-11-25`, and `2025-06-18`.
|
|
17
17
|
- All three standard revisions are served automatically on inbound transports.
|
|
18
18
|
- Full downstream tool metadata and JSON Schema 2020-12 preservation.
|
|
19
|
-
-
|
|
19
|
+
- Form-mode elicitation bridging across all supported revisions, opaque `requestState` forwarding, arbitrary JSON `structuredContent`, cancellation, and `x-mcp-header` handling through the SDK.
|
|
20
20
|
- SDK-managed response cache hints and client-local private caches.
|
|
21
21
|
- Hot reload, lifecycle recovery, atomic invalid-config rejection, and optional file logging.
|
|
22
22
|
- Host and Origin validation for inbound HTTP. Operational logs never use MCP stdout.
|
|
@@ -43,6 +43,8 @@ When HTTP is enabled, the process serves both stdio and HTTP. Use `--version` or
|
|
|
43
43
|
|
|
44
44
|
Protocol selection is automatic. The gateway and its downstream clients prefer MCP `2026-07-28`, then negotiate `2025-11-25` or `2025-06-18` when required by the peer. These are the only accepted protocol revisions. Inbound Streamable HTTP response shaping is selected automatically by the official SDK.
|
|
45
45
|
|
|
46
|
+
Form-mode elicitation works across all three revisions. A 2025 downstream `elicitation/create` request is forwarded directly to a 2025 upstream client; for a 2026 upstream client, it is converted into `input_required` and resumed with the returned opaque `requestState` and `inputResponses`. Tool calls on each 2025 downstream connection are serialized so form responses cannot cross between calls.
|
|
47
|
+
|
|
46
48
|
For MCP `2025-11-25`, the gateway negotiates only capabilities it implements. Experimental Tasks, URL-mode elicitation, and sampling tool calls are not advertised.
|
|
47
49
|
|
|
48
50
|
## Service Configuration
|
|
@@ -117,7 +119,7 @@ Recommended flow:
|
|
|
117
119
|
3. Reuse schemas returned by `includeSchema: true`, or batch exact names with `gateway_get_tool_schema`.
|
|
118
120
|
4. Call `gateway_call_tool`; always provide `arguments`, using `{}` for a no-argument tool.
|
|
119
121
|
|
|
120
|
-
`gateway_call_tool` intentionally has no fixed output schema because downstream `structuredContent` may be any JSON value. MCP `2026-07-28` MRTR
|
|
122
|
+
`gateway_call_tool` intentionally has no fixed output schema because downstream `structuredContent` may be any JSON value. MCP `2026-07-28` MRTR results and bridged 2025 form elicitation are returned as `input_required` without automatic approval; the upstream client must declare form elicitation support and retry with matching `inputResponses` and the opaque `requestState`.
|
|
121
123
|
|
|
122
124
|
## Client Examples
|
|
123
125
|
|
package/README_zh.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
MCP Gateway 是一个节省 token 的 Model Context Protocol 网关。它不把所有下游工具展开到每个客户端的启动上下文,而是通过 6 个稳定工具完成发现、管理和转发。
|
|
6
6
|
|
|
7
|
-
MCP Gateway v0.6.
|
|
7
|
+
MCP Gateway v0.6.4 使用官方 TypeScript SDK v2。
|
|
8
8
|
|
|
9
9
|
> [!IMPORTANT]
|
|
10
10
|
> MCP Gateway v0.6.2 及后续版本仅接受 MCP `2026-07-28`、`2025-11-25` 和 `2025-06-18`,传输方式仅支持标准换行分隔 stdio 和单端点 Streamable HTTP。不支持独立 HTTP+SSE(`/sse`)、`Content-Length` framing 和其他协议版本。
|
|
@@ -16,7 +16,7 @@ MCP Gateway v0.6.2 使用官方 TypeScript SDK v2。
|
|
|
16
16
|
- 在 MCP `2026-07-28`、`2025-11-25` 与 `2025-06-18` 之间自动协商。
|
|
17
17
|
- 入站传输自动服务这三个标准版本。
|
|
18
18
|
- 完整转发下游工具元数据和 JSON Schema 2020-12。
|
|
19
|
-
-
|
|
19
|
+
- 在全部受支持版本间桥接表单模式 elicitation,并由 SDK 处理 opaque `requestState`、任意 JSON `structuredContent`、取消传播和 `x-mcp-header`。
|
|
20
20
|
- 遵守 SDK 缓存提示,private 缓存按客户端实例隔离。
|
|
21
21
|
- 支持配置热更新、生命周期恢复、无效配置原子拒绝和可选文件日志。
|
|
22
22
|
- 入站 HTTP 校验 Host 和 Origin;运行日志不会写入 MCP stdout。
|
|
@@ -43,6 +43,8 @@ mcp-gateway-service --config ./config.json --http --host 127.0.0.1 --port 3100 -
|
|
|
43
43
|
|
|
44
44
|
协议选择完全自动:网关及其下游客户端优先协商 MCP `2026-07-28`,并根据对端能力协商 `2025-11-25` 或 `2025-06-18`。只接受这三个协议版本。入站 Streamable HTTP 的响应形态由官方 SDK 自动选择。
|
|
45
45
|
|
|
46
|
+
三个协议版本都支持表单模式 elicitation。2025 下游发出的 `elicitation/create` 会直接转发给 2025 上游客户端;面对 2026 上游客户端时,网关会将其转换为 `input_required`,再使用返回的 opaque `requestState` 和 `inputResponses` 续调。每个 2025 下游连接上的工具调用会串行执行,避免表单响应串到其他调用。
|
|
47
|
+
|
|
46
48
|
对于 MCP `2025-11-25`,网关只协商已实现的能力,不声明实验性 Tasks、URL 模式 elicitation 和 sampling 工具调用。
|
|
47
49
|
|
|
48
50
|
## 服务配置
|
|
@@ -117,7 +119,7 @@ MCP `2026-07-28` HTTP 是无状态的:每个请求都是独立的 `POST /mcp`
|
|
|
117
119
|
3. 复用 `includeSchema: true` 返回的 schema;已知准确名称时用 `gateway_get_tool_schema` 一次批量查询。
|
|
118
120
|
4. 使用 `gateway_call_tool` 调用;`arguments` 始终必填,无参数工具传 `{}`。
|
|
119
121
|
|
|
120
|
-
`gateway_call_tool` 不声明固定输出 schema,因为下游 `structuredContent` 可以是任意 JSON。MCP `2026-07-28` MRTR
|
|
122
|
+
`gateway_call_tool` 不声明固定输出 schema,因为下游 `structuredContent` 可以是任意 JSON。MCP `2026-07-28` MRTR 结果和由 2025 表单 elicitation 转换出的 `input_required` 都不会被自动同意;上游必须声明表单 elicitation 支持,并使用匹配的 `inputResponses` 和 opaque `requestState` 重试。
|
|
121
123
|
|
|
122
124
|
## 客户端示例
|
|
123
125
|
|
package/dist/mcp/client.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { execFileSync } from "node:child_process";
|
|
2
|
-
import {
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { dirname, isAbsolute, relative, resolve } from "node:path";
|
|
3
4
|
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
|
4
5
|
import { SdkMcpClient } from "./sdk-client.js";
|
|
5
6
|
/**
|
|
@@ -39,11 +40,24 @@ function createStandardTransport(command, args, cwd, env, logger, serviceId) {
|
|
|
39
40
|
/**
|
|
40
41
|
* Resolves a configured command into a directly spawnable executable on Windows.
|
|
41
42
|
*/
|
|
42
|
-
function resolveCommandSpec(command, args) {
|
|
43
|
+
export function resolveCommandSpec(command, args) {
|
|
43
44
|
if (process.platform !== "win32" || /[\\/]/.test(command) || /\.[A-Za-z0-9]+$/.test(command)) {
|
|
44
45
|
return { command, args };
|
|
45
46
|
}
|
|
46
|
-
const
|
|
47
|
+
const candidates = resolveViaWhere(command);
|
|
48
|
+
const native = candidates.find((candidate) => /\.(?:com|exe)$/i.test(candidate));
|
|
49
|
+
if (native) {
|
|
50
|
+
return { command: native, args };
|
|
51
|
+
}
|
|
52
|
+
const commandShim = candidates.find((candidate) => candidate.toLowerCase().endsWith(".cmd"));
|
|
53
|
+
const nodeExecutable = resolveViaWhere("node.exe").find((candidate) => candidate.toLowerCase().endsWith("node.exe"));
|
|
54
|
+
if (commandShim && nodeExecutable) {
|
|
55
|
+
const npmLaunch = resolveWindowsNpmShim(commandShim, nodeExecutable, args);
|
|
56
|
+
if (npmLaunch) {
|
|
57
|
+
return npmLaunch;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const resolved = resolveViaPowerShell(command);
|
|
47
61
|
if (!resolved) {
|
|
48
62
|
return { command, args };
|
|
49
63
|
}
|
|
@@ -53,6 +67,29 @@ function resolveCommandSpec(command, args) {
|
|
|
53
67
|
}
|
|
54
68
|
return { command: resolved, args };
|
|
55
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Converts a standard Windows npm command shim into a directly managed Node.js process.
|
|
72
|
+
*/
|
|
73
|
+
export function resolveWindowsNpmShim(shimPath, nodeExecutable, args) {
|
|
74
|
+
try {
|
|
75
|
+
const base = dirname(shimPath);
|
|
76
|
+
const content = readFileSync(shimPath, "utf8");
|
|
77
|
+
const matches = Array.from(content.matchAll(/"%dp0%\\([^"\r\n]+\.js)"/gi));
|
|
78
|
+
const relativeScript = matches.at(-1)?.[1];
|
|
79
|
+
if (!relativeScript) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
const script = resolve(base, relativeScript.replace(/\\/g, "/"));
|
|
83
|
+
const relativePath = relative(base, script);
|
|
84
|
+
if (relativePath.startsWith("..") || isAbsolute(relativePath) || !existsSync(script)) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
return { command: nodeExecutable, args: [script, ...args] };
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
56
93
|
/**
|
|
57
94
|
* Resolves a command name through PowerShell so script shims map to their real targets.
|
|
58
95
|
*/
|
|
@@ -82,10 +119,10 @@ function resolveViaWhere(command) {
|
|
|
82
119
|
encoding: "utf8",
|
|
83
120
|
stdio: ["ignore", "pipe", "ignore"]
|
|
84
121
|
});
|
|
85
|
-
return output.split(/\r?\n/).map((line) => line.trim()).
|
|
122
|
+
return output.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
|
|
86
123
|
}
|
|
87
124
|
catch {
|
|
88
|
-
return
|
|
125
|
+
return [];
|
|
89
126
|
}
|
|
90
127
|
}
|
|
91
128
|
/**
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { isDeepStrictEqual } from "node:util";
|
|
3
|
+
import { ProtocolError, ProtocolErrorCode } from "@modelcontextprotocol/client";
|
|
4
|
+
const BRIDGE_STATE_PREFIX = "mcp-gateway-form-elicitation-v1.";
|
|
5
|
+
const INPUT_RESPONSE_KEY = "form";
|
|
6
|
+
/**
|
|
7
|
+
* Serializes 2025-era downstream calls and bridges their push-style form elicitation.
|
|
8
|
+
*/
|
|
9
|
+
export class FormElicitationBridge {
|
|
10
|
+
mutex = new AsyncMutex();
|
|
11
|
+
activeCall = null;
|
|
12
|
+
/**
|
|
13
|
+
* Executes or resumes one 2025-era downstream tool call.
|
|
14
|
+
*/
|
|
15
|
+
async execute(name, args, context, invoke) {
|
|
16
|
+
const requestState = context.requestState;
|
|
17
|
+
if (requestState?.startsWith(BRIDGE_STATE_PREFIX)) {
|
|
18
|
+
return this.resume(requestState, name, args, context);
|
|
19
|
+
}
|
|
20
|
+
const release = await this.mutex.acquire(context.signal);
|
|
21
|
+
const state = createActiveCall(name, args, context.elicitForm, release);
|
|
22
|
+
this.activeCall = state;
|
|
23
|
+
void invoke(state.abortController.signal).then((value) => this.settle(state, { status: "fulfilled", value }), (error) => this.settle(state, { status: "rejected", error: normalizeError(error) }));
|
|
24
|
+
return this.waitForResult(state, context);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Handles one downstream form elicitation request for the active tool call.
|
|
28
|
+
*/
|
|
29
|
+
async handle(params, signal) {
|
|
30
|
+
const state = this.activeCall;
|
|
31
|
+
if (!state) {
|
|
32
|
+
throw new ProtocolError(ProtocolErrorCode.InvalidRequest, "Received form elicitation without an active downstream tool call.");
|
|
33
|
+
}
|
|
34
|
+
if (state.elicitForm) {
|
|
35
|
+
return state.elicitForm(params, signal);
|
|
36
|
+
}
|
|
37
|
+
if (state.pendingElicitation) {
|
|
38
|
+
throw new ProtocolError(ProtocolErrorCode.InvalidRequest, "Received overlapping form elicitation requests for one downstream tool call.");
|
|
39
|
+
}
|
|
40
|
+
const response = createDeferred();
|
|
41
|
+
const abort = () => response.reject(abortReason(signal));
|
|
42
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
43
|
+
response.promise.finally(() => signal.removeEventListener("abort", abort)).catch(() => undefined);
|
|
44
|
+
state.pendingElicitation = { params: normalizeFormParams(params), response };
|
|
45
|
+
notifyStateChanged(state);
|
|
46
|
+
return response.promise;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Aborts any parked call before its downstream connection is replaced.
|
|
50
|
+
*/
|
|
51
|
+
reset(reason) {
|
|
52
|
+
const state = this.activeCall;
|
|
53
|
+
if (!state) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
state.outcome = { status: "rejected", error: reason };
|
|
57
|
+
state.abortController.abort(reason);
|
|
58
|
+
state.pendingElicitation?.response.reject(reason);
|
|
59
|
+
notifyStateChanged(state);
|
|
60
|
+
this.release(state);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Resumes a parked call with the upstream client's form response.
|
|
64
|
+
*/
|
|
65
|
+
async resume(requestState, name, args, context) {
|
|
66
|
+
const state = this.activeCall;
|
|
67
|
+
if (!state || state.requestState !== requestState) {
|
|
68
|
+
throw new ProtocolError(ProtocolErrorCode.InvalidParams, "Form elicitation state is invalid or has expired.");
|
|
69
|
+
}
|
|
70
|
+
if (state.name !== name || !isDeepStrictEqual(state.args, args)) {
|
|
71
|
+
throw new ProtocolError(ProtocolErrorCode.InvalidParams, "Form elicitation retry does not match the original downstream tool call.");
|
|
72
|
+
}
|
|
73
|
+
const pending = state.pendingElicitation;
|
|
74
|
+
if (pending && Object.hasOwn(context.inputResponses ?? {}, INPUT_RESPONSE_KEY)) {
|
|
75
|
+
state.pendingElicitation = null;
|
|
76
|
+
pending.response.resolve(context.inputResponses?.[INPUT_RESPONSE_KEY]);
|
|
77
|
+
notifyStateChanged(state);
|
|
78
|
+
}
|
|
79
|
+
return this.waitForResult(state, context);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Waits until the downstream call completes or asks the modern upstream for input.
|
|
83
|
+
*/
|
|
84
|
+
async waitForResult(state, context) {
|
|
85
|
+
state.parked = false;
|
|
86
|
+
const detachAbort = forwardAbort(context.signal, state.abortController);
|
|
87
|
+
try {
|
|
88
|
+
while (!state.outcome && !state.pendingElicitation) {
|
|
89
|
+
const changed = state.changed.promise;
|
|
90
|
+
if (!state.outcome && !state.pendingElicitation) {
|
|
91
|
+
await changed;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (state.outcome) {
|
|
95
|
+
const outcome = state.outcome;
|
|
96
|
+
this.release(state);
|
|
97
|
+
if (outcome.status === "rejected") {
|
|
98
|
+
throw outcome.error;
|
|
99
|
+
}
|
|
100
|
+
return outcome.value;
|
|
101
|
+
}
|
|
102
|
+
state.parked = true;
|
|
103
|
+
return buildInputRequiredResult(state);
|
|
104
|
+
}
|
|
105
|
+
finally {
|
|
106
|
+
detachAbort();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Records the detached downstream call result and wakes any active waiter.
|
|
111
|
+
*/
|
|
112
|
+
settle(state, outcome) {
|
|
113
|
+
if (this.activeCall !== state) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
state.outcome = outcome;
|
|
117
|
+
if (state.pendingElicitation) {
|
|
118
|
+
const reason = outcome.status === "rejected"
|
|
119
|
+
? outcome.error
|
|
120
|
+
: new Error("Downstream tool call completed before form elicitation was answered.");
|
|
121
|
+
state.pendingElicitation.response.reject(reason);
|
|
122
|
+
state.pendingElicitation = null;
|
|
123
|
+
}
|
|
124
|
+
notifyStateChanged(state);
|
|
125
|
+
if (state.parked) {
|
|
126
|
+
this.release(state);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Releases the per-client serialization lock exactly once.
|
|
131
|
+
*/
|
|
132
|
+
release(state) {
|
|
133
|
+
if (state.released) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
state.released = true;
|
|
137
|
+
state.pendingElicitation = null;
|
|
138
|
+
if (this.activeCall === state) {
|
|
139
|
+
this.activeCall = null;
|
|
140
|
+
}
|
|
141
|
+
state.release();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Provides a cancellation-aware FIFO mutex for 2025-era calls.
|
|
146
|
+
*/
|
|
147
|
+
class AsyncMutex {
|
|
148
|
+
locked = false;
|
|
149
|
+
queue = [];
|
|
150
|
+
/**
|
|
151
|
+
* Acquires the lock and returns an idempotent release callback.
|
|
152
|
+
*/
|
|
153
|
+
async acquire(signal) {
|
|
154
|
+
if (signal?.aborted) {
|
|
155
|
+
throw abortReason(signal);
|
|
156
|
+
}
|
|
157
|
+
if (!this.locked) {
|
|
158
|
+
this.locked = true;
|
|
159
|
+
return createRelease(() => this.releaseNext());
|
|
160
|
+
}
|
|
161
|
+
return new Promise((resolve, reject) => {
|
|
162
|
+
const waiter = { resolve, reject, signal };
|
|
163
|
+
if (signal) {
|
|
164
|
+
waiter.abort = () => {
|
|
165
|
+
const index = this.queue.indexOf(waiter);
|
|
166
|
+
if (index >= 0) {
|
|
167
|
+
this.queue.splice(index, 1);
|
|
168
|
+
}
|
|
169
|
+
reject(abortReason(signal));
|
|
170
|
+
};
|
|
171
|
+
signal.addEventListener("abort", waiter.abort, { once: true });
|
|
172
|
+
}
|
|
173
|
+
this.queue.push(waiter);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Hands the lock to the next live waiter or marks it available.
|
|
178
|
+
*/
|
|
179
|
+
releaseNext() {
|
|
180
|
+
const waiter = this.queue.shift();
|
|
181
|
+
if (!waiter) {
|
|
182
|
+
this.locked = false;
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (waiter.abort && waiter.signal) {
|
|
186
|
+
waiter.signal.removeEventListener("abort", waiter.abort);
|
|
187
|
+
}
|
|
188
|
+
waiter.resolve(createRelease(() => this.releaseNext()));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Creates one active call state with an unguessable continuation token.
|
|
193
|
+
*/
|
|
194
|
+
function createActiveCall(name, args, elicitForm, release) {
|
|
195
|
+
return {
|
|
196
|
+
name,
|
|
197
|
+
args: structuredClone(args),
|
|
198
|
+
requestState: `${BRIDGE_STATE_PREFIX}${randomUUID()}`,
|
|
199
|
+
...(elicitForm ? { elicitForm } : {}),
|
|
200
|
+
release,
|
|
201
|
+
released: false,
|
|
202
|
+
parked: false,
|
|
203
|
+
abortController: new AbortController(),
|
|
204
|
+
pendingElicitation: null,
|
|
205
|
+
outcome: null,
|
|
206
|
+
changed: createDeferred()
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Converts the pending 2025 form request into a 2026 input-required result.
|
|
211
|
+
*/
|
|
212
|
+
function buildInputRequiredResult(state) {
|
|
213
|
+
const pending = state.pendingElicitation;
|
|
214
|
+
if (!pending) {
|
|
215
|
+
throw new ProtocolError(ProtocolErrorCode.InternalError, "Missing pending form elicitation state.");
|
|
216
|
+
}
|
|
217
|
+
return {
|
|
218
|
+
resultType: "input_required",
|
|
219
|
+
inputRequests: {
|
|
220
|
+
[INPUT_RESPONSE_KEY]: {
|
|
221
|
+
method: "elicitation/create",
|
|
222
|
+
params: pending.params
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
requestState: state.requestState
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Removes unsupported task metadata while preserving the form request.
|
|
230
|
+
*/
|
|
231
|
+
function normalizeFormParams(params) {
|
|
232
|
+
return {
|
|
233
|
+
mode: "form",
|
|
234
|
+
message: params.message,
|
|
235
|
+
requestedSchema: structuredClone(params.requestedSchema),
|
|
236
|
+
...(params._meta ? { _meta: structuredClone(params._meta) } : {})
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Resolves the current state-change waiter and installs the next one.
|
|
241
|
+
*/
|
|
242
|
+
function notifyStateChanged(state) {
|
|
243
|
+
const changed = state.changed;
|
|
244
|
+
state.changed = createDeferred();
|
|
245
|
+
changed.resolve();
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Creates a Promise with externally controlled completion.
|
|
249
|
+
*/
|
|
250
|
+
function createDeferred() {
|
|
251
|
+
let resolvePromise;
|
|
252
|
+
let rejectPromise;
|
|
253
|
+
const promise = new Promise((resolve, reject) => {
|
|
254
|
+
resolvePromise = resolve;
|
|
255
|
+
rejectPromise = reject;
|
|
256
|
+
});
|
|
257
|
+
return { promise, resolve: resolvePromise, reject: rejectPromise };
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Forwards a caller abort only while that upstream round remains active.
|
|
261
|
+
*/
|
|
262
|
+
function forwardAbort(source, target) {
|
|
263
|
+
if (!source) {
|
|
264
|
+
return () => undefined;
|
|
265
|
+
}
|
|
266
|
+
const abort = () => target.abort(abortReason(source));
|
|
267
|
+
if (source.aborted) {
|
|
268
|
+
abort();
|
|
269
|
+
return () => undefined;
|
|
270
|
+
}
|
|
271
|
+
source.addEventListener("abort", abort, { once: true });
|
|
272
|
+
return () => source.removeEventListener("abort", abort);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Converts an AbortSignal reason into an Error instance.
|
|
276
|
+
*/
|
|
277
|
+
function abortReason(signal) {
|
|
278
|
+
return signal.reason instanceof Error
|
|
279
|
+
? signal.reason
|
|
280
|
+
: new DOMException("The operation was aborted.", "AbortError");
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Wraps one release function so repeated calls are harmless.
|
|
284
|
+
*/
|
|
285
|
+
function createRelease(release) {
|
|
286
|
+
let released = false;
|
|
287
|
+
return () => {
|
|
288
|
+
if (released) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
released = true;
|
|
292
|
+
release();
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Normalizes rejected values before they cross the bridge boundary.
|
|
297
|
+
*/
|
|
298
|
+
function normalizeError(error) {
|
|
299
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
300
|
+
}
|
package/dist/mcp/sdk-client.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Client, ProtocolError, SdkHttpError } from "@modelcontextprotocol/client";
|
|
2
2
|
import { VERSION } from "../version.js";
|
|
3
|
+
import { FormElicitationBridge } from "./form-elicitation-bridge.js";
|
|
3
4
|
import { SUPPORTED_MCP_PROTOCOL_VERSIONS } from "./versions.js";
|
|
4
5
|
const MAX_RESTART_ATTEMPTS = 3;
|
|
5
6
|
const CONNECT_TIMEOUT_MS = 30_000;
|
|
@@ -19,6 +20,7 @@ export class SdkMcpClient {
|
|
|
19
20
|
restartAttempts = 0;
|
|
20
21
|
unavailableReason = null;
|
|
21
22
|
tools = new Map();
|
|
23
|
+
formElicitationBridge = new FormElicitationBridge();
|
|
22
24
|
/**
|
|
23
25
|
* Creates a reusable SDK-backed downstream client.
|
|
24
26
|
*/
|
|
@@ -115,11 +117,18 @@ export class SdkMcpClient {
|
|
|
115
117
|
...(context.inputResponses ? { inputResponses: context.inputResponses } : {}),
|
|
116
118
|
...(context.requestState ? { requestState: context.requestState } : {})
|
|
117
119
|
};
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
120
|
+
const invoke = async (signal) => {
|
|
121
|
+
const result = await client.callTool(params, {
|
|
122
|
+
signal,
|
|
123
|
+
allowInputRequired: true,
|
|
124
|
+
toolDefinition: this.tools.get(name)
|
|
125
|
+
});
|
|
126
|
+
return result;
|
|
127
|
+
};
|
|
128
|
+
if (client.getProtocolEra() === "legacy") {
|
|
129
|
+
return this.formElicitationBridge.execute(name, args, context, invoke);
|
|
130
|
+
}
|
|
131
|
+
const result = await invoke(context.signal ?? new AbortController().signal);
|
|
123
132
|
return result;
|
|
124
133
|
});
|
|
125
134
|
}
|
|
@@ -152,6 +161,10 @@ export class SdkMcpClient {
|
|
|
152
161
|
}
|
|
153
162
|
};
|
|
154
163
|
const client = new Client({ name: "mcp-gateway", version: VERSION }, options);
|
|
164
|
+
client.setRequestHandler("elicitation/create", async (request, context) => {
|
|
165
|
+
const params = request.params;
|
|
166
|
+
return this.formElicitationBridge.handle(params, context.mcpReq.signal);
|
|
167
|
+
});
|
|
155
168
|
client.onerror = (error) => {
|
|
156
169
|
this.logger.warn("downstream.sdk_error", {
|
|
157
170
|
serviceId: this.key,
|
|
@@ -225,6 +238,7 @@ export class SdkMcpClient {
|
|
|
225
238
|
* Closes the active SDK connection and clears derived tool views.
|
|
226
239
|
*/
|
|
227
240
|
async disposeConnection() {
|
|
241
|
+
this.formElicitationBridge.reset(new Error(`Downstream service '${this.key}' connection was closed.`));
|
|
228
242
|
const client = this.client;
|
|
229
243
|
this.client = null;
|
|
230
244
|
this.tools.clear();
|
package/dist/mcp/server.js
CHANGED
|
@@ -57,7 +57,12 @@ function toDownstreamContext(context, mcp2025ClientCapabilities) {
|
|
|
57
57
|
signal: context.mcpReq.signal,
|
|
58
58
|
...(clientCapabilities ? { clientCapabilities } : {}),
|
|
59
59
|
...(context.mcpReq.inputResponses ? { inputResponses: context.mcpReq.inputResponses } : {}),
|
|
60
|
-
...(context.mcpReq.requestState() ? { requestState: context.mcpReq.requestState() } : {})
|
|
60
|
+
...(context.mcpReq.requestState() ? { requestState: context.mcpReq.requestState() } : {}),
|
|
61
|
+
...(!context.mcpReq.envelope
|
|
62
|
+
? {
|
|
63
|
+
elicitForm: (params, signal) => context.mcpReq.elicitInput(params, { signal })
|
|
64
|
+
}
|
|
65
|
+
: {})
|
|
61
66
|
};
|
|
62
67
|
}
|
|
63
68
|
/**
|
package/package.json
CHANGED
|
@@ -52,7 +52,7 @@ Operate downstream MCP services through the gateway's fixed, token-efficient dis
|
|
|
52
52
|
- `includeSchema` is optional and defaults to `false`. Keep it false for name-only discovery or broad unfiltered inventories.
|
|
53
53
|
- `gateway_get_tool_schema` requires unique exact, case-sensitive names in `toolName` and returns `schemas`, keyed by requested tool name; each value contains `inputSchema` and `outputSchema`.
|
|
54
54
|
- Stable gateway-owned structured results expose `outputSchema`; `gateway_call_tool` omits a fixed output schema and forwards the downstream result unchanged.
|
|
55
|
-
- `gateway_call_tool` may return an MCP `input_required` continuation. Never auto-approve it; obtain the required user input and retry with the returned opaque `requestState` and matching `inputResponses`.
|
|
55
|
+
- `gateway_call_tool` may return an MCP `input_required` continuation, including one converted from a 2025 downstream form elicitation. Never auto-approve it; obtain the required user input and retry with the returned opaque `requestState` and matching `inputResponses`.
|
|
56
56
|
- Treat downstream `structuredContent` as arbitrary JSON rather than assuming it is an object.
|
|
57
57
|
|
|
58
58
|
## Token-Efficient Strategy
|