一、基于HTTP流式传输的MCP服务器开发流程
- 创建项目文件:
# cd /root/autodl-tmp/MCP
uv init mcp-weather-http
cd mcp-weather-http
# 创建虚拟环境
uv venv
# 激活虚拟环境
source .venv/bin/activate
uv add mcp httpx
代码解释
如果你传的是 --port 3000,访问路径就是 http://localhost:3000/mcp
修改配置文件pyproject.toml:
- 参考:
[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "mcp-weather-http"
version = "1.1.0"
description = "输入OpenWeather-API-KEY,获取天气信息。"
readme = "README.md"
requires-python = ">=3.9"
dependencies = [
"httpx>=0.28.1",
"mcp>=1.8.0",
]
[project.scripts]
mcp-weather-http = "mcp_weather_http:main"
[tool.setuptools]
package-dir = {"" = "src"}
[tool.setuptools.packages.find]
where = ["src"]
二、HTTP流式传输MCP服务器开启与测试
- 在创建完server.py后,我们可以开启服务并进行测试。需要注意的是,我们需要先开启流式HTTP MCP服务器,然后再开启Inspector:
开启流式HTTP MCP服务器
# 回到项目主目录
# cd /root/autodl-tmp/MCP/mcp-weather-http
uv run ./src/mcp_weather_http/server.py --port 3001 --api-key YOUR_KEY
开启Inspector
# 回到项目主目录
# cd /root/autodl-tmp/MCP/mcp-weather-http
# source .venv/bin/activate
npx -y @modelcontextprotocol/inspector
访问: MCP Inspector is up and running at http://127.0.0.1:6274 进入inspector界面:
- Transport Type选择“Streamable HTTP”
- URL设置 http://localhost:3001/mcp
- connect连接成功
三、流式HTTP MCP服务器异地调用
- 接下来即可在异地环境(也可以是本地)通过HTTP方式调用MCP服务了。
- 这里以本地安装的Cherry Studio为例,进行调用演示。
- 此处需要保持远程流式HTTP MCP服务器处于开启状态,然后配置即可。
四、流式HTTP MCP服务器发布流程
- 测试完成后,即可上线发布。这里仍然考虑发布到pypi平台,并使用cherry studio进行本地调用测试。
打包上传:
# 回到项目主目录
# cd xx/MCP/mcp-http-streamable
uv pip install build twine
python -m build
python -m twine upload dist/*
# View at: https://pypi.org/project/mcp-http-streamable-ysx-test/0.1.0/
Release files for mcp-http-streamable-ysx-test 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mcp_http_streamable_ysx_test-0.1.1.tar.gz | 4.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mcp_http_streamable_ysx_test-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.5 kB
Release files / mcp_http_streamable_ysx_test-0.1.1.tar.gz
| Download URL | mcp_http_streamable_ysx_test-0.1.1.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
de1504ebedeba2089c854b836d4613f11ac96b4a9b47159184fb2b0b9bedf214
|
|
BLAKE2b-256 checksum How to use checksums |
61193fc644233bbef53e6e70c37de9688068f5f559c2a23beb0294a0f057f93d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Release files / mcp_http_streamable_ysx_test-0.1.1-py3-none-any.whl
| Download URL | mcp_http_streamable_ysx_test-0.1.1-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4ee90fb1a3dd902b4102031f727b6e7b06557c4c8d4e965da802618cb475b3a8
|
|
BLAKE2b-256 checksum How to use checksums |
8c2b1cbc08e414796a2208567010b7965077e4622452580c43205ee2eec84412
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|