mcp2service 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/README.md +272 -0
- package/dist/cjs/adapters/factory.js +51901 -0
- package/dist/cjs/adapters/graphql.js +28866 -0
- package/dist/cjs/adapters/grpc.js +36898 -0
- package/dist/cjs/adapters/http.js +28835 -0
- package/dist/cjs/adapters/index.js +51906 -0
- package/dist/cjs/cli.js +2283 -0
- package/dist/cjs/errors.js +88 -0
- package/dist/cjs/index.js +424 -0
- package/dist/cjs/mpc-proxy.js +188884 -0
- package/dist/cjs/types.js +29 -0
- package/dist/cjs/validation.js +13971 -0
- package/dist/cli/cli.js +2272 -0
- package/dist/esm/index.js +424 -0
- package/dist/types/adapters/factory.d.ts +10 -0
- package/dist/types/adapters/graphql.d.ts +28 -0
- package/dist/types/adapters/grpc.d.ts +32 -0
- package/dist/types/adapters/http.d.ts +15 -0
- package/dist/types/adapters/index.d.ts +4 -0
- package/dist/types/cli.d.ts +2 -0
- package/dist/types/errors.d.ts +23 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/mpc-proxy.d.ts +12 -0
- package/dist/types/types.d.ts +45 -0
- package/dist/types/validation.d.ts +49 -0
- package/package.json +96 -0
package/README.md
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
# mcp2service - MCP代理服务
|
|
2
|
+
|
|
3
|
+
Model Context Protocol (MCP)代理服务,支持通过多种适配器将工具调用代理到后端服务。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- ✅ **多协议支持**: HTTP, GraphQL, gRPC适配器
|
|
8
|
+
- ✅ **类型安全**: 完整的TypeScript支持,输入参数验证
|
|
9
|
+
- ✅ **插件化架构**: 动态适配器注册,工厂模式
|
|
10
|
+
- ✅ **错误处理**: 统一的错误类型和恢复机制
|
|
11
|
+
- ✅ **配置验证**: 使用Zod进行运行时配置验证
|
|
12
|
+
- ✅ **脚手架工具**: 快速项目初始化CLI
|
|
13
|
+
|
|
14
|
+
## 快速开始
|
|
15
|
+
|
|
16
|
+
### 安装
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
# 作为依赖安装
|
|
20
|
+
bun add mcp2service
|
|
21
|
+
|
|
22
|
+
# 或从源码安装
|
|
23
|
+
git clone https://github.com/your-org/mcp2service.git
|
|
24
|
+
cd mcp2service
|
|
25
|
+
bun install
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### 基本使用
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { McpProxy } from 'mcp2service';
|
|
32
|
+
|
|
33
|
+
const proxy = new McpProxy({
|
|
34
|
+
name: 'my-mcp-service',
|
|
35
|
+
version: '1.0.0',
|
|
36
|
+
transport: {
|
|
37
|
+
httpStream: {
|
|
38
|
+
port: 3000,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
tools: {
|
|
42
|
+
'http-example': {
|
|
43
|
+
type: 'http',
|
|
44
|
+
description: '示例HTTP工具',
|
|
45
|
+
params: {},
|
|
46
|
+
options: {
|
|
47
|
+
url: 'https://api.example.com/data',
|
|
48
|
+
method: 'GET',
|
|
49
|
+
headers: {
|
|
50
|
+
'Content-Type': 'application/json',
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
'graphql-example': {
|
|
55
|
+
type: 'graphql',
|
|
56
|
+
description: '示例GraphQL查询',
|
|
57
|
+
params: {
|
|
58
|
+
query: {
|
|
59
|
+
type: 'string',
|
|
60
|
+
description: 'GraphQL查询语句',
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
options: {
|
|
64
|
+
endpoint: 'https://api.example.com/graphql',
|
|
65
|
+
headers: {
|
|
66
|
+
Authorization: 'Bearer ${TOKEN}',
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
await proxy.start();
|
|
74
|
+
console.log('MCP代理服务已启动,端口: 3000');
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### 使用脚手架工具
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
# 初始化新项目
|
|
81
|
+
bun run cli.ts init my-mcp-project
|
|
82
|
+
|
|
83
|
+
# 或直接使用(安装后)
|
|
84
|
+
mcp2service init my-mcp-project
|
|
85
|
+
|
|
86
|
+
# 生成适配器模板
|
|
87
|
+
mcp2service generate:adapter custom-adapter
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## 适配器系统
|
|
91
|
+
|
|
92
|
+
### 内置适配器
|
|
93
|
+
|
|
94
|
+
1. **HTTP适配器** (`type: 'http'`)
|
|
95
|
+
- 支持GET, POST, PUT, DELETE, PATCH方法
|
|
96
|
+
- 请求/响应头配置
|
|
97
|
+
- 超时和重试机制
|
|
98
|
+
|
|
99
|
+
2. **GraphQL适配器** (`type: 'graphql'`)
|
|
100
|
+
- GraphQL查询执行
|
|
101
|
+
- 变量和操作名支持
|
|
102
|
+
- 错误处理
|
|
103
|
+
|
|
104
|
+
3. **gRPC适配器** (`type: 'grpc'`)
|
|
105
|
+
- Protocol Buffers支持
|
|
106
|
+
- 服务和方法发现
|
|
107
|
+
- 元数据传递
|
|
108
|
+
|
|
109
|
+
### 自定义适配器
|
|
110
|
+
|
|
111
|
+
实现 `ServiceAdapter` 接口:
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
import type { ServiceAdapter } from 'mcp2service';
|
|
115
|
+
|
|
116
|
+
export class CustomAdapter implements ServiceAdapter {
|
|
117
|
+
config(options: Record<string, any>): void {
|
|
118
|
+
// 配置适配器
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async call(args: any, context: any): Promise<any> {
|
|
122
|
+
// 执行工具调用
|
|
123
|
+
return { result: 'success' };
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
注册到适配器工厂:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import { defaultAdapterFactory } from 'mcp2service';
|
|
132
|
+
import { CustomAdapter } from './adapters/custom';
|
|
133
|
+
|
|
134
|
+
defaultAdapterFactory.registerAdapter('custom', CustomAdapter);
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## 配置验证
|
|
138
|
+
|
|
139
|
+
所有配置都使用Zod进行验证:
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { mcpOptionsSchema, validate } from 'mcp2service';
|
|
143
|
+
|
|
144
|
+
// 验证配置
|
|
145
|
+
const validatedConfig = validate(mcpOptionsSchema, userConfig, 'MCP配置');
|
|
146
|
+
|
|
147
|
+
// 创建代理
|
|
148
|
+
const proxy = new McpProxy(validatedConfig);
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## 错误处理
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
import {
|
|
155
|
+
MCPError,
|
|
156
|
+
ValidationError,
|
|
157
|
+
AdapterError,
|
|
158
|
+
ConfigurationError
|
|
159
|
+
} from 'mcp2service';
|
|
160
|
+
|
|
161
|
+
try {
|
|
162
|
+
await proxy.start();
|
|
163
|
+
} catch (error) {
|
|
164
|
+
if (error instanceof ValidationError) {
|
|
165
|
+
console.error('配置验证失败:', error.details);
|
|
166
|
+
} else if (error instanceof AdapterError) {
|
|
167
|
+
console.error('适配器错误:', error.adapterName);
|
|
168
|
+
}
|
|
169
|
+
// 处理其他错误...
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## API参考
|
|
174
|
+
|
|
175
|
+
### McpProxy 类
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
class McpProxy {
|
|
179
|
+
constructor(options: McpOptions);
|
|
180
|
+
start(): Promise<void>;
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### 类型定义
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
interface McpOptions {
|
|
188
|
+
name: string;
|
|
189
|
+
version: `${number}.${number}.${number}`;
|
|
190
|
+
transport: TransportOptions;
|
|
191
|
+
tools: Record<string, ToolDefine>;
|
|
192
|
+
headers?: string[];
|
|
193
|
+
authedFn?: AuthenticateFunction;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
interface ToolDefine {
|
|
197
|
+
type: string;
|
|
198
|
+
description?: string;
|
|
199
|
+
params: ToolParameters;
|
|
200
|
+
options: Record<string, any>;
|
|
201
|
+
execute?: ToolFn;
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### 适配器接口
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
interface ServiceAdapter {
|
|
209
|
+
config(option: Record<string, any>): void;
|
|
210
|
+
call: ToolFn;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
interface AdapterFactory {
|
|
214
|
+
createAdapter(type: string, options: Record<string, any>): ServiceAdapter;
|
|
215
|
+
registerAdapter(type: string, adapterClass: new () => ServiceAdapter): void;
|
|
216
|
+
getSupportedTypes(): string[];
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## 开发
|
|
221
|
+
|
|
222
|
+
### 项目结构
|
|
223
|
+
|
|
224
|
+
```
|
|
225
|
+
mcp2service/
|
|
226
|
+
├── src/
|
|
227
|
+
│ ├── mpc-proxy.ts # MCP代理主类
|
|
228
|
+
│ ├── types.ts # 类型定义
|
|
229
|
+
│ ├── validation.ts # 验证逻辑
|
|
230
|
+
│ ├── errors.ts # 错误类型
|
|
231
|
+
│ └── adapters/ # 适配器实现
|
|
232
|
+
│ ├── http.ts # HTTP适配器
|
|
233
|
+
│ ├── graphql.ts # GraphQL适配器
|
|
234
|
+
│ ├── grpc.ts # gRPC适配器
|
|
235
|
+
│ ├── factory.ts # 适配器工厂
|
|
236
|
+
│ └── index.ts # 适配器导出
|
|
237
|
+
├── cli.ts # 脚手架工具
|
|
238
|
+
├── index.ts # 主入口点
|
|
239
|
+
└── package.json
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### 构建
|
|
243
|
+
|
|
244
|
+
```bash
|
|
245
|
+
# 开发模式
|
|
246
|
+
bun run index.ts
|
|
247
|
+
|
|
248
|
+
# 构建
|
|
249
|
+
bun build index.ts --outdir ./dist
|
|
250
|
+
|
|
251
|
+
# 测试
|
|
252
|
+
bun test
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
## 路线图
|
|
256
|
+
|
|
257
|
+
查看 [ROADMAP.md](ROADMAP.md) 了解项目开发计划和功能规划。
|
|
258
|
+
|
|
259
|
+
## 许可证
|
|
260
|
+
|
|
261
|
+
MIT
|
|
262
|
+
|
|
263
|
+
## 贡献
|
|
264
|
+
|
|
265
|
+
欢迎提交Issue和Pull Request!
|
|
266
|
+
|
|
267
|
+
## 支持
|
|
268
|
+
|
|
269
|
+
如有问题,请:
|
|
270
|
+
1. 查看 [文档](https://github.com/your-org/mcp2service/wiki)
|
|
271
|
+
2. 提交 [Issue](https://github.com/your-org/mcp2service/issues)
|
|
272
|
+
3. 加入 [Discussions](https://github.com/your-org/mcp2service/discussions)
|