클라이언트에서 리소스 목록 조회하기
서버가 currency_server.py 파일에 작성되어 연결할 준비가 되었습니다! 먼저 이 서버에서 사용 가능한 리소스를 나열할 수 있는 클라이언트를 만들어 보겠습니다.
이 연습은 강의의 일부입니다
Model Context Protocol (MCP) 입문
연습 안내
- 서버 세션 컨텍스트 안에서 사용 가능한 리소스를 가져오세요.
response.resources를 반복 처리하여 각 리소스의 URI, 이름, 설명을 해당 속성을 사용해 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def list_resources():
"""List all available resources from the MCP server."""
params = StdioServerParameters(command=sys.executable, args=["currency_server.py"])
async with stdio_client(params) as (reader, writer):
async with ClientSession(reader, writer) as session:
await session.initialize()
# Get the list of resources
response = ____ ____()
print("Available resources:")
# Print each resource's URI, name, and description
for resource in response.____:
print(f" - {resource.____}")
print(f" Name: {resource.____}")
print(f" Description: {resource.____}")
return response.resources
asyncio.run(list_resources())