mcp-multiplexer 1.0.0
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/LICENSE +201 -0
- package/README.md +283 -0
- package/dist/cli.d.ts +11 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +122 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +70 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +30 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +65 -0
- package/dist/utils.js.map +1 -0
- package/dist/wrapper.d.ts +72 -0
- package/dist/wrapper.d.ts.map +1 -0
- package/dist/wrapper.js +511 -0
- package/dist/wrapper.js.map +1 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
# mcp-multiplexer
|
|
2
|
+
|
|
3
|
+
MCP 服务器多路复用器——将多个 MCP 服务器合并为一个,自动为工具名添加服务器前缀以避免命名冲突。
|
|
4
|
+
|
|
5
|
+
## 功能
|
|
6
|
+
|
|
7
|
+
- **工具名前缀**:自动为工具名添加服务器名前缀(如 `serverName__toolName`)
|
|
8
|
+
- **多服务器支持**:同时连接多个底层 MCP 服务器
|
|
9
|
+
- **透明代理**:将工具调用路由到正确的底层服务器
|
|
10
|
+
- **自定义分隔符**:可配置服务器名与工具名之间的分隔符
|
|
11
|
+
- **CLI 支持**:通过 `npx` 直接运行,无需编写脚本
|
|
12
|
+
- **Windows 环境变量补全**:自动传递 `ProgramData` 等 SDK 白名单遗漏的环境变量,避免 SSH 等工具静默失败
|
|
13
|
+
|
|
14
|
+
## 安装
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install mcp-multiplexer
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 使用方法
|
|
21
|
+
|
|
22
|
+
### CLI 用法(推荐,无需脚本文件)
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
# 使用配置文件
|
|
26
|
+
npx mcp-multiplexer --config ./config.json
|
|
27
|
+
|
|
28
|
+
# 使用内联 JSON
|
|
29
|
+
npx mcp-multiplexer --config '{"name":"my-mux","version":"1.0.0","servers":[{"name":"github","command":"npx","args":["-y","@modelcontextprotocol/server-github"]}]}'
|
|
30
|
+
|
|
31
|
+
# 使用环境变量
|
|
32
|
+
export MCP_WRAPPER_CONFIG='{"name":"my-mux","version":"1.0.0","servers":[...]}'
|
|
33
|
+
npx mcp-multiplexer
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
配置文件示例(`config.json`):
|
|
37
|
+
|
|
38
|
+
```json
|
|
39
|
+
{
|
|
40
|
+
"name": "my-mux",
|
|
41
|
+
"version": "1.0.0",
|
|
42
|
+
"separator": "__",
|
|
43
|
+
"servers": [
|
|
44
|
+
{
|
|
45
|
+
"name": "github1",
|
|
46
|
+
"command": "npx",
|
|
47
|
+
"args": ["-y", "@modelcontextprotocol/server-github"],
|
|
48
|
+
"env": { "GITHUB_TOKEN": "token1" }
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"name": "github2",
|
|
52
|
+
"command": "npx",
|
|
53
|
+
"args": ["-y", "@modelcontextprotocol/server-github"],
|
|
54
|
+
"env": { "GITHUB_TOKEN": "token2" }
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
"name": "remote",
|
|
58
|
+
"url": "http://localhost:3000/sse"
|
|
59
|
+
}
|
|
60
|
+
]
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 编程用法
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { McpWrapper } from "mcp-multiplexer";
|
|
68
|
+
|
|
69
|
+
const wrapper = new McpWrapper({
|
|
70
|
+
name: "my-mux",
|
|
71
|
+
version: "1.0.0",
|
|
72
|
+
servers: [
|
|
73
|
+
{
|
|
74
|
+
name: "github",
|
|
75
|
+
command: "npx",
|
|
76
|
+
args: ["-y", "@modelcontextprotocol/server-github"],
|
|
77
|
+
env: { GITHUB_TOKEN: "your-token" },
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
name: "filesystem",
|
|
81
|
+
command: "npx",
|
|
82
|
+
args: ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/dir"],
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
name: "remote",
|
|
86
|
+
url: "http://localhost:3000/sse",
|
|
87
|
+
},
|
|
88
|
+
],
|
|
89
|
+
separator: "__", // 可选,默认为 "__"
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// 连接所有底层服务器
|
|
93
|
+
await wrapper.connectToServers();
|
|
94
|
+
|
|
95
|
+
// 启动多路复用服务器
|
|
96
|
+
await wrapper.start();
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### 配置说明
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
interface WrapperConfig {
|
|
103
|
+
name: string; // 多路复用服务器名称
|
|
104
|
+
version?: string; // 版本号(默认 "1.0.0")
|
|
105
|
+
servers: WrappedServerConfig[]; // 底层 MCP 服务器列表
|
|
106
|
+
separator?: string; // 分隔符(默认 "__")
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
interface WrappedServerConfig {
|
|
110
|
+
name: string; // 此服务器的唯一前缀名
|
|
111
|
+
command?: string; // 启动命令(stdio 传输)
|
|
112
|
+
args?: string[]; // 命令参数
|
|
113
|
+
env?: Record<string, string>; // 环境变量
|
|
114
|
+
cwd?: string; // 工作目录
|
|
115
|
+
url?: string; // MCP 服务器 URL(HTTP 传输)
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
> `command` 和 `url` 必须二选一,不可同时指定或同时缺省。
|
|
120
|
+
|
|
121
|
+
#### 服务器类型
|
|
122
|
+
|
|
123
|
+
**1. Stdio 服务器(使用 `command`)**
|
|
124
|
+
|
|
125
|
+
作为子进程启动,通过 stdin/stdout 通信:
|
|
126
|
+
|
|
127
|
+
```json
|
|
128
|
+
{
|
|
129
|
+
"name": "local-server",
|
|
130
|
+
"command": "npx",
|
|
131
|
+
"args": ["-y", "@modelcontextprotocol/server-github"],
|
|
132
|
+
"env": { "GITHUB_TOKEN": "your_token" }
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
**2. HTTP 服务器(使用 `url`)**
|
|
137
|
+
|
|
138
|
+
通过 HTTP 访问。自动优先尝试 Streamable HTTP(现代协议),失败则回退到 SSE(旧协议):
|
|
139
|
+
|
|
140
|
+
```json
|
|
141
|
+
{
|
|
142
|
+
"name": "remote-server",
|
|
143
|
+
"url": "http://localhost:3000/mcp"
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### 在 Claude Desktop 中使用
|
|
148
|
+
|
|
149
|
+
**方法一:CLI + 配置文件(推荐)**
|
|
150
|
+
|
|
151
|
+
```json
|
|
152
|
+
{
|
|
153
|
+
"mcpServers": {
|
|
154
|
+
"my-mux": {
|
|
155
|
+
"command": "npx",
|
|
156
|
+
"args": ["-y", "mcp-multiplexer", "--config", "/path/to/config.json"]
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
**方法二:内联 JSON(无需任何文件)**
|
|
163
|
+
|
|
164
|
+
```json
|
|
165
|
+
{
|
|
166
|
+
"mcpServers": {
|
|
167
|
+
"my-mux": {
|
|
168
|
+
"command": "npx",
|
|
169
|
+
"args": ["-y", "mcp-multiplexer", "--config", "{\"name\":\"my-mux\",\"version\":\"1.0.0\",\"servers\":[{\"name\":\"github1\",\"command\":\"npx\",\"args\":[\"-y\",\"@modelcontextprotocol/server-github\"],\"env\":{\"GITHUB_TOKEN\":\"token1\"}}]}"]
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### 在 VS Code(GitHub Copilot)中使用
|
|
176
|
+
|
|
177
|
+
在工作区创建 `.vscode/mcp.json`:
|
|
178
|
+
|
|
179
|
+
```json
|
|
180
|
+
{
|
|
181
|
+
"servers": {
|
|
182
|
+
"my-mux": {
|
|
183
|
+
"command": "npx",
|
|
184
|
+
"args": ["-y", "mcp-multiplexer", "--config", "/path/to/config.json"]
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
配置完成后,所有底层服务器的工具都会以前缀名形式出现:
|
|
191
|
+
- `github1__create_issue`
|
|
192
|
+
- `github1__list_pull_requests`
|
|
193
|
+
- `github2__create_issue`
|
|
194
|
+
- `github2__list_pull_requests`
|
|
195
|
+
|
|
196
|
+
## 错误处理与容错
|
|
197
|
+
|
|
198
|
+
### 优雅降级
|
|
199
|
+
|
|
200
|
+
当部分底层服务器连接失败时:
|
|
201
|
+
- 复用器会**继续启动**,使用已成功连接的服务器
|
|
202
|
+
- 失败的连接信息会输出到 stderr
|
|
203
|
+
- 已连接服务器的工具仍然可用
|
|
204
|
+
- 客户端(Claude Desktop、VS Code 等)可以正常初始化
|
|
205
|
+
|
|
206
|
+
### 连接超时
|
|
207
|
+
|
|
208
|
+
每个底层服务器有 **30 秒连接超时**,防止因服务器无响应导致复用器卡死。
|
|
209
|
+
|
|
210
|
+
### 示例场景
|
|
211
|
+
|
|
212
|
+
**场景一**:部分服务器失败
|
|
213
|
+
```
|
|
214
|
+
Failed to connect to server "unavailable": Error: spawn ENOENT
|
|
215
|
+
Skipping server "unavailable", will continue with remaining servers
|
|
216
|
+
Connected to server "github" with 15 tools
|
|
217
|
+
Connected to server "filesystem" with 8 tools
|
|
218
|
+
Successfully connected to 2 server(s)
|
|
219
|
+
Failed to connect to 1 server(s): unavailable
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
**场景二**:所有服务器失败
|
|
223
|
+
```
|
|
224
|
+
Failed to connect to server "server1": Error: spawn ENOENT
|
|
225
|
+
Failed to connect to server "server2": Connection timeout after 30000ms
|
|
226
|
+
Successfully connected to 0 server(s)
|
|
227
|
+
Failed to connect to 2 server(s): server1, server2
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
即使在场景二中,复用器也能正常启动(0 个工具),客户端可以完成初始化并收到清晰的错误信息。
|
|
231
|
+
|
|
232
|
+
## 故障排除
|
|
233
|
+
|
|
234
|
+
### HTTP 连接错误
|
|
235
|
+
|
|
236
|
+
**HTTP 405(Method Not Allowed)**:URL 不支持 MCP 协议,请检查是否指向了正确的 MCP 端点。
|
|
237
|
+
|
|
238
|
+
**HTTP 404(Not Found)**:端点不存在,请检查 URL 是否正确、服务器是否在运行。
|
|
239
|
+
|
|
240
|
+
### Windows SSH 静默失败
|
|
241
|
+
|
|
242
|
+
如果通过 wrapper 启动 SSH 连接的 MCP 服务器时遇到 exit code 255 且无 stderr 输出,这是因为 `@modelcontextprotocol/sdk` 的环境变量白名单缺少 `ProgramData`。本项目已内置修复——自动补充 `ProgramData` 和 `ALLUSERSPROFILE` 环境变量。
|
|
243
|
+
|
|
244
|
+
## API
|
|
245
|
+
|
|
246
|
+
### McpWrapper
|
|
247
|
+
|
|
248
|
+
主类,用于创建多路复用服务器。
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
new McpWrapper(config: WrapperConfig)
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
#### 方法
|
|
255
|
+
|
|
256
|
+
- `connectToServers(): Promise<void>` — 连接所有配置的底层 MCP 服务器
|
|
257
|
+
- `start(): Promise<void>` — 通过 stdio 传输启动多路复用服务器
|
|
258
|
+
- `close(): Promise<void>` — 关闭所有连接并停止服务器
|
|
259
|
+
- `getWrappedTools(): Array<WrappedToolInfo>` — 返回所有已包装工具的信息
|
|
260
|
+
- `reconnectServer(name: string): Promise<{success, message}>` — 尝试重连失败的服务器
|
|
261
|
+
|
|
262
|
+
### 工具函数
|
|
263
|
+
|
|
264
|
+
```typescript
|
|
265
|
+
// 创建带前缀的工具名
|
|
266
|
+
prefixToolName(serverName: string, toolName: string, separator?: string): string
|
|
267
|
+
|
|
268
|
+
// 解析带前缀的工具名
|
|
269
|
+
parseToolName(prefixedName: string, separator?: string): { serverName: string; originalName: string } | null
|
|
270
|
+
|
|
271
|
+
// 验证服务器名(不包含分隔符)
|
|
272
|
+
isValidServerName(serverName: string, separator?: string): boolean
|
|
273
|
+
|
|
274
|
+
// 验证工具名(不包含分隔符)
|
|
275
|
+
isValidToolName(toolName: string, separator?: string): boolean
|
|
276
|
+
|
|
277
|
+
// 获取 Windows 上需要补充的环境变量
|
|
278
|
+
getSupplementalEnv(): Record<string, string>
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
## 许可证
|
|
282
|
+
|
|
283
|
+
Apache-2.0
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* CLI entry point for mcp-wrapper
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* npx mcp-wrapper --config /path/to/config.json
|
|
7
|
+
* npx mcp-wrapper --config '{"name":"wrapper","version":"1.0.0","servers":[...]}'
|
|
8
|
+
* MCP_WRAPPER_CONFIG='{"name":"wrapper",...}' npx mcp-wrapper
|
|
9
|
+
*/
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=cli.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAEA;;;;;;;GAOG"}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* CLI entry point for mcp-wrapper
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* npx mcp-wrapper --config /path/to/config.json
|
|
7
|
+
* npx mcp-wrapper --config '{"name":"wrapper","version":"1.0.0","servers":[...]}'
|
|
8
|
+
* MCP_WRAPPER_CONFIG='{"name":"wrapper",...}' npx mcp-wrapper
|
|
9
|
+
*/
|
|
10
|
+
import { readFileSync, existsSync } from "node:fs";
|
|
11
|
+
import { McpWrapper } from "./wrapper.js";
|
|
12
|
+
function printUsage() {
|
|
13
|
+
console.error(`
|
|
14
|
+
mcp-wrapper - MCP server wrapper with tool name prefixing
|
|
15
|
+
|
|
16
|
+
Usage:
|
|
17
|
+
npx mcp-wrapper --config <path-to-config.json>
|
|
18
|
+
npx mcp-wrapper --config '<json-config-string>'
|
|
19
|
+
MCP_WRAPPER_CONFIG='<json-config>' npx mcp-wrapper
|
|
20
|
+
|
|
21
|
+
Configuration JSON format:
|
|
22
|
+
{
|
|
23
|
+
"name": "my-wrapper",
|
|
24
|
+
"version": "1.0.0", // optional, default "1.0.0"
|
|
25
|
+
"separator": "__", // optional, default "__"
|
|
26
|
+
"servers": [
|
|
27
|
+
{
|
|
28
|
+
"name": "server1",
|
|
29
|
+
"command": "npx",
|
|
30
|
+
"args": ["-y", "@modelcontextprotocol/server-github"],
|
|
31
|
+
"env": { "GITHUB_TOKEN": "token" }
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
Examples:
|
|
37
|
+
# Using config file
|
|
38
|
+
npx mcp-wrapper --config ./mcp-wrapper.json
|
|
39
|
+
|
|
40
|
+
# Using inline JSON (minimal - version is optional)
|
|
41
|
+
npx mcp-wrapper --config '{"name":"w","servers":[{"name":"gh","command":"npx","args":["-y","@modelcontextprotocol/server-github"]}]}'
|
|
42
|
+
|
|
43
|
+
# Using environment variable
|
|
44
|
+
export MCP_WRAPPER_CONFIG='{"name":"w","servers":[...]}'
|
|
45
|
+
npx mcp-wrapper
|
|
46
|
+
`);
|
|
47
|
+
}
|
|
48
|
+
function parseConfig(configArg) {
|
|
49
|
+
// Check if it's a file path
|
|
50
|
+
if (existsSync(configArg)) {
|
|
51
|
+
const content = readFileSync(configArg, "utf-8");
|
|
52
|
+
return JSON.parse(content);
|
|
53
|
+
}
|
|
54
|
+
// Try to parse as JSON directly
|
|
55
|
+
try {
|
|
56
|
+
return JSON.parse(configArg);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
throw new Error(`Config is not a valid file path or JSON: ${configArg}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async function main() {
|
|
63
|
+
const args = process.argv.slice(2);
|
|
64
|
+
let configArg;
|
|
65
|
+
// Parse command line arguments
|
|
66
|
+
for (let i = 0; i < args.length; i++) {
|
|
67
|
+
if (args[i] === "--config" || args[i] === "-c") {
|
|
68
|
+
configArg = args[i + 1];
|
|
69
|
+
i++;
|
|
70
|
+
}
|
|
71
|
+
else if (args[i] === "--help" || args[i] === "-h") {
|
|
72
|
+
printUsage();
|
|
73
|
+
process.exit(0);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// Fall back to environment variable
|
|
77
|
+
if (!configArg) {
|
|
78
|
+
configArg = process.env.MCP_WRAPPER_CONFIG;
|
|
79
|
+
}
|
|
80
|
+
if (!configArg) {
|
|
81
|
+
console.error("Error: No configuration provided.");
|
|
82
|
+
printUsage();
|
|
83
|
+
process.exit(1);
|
|
84
|
+
}
|
|
85
|
+
let config;
|
|
86
|
+
try {
|
|
87
|
+
config = parseConfig(configArg);
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
console.error(`Error parsing configuration: ${error instanceof Error ? error.message : error}`);
|
|
91
|
+
process.exit(1);
|
|
92
|
+
}
|
|
93
|
+
// Validate required fields
|
|
94
|
+
if (!config.name || !config.servers) {
|
|
95
|
+
console.error("Error: Configuration must include 'name' and 'servers' fields.");
|
|
96
|
+
process.exit(1);
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
const wrapper = new McpWrapper(config);
|
|
100
|
+
console.error("Connecting to servers...");
|
|
101
|
+
await wrapper.connectToServers();
|
|
102
|
+
const tools = wrapper.getWrappedTools();
|
|
103
|
+
const serverNames = new Set(tools.map((t) => t.serverName));
|
|
104
|
+
console.error(`Loaded ${tools.length} tools from ${serverNames.size} server(s)`);
|
|
105
|
+
console.error("Starting wrapper server...");
|
|
106
|
+
await wrapper.start();
|
|
107
|
+
// Handle shutdown
|
|
108
|
+
const shutdown = async () => {
|
|
109
|
+
console.error("Shutting down...");
|
|
110
|
+
await wrapper.close();
|
|
111
|
+
process.exit(0);
|
|
112
|
+
};
|
|
113
|
+
process.on("SIGINT", shutdown);
|
|
114
|
+
process.on("SIGTERM", shutdown);
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
console.error(`Error: ${error instanceof Error ? error.message : error}`);
|
|
118
|
+
process.exit(1);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
main();
|
|
122
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAEA;;;;;;;GAOG;AAEH,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAG1C,SAAS,UAAU;IACjB,OAAO,CAAC,KAAK,CAAC;;;;;;;;;;;;;;;;;;;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AAC,KAAK,EAAE,CAAC;QAEtB,kBAAkB;QAClB,MAAM,QAAQ,GAAG,KAAK,IAAI,EAAE;YAC1B,OAAO,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;YAClC,MAAM,OAAO,CAAC,KAAK,EAAE,CAAC;YACtB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC,CAAC;QAEF,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;QAC/B,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAClC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,UAAU,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC;QAC1E,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAED,IAAI,EAAE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
export { McpWrapper } from "./wrapper.js";
|
|
3
|
+
export type { WrapperConfig, WrappedServerConfig, WrappedToolInfo } from "./types.js";
|
|
4
|
+
export { prefixToolName, parseToolName, isValidServerName, isValidToolName, getSupplementalEnv } from "./utils.js";
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,YAAY,EAAE,aAAa,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AACtF,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,iBAAiB,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,iBAAiB,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC"}
|