FastMCP 客户端示例
这是一个使用 uv 和 FastMCP 创建的客户端项目示例。
项目结构
.
├── pyproject.toml # 项目配置和依赖
├── client.py # FastMCP 客户端示例
├── server.py # 简单的 FastMCP 服务器(用于测试)
├── test_client.py # 客户端测试
└── README.md # 本文件
快速开始
1. 安装依赖
使用 uv 安装项目依赖:
uv sync
2. 运行服务器
在一个终端中启动 MCP 服务器:
uv run python server.py
3. 运行客户端
在另一个终端中运行客户端:
uv run python client.py
4. 运行测试
运行客户端测试:
uv run pytest test_client.py -v
文件说明
server.py
一个简单的 FastMCP 服务器示例,包含:
add(a, b)- 加法工具multiply(a, b)- 乘法工具greet(name)- 问候工具hello_resource- 资源示例greeting_prompt- 提示示例
client.py
FastMCP 客户端示例,演示:
- 连接到 STDIO 服务器
- 列出可用工具
- 调用工具
- 列出资源和提示
- HTTP 服务器连接示例
- 从配置创建客户端
test_client.py
使用 pytest 的客户端测试示例,演示:
- 工具列表测试
- 工具调用测试
- 错误处理测试
FastMCP 客户端特性
FastMCP 客户端支持三种传输方式:
-
In-Memory Transport - 直接连接到同一进程中的 FastMCP 服务器
client = Client(server=mcp_instance)
-
STDIO Transport - 通过 stdin/stdout 与子进程通信
client = Client( name="my-client", command="python", args=["server.py"], )
-
HTTP Transport - 连接到 HTTP 服务器
client = Client( name="http-client", url="http://localhost:8000", )
常用操作
列出工具
async with client:
tools = await client.list_tools()
for tool in tools:
print(f"{tool.name}: {tool.description}")
调用工具
async with client:
result = await client.call_tool("add", {"a": 5, "b": 3})
print(result)
读取资源
async with client:
resource = await client.read_resource("text://hello")
print(resource)
获取提示
async with client:
prompt = await client.get_prompt("greeting_prompt", {"name": "World"})
print(prompt)
更多信息
Metadata
Release files for fastmcp-client-demo 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| fastmcp_client_demo-0.1.2.tar.gz | 10.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastmcp_client_demo-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.5 kB
Release files / fastmcp_client_demo-0.1.2.tar.gz
| Download URL | fastmcp_client_demo-0.1.2.tar.gz |
|---|---|
| Size | 10.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0e9dc5b75f6f8b0a7ad3f47097c1222a66ec012dd719b9ccfb141525dcef4cd6
|
|
BLAKE2b-256 checksum How to use checksums |
45c452050d780579685364a01e691f7d4d5119016e64d4b74d1d9a22b6900b78
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.7
|
Release files / fastmcp_client_demo-0.1.2-py3-none-any.whl
| Download URL | fastmcp_client_demo-0.1.2-py3-none-any.whl |
|---|---|
| Size | 14.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c655e29e92dde757a5e7ece21adab52c5b8937944a81590781a18492f62c5e7e
|
|
BLAKE2b-256 checksum How to use checksums |
73b85de86f1927aa9ffbec6c04c14095f934214da6da5d5b3eaa019d9c1209c0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.7
|
Security for fastmcp-client-demo 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.