What you’ll build: A custom gateway that connects agents to a messaging platformTime: ~25 minutesPrerequisites:
- Completed the Hello World tutorial
- Python programming experience
- Understanding of async/await in Python
What you’ll learn
This tutorial demonstrates:- Gateway architecture in SAM
- Creating custom gateway adapters
- Bidirectional communication patterns
- User identity management
- Message formatting and artifacts
- Deployment strategies
Understanding gateways
Gateways are the bridge between external systems and your agent mesh:Built-in gateways
SAM includes several gateways:- WebUI Gateway: Web-based chat interface
- Slack Gateway: Slack integration
- REST Gateway: HTTP API access
- Webhook Gateway: Incoming webhooks
- MCP Gateway: Model Context Protocol server
Why build custom gateways?
Build custom gateways to:- Integrate with proprietary systems
- Support custom protocols or interfaces
- Add specialized authentication
- Implement custom message routing
- Connect legacy systems
Step-by-step guide
Understand the gateway base classes
SAM provides base classes to build upon:BaseGatewayApp:
- Manages broker connections
- Handles agent discovery
- Routes messages to/from agents
- Manages user sessions
- Interfaces with external systems
- Converts external messages to SAM format
- Formats agent responses for external system
Create a simple console gateway
Let’s build a console/terminal gateway as a learning example.Create
console_gateway_adapter.py:console_gateway_adapter.py
Real-world gateway: Telegram bot
Complete Telegram gateway implementation
Complete Telegram gateway implementation
Here’s a production-ready Telegram gateway:Configuration:Get a bot token from @BotFather on Telegram.
telegram_gateway_adapter.py
Gateway adapter patterns
Message routing
Session management
Error handling
Testing your gateway
Create comprehensive tests:test_gateway.py
Deployment strategies
Docker container
Dockerfile
Kubernetes deployment
k8s/gateway-deployment.yaml
Next steps
Multi-Agent Collaboration
Create collaborative agent teams
Complex Workflows
Build advanced workflow patterns
Production Deployment
Deploy to production environments
Gateway Reference
Complete gateway documentation
Troubleshooting
Adapter not loading
Adapter not loading
Problem: “Module ‘my_adapter’ not found”Solution:
- Verify the module path is correct
- Ensure the file is in the
app_base_pathdirectory - Check the class name matches exactly
- Add
__init__.pyif using package structure
Messages not reaching agents
Messages not reaching agents
Problem: Messages sent but no responseSolution:
- Check broker connection in logs
- Verify
namespacematches agent configuration - Ensure
default_agent_nameis correct - Check agent discovery is enabled
Async/await errors
Async/await errors
Problem: “RuntimeError: Event loop is closed”Solution:
Key concepts learned
- Gateway architecture and purpose
- Creating custom gateway adapters
- Bidirectional communication patterns
- Message routing and session management
- Error handling and testing
- Deployment strategies