openai-compat-mcp 0.0.0-stage → 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/.env.example +14 -0
- package/LICENSE +9 -0
- package/README.md +69 -2
- package/index.js +72 -0
- package/package.json +39 -4
- package/smithery.yaml +35 -0
package/.env.example
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# ===== openai-compat-mcp 配置 =====
|
|
2
|
+
# 任意 OpenAI 兼容网关地址(取消注释替换成你的)
|
|
3
|
+
OPENAI_BASE_URL=https://api.openai.com/v1
|
|
4
|
+
# OPENAI_BASE_URL=https://api.deepseek.com/v1
|
|
5
|
+
# OPENAI_BASE_URL=https://open.bigmodel.cn/api/paas/v4
|
|
6
|
+
# OPENAI_BASE_URL=http://localhost:8047/v1 # 自建 vLLM / one-api 网关
|
|
7
|
+
|
|
8
|
+
# 网关密钥
|
|
9
|
+
OPENAI_API_KEY=sk-xxxx
|
|
10
|
+
|
|
11
|
+
# 默认模型(可被工具参数 model 覆盖)
|
|
12
|
+
MODEL=gpt-4o-mini
|
|
13
|
+
# MODEL=deepseek-chat
|
|
14
|
+
# MODEL=glm-4-flash
|
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 zhanlu-mcp
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,70 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 🔌 openai-compat-mcp
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**English** | [中文](#中文)
|
|
4
|
+
|
|
5
|
+
Bridge **any OpenAI-compatible API** into a [Model Context Protocol](https://modelcontextprotocol.io) server — so Claude Desktop, Cursor, and every MCP client can call your LLM gateway directly.
|
|
6
|
+
|
|
7
|
+
Works with: DeepSeek · Zhipu (GLM) · Qwen · Moonshot · OpenRouter · one-api / new-api / vLLM self-hosted gateways · OpenAI itself.
|
|
8
|
+
|
|
9
|
+
> ⭐ If this saves you time, a star helps others find it.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## 中文
|
|
14
|
+
|
|
15
|
+
把**任意 OpenAI 兼容 API** 桥接成 [MCP server](https://modelcontextprotocol.io)——让 Claude Desktop、Cursor 和所有 MCP 客户端直接调用你的 LLM 网关。
|
|
16
|
+
|
|
17
|
+
支持:DeepSeek · 智谱 GLM · 通义 Qwen · Moonshot · OpenRouter · one-api / new-api / vLLM 自建网关 · OpenAI 官方。
|
|
18
|
+
|
|
19
|
+
## 🚀 快速开始
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
git clone https://github.com/zjukop3/openai-compat-mcp.git
|
|
23
|
+
cd openai-compat-mcp
|
|
24
|
+
npm install
|
|
25
|
+
cp .env.example .env # 填入你的网关地址和密钥
|
|
26
|
+
node index.js
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## ⚙️ 客户端配置
|
|
30
|
+
|
|
31
|
+
### Claude Desktop
|
|
32
|
+
|
|
33
|
+
`claude_desktop_config.json`:
|
|
34
|
+
|
|
35
|
+
```json
|
|
36
|
+
{
|
|
37
|
+
"mcpServers": {
|
|
38
|
+
"openai-compat": {
|
|
39
|
+
"command": "node",
|
|
40
|
+
"args": ["/path/to/openai-compat-mcp/index.js"],
|
|
41
|
+
"env": {
|
|
42
|
+
"OPENAI_BASE_URL": "https://api.deepseek.com/v1",
|
|
43
|
+
"OPENAI_API_KEY": "sk-xxx",
|
|
44
|
+
"MODEL": "deepseek-chat"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Cursor
|
|
52
|
+
|
|
53
|
+
`.cursor/mcp.json` 同样格式,`env` 里填网关配置。
|
|
54
|
+
|
|
55
|
+
## 🛠️ 提供的工具
|
|
56
|
+
|
|
57
|
+
| 工具 | 说明 |
|
|
58
|
+
|------|------|
|
|
59
|
+
| `chat` | 与网关对话。参数:`prompt`(必填)、`system` / `model` / `temperature`(可选) |
|
|
60
|
+
| `list_models` | 列出网关当前可用的模型 |
|
|
61
|
+
|
|
62
|
+
## 💡 典型用法
|
|
63
|
+
|
|
64
|
+
- **Claude Desktop 里用国产模型**:把 `OPENAI_BASE_URL` 指向 DeepSeek / 智谱,客户端里直接调 `chat`
|
|
65
|
+
- **一个网关多个模型**:`list_models` 看可用模型,`chat` 的 `model` 参数随时切换
|
|
66
|
+
- **自建 vLLM / one-api**:局域网网关直接接入 MCP 生态
|
|
67
|
+
|
|
68
|
+
## 📄 License
|
|
69
|
+
|
|
70
|
+
MIT © zjukop3
|
package/index.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* openai-compat-mcp — 把任意 OpenAI 兼容 API 桥接为 MCP server
|
|
4
|
+
* 让 Claude Desktop / Cursor / 任何 MCP 客户端直接调用你的 LLM 网关
|
|
5
|
+
*
|
|
6
|
+
* 环境变量(见 .env.example):
|
|
7
|
+
* OPENAI_BASE_URL 网关地址(默认 https://api.openai.com/v1)
|
|
8
|
+
* OPENAI_API_KEY 网关密钥
|
|
9
|
+
* MODEL 默认模型名
|
|
10
|
+
*/
|
|
11
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
12
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
13
|
+
import { z } from "zod";
|
|
14
|
+
|
|
15
|
+
const BASE_URL = (process.env.OPENAI_BASE_URL || "https://api.openai.com/v1").replace(/\/$/, "");
|
|
16
|
+
const API_KEY = process.env.OPENAI_API_KEY || "";
|
|
17
|
+
const DEFAULT_MODEL = process.env.MODEL || "gpt-4o-mini";
|
|
18
|
+
|
|
19
|
+
/** 调用 OpenAI 兼容 chat/completions */
|
|
20
|
+
async function chatComplete(messages, { model, temperature, maxTokens } = {}) {
|
|
21
|
+
if (!API_KEY) throw new Error("未配置 OPENAI_API_KEY(参考 .env.example)");
|
|
22
|
+
const res = await fetch(`${BASE_URL}/chat/completions`, {
|
|
23
|
+
method: "POST",
|
|
24
|
+
headers: { "Content-Type": "application/json", Authorization: `Bearer ${API_KEY}` },
|
|
25
|
+
body: JSON.stringify({
|
|
26
|
+
model: model || DEFAULT_MODEL,
|
|
27
|
+
messages,
|
|
28
|
+
temperature: temperature ?? 0.7,
|
|
29
|
+
max_tokens: maxTokens ?? 4000,
|
|
30
|
+
}),
|
|
31
|
+
signal: AbortSignal.timeout(120000),
|
|
32
|
+
});
|
|
33
|
+
if (!res.ok) {
|
|
34
|
+
const t = await res.text().catch(() => "");
|
|
35
|
+
throw new Error(`LLM ${res.status}: ${t.slice(0, 300)}`);
|
|
36
|
+
}
|
|
37
|
+
const data = await res.json();
|
|
38
|
+
return data.choices?.[0]?.message?.content ?? "";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const server = new McpServer({ name: "openai-compat-mcp", version: "1.0.0" });
|
|
42
|
+
|
|
43
|
+
server.tool(
|
|
44
|
+
"chat",
|
|
45
|
+
"与 OpenAI 兼容网关对话。支持任意 OpenAI 兼容 API:DeepSeek / 智谱 / 通义 / Moonshot / 自建 vLLM 网关",
|
|
46
|
+
{
|
|
47
|
+
prompt: z.string().describe("用户消息"),
|
|
48
|
+
system: z.string().optional().describe("系统提示词(可选)"),
|
|
49
|
+
model: z.string().optional().describe("模型名(默认取环境变量 MODEL)"),
|
|
50
|
+
temperature: z.number().optional().describe("温度 0-2(可选)"),
|
|
51
|
+
},
|
|
52
|
+
async ({ prompt, system, model, temperature }) => {
|
|
53
|
+
const messages = [];
|
|
54
|
+
if (system) messages.push({ role: "system", content: system });
|
|
55
|
+
messages.push({ role: "user", content: prompt });
|
|
56
|
+
const text = await chatComplete(messages, { model, temperature });
|
|
57
|
+
return { content: [{ type: "text", text }] };
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
server.tool("list_models", "列出网关当前可用的模型", {}, async () => {
|
|
62
|
+
if (!API_KEY) throw new Error("未配置 OPENAI_API_KEY");
|
|
63
|
+
const res = await fetch(`${BASE_URL}/models`, {
|
|
64
|
+
headers: { Authorization: `Bearer ${API_KEY}` },
|
|
65
|
+
signal: AbortSignal.timeout(30000),
|
|
66
|
+
});
|
|
67
|
+
const data = await res.json();
|
|
68
|
+
const ids = (data.data || []).map((m) => m.id);
|
|
69
|
+
return { content: [{ type: "text", text: ids.length ? ids.join("\n") : JSON.stringify(data) }] };
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
await server.connect(new StdioServerTransport());
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openai-compat-mcp",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Bridge any OpenAI-compatible API (DeepSeek / Zhipu / Qwen / Moonshot / self-hosted vLLM) into a Model Context Protocol server for Claude Desktop, Cursor and other MCP clients.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"openai-compat-mcp": "index.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"start": "node index.js"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"mcp",
|
|
15
|
+
"mcp-server",
|
|
16
|
+
"openai",
|
|
17
|
+
"openai-compatible",
|
|
18
|
+
"llm",
|
|
19
|
+
"claude",
|
|
20
|
+
"cursor",
|
|
21
|
+
"deepseek",
|
|
22
|
+
"zhipu",
|
|
23
|
+
"qwen",
|
|
24
|
+
"gateway"
|
|
25
|
+
],
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=18"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@modelcontextprotocol/sdk": "^1.0.0",
|
|
32
|
+
"zod": "^3.24.0"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"index.js",
|
|
36
|
+
"README.md",
|
|
37
|
+
"LICENSE",
|
|
38
|
+
".env.example",
|
|
39
|
+
"smithery.yaml"
|
|
40
|
+
]
|
|
41
|
+
}
|
package/smithery.yaml
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Smithery configuration file: https://smithery.ai
|
|
2
|
+
# 让 Smithery 能一键安装并运行本 MCP server
|
|
3
|
+
|
|
4
|
+
startCommand:
|
|
5
|
+
type: stdio
|
|
6
|
+
configSchema:
|
|
7
|
+
type: object
|
|
8
|
+
properties:
|
|
9
|
+
openaiBaseUrl:
|
|
10
|
+
type: string
|
|
11
|
+
title: API Base URL
|
|
12
|
+
description: 任意 OpenAI 兼容网关地址
|
|
13
|
+
default: https://api.deepseek.com/v1
|
|
14
|
+
openaiApiKey:
|
|
15
|
+
type: string
|
|
16
|
+
title: API Key
|
|
17
|
+
description: 网关密钥
|
|
18
|
+
model:
|
|
19
|
+
type: string
|
|
20
|
+
title: Default Model
|
|
21
|
+
description: 默认模型名
|
|
22
|
+
default: deepseek-chat
|
|
23
|
+
required:
|
|
24
|
+
- openaiApiKey
|
|
25
|
+
commandFunction:
|
|
26
|
+
|-
|
|
27
|
+
(config) => ({
|
|
28
|
+
"command": "node",
|
|
29
|
+
"args": ["index.js"],
|
|
30
|
+
"env": {
|
|
31
|
+
"OPENAI_BASE_URL": config.openaiBaseUrl || "https://api.deepseek.com/v1",
|
|
32
|
+
"OPENAI_API_KEY": config.openaiApiKey,
|
|
33
|
+
"MODEL": config.model || "deepseek-chat"
|
|
34
|
+
}
|
|
35
|
+
})
|