|
| 1 | +# AWS Strands Agent SDK Integration with MemMachine |
| 2 | + |
| 3 | +This directory contains tools for integrating MemMachine memory operations into AWS Strands Agent SDK workflows. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +MemMachine provides memory tools that can be integrated into AWS Strands Agent SDK to enable AI agents with persistent memory capabilities. This allows agents to remember past interactions, user preferences, and context across multiple sessions. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +Ensure you have the required dependencies: |
| 12 | + |
| 13 | +```bash |
| 14 | +pip install memmachine |
| 15 | +# If using AWS Strands Agent SDK |
| 16 | +pip install strands-agents bedrock-agentcore |
| 17 | +``` |
| 18 | + |
| 19 | +## Configuration |
| 20 | + |
| 21 | +### AWS Credentials |
| 22 | + |
| 23 | +AWS Strands Agent SDK requires AWS credentials to access AWS Bedrock services. Configure credentials using one of these methods: |
| 24 | + |
| 25 | +1. **AWS CLI** (recommended): |
| 26 | + ```bash |
| 27 | + aws configure |
| 28 | + ``` |
| 29 | + |
| 30 | +2. **Environment variables**: |
| 31 | + ```bash |
| 32 | + export AWS_ACCESS_KEY_ID=your_access_key |
| 33 | + export AWS_SECRET_ACCESS_KEY=your_secret_key |
| 34 | + export AWS_REGION=us-east-1 |
| 35 | + ``` |
| 36 | + |
| 37 | +3. **IAM role** (if running on EC2/ECS/Lambda) |
| 38 | + |
| 39 | +4. **AWS credentials file**: `~/.aws/credentials` |
| 40 | + |
| 41 | +### MemMachine Configuration |
| 42 | + |
| 43 | +The tools can be configured via environment variables or directly in code: |
| 44 | + |
| 45 | +| Variable | Description | Default | |
| 46 | +|----------|-------------|---------| |
| 47 | +| `MEMORY_BACKEND_URL` | URL of the MemMachine backend service | `http://localhost:8080` | |
| 48 | +| `MEMORY_API_KEY` | API key for MemMachine authentication | `None` | |
| 49 | +| `STRANDS_GROUP_ID` | Group identifier for the demo | `strands_group` | |
| 50 | +| `STRANDS_AGENT_ID` | Agent identifier for the demo | `strands_agent` | |
| 51 | +| `STRANDS_USER_ID` | User identifier for the demo | `default_user` | |
| 52 | +| `STRANDS_SESSION_ID` | Session identifier for the demo | Auto-generated | |
| 53 | + |
| 54 | +## Usage |
| 55 | + |
| 56 | +### Basic Usage |
| 57 | + |
| 58 | +```python |
| 59 | +from tool import get_memmachine_tools, create_tool_handler |
| 60 | + |
| 61 | +# Initialize tools |
| 62 | +tools, tool_schemas = get_memmachine_tools( |
| 63 | + base_url="http://localhost:8080", |
| 64 | + group_id="my_group", |
| 65 | + agent_id="my_agent", |
| 66 | + user_id="user123", |
| 67 | +) |
| 68 | + |
| 69 | +# Get tool schemas for Strands Agent SDK |
| 70 | +# tool_schemas can be passed to your Strands Agent configuration |
| 71 | + |
| 72 | +# Create tool handler for executing tool calls |
| 73 | +tool_handler = create_tool_handler(tools) |
| 74 | + |
| 75 | +# Use tools directly |
| 76 | +result = tools.add_memory( |
| 77 | + content="User prefers Python for backend development", |
| 78 | + metadata={"category": "preference"} |
| 79 | +) |
| 80 | + |
| 81 | +search_result = tools.search_memory( |
| 82 | + query="What does the user prefer for development?", |
| 83 | + limit=5 |
| 84 | +) |
| 85 | +``` |
| 86 | + |
| 87 | +### Integration with Strands Agent SDK |
| 88 | + |
| 89 | +```python |
| 90 | +from strands import Agent |
| 91 | +from tool import get_memmachine_tools, create_tool_handler |
| 92 | +from strands_tools import set_tools_instance |
| 93 | +import sys |
| 94 | +import os |
| 95 | + |
| 96 | +# Initialize MemMachine tools |
| 97 | +tools, tool_schemas = get_memmachine_tools( |
| 98 | + base_url="http://localhost:8080", |
| 99 | + group_id="my_group", |
| 100 | + agent_id="my_agent", |
| 101 | + user_id="user123", |
| 102 | +) |
| 103 | + |
| 104 | +# Set up tools for Strands SDK |
| 105 | +script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 106 | +if script_dir not in sys.path: |
| 107 | + sys.path.insert(0, script_dir) |
| 108 | + |
| 109 | +from strands_tools import add_memory, search_memory, get_context |
| 110 | +set_tools_instance(tools) |
| 111 | + |
| 112 | +# Create tool handler for manual execution |
| 113 | +tool_handler = create_tool_handler(tools) |
| 114 | + |
| 115 | +# Create Strands Agent with function objects |
| 116 | +agent = Agent( |
| 117 | + tools=[add_memory, search_memory, get_context], # Pass function objects |
| 118 | + system_prompt="You are a helpful assistant with memory capabilities." |
| 119 | +) |
| 120 | + |
| 121 | +# The agent can now use the memory tools automatically |
| 122 | +response = agent("Remember that I like Python programming") |
| 123 | +print(response) |
| 124 | + |
| 125 | +# Later, the agent can recall memories |
| 126 | +response = agent("What programming language do I prefer?") |
| 127 | +print(response) |
| 128 | + |
| 129 | +# Cleanup |
| 130 | +tools.close() |
| 131 | +``` |
| 132 | + |
| 133 | +**Note:** AWS credentials must be configured for the Strands Agent to work. See [AWS Credentials Configuration](#aws-credentials) below. |
| 134 | + |
| 135 | +### Direct Tool Usage |
| 136 | + |
| 137 | +```python |
| 138 | +from tool import MemMachineTools |
| 139 | + |
| 140 | +# Initialize tools |
| 141 | +tools = MemMachineTools( |
| 142 | + base_url="http://localhost:8080", |
| 143 | + group_id="my_group", |
| 144 | + agent_id="my_agent", |
| 145 | + user_id="user123", |
| 146 | +) |
| 147 | + |
| 148 | +# Add a memory |
| 149 | +result = tools.add_memory( |
| 150 | + content="User mentioned they have a meeting tomorrow at 10 AM", |
| 151 | + metadata={"type": "reminder", "time": "10:00 AM"} |
| 152 | +) |
| 153 | + |
| 154 | +# Search memories |
| 155 | +search_result = tools.search_memory( |
| 156 | + query="What meetings does the user have?", |
| 157 | + limit=5 |
| 158 | +) |
| 159 | + |
| 160 | +# Get context |
| 161 | +context = tools.get_context() |
| 162 | +print(f"Current context: {context}") |
| 163 | + |
| 164 | +# Cleanup |
| 165 | +tools.close() |
| 166 | +``` |
| 167 | + |
| 168 | +## Available Tools |
| 169 | + |
| 170 | +### 1. add_memory |
| 171 | + |
| 172 | +Store important information about the user or conversation into memory. |
| 173 | + |
| 174 | +**Parameters:** |
| 175 | +- `content` (required): The content to store in memory |
| 176 | +- `user_id` (optional): User ID override |
| 177 | +- `episode_type` (optional): Type of episode (default: "text") |
| 178 | +- `metadata` (optional): Additional metadata dictionary |
| 179 | + |
| 180 | +**Returns:** |
| 181 | +- Dictionary with `status`, `message`, and `content` fields |
| 182 | + |
| 183 | +### 2. search_memory |
| 184 | + |
| 185 | +Retrieve relevant context, memories, or profile for a user. |
| 186 | + |
| 187 | +**Parameters:** |
| 188 | +- `query` (required): Search query string |
| 189 | +- `user_id` (optional): User ID override |
| 190 | +- `limit` (optional): Maximum number of results (default: 5, max: 20) |
| 191 | + |
| 192 | +**Returns:** |
| 193 | +- Dictionary with `status`, `results`, and `summary` fields |
| 194 | + |
| 195 | +### 3. get_context |
| 196 | + |
| 197 | +Get the current memory context configuration. |
| 198 | + |
| 199 | +**Parameters:** |
| 200 | +- `user_id` (optional): User ID override |
| 201 | + |
| 202 | +**Returns:** |
| 203 | +- Dictionary containing `group_id`, `agent_id`, `user_id`, and `session_id` |
| 204 | + |
| 205 | +## Requirements |
| 206 | + |
| 207 | +- MemMachine server running (default: http://localhost:8080) |
| 208 | +- Python 3.8+ |
| 209 | +- AWS Strands Agent SDK (for full integration) |
| 210 | + |
| 211 | +## Example Workflow |
| 212 | + |
| 213 | +1. **Configure AWS Credentials**: Set up AWS credentials (see above) |
| 214 | +2. **Start MemMachine Server**: Ensure MemMachine server is running |
| 215 | +3. **Initialize Tools**: Create MemMachine tools with your configuration |
| 216 | +4. **Set Up Tool Functions**: Import and configure `strands_tools` functions |
| 217 | +5. **Create Agent**: Pass function objects to Strands Agent |
| 218 | +6. **Use Agent**: The agent automatically calls tools when needed |
| 219 | +7. **Cleanup**: Close the client when done |
| 220 | + |
| 221 | +## Error Handling |
| 222 | + |
| 223 | +All tool methods return dictionaries with a `status` field: |
| 224 | +- `"success"`: Operation completed successfully |
| 225 | +- `"error"`: Operation failed (check `message` field for details) |
| 226 | + |
| 227 | +## Notes |
| 228 | + |
| 229 | +- The tools automatically manage memory sessions |
| 230 | +- Memories are stored in both episodic (short-term) and profile (long-term) memory |
| 231 | +- Search queries are embedded and compared semantically |
| 232 | +- Tool schemas follow AWS Bedrock tool specification format |
| 233 | + |
0 commit comments