File size: 1,613 Bytes
ad95a4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Utility client for validating the FastMCP sample server."""
from __future__ import annotations
import argparse
import asyncio
from typing import Any, Iterable, Optional
from fastmcp import Client
from fastmcp.types import TextContent
def extract_first_text_block(content_blocks: Optional[Iterable[Any]]) -> Optional[str]:
    """Return the first text block contained in the MCP tool response."""
    if not content_blocks:
        return None
    for block in content_blocks:
        if isinstance(block, TextContent):
            return block.text
    return None
async def run(url: str) -> None:
    """Execute a simple validation workflow against the MCP server."""
    client = Client(url=url)
    async with client:
        ping_result = await client.ping()
        print("Ping:", ping_result)
        tools_response = await client.list_tools()
        print("Tools:", tools_response)
        tool_call = await client.call_tool("echo", {"text": "Hello MCP"})
        text = extract_first_text_block(getattr(tool_call, "content", None))
        if text is not None:
            print("Echo response:", text)
        else:
            print("Echo response contains no TextContent blocks:", tool_call)
def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="FastMCP sample client")
    parser.add_argument(
        "--url",
        default="http://localhost:7860/mcp",
        help="MCP server URL (default: %(default)s)",
    )
    return parser.parse_args()
def main() -> None:
    args = parse_args()
    asyncio.run(run(args.url))
if __name__ == "__main__":
    main()