"""
Example Usage of MCP Gateway
Demonstrates how to use the MCP Gateway to wrap Lambda functions and REST APIs.
Note: In production (Lambda deployment), tools are automatically registered
based on environment and account ID. This example shows manual registration
for local development and understanding the API.
"""
import asyncio
import json
import sys
import os
# Add parent directory to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from gateway.gateway import MCPGateway
async def example_usage():
"""Example of using MCP Gateway"""
print("=" * 70)
print("MCP Gateway - Example Usage")
print("=" * 70)
# 1. Initialize Gateway
print("\n1. Initializing MCP Gateway...")
gateway = MCPGateway(region="us-east-1")
# 2. Register Lambda Function as MCP Tool
print("\n2. Registering Lambda function as MCP tool...")
gateway.register_lambda_tool(
name="weather_lookup",
lambda_arn="arn:aws:lambda:us-east-1:123456789012:function:weather-tool",
description="Get weather information for a location",
input_schema={
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name or location"
},
"units": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"default": "celsius"
}
},
"required": ["location"]
}
)
print(" ✓ Registered: weather_lookup")
# 3. Register REST API as MCP Tool
print("\n3. Registering REST API as MCP tool...")
gateway.register_rest_tool(
name="stock_price",
endpoint="https://api.example.com/stock/price",
description="Get stock price information",
input_schema={
"type": "object",
"properties": {
"symbol": {
"type": "string",
"description": "Stock symbol (e.g., AAPL, GOOGL)"
}
},
"required": ["symbol"]
},
method="POST",
headers={"Content-Type": "application/json"}
)
print(" ✓ Registered: stock_price")
# 4. List Available Tools
print("\n4. Listing available tools...")
list_request = {
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list",
"params": {}
}
list_response = await gateway.handle_request(list_request)
tools = list_response.get('result', {}).get('tools', [])
print(f" Available tools ({len(tools)}):")
for tool in tools:
print(f" - {tool['name']}: {tool['description']}")
# 5. Call a Tool (Example - would need actual Lambda/REST API)
print("\n5. Example: Calling weather_lookup tool...")
call_request = {
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "weather_lookup",
"arguments": {
"location": "San Francisco, CA",
"units": "celsius"
}
}
}
print(" Request:", json.dumps(call_request, indent=2))
print("\n Note: This would invoke the actual Lambda function")
print(" In production, the gateway handles protocol translation automatically")
# 6. Show Gateway Architecture
print("\n" + "=" * 70)
print("Gateway Architecture:")
print("=" * 70)
print("""
Bedrock AgentCore
│
▼
MCP Gateway (Lambda)
│
┌────┴────┐
▼ ▼
Lambda REST API
Functions Endpoints
The gateway automatically:
- Translates MCP protocol to Lambda invocations
- Translates MCP protocol to REST API calls
- Handles authentication and error handling
- Provides unified MCP interface
""")
print("\n" + "=" * 70)
print("Example completed!")
print("=" * 70)
if __name__ == "__main__":
asyncio.run(example_usage())