import os
import requests
def attach_memory_block(agent_id: str, block_id: str) -> str:
"""Attach an existing memory block to a Letta agent via API.
This tool attaches a memory block (identified by block_id) to a specific agent.
Useful when the UI doesn't show certain blocks or for automated agent configuration.
Args:
agent_id (str): The ID of the agent to attach the block to (e.g., "agent-abc123...").
block_id (str): The ID of the memory block to attach (e.g., "block-abc123...").
Returns:
str: Success message with attachment details, or error message if attachment fails.
Examples:
>>> attach_memory_block("agent-abc123", "block-xyz789")
"✅ Successfully attached memory block 'block-xyz789' to agent 'agent-abc123'"
"""
try:
# Get base URL and token from environment
base_url = os.getenv("LETTA_BASE_URL")
token = os.getenv("LETTA_API_KEY")
if not base_url:
return "❌ Error: LETTA_BASE_URL not found in environment variables"
if not token:
return "❌ Error: LETTA_API_KEY not found in environment variables"
# Make API request to attach memory block to agent
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
# Endpoint to attach memory block to agent
url = f"{base_url}/v1/agents/{agent_id}/memory/blocks/{block_id}/attach"
response = requests.post(url, headers=headers)
if response.status_code == 200:
return f"✅ Successfully attached memory block '{block_id}' to agent '{agent_id}'"
elif response.status_code == 404:
return f"❌ Error: Agent '{agent_id}' or block '{block_id}' not found"
elif response.status_code == 409:
return f"⚠️ Warning: Block '{block_id}' may already be attached to agent '{agent_id}'"
else:
return f"❌ Error: API returned status {response.status_code}: {response.text}"
except requests.exceptions.RequestException as e:
return f"❌ Network error: {str(e)}"
except Exception as e:
return f"❌ Unexpected error: {str(e)}"
#attach_memory_block
1 messages · Page 1 of 1 (latest)