"""Basic usage examples for the Mem.ai MCP server.
This script demonstrates how to use the Mem.ai client directly
for basic operations like saving content, creating notes, and
managing collections.
"""
import asyncio
import os
from dotenv import load_dotenv
from mcp_mem import MemClient
# Load environment variables
load_dotenv()
async def main():
"""Run basic usage examples."""
# Initialize client
async with MemClient() as client:
print("=== Mem.ai MCP Client Examples ===\n")
# Example 1: Save content with Mem It
print("1. Saving content with Mem It...")
mem_it_response = await client.mem_it(
input="Just finished reading an excellent article about async Python. "
"Key takeaways: asyncio enables concurrent I/O, use async/await for "
"better performance, and FastAPI is built on async principles.",
instructions="Extract key learnings and save as a technical note",
context="Python Programming",
)
print(f" ✓ Saved: {mem_it_response.request_id}\n")
# Example 2: Create a structured note
print("2. Creating a structured note...")
note = await client.create_note(
content="""# Weekly Team Meeting - January 15, 2024
## Attendees
- Alice (Engineering Lead)
- Bob (Product Manager)
- Charlie (Designer)
## Agenda Items
### 1. Sprint Review
- Completed 12/15 story points
- Shipped new dashboard feature
- Fixed critical bug in payment processing
### 2. Next Sprint Planning
- Focus on mobile app improvements
- Plan API v2 migration
- Design system updates
## Action Items
- [ ] Alice: Review API migration plan by Friday
- [ ] Bob: Schedule customer interviews
- [ ] Charlie: Update design system documentation
## Decisions Made
- Moving forward with React Native for mobile
- API v2 launch date: March 1, 2024
""",
)
print(f" ✓ Created note: '{note.title}' (ID: {note.id})\n")
# Example 3: Read the note we just created
print("3. Reading the note...")
retrieved_note = await client.read_note(str(note.id))
print(f" ✓ Retrieved: '{retrieved_note.title}'")
print(f" Created: {retrieved_note.created_at}")
print(f" Content length: {len(retrieved_note.content)} characters\n")
# Example 4: Create a collection
print("4. Creating a collection...")
collection = await client.create_collection(
title="Team Meetings 2024",
description="""# Team Meetings 2024
A collection of all team meeting notes from 2024, including:
- Weekly standups
- Sprint planning sessions
- Retrospectives
- All-hands meetings
""",
)
print(f" ✓ Created collection: '{collection.title}' (ID: {collection.id})\n")
# Example 5: Create a note in a collection
print("5. Creating a note in the collection...")
note_in_collection = await client.create_note(
content="""# Sprint Retrospective - Sprint 42
## What Went Well
- Great team collaboration
- Smooth deployment process
- Clear communication
## What Could Be Improved
- Testing coverage needs work
- Documentation updates lagging
- Need better estimation
## Action Items
- Increase test coverage to 80%
- Schedule documentation sprint
- Review estimation process
""",
collection_ids=[str(collection.id)],
)
print(
f" ✓ Created note in collection: '{note_in_collection.title}' "
f"(ID: {note_in_collection.id})\n"
)
# Example 6: Save web content
print("6. Saving web article content...")
article_response = await client.mem_it(
input="""<!DOCTYPE html>
<html>
<head><title>The Future of AI Development</title></head>
<body>
<h1>The Future of AI Development</h1>
<p>Artificial Intelligence is rapidly evolving. Key trends include:</p>
<ul>
<li>Multimodal models combining text, image, and audio</li>
<li>More efficient training techniques</li>
<li>Better alignment with human values</li>
<li>Increased focus on safety and ethics</li>
</ul>
<p>These developments will shape how we build AI systems in the coming years.</p>
</body>
</html>
""",
instructions="Summarize the key AI trends and save as research",
context="AI Research",
)
print(f" ✓ Saved article: {article_response.request_id}\n")
print("=== Cleanup ===\n")
# Example 7: Delete the notes (optional - comment out to keep them)
# print("7. Deleting notes...")
# await client.delete_note(str(note.id))
# await client.delete_note(str(note_in_collection.id))
# print(" ✓ Notes deleted\n")
# Example 8: Delete the collection (optional - comment out to keep it)
# print("8. Deleting collection...")
# await client.delete_collection(str(collection.id))
# print(" ✓ Collection deleted\n")
print("✨ All examples completed successfully!")
if __name__ == "__main__":
# Check for API key
if not os.getenv("MEM_API_KEY"):
print("Error: MEM_API_KEY environment variable not set")
print("Please copy .env.example to .env and add your API key")
exit(1)
# Run examples
asyncio.run(main())