pull/1184/head
Aksh Parekh 2 months ago
parent 7b5ef1e63c
commit fa3df199cc

@ -1,23 +1,56 @@
import json """
Simple AOP Server with Custom Authentication Callback
The auth_callback function determines ALL authentication logic.
If you provide auth_callback, authentication is enabled.
If you don't provide it, no authentication is required.
"""
from swarms import Agent from swarms import Agent
from swarms.structs.aop import AOP
# This function governs ALL security
def custom_auth(token: str) -> bool:
"""
Your custom authentication logic goes here.
Return True to allow access, False to deny.
This function determines everything:
- What tokens are valid
- Token format (API key, JWT, whatever)
- Any additional validation logic
"""
# Simple example: check against valid tokens
valid_tokens = {
"mytoken123",
"anothertoken456",
}
return token in valid_tokens
# Initialize the agent
# Create agents
agent = Agent( agent = Agent(
agent_name="Quantitative-Trading-Agent", agent_name="Research-Agent",
agent_description="Advanced quantitative trading and algorithmic analysis agent", model_name="claude-sonnet-4-5-20250929",
model_name="gpt-4.1",
dynamic_temperature_enabled=True,
max_loops=1, max_loops=1,
dynamic_context_window=True, system_prompt="You are a helpful research assistant.",
streaming_on=False, temperature=0.7,
top_p=None, top_p=None, # Can't use both temperature and top_p with Claude
output_type="dict",
) )
out = agent.run( # Create server with auth callback
task="What are the top five best energy stocks across nuclear, solar, gas, and other energy sources?", # If auth_callback is provided, auth is automatically enabled
n=1, server = AOP(
server_name="SimpleAuthServer",
port=5932,
auth_callback=custom_auth, # This enables and governs auth
) )
print(json.dumps(out, indent=4)) server.add_agent(agent)
print("\n🚀 Server starting on port 5932")
print("🔐 Authentication: ENABLED")
print("✅ Valid tokens: mytoken123, anothertoken456\n")
server.run()

@ -33,7 +33,7 @@ async def call_server():
result = await session.call_tool( result = await session.call_tool(
name="discover_agents", name="discover_agents",
arguments={ arguments={
"auth_token": "mytoken123" # That's it! "auth_token": "mytoken123"
}, },
) )
@ -43,12 +43,11 @@ async def call_server():
print("Calling Research-Agent...") print("Calling Research-Agent...")
print("=" * 60 + "\n") print("=" * 60 + "\n")
# Same for any tool
result = await session.call_tool( result = await session.call_tool(
name="Research-Agent", name="Research-Agent",
arguments={ arguments={
"task": "What is Python?", "task": "What is Python?",
"auth_token": "mytoken123" # Just include it "auth_token": "mytoken123"
}, },
) )

@ -10,7 +10,7 @@ from swarms import Agent
from swarms.structs.aop import AOP from swarms.structs.aop import AOP
# This function governs ALL security # EXAMPLE: This function governs ALL security
def custom_auth(token: str) -> bool: def custom_auth(token: str) -> bool:
""" """
Your custom authentication logic goes here. Your custom authentication logic goes here.
@ -21,7 +21,7 @@ def custom_auth(token: str) -> bool:
- Token format (API key, JWT, whatever) - Token format (API key, JWT, whatever)
- Any additional validation logic - Any additional validation logic
""" """
# Simple example: check against valid tokens
valid_tokens = { valid_tokens = {
"mytoken123", "mytoken123",
"anothertoken456", "anothertoken456",
@ -36,15 +36,14 @@ agent = Agent(
max_loops=1, max_loops=1,
system_prompt="You are a helpful research assistant.", system_prompt="You are a helpful research assistant.",
temperature=0.7, temperature=0.7,
top_p=None, # Can't use both temperature and top_p with Claude top_p=None,
) )
# Create server with auth callback
# If auth_callback is provided, auth is automatically enabled
server = AOP( server = AOP(
server_name="SimpleAuthServer", server_name="SimpleAuthServer",
port=5932, port=5932,
auth_callback=custom_auth, # This enables and governs auth auth_callback=custom_auth,
) )
server.add_agent(agent) server.add_agent(agent)

@ -0,0 +1,59 @@
"""
Simple AOP Client with Authentication
Just pass your token when calling tools. That's it.
The server's auth_callback determines if it's valid.
"""
import json
import asyncio
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client
async def call_server():
"""Call the AOP server with authentication."""
url = "http://localhost:5932/mcp"
async with streamablehttp_client(url, timeout=10) as ctx:
if len(ctx) == 2:
read, write = ctx
else:
read, write, *_ = ctx
async with ClientSession(read, write) as session:
await session.initialize()
print("\n" + "=" * 60)
print("Calling discover_agents...")
print("=" * 60 + "\n")
result = await session.call_tool(
name="discover_agents",
arguments={
"auth_token": "mytoken1234"
},
)
print(json.dumps(result.model_dump(), indent=2))
print("\n" + "=" * 60)
print("Calling Research-Agent...")
print("=" * 60 + "\n")
result = await session.call_tool(
name="Research-Agent",
arguments={
"task": "What is Python?",
"auth_token": "mytoken123"
},
)
print(json.dumps(result.model_dump(), indent=2))
if __name__ == "__main__":
print("\n🔐 Simple Auth Client")
print("Token: mytoken123\n")
asyncio.run(call_server())
Loading…
Cancel
Save