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()
|
||||||
|
|||||||
@ -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…
Reference in new issue