AgentCore 게이트웨이에서 프롬프트 가져오기
특정 프롬프트를 가져오려면 게이트웨이의 MCP 엔드포인트에 POST 요청을 하고 요청 본문의 메서드, 프롬프트 이름 및 인수prompts/get로를 지정합니다.
POST /mcp HTTP/1.1
Host: ${GatewayEndpoint}
Content-Type: application/json
Authorization: ${Authorization header}
${RequestBody}
다음 값을 교체합니다.
응답은 렌더링된 프롬프트를 각각 역할 및 콘텐츠가 포함된 메시지 배열로 반환합니다.
prompts/get 작업은 요청을 다운스트림 MCP 서버에 라이브로 프록시합니다. 프롬프트 이름에는 대상 접두사(예: myTarget___myPrompt)가 포함되어야 합니다.
프롬프트를 가져오기 위한 코드 샘플
게이트웨이에서 프롬프트를 가져오는 예를 보려면 다음 방법 중 하나를 선택합니다.
예
- curl
-
-
다음 curl 요청은 ID가 인 게이트웨이를 myTarget___code_review 통해 라는 프롬프트를 가져오기 위한 예제 요청을 보여줍니다mygateway-abcdefghij.
curl -X POST \
https://mygateway-abcdefghij.gateway.bedrock-agentcore.us-west-2.amazonaws.com/mcp \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-d '{
"jsonrpc": "2.0",
"id": "get-prompt-request",
"method": "prompts/get",
"params": {
"name": "myTarget___code_review",
"arguments": {
"language": "python",
"code": "print(\"hello\")"
}
}
}'
- Python requests package
-
-
import requests
import json
def get_prompt(gateway_url, access_token, prompt_name, arguments):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}"
}
payload = {
"jsonrpc": "2.0",
"id": "get-prompt-request",
"method": "prompts/get",
"params": {
"name": prompt_name,
"arguments": arguments
}
}
response = requests.post(gateway_url, headers=headers, json=payload)
return response.json()
# Example usage
gateway_url = "https://${GatewayEndpoint}/mcp" # Replace with your actual gateway endpoint
access_token = "${AccessToken}" # Replace with your actual access token
result = get_prompt(
gateway_url,
access_token,
"myTarget___code_review", # Replace with {targetName}___{promptName}
{"language": "python", "code": "print('hello')"}
)
print(json.dumps(result, indent=2))
- MCP Client
-
-
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client
import asyncio
async def execute_mcp(
url,
token,
prompt_name,
prompt_arguments,
headers=None
):
default_headers = {
"Authorization": f"Bearer {token}"
}
headers = {**default_headers, **(headers or {})}
async with streamablehttp_client(
url=url,
headers=headers,
) as (
read_stream,
write_stream,
callA,
):
async with ClientSession(read_stream, write_stream) as session:
# 1. Perform initialization handshake
print("Initializing MCP...")
_init_response = await session.initialize()
print(f"MCP Server Initialize successful! - {_init_response}")
# 2. Get specific prompt
print(f"Getting prompt: {prompt_name}")
prompt_response = await session.get_prompt(
name=prompt_name,
arguments=prompt_arguments
)
print(f"Prompt response: {prompt_response}")
return prompt_response
async def main():
url = "https://${GatewayEndpoint}/mcp"
token = "your_bearer_token_here"
prompt_name = "myTarget___code_review"
prompt_arguments = {
"language": "python",
"code": "print('hello')"
}
await execute_mcp(
url=url,
token=token,
prompt_name=prompt_name,
prompt_arguments=prompt_arguments
)
if __name__ == "__main__":
asyncio.run(main())
- LangGraph MCP Client
-
-
참고: LangGraph MCP 어댑터 프롬프트 지원은 다를 수 있습니다. 가장 안정적인 prompts/get 구현을 위해 위의 MCP 클라이언트 접근 방식을 사용합니다.
import asyncio
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client
async def get_prompt(url, token, prompt_name, arguments):
headers = {"Authorization": f"Bearer {token}"}
async with streamablehttp_client(url=url, headers=headers) as (
read_stream, write_stream, callA
):
async with ClientSession(read_stream, write_stream) as session:
await session.initialize()
response = await session.get_prompt(
name=prompt_name,
arguments=arguments
)
for message in response.messages:
print(f"{message.role}: {message.content}")
asyncio.run(get_prompt(
"https://${GatewayEndpoint}/mcp",
"${AccessToken}",
"myTarget___code_review",
{"language": "python", "code": "print('hello')"}
))
오류
prompts/get 작업은 다음과 같은 유형의 오류를 반환할 수 있습니다.