当你第一次看到这段代码时,可能会感到困惑:
typescript展开代码// server/api/mcp.post.ts
const transport = new StreamableHTTPServerTransport(...)
await transport.handleRequest(req, res, await readBody(event))
同一个 StreamableHTTPServerTransport,为什么既能处理 Cursor 的同步 HTTP 请求,又能处理 Inspector 的流式 SSE 连接?
这篇文章将揭开这个"魔法"背后的技术原理,带你理解 MCP 协议、HTTP vs Streamable HTTP 的本质区别,以及如何用一套代码优雅地支持两种模式。
bash展开代码import asyncio
import json
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def call_newsnow_mcp():
"""调用 newsnow MCP 获取知乎热门新闻"""
# 1. 配置 MCP 服务器参数
server_params = StdioServerParameters(
command="npx",
args=["-y", "newsnow-mcp-server"],
env={"BASE_URL": "http://101.126.150.28:9044"}
)
# 2. 连接到 MCP 服务器
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
# 3. 初始化会话
await session.initialize()
# 4. 调用工具获取知乎新闻
result = await session.call_tool(
name="get_hottest_latest_news",
arguments={
"id": "zhihu",
"count": 10
}
)
# 5. 打印结果
print("=" * 50)
print("知乎热门新闻(前10条)")
print("=" * 50)
# 解析返回的内容
for item in result.content:
if hasattr(item, 'text'):
# 尝试解析 JSON
try:
news_data = json.loads(item.text)
print(json.dumps(news_data, indent=2, ensure_ascii=False))
except:
print(item.text)
# 运行
if __name__ == "__main__":
asyncio.run(call_newsnow_mcp())
有个mcp服务是这样的:
bash展开代码 "newsnow": {
"command": "npx",
"args": [
"-y",
"newsnow-mcp-server"
],
"env": {
"BASE_URL": "http://101.126.150.28:9044"
}
}
我服务器的801端口是我的博客, 我现在想在外面套一层caddy,自动获取证书 我的域名是tao-blog.site 我如何配置这个caddy
https://huggingface.co/mPLUG/GUI-Owl-32B/tree/main
https://github.com/X-PLUG/MobileAgent
下载:
bash展开代码apt update && apt install -y aria2
wget https://hf-mirror.com/hfd/hfd.sh
chmod a+x hfd.sh
export HF_ENDPOINT=https://hf-mirror.com
./hfd.sh mPLUG/GUI-Owl-32B
部署:
bash展开代码# 使用vLLM启动
python -m vllm.entrypoints.openai.api_server \
--model /mnt/jfs6/model/GUI-Owl-32B \
--served-model-name ui-tars \
--host 0.0.0.0 \
--port 8000 \
--trust-remote-code \
--max-model-len 8192 \
--gpu-memory-utilization 0.9 \
--tensor-parallel-size 4 --api-key "123"
d.xpath('//*[@text="设置"]')d(resourceId="com.example:id/button")d(className="android.widget.Button")d(description="搜索")d.xpath('//android.widget.Button[@text="确定"]')bash展开代码pip install -U huggingface_hub
bash展开代码export HF_ENDPOINT=https://hf-mirror.com
建议写入 ~/.bashrc 或 ~/.zshrc 永久生效:
bash展开代码echo 'export HF_ENDPOINT=https://hf-mirror.com' >> ~/.bashrc
source ~/.bashrc
安装uv:
bash展开代码# 使用官方安装脚本(推荐)
curl -LsSf https://astral.sh/uv/install.sh | sh
source $HOME/.local/bin/env
# 这样也可以:
pip install uv
# win的powershell安装:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
创建虚拟环境,指定D:\python_envs\py313为环境安装目录:
部署:
bash展开代码# 使用vLLM启动
python -m vllm.entrypoints.openai.api_server \
--model /mnt/jfs6/model/UI-TARS-72B-DPO \
--served-model-name ui-tars \
--host 0.0.0.0 \
--port 8000 \
--trust-remote-code \
--max-model-len 8192 \
--gpu-memory-utilization 0.9 \
--tensor-parallel-size 8