클라이언트에서 프롬프트 가져오기
이번에는 사용자 입력을 활용해 통화 서버에서 특정 프롬프트를 가져옵니다. 이를 통해 템플릿과 사용자의 요청이 결합됩니다. 이렇게 완성된 프롬프트는 LLM이 변환 도구를 호출할지 여부를 결정하기 전에 전달됩니다.
이 연습은 강의의 일부입니다
Model Context Protocol (MCP) 입문
연습 안내
- 세션을 초기화한 후, 이름으로 프롬프트를 가져오는 메서드를 호출하세요. 프롬프트 이름과 사용자 입력이 담긴
arguments딕셔너리를 함께 전달하세요. - 프롬프트 결과에서 첫 번째 메시지의 텍스트 콘텐츠를 추출하여 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def read_prompt(user_input: str = "How much is 50 GBP in euros?", prompt_name: str = "convert_currency_prompt") -> str:
"""Retrieve a prompt from the MCP server with user input."""
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()
# Retrieve the prompt with the user's input
prompt = await session.____(prompt_name, arguments={"currency_request": user_input})
# Print the full prompt text (template + user request)
text = prompt.____[0].____.____
print(text)
return text
asyncio.run(read_prompt(user_input="How much is 50 GBP in euros?"))