Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
"import argparse\n",
"from boto3.session import Session\n",
"from opentelemetry import baggage, context\n",
"from scripts.utils import get_ssm_parameter\n",
"from lab_helpers.utils import get_ssm_parameter\n",
"\n",
"from strands import Agent\n",
"from strands.models import BedrockModel\n",
Expand Down
2 changes: 1 addition & 1 deletion 01-tutorials/07-AgentCore-E2E/Optional-lab-identity.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@
"try:\n",
" print(\"📥 Fetching Cognito configuration from SSM...\")\n",
" \n",
" client_id = get_ssm_parameter(\"/app/customersupport/agentcore/machine_client_id\")\n",
" client_id = get_ssm_parameter(\"/app/customersupport/agentcore/client_id\")\n",
" print(f\"✅ Retrieved client ID: {client_id}\")\n",
"\n",
" client_secret = get_ssm_parameter(\"/app/customersupport/agentcore/cognito_secret\")\n",
Expand Down
4 changes: 2 additions & 2 deletions 01-tutorials/07-AgentCore-E2E/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ Each lab builds on the previous one, but you can jump ahead if you understand th

Watch your architecture grow from a simple local agent to a production system:

**Lab 1:** Local agent with embedded tools
**Lab 1:** Local agent with embedded tools
**Lab 2:** Agent + AgentCore Memory for persistence
**Lab 3:** Agent + AgentCore Memory + AgentCore Gateway and AgentCore Identity for shared tools
**Lab 4:** Deployment to AgentCore Runtime and observability with AgentCore Observability
**Lab 5:** Customer-facing application with authentication

Ready to build? [Start with Lab 1 →](lab-01-create-an-agent.ipynb)
Ready to build? [Start with Lab 1 →](lab-01-create-an-agent.ipynb)
143 changes: 45 additions & 98 deletions 01-tutorials/07-AgentCore-E2E/lab-03-agentcore-gateway.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,7 @@
"id": "ee591955-83e2-4271-a596-b4a408ee6946",
"metadata": {},
"source": [
"## Step 1: Install and import required libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1d54201a-4a1b-4947-85fd-20c90d488f99",
"metadata": {},
"outputs": [],
"source": [
"# Install required packages\n",
"%pip install strands-agents \"boto3>=1.39.15\" strands-agents-tools bedrock_agentcore ddgs -q"
"## Step 1: Import required libraries"
]
},
{
Expand All @@ -108,18 +97,17 @@
"outputs": [],
"source": [
"# Import libraries\n",
"from strands import Agent\n",
"from strands.models import BedrockModel\n",
"from strands.tools.mcp import MCPClient\n",
"import os\n",
"import sys\n",
"import boto3\n",
"import json\n",
"from bedrock_agentcore.identity.auth import requires_access_token\n",
"\n",
"from strands import Agent\n",
"from strands.models import BedrockModel\n",
"from strands.tools.mcp import MCPClient\n",
"from mcp.client.streamable_http import streamablehttp_client\n",
"import requests\n",
"from lab_helpers.utils import get_or_create_cognito_pool, put_ssm_parameter, get_ssm_parameter, load_api_spec\n",
"\n",
"from scripts.utils import get_ssm_parameter, put_ssm_parameter, load_api_spec, get_cognito_client_secret\n",
"\n",
"sts_client = boto3.client('sts')\n",
"\n",
Expand Down Expand Up @@ -286,12 +274,11 @@
"source": [
"gateway_name = \"customersupport-gw\"\n",
"\n",
"cognito_config = get_or_create_cognito_pool(refresh_token=True)\n",
"auth_config = {\n",
" \"customJWTAuthorizer\": {\n",
" \"allowedClients\": [\n",
" get_ssm_parameter(\"/app/customersupport/agentcore/machine_client_id\")\n",
" ],\n",
" \"discoveryUrl\": get_ssm_parameter(\"/app/customersupport/agentcore/cognito_discovery_url\")\n",
" \"allowedClients\": [cognito_config[\"client_id\"]],\n",
" \"discoveryUrl\": cognito_config[\"discovery_url\"]\n",
" }\n",
"}\n",
"\n",
Expand All @@ -317,7 +304,13 @@
" \"gateway_arn\": create_response[\"gatewayArn\"],\n",
" }\n",
" put_ssm_parameter(\"/app/customersupport/agentcore/gateway_id\", gateway_id)\n",
"\n",
" put_ssm_parameter(\"/app/customersupport/agentcore/gateway_name\", gateway_name)\n",
" put_ssm_parameter(\n",
" \"/app/customersupport/agentcore/gateway_arn\", create_response[\"gatewayArn\"]\n",
" )\n",
" put_ssm_parameter(\n",
" \"/app/customersupport/agentcore/gateway_url\", create_response[\"gatewayUrl\"]\n",
" )\n",
" print(f\"✅ Gateway created successfully with ID: {gateway_id}\")\n",
"\n",
"except Exception as e:\n",
Expand Down Expand Up @@ -354,14 +347,6 @@
"metadata": {},
"outputs": [],
"source": [
"def load_api_spec(file_path: str) -> list:\n",
" with open(file_path, \"r\") as f:\n",
" data = json.load(f)\n",
" \n",
" if not isinstance(data, list):\n",
" raise ValueError(\"Expected a list in the JSON file\")\n",
" return data\n",
"\n",
"try:\n",
" api_spec_file = \"./prerequisite/lambda/api_spec.json\"\n",
"\n",
Expand Down Expand Up @@ -406,40 +391,8 @@
"metadata": {},
"source": [
"## Step 7: Add our new MCP-based tools to our support agent\n",
"Here we integrate our authentication token from Cognito into an MCPClient from Strands SDK to create an MCP Server object to integrate with our Strands Agent"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6fec14df-e398-4321-9903-21a5f5f76c3a",
"metadata": {},
"outputs": [],
"source": [
"def get_token(client_id: str, client_secret: str, scope_string: str, url: str) -> dict:\n",
" try:\n",
" headers = {\"Content-Type\": \"application/x-www-form-urlencoded\"}\n",
" data = {\n",
" \"grant_type\": \"client_credentials\",\n",
" \"client_id\": client_id,\n",
" \"client_secret\": client_secret,\n",
" \"scope\": scope_string,\n",
"\n",
" }\n",
" response = requests.post(url, headers=headers, data=data)\n",
" response.raise_for_status()\n",
" return response.json()\n",
"\n",
" except requests.exceptions.RequestException as err:\n",
" return {\"error\": str(err)}"
]
},
{
"cell_type": "markdown",
"id": "bff0a6b8-fdd3-4536-8012-6d0ab34f568f",
"metadata": {},
"source": [
"## Step 7.1. Set up a secure MCP client object"
"Here we integrate our authentication token from Cognito into an MCPClient from Strands SDK to create an MCP Server object to integrate with our Strands Agent\n",
"### Step 7.1. Set up a secure MCP client object"
]
},
{
Expand All @@ -449,19 +402,12 @@
"metadata": {},
"outputs": [],
"source": [
"gateway_access_token = get_token(\n",
" get_ssm_parameter(\"/app/customersupport/agentcore/machine_client_id\"),\n",
" get_cognito_client_secret(),\n",
" get_ssm_parameter(\"/app/customersupport/agentcore/cognito_auth_scope\"),\n",
" get_ssm_parameter(\"/app/customersupport/agentcore/cognito_token_url\"))\n",
"\n",
"print(f\"Gateway Endpoint - MCP URL: {gateway['gateway_url']}\")\n",
"\n",
"# Set up MCP client\n",
"mcp_client = MCPClient(\n",
" lambda: streamablehttp_client(\n",
" gateway['gateway_url'],\n",
" headers={\"Authorization\": f\"Bearer {gateway_access_token['access_token']}\"},\n",
" headers={\"Authorization\": f\"Bearer {cognito_config['bearer_token']}\"},\n",
" )\n",
")"
]
Expand Down Expand Up @@ -501,28 +447,29 @@
" temperature=0.3, # Balanced between creativity and consistency\n",
" region_name=REGION\n",
")\n",
"\n",
"try:\n",
" mcp_client.start()\n",
"except Exception as e:\n",
" print(f\"Error initializing agent: {str(e)}\")\n",
"\n",
"tools = (\n",
" [\n",
" get_product_info,\n",
" get_return_policy,\n",
" get_technical_support\n",
" ]\n",
" + mcp_client.list_tools_sync()\n",
" )\n",
"\n",
"# Create the customer support agent\n",
"agent = Agent(\n",
" model=model,\n",
" tools=tools,\n",
" hooks=[memory_hooks],\n",
" system_prompt=SYSTEM_PROMPT\n",
")\n",
"def create_agent(prompt):\n",
" try:\n",
" with mcp_client:\n",
" tools = (\n",
" [\n",
" get_product_info,\n",
" get_return_policy,\n",
" get_technical_support\n",
" ]\n",
" + mcp_client.list_tools_sync()\n",
" )\n",
" \n",
" # Create the customer support agent\n",
" agent = Agent(\n",
" model=model,\n",
" tools=tools,\n",
" hooks=[memory_hooks],\n",
" system_prompt=SYSTEM_PROMPT\n",
" )\n",
" response = agent(prompt)\n",
" return response\n",
" except Exception as e:\n",
" raise e\n",
"\n",
"print(\"✅ Customer support agent created successfully!\")"
]
Expand Down Expand Up @@ -557,18 +504,18 @@
"]\n",
"\n",
"# Function to test the agent\n",
"def test_agent_responses(agent, prompts):\n",
"def test_agent_responses(prompts):\n",
" for i, prompt in enumerate(prompts, 1):\n",
" print(f\"\\nTest Case {i}: {prompt}\")\n",
" print(\"-\" * 50)\n",
" try:\n",
" response = agent(prompt)\n",
" response = create_agent(prompt)\n",
" except Exception as e:\n",
" print(f\"Error: {str(e)}\")\n",
" print(\"-\" * 50)\n",
"\n",
"# Run the tests\n",
"test_agent_responses(agent, test_prompts)\n",
"test_agent_responses(test_prompts)\n",
"\n",
"print(\"\\\\n✅ Basic testing completed!\")\n"
]
Expand Down
Loading
Loading