#!/usr/bin/env python3
"""Test script to read resources from MCP servers."""
import asyncio
import sys
from pathlib import Path
# Add src to path
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
from mcp.client.session import ClientSession
from mcp.client.stdio import stdio_client
from mcp import StdioServerParameters
async def test_resource_read() -> None:
"""Test reading resources from the basic server."""
server_command = ["uv", "run", "python", "src/mcp_server_hero/examples/basic_server.py"]
try:
server_params = StdioServerParameters(
command=server_command[0],
args=server_command[1:]
)
async with stdio_client(server_params) as streams:
async with ClientSession(streams[0], streams[1]) as session:
await session.initialize()
print("🔍 Testing resource reading...")
# List resources
resources = await session.list_resources()
print(f"📁 Found {len(resources.resources)} resources:")
for resource in resources.resources:
print(f" - {resource.name} ({resource.uri}): {resource.description}")
# Read the greeting resource
if resources.resources:
resource = resources.resources[0]
print(f"\n📖 Reading resource: {resource.uri}")
try:
content = await session.read_resource(resource.uri)
print(f"✅ Resource content: {content.contents}")
except Exception as e:
print(f"❌ Failed to read resource: {e}")
print("🎉 Resource test completed!")
except Exception as e:
print(f"❌ Test failed: {e}")
if __name__ == "__main__":
asyncio.run(test_resource_read())