包装MCP 本地命令行 (stdio) 工具并暴露到 MCP 服务端 (StreamHTTP)接口

下面我给你一个 可直接落地的方案
Python 包装 Model Context Protocol 的 fetch CLI 工具 → 抽象成内部 Python 客户端 API → 再通过 FastAPI 暴露 HTTP + SSE(或 WebSocket)流式接口 → 最终打包成 Docker 可部署服务


✅ 整体架构

+------------------------+
|   Your Clients         |
|  (HTTP/SSE/Websocket)  |
+-----------+------------+
            |
            v
+------------------------+
|   Python HTTP Server   |  ← 用 FastAPI
|  /mcp/fetch (stream)   |
+-----------+------------+
            |
            v
+------------------------+
| Python Wrapper for MCP |
|   调用 fetch CLI       |
+-----------+------------+
            |
            v
+------------------------+
|  modelcontextprotocol  |
|        fetch CLI       |
+------------------------+

🌟 你最终将获得

  • 一个 Python 库 + HTTP 服务,用来执行 MCP fetch
  • 支持 stream(Server-Sent Events)
  • 支持 Docker 部署

1. Python 包装 MCP fetch CLI

假设你的 MCP CLI 是:

mcp fetch --model hf:meta-llama/Llama-3.1-8B --input "hello"

📌 Python wrapper(支持流式输出)

import subprocess
import json

def mcp_fetch_stream(model: str, prompt: str):
    proc = subprocess.Popen(
        [
            "mcp", "fetch",
            "--model", model,
            "--input", prompt,
            "--stream"
        ],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
        bufsize=1
    )

    # 流式读取 stdout
    for line in proc.stdout:
        line = line.strip()
        if not line:
            continue

        # 假设 mcp fetch 输出 JSON lines
        try:
            yield json.loads(line)
        except:
            yield {"raw": line}

    proc.wait()

2. 用 FastAPI 暴露 HTTP 接口

📌 普通 HTTP 接口(一次性返回)

from fastapi import FastAPI
from fastapi.responses import JSONResponse

app = FastAPI()

@app.post("/mcp/fetch")
async def fetch(data: dict):
    model = data["model"]
    prompt = data["prompt"]

    result = list(mcp_fetch_stream(model, prompt))

    return JSONResponse(content={"output": result})

3. 实现流式 HTTP(SSE 流)

ModelContextProtocol 的命令行是逐行输出,所以用 SSE 正好合适。

📌 SSE 返回流式响应

from fastapi import FastAPI
from fastapi.responses import StreamingResponse

app = FastAPI()

def sse_event(data: dict):
    return f"data: {json.dumps(data, ensure_ascii=False)}\n\n"

@app.post("/mcp/fetch/stream")
async def fetch_stream(req: dict):
    model = req["model"]
    prompt = req["prompt"]

    def event_generator():
        for item in mcp_fetch_stream(model, prompt):
            yield sse_event(item)

    return StreamingResponse(event_generator(), media_type="text/event-stream")

前端可这样消费:

const es = new EventSource("/mcp/fetch/stream");

es.onmessage = (ev) => {
    console.log("chunk:", ev.data);
};

4. Dockerfile

📌 Dockerfile (适合生产环境)

FROM python:3.11-slim

# 安装 MCP CLI 工具
RUN pip install modelcontextprotocol

# 工作目录
WORKDIR /app

# 拷贝代码
COPY . .

# 安装依赖
RUN pip install -r requirements.txt

EXPOSE 8000

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

5. requirements.txt

fastapi
uvicorn[standard]
modelcontextprotocol

6. 运行方式

docker build -t mcp-proxy .
docker run -p 8000:8000 mcp-proxy

访问:

POST /mcp/fetch
POST /mcp/fetch/stream

🔥 可选增强功能(可帮你继续写)

如果你需要,我可以继续帮你加入:

  • 🔐 API Key 鉴权
  • 🧵 队列+并发限制
  • 🐳 多模型缓存 / HuggingFace token
  • 🚀 WebSocket 流式接口
  • 📊 Prometheus metrics
  • 📦 Traefik / NGINX / Kubernetes 部署模板

如果你愿意,我可以直接给你:

🟦 完整可运行项目:目录结构 + 全部 Python 代码 + Dockerfile
🟦 配合你正在做的 HuggingFace 代理服务一起工作
🟦 支持 streamingHTTP 正式的 MCP 结构

要吗?

评论栏