基于 Bun + TypeScript 的 stdio MCP 服务,作用是把外部 OpenAI Chat Completions 请求转发到 Claude Code 的 channels,再把 Claude Code 回复回写给 API 调用方。
- MCP 服务通过
stdio运行,可直接被 Claude Code 接入。 - 声明
experimental["claude/channel"],支持notifications/claude/channel。 - 提供 OpenAI 兼容 HTTP API:
POST /v1/chat/completions。 - 外部请求进入后:
- 转成 channel 消息发给 Claude Code
- Claude Code 调用
channel_reply返回内容 - MCP 服务把结果封装成 OpenAI Chat Completions 响应返回
flowchart LR
App[其他应用]
API[OpenAI Compatible API<br/>/v1/chat/completions<br/>/v1/models]
MCP[claude-channel MCP Server<br/>stdio + channel bridge]
Claude[Claude Code]
App --> API
API --> MCP
MCP --> Claude
Claude --> MCP
MCP --> API
API --> App
sequenceDiagram
participant App as 其他应用
participant API as claude-channel HTTP API
participant MCP as claude-channel MCP (stdio)
participant Claude as Claude Code
Claude->>MCP: 启动并接入 MCP
Claude->>MCP: http_server_start (或 BRIDGE_HTTP_AUTO_START=1)
App->>API: POST /v1/chat/completions
API->>MCP: 创建 pending request_id
MCP-->>Claude: notifications/claude/channel<br/><channel source=claude-channel ...>
Claude->>MCP: 调用 channel_reply(request_id, content)
MCP->>API: 匹配 request_id,回填回复
API-->>App: OpenAI Chat Completions 响应
App->>API: GET /v1/models
API-->>App: 模型列表 (claude-channel)
bun installbun run src/index.ts这是一个 stdio MCP 进程,通常由 Claude Code 拉起。
.mcp.json 示例:
{
"mcpServers": {
"claude-channel": {
"command": "bun",
"args": ["run", "/ABS/PATH/TO/claude-channel/src/index.ts"]
}
}
}http_server_start:开启 OpenAI 兼容 HTTP 服务http_server_stop:停止 HTTP 服务http_server_status:查看服务状态runtime_status:查看 MCP 运行状态(含监听 host/port、listen_url、pending 请求数、OpenAI routes 和模型列表)channel_debug_events:调试查看输入输出事件,包括进入 channels 的消息、Claude 回包、以及发给 OpenAI 客户端的响应;支持按request_id、kind、source_type、stream过滤,以及limitchannel_reply:Claude Code 用它回复某个request_idchannel_reply_stream:流式回复工具(按request_id多次发送delta,最后done=true)channel_publish:手动发送测试 channel
curl http://127.0.0.1:8787/healthcurl http://127.0.0.1:8787/v1/modelscurl -X POST http://127.0.0.1:8787/v1/chat/completions \
-H 'Content-Type: application/json' \
-d '{
"model": "claude-channel",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你好,介绍一下你是谁"}
]
}'curl -N -X POST http://127.0.0.1:8787/v1/chat/completions \
-H 'Content-Type: application/json' \
-d '{
"model": "claude-channel",
"stream": true,
"messages": [
{"role": "user", "content": "请分三段介绍你自己"}
]
}'提示:如果上游一次回传整段 delta,可通过 BRIDGE_STREAM_EMIT_CHUNK_SIZE、BRIDGE_STREAM_EMIT_INTERVAL_MS、BRIDGE_STREAM_EMIT_PUNCT_PAUSE_MS 在服务端二次切片,前端会看到更连续、更接近打字机的流式输出。
启动前设置:
export BRIDGE_API_KEY='your-token'请求时:
-H 'Authorization: Bearer your-token'BRIDGE_HTTP_HOST:默认127.0.0.1BRIDGE_HTTP_PORT:默认8787BRIDGE_CHAT_TIMEOUT_MS:默认180000BRIDGE_API_KEY:可选,设置后启用 Bearer 校验BRIDGE_HTTP_AUTO_START=1:MCP 连接后自动启动 HTTP 服务BRIDGE_STREAM_EMIT_CHUNK_SIZE:stream 输出切片大小(按字符切分,默认3)BRIDGE_STREAM_EMIT_INTERVAL_MS:stream 每个切片之间的基础间隔毫秒(默认28)BRIDGE_STREAM_EMIT_PUNCT_PAUSE_MS:遇到句号/问号/叹号等标点时的额外停顿(默认90)
当外部请求到达时,Claude Code 会收到形如:
<channel source="claude-channel" request_id="...">...</channel>Claude Code 需要调用 channel_reply:
request_id:来自 channel 属性content:要回给 API 客户端的文本