Skip to main content

FastMCP client demo using uv

Project description

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 客户端支持三种传输方式:

  1. In-Memory Transport - 直接连接到同一进程中的 FastMCP 服务器

    client = Client(server=mcp_instance)
    
  2. STDIO Transport - 通过 stdin/stdout 与子进程通信

    client = Client(
        name="my-client",
        command="python",
        args=["server.py"],
    )
    
  3. 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)

更多信息

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastmcp_client_demo-0.1.2.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastmcp_client_demo-0.1.2-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file fastmcp_client_demo-0.1.2.tar.gz.

File metadata

  • Download URL: fastmcp_client_demo-0.1.2.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for fastmcp_client_demo-0.1.2.tar.gz
Algorithm Hash digest
SHA256 0e9dc5b75f6f8b0a7ad3f47097c1222a66ec012dd719b9ccfb141525dcef4cd6
MD5 082375bd5ed9990b10ac54a95c0cdf20
BLAKE2b-256 45c452050d780579685364a01e691f7d4d5119016e64d4b74d1d9a22b6900b78

See more details on using hashes here.

File details

Details for the file fastmcp_client_demo-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for fastmcp_client_demo-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c655e29e92dde757a5e7ece21adab52c5b8937944a81590781a18492f62c5e7e
MD5 77671c962fac020f20ae7563457182fc
BLAKE2b-256 73b85de86f1927aa9ffbec6c04c14095f934214da6da5d5b3eaa019d9c1209c0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page