View a markdown version of this page

Ricevi un prompt da un AgentCore gateway - Fondamento Amazon AgentCore

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Ricevi un prompt da un AgentCore gateway

Per ottenere un prompt specifico, effettua una richiesta POST all'endpoint MCP del gateway e specifica prompts/get come metodo nel corpo della richiesta, il nome del prompt e gli argomenti:

Esempio
2025-11-25 and earlier
POST /mcp HTTP/1.1 Host: ${GatewayEndpoint} Accept: application/json, text/event-stream Content-Type: application/json Authorization: ${Authorization header} MCP-Protocol-Version: ${McpProtocolVersion} ${RequestBody}
2026-07-28

In base alla versione2026-07-28, ogni richiesta contiene l'MCP-Protocol-Versionintestazione, le intestazioni Mcp-Method e i Mcp-Name metadati della richiesta e i campi della versione nel corpo. _meta

POST /mcp HTTP/1.1 Host: ${GatewayEndpoint} Accept: application/json, text/event-stream Content-Type: application/json Authorization: ${Authorization header} MCP-Protocol-Version: 2026-07-28 Mcp-Method: prompts/get Mcp-Name: ${PromptName} ${RequestBody}
Nota

Il gateway accetta solo le versioni del protocollo MCP elencate nel campo della sua configurazione. supportedVersions protocolConfiguration.mcp Per utilizzare la versione2026-07-28, assicurati che sia supportedVersions inclusa nel gateway. Puoi modificare le versioni supportate con l'UpdateGatewayAPI.

Sostituisci i valori seguenti:

  • ${GatewayEndpoint}— L'URL del gateway, come fornito nella risposta dell'CreateGatewayAPI.

  • ${Authorization header}— Le credenziali di autorizzazione del provider di identità quando si imposta l'autorizzazione in entrata.

  • ${McpProtocolVersion}— La versione del protocollo MCP per la richiesta, ad esempio. 2025-11-25 La versione deve essere quella supportata dal gateway.

  • ${PromptName}— Il nome del prompt, corrispondente a quello name nel corpo della richiesta.

  • ${RequestBody}— Il payload JSON del corpo della richiesta, come specificato in Getting a prompt in the Model Context Protocol (MCP). Includi prompts/get come method e name includi il valore del prompt e il relativo. arguments Nella versione2026-07-28, includi anche i campi della _meta versione inparams.

La risposta restituisce il prompt renderizzato come una serie di messaggi, ciascuno con un ruolo e un contenuto.

Nota

L'prompts/getoperazione invia la richiesta in tempo reale al server MCP a valle. Il nome del prompt deve includere il prefisso di destinazione (ad esempio,). myTarget___myPrompt

Esempi di codice per ottenere un prompt

Per visualizzare esempi di ricezione di un prompt dal gateway, seleziona uno dei seguenti metodi:

Esempio
curl (2025-11-25 and earlier)

La seguente richiesta curl mostra un esempio di richiesta per ottenere un prompt richiamato myTarget___code_review tramite un gateway con l'IDmygateway-abcdefghij. Imposta l'MCP-Protocol-Versionintestazione su una versione supportata dal tuo gateway.

curl -X POST \ https://mygateway-abcdefghij.gateway.bedrock-agentcore.us-west-2.amazonaws.com/mcp \ -H "Accept: application/json, text/event-stream" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -H "MCP-Protocol-Version: 2025-11-25" \ -d '{ "jsonrpc": "2.0", "id": "get-prompt-request", "method": "prompts/get", "params": { "name": "myTarget___code_review", "arguments": { "language": "python", "code": "print(\"hello\")" } } }'
curl (2026-07-28)

A seconda della versione2026-07-28, includi le intestazioni Mcp-Method e dei Mcp-Name metadati di richiesta e i campi della versione nel _meta corpo. L'intestazione deve corrispondere. MCP-Protocol-Version _meta.io.modelcontextprotocol/protocolVersion Il tuo gateway supportedVersions deve includere2026-07-28.

curl -X POST \ https://mygateway-abcdefghij.gateway.bedrock-agentcore.us-west-2.amazonaws.com/mcp \ -H "Accept: application/json, text/event-stream" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -H "MCP-Protocol-Version: 2026-07-28" \ -H "Mcp-Method: prompts/get" \ -H "Mcp-Name: myTarget___code_review" \ -d '{ "jsonrpc": "2.0", "id": "get-prompt-request", "method": "prompts/get", "params": { "name": "myTarget___code_review", "arguments": { "language": "python", "code": "print(\"hello\")" }, "_meta": { "io.modelcontextprotocol/protocolVersion": "2026-07-28", "io.modelcontextprotocol/clientInfo": { "name": "my-agent", "version": "1.0.0" }, "io.modelcontextprotocol/clientCapabilities": {} } } }'
Python requests package (2025-11-25 and earlier)

Imposta l'MCP-Protocol-Versionintestazione su una versione supportata dal tuo gateway.

import requests import json def get_prompt(gateway_url, access_token, prompt_name, arguments): headers = { "Content-Type": "application/json", "Authorization": f"Bearer {access_token}", "MCP-Protocol-Version": "2025-11-25" } 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))
Python requests package (2026-07-28)

A seconda della versione2026-07-28, includi le intestazioni Mcp-Method e dei Mcp-Name metadati di richiesta e i campi della versione nel _meta corpo. L'intestazione deve corrispondere. MCP-Protocol-Version _meta.io.modelcontextprotocol/protocolVersion Il tuo gateway supportedVersions deve includere2026-07-28.

import requests import json def get_prompt(gateway_url, access_token, prompt_name, arguments): headers = { "Content-Type": "application/json", "Authorization": f"Bearer {access_token}", "MCP-Protocol-Version": "2026-07-28", "Mcp-Method": "prompts/get", "Mcp-Name": prompt_name } payload = { "jsonrpc": "2.0", "id": "get-prompt-request", "method": "prompts/get", "params": { "name": prompt_name, "arguments": arguments, "_meta": { "io.modelcontextprotocol/protocolVersion": "2026-07-28", "io.modelcontextprotocol/clientInfo": {"name": "my-agent", "version": "1.0.0"}, "io.modelcontextprotocol/clientCapabilities": {} } } } 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

NOTA: il supporto richiesto per l'adattatore LangGraph MCP può variare. Utilizzate l'approccio MCP Client mostrato in precedenza per l'implementazione più affidabile. 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')"} ))

Errori

L'prompts/getoperazione può restituire i seguenti tipi di errori:

  • Errori restituiti come parte del codice di stato HTTP:

    AuthenticationError

    La richiesta non è riuscita a causa di credenziali di autenticazione non valide.

    Codice di stato HTTP: 401

    AuthorizationError

    Il chiamante non è autorizzato a ricevere la richiesta.

    Codice di stato HTTP: 403

    ResourceNotFoundError

    Il prompt specificato non esiste.

    Codice di stato HTTP: 404

    ValidationError

    Gli argomenti forniti non soddisfano gli argomenti richiesti dal prompt.

    Codice di stato HTTP: 400

    InternalServerError

    Si è verificato un errore interno del server.

    Codice di stato HTTP: 500

  • Errori MCP. Per ulteriori informazioni su questi tipi di errori, vedere le istruzioni nella documentazione del Model Context Protocol (MCP).