Skip to main content
Glama
digital-duck

Model Context Protocol Demo

by digital-duck
mcp_client.py1.95 kB
import asyncio from mcp import ClientSession, StdioServerParameters from mcp.client.stdio import stdio_client from pathlib import Path def get_mcp_server_path(server_filename="mcp_server.py"): """ Get absolute path to MCP server file in the same directory as the current script. Args: server_filename (str): Name of the MCP server file (default: "mcp_server.py") Returns: Path: Absolute path to the MCP server file Raises: FileNotFoundError: If the server file doesn't exist RuntimeError: If unable to determine current script directory """ try: # Method 1: Use __file__ if available (works in most cases) if '__file__' in globals(): current_script_dir = Path(__file__).parent.resolve() else: # Method 2: Fallback for interactive environments current_script_dir = Path.cwd() # Construct the server path server_path = current_script_dir / server_filename # Verify the file exists if not server_path.exists(): raise FileNotFoundError(f"MCP server file not found: {server_path}") return server_path.resolve() # Return absolute path except Exception as e: raise RuntimeError(f"Failed to determine MCP server path: {e}") async def run_mcp_client(): server_params = StdioServerParameters( command="python", # args=[str(get_mcp_server_path("my_server.py"))] args=["my_server.py"] # Assuming the server script is in the same directory)] ) async with stdio_client(server_params) as (read, write): async with ClientSession(read, write) as session: # Initialize await session.initialize() # Call tool result = await session.call_tool("greet", {"name": "Wen"}) print(result) asyncio.run(run_mcp_client())

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/digital-duck/mcp_demo'

If you have feedback or need assistance with the MCP directory API, please join our Discord server