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.1.tar.gz (4.2 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.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for fastmcp_client_demo-0.1.1.tar.gz
Algorithm Hash digest
SHA256 35ba8d740b539255fda9480766eb891324c297c3cac7bac12e6eb84908eb93a5
MD5 883dede0adedcc8bf267c67efa5927dd
BLAKE2b-256 fe3cdc4d7b0379c76cde8cc96e2a88633150022aeed7a7fe78ef32cc3a5d8b02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastmcp_client_demo-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4bb251a6bd9a8d122f079e3f8ff20366659ad23a91b1e82d9dbb4646b70b876e
MD5 b82f718d51813e31c51f5c86bb56a8d7
BLAKE2b-256 5ee701e70016f488e7ba126998e6d8a29ffa75a9947e69a20a5d9d4d779ae456

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