|
| 1 | +"""AG-UI Strands agent with Gateway MCP tools, Memory, and Code Interpreter. |
| 2 | +
|
| 3 | +Uses ag-ui-strands to produce native AG-UI SSE events. |
| 4 | +AgentCore proxies these unchanged when deployed with --protocol AGUI. |
| 5 | +""" |
| 6 | + |
| 7 | +import logging |
| 8 | +import os |
| 9 | + |
| 10 | +from ag_ui.core import RunAgentInput, RunErrorEvent |
| 11 | +from ag_ui_strands import StrandsAgent |
| 12 | +from bedrock_agentcore.memory.integrations.strands.config import AgentCoreMemoryConfig |
| 13 | +from bedrock_agentcore.memory.integrations.strands.session_manager import ( |
| 14 | + AgentCoreMemorySessionManager, |
| 15 | +) |
| 16 | +from bedrock_agentcore.runtime import BedrockAgentCoreApp, RequestContext |
| 17 | +from strands import Agent |
| 18 | +from strands.models import BedrockModel |
| 19 | +from tools.code_interpreter import StrandsCodeInterpreterTools |
| 20 | +from tools.gateway import create_gateway_mcp_client |
| 21 | +from utils.auth import extract_user_id_from_context |
| 22 | + |
| 23 | +logger = logging.getLogger(__name__) |
| 24 | + |
| 25 | +app = BedrockAgentCoreApp() |
| 26 | + |
| 27 | +SYSTEM_PROMPT = ( |
| 28 | + "You are a helpful assistant with access to tools via the Gateway and Code Interpreter. " |
| 29 | + "When asked about your tools, list them and explain what they do." |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +def _create_session_manager( |
| 34 | + user_id: str, session_id: str |
| 35 | +) -> AgentCoreMemorySessionManager: |
| 36 | + memory_id = os.environ.get("MEMORY_ID") |
| 37 | + if not memory_id: |
| 38 | + raise ValueError("MEMORY_ID environment variable is required") |
| 39 | + config = AgentCoreMemoryConfig( |
| 40 | + memory_id=memory_id, session_id=session_id, actor_id=user_id |
| 41 | + ) |
| 42 | + return AgentCoreMemorySessionManager( |
| 43 | + agentcore_memory_config=config, |
| 44 | + region_name=os.environ.get("AWS_DEFAULT_REGION", "us-east-1"), |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +def create_strands_agent(user_id: str, session_id: str) -> StrandsAgent: |
| 49 | + """Create AG-UI Strands agent with Gateway tools, Memory, and Code Interpreter.""" |
| 50 | + bedrock_model = BedrockModel( |
| 51 | + model_id="us.anthropic.claude-sonnet-4-5-20250929-v1:0", temperature=0.1 |
| 52 | + ) |
| 53 | + |
| 54 | + session_manager = _create_session_manager(user_id, session_id=session_id) |
| 55 | + |
| 56 | + gateway_client = create_gateway_mcp_client() |
| 57 | + tools = [gateway_client] |
| 58 | + |
| 59 | + region = os.environ.get("AWS_DEFAULT_REGION", "us-east-1") |
| 60 | + code_tools = StrandsCodeInterpreterTools(region) |
| 61 | + tools.append(code_tools.execute_python_securely) |
| 62 | + |
| 63 | + strands_agent = Agent( |
| 64 | + name="strands_agent", |
| 65 | + system_prompt=SYSTEM_PROMPT, |
| 66 | + tools=tools, |
| 67 | + model=bedrock_model, |
| 68 | + session_manager=session_manager, |
| 69 | + ) |
| 70 | + |
| 71 | + return StrandsAgent( |
| 72 | + agent=strands_agent, |
| 73 | + name="agui_strands_agent", |
| 74 | + description="AG-UI Strands agent with Gateway MCP tools and Code Interpreter", |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +@app.entrypoint |
| 79 | +async def invocations(payload: dict, context: RequestContext): |
| 80 | + input_data = RunAgentInput.model_validate(payload) |
| 81 | + |
| 82 | + user_id = extract_user_id_from_context(context) |
| 83 | + agent = create_strands_agent(user_id, session_id=input_data.thread_id) |
| 84 | + |
| 85 | + try: |
| 86 | + async for event in agent.run(input_data): |
| 87 | + if event is not None: |
| 88 | + yield event.model_dump(mode="json", by_alias=True, exclude_none=True) |
| 89 | + except Exception as exc: |
| 90 | + logger.exception("Agent run failed") |
| 91 | + yield RunErrorEvent( |
| 92 | + message=str(exc) or type(exc).__name__, |
| 93 | + code=type(exc).__name__, |
| 94 | + ).model_dump(mode="json", by_alias=True, exclude_none=True) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + app.run() |
0 commit comments