Skip to main content
Glama
test_fastmcp_client.pyโ€ข4.16 kB
#!/usr/bin/env python3 """ Test FastMCP Client with OAuth This script tests the MCP server using FastMCP's built-in client with OAuth authentication support. """ import asyncio import os from fastmcp import Client async def test_with_demo_client(): """Test using pre-registered demo client credentials.""" print("\n" + "="*60) print("๐Ÿงช FastMCP Client Test with OAuth") print("="*60) # Set OAuth credentials as environment variables os.environ["OAUTH_CLIENT_ID"] = "demo_client" os.environ["OAUTH_CLIENT_SECRET"] = "demo_secret" # Create client pointing to MCP endpoint (HTTP transport) client = Client("http://localhost:8000/mcp") print("\n๐Ÿ“ก Connecting to MCP server with OAuth...") print(f" MCP Endpoint: http://localhost:8000/mcp") print(f" Client ID: {os.environ['OAUTH_CLIENT_ID']}") try: async with client: print(" โœ… Connected successfully!") # List available tools print("\n๐Ÿ”ง Available tools:") tools = await client.get_available_tools() for tool in tools: print(f" - {tool.name}: {tool.description}") # Test greet tool print("\n๐Ÿ“ž Testing greet tool...") result = await client.call_tool("greet", {"name": "OAuth User"}) print(f" Result: {result.content[0].text}") # Test get_secret tool print("\n๐Ÿ” Testing get_secret tool...") result = await client.call_tool("get_secret", {"key": "api_key"}) print(f" Result: {result.content[0].text}") # Test with invalid key print("\n๐Ÿ” Testing get_secret with invalid key...") result = await client.call_tool("get_secret", {"key": "nonexistent"}) print(f" Result: {result.content[0].text}") print("\nโœ… All tests completed successfully!") except Exception as e: print(f"\nโŒ Error: {str(e)}") import traceback traceback.print_exc() async def test_without_credentials(): """Test without credentials (should fail).""" print("\n" + "="*60) print("๐Ÿงช FastMCP Client Test without Credentials") print("="*60) # Clear any existing OAuth environment variables os.environ.pop("OAUTH_CLIENT_ID", None) os.environ.pop("OAUTH_CLIENT_SECRET", None) # Create client without credentials client = Client("http://localhost:8000/mcp") print("\n๐Ÿ“ก Attempting to connect without credentials...") print(f" MCP Endpoint: http://localhost:8000/mcp") try: async with client: print(" โŒ Should not have connected without auth!") except Exception as e: print(f" โœ… Expected error (no auth): {str(e)[:100]}...") # This is expected behavior - server requires OAuth async def main(): """Run all tests.""" print("\n๐Ÿš€ Testing FastMCP Client with OAuth MCP Server") print("="*60) print("Server URL: http://localhost:8000") print("="*60) # Check if server is running try: import httpx async with httpx.AsyncClient() as http_client: response = await http_client.get("http://localhost:8000/.well-known/openid-configuration") if response.status_code == 200: print("โœ… MCP OAuth server is running") else: print("โŒ Server responded but OAuth metadata not found") return except Exception as e: print("โŒ MCP server is not running! Please start it first.") print(f" Error: {str(e)}") return # Test 1: Use demo client credentials await test_with_demo_client() # Test 2: Try without credentials (should fail) print("\nโณ Waiting 2 seconds before next test...") await asyncio.sleep(2) await test_without_credentials() print("\n" + "="*60) print("๐ŸŽ‰ All tests completed!") print("="*60) if __name__ == "__main__": asyncio.run(main())

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/jaysunmah/oauth_mcp'

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