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.0.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.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastmcp_client_demo-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 c66947acb713ac8c7d900b8ba232bd5f0de5a06664226a9ad37a7f39a0c5f90e
MD5 f9f2af6367d6a962c36060c084f6efdb
BLAKE2b-256 200289c9e3193df29004fa2994144943dc0718d7644523175503dfd266ce610a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastmcp_client_demo-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9106d3d5607e02179b93ae53609dc3503c68582d68bf65f7cb9d785bae840271
MD5 b4b10a14c69f95a4f42d3c4962c36692
BLAKE2b-256 fd17386ea1f47d50c1083fe577b513be39859b314fdaed19bf7b0b2de25a3cd5

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