View a markdown version of this page

從 AgentCore 閘道取得提示 - Amazon Bedrock AgentCore

從 AgentCore 閘道取得提示

若要取得特定提示,請對閘道的 MCP 端點提出 POST 請求,並在請求內文、提示名稱和引數中指定 prompts/get作為方法:

POST /mcp HTTP/1.1 Host: ${GatewayEndpoint} Content-Type: application/json Authorization: ${Authorization header} ${RequestBody}

取代以下的值:

  • ${GatewayEndpoint} – 閘道的 URL,如 CreateGateway API 的回應所提供。

  • ${Authorization header} – 當您設定傳入授權時,來自身分提供者的授權憑證。

  • ${RequestBody} – 請求內文的 JSON 承載,如模型內容通訊協定 (MCP) 中的取得提示中所指定。包含 prompts/get做為 method,並包含提示name的 及其 arguments

回應會以訊息陣列的形式傳回轉譯的提示,每個提示都具有角色和內容。

注意

prompts/get 操作會將即時請求代理到下游 MCP 伺服器。提示名稱必須包含目標字首 (例如 myTarget___myPrompt)。

用於取得提示的程式碼範例

若要查看從閘道取得提示的範例,請選取下列其中一種方法:

範例
curl
  1. 下列 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
  1. 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
  1. 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
  1. 注意:LangGraph MCP 轉接器提示支援可能會有所不同。使用上面的 MCP 用戶端方法進行最可靠的prompts/get實作。

    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 操作可能會傳回下列類型的錯誤:

  • 在 HTTP 狀態碼中傳回的錯誤:

    AuthenticationError

    由於身分驗證登入資料無效,請求失敗。

    HTTP 狀態碼:401

    AuthorizationError

    發起人沒有取得提示的許可。

    HTTP 狀態碼:403

    ResourceNotFoundError

    指定的提示不存在。

    HTTP 狀態碼:404

    ValidationError

    提供的引數不符合提示所需的引數。

    HTTP 狀態碼:400

    InternalServerError

    發生內部伺服器錯誤。

    HTTP 狀態碼:500

  • MCP 錯誤。如需這些錯誤類型的詳細資訊,請參閱模型內容通訊協定 (MCP) 文件中的提示