parent
638e9e2ba2
commit
7b5ef1e63c
@ -0,0 +1,61 @@
|
||||
"""
|
||||
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")
|
||||
|
||||
# Just pass auth_token in arguments
|
||||
result = await session.call_tool(
|
||||
name="discover_agents",
|
||||
arguments={
|
||||
"auth_token": "mytoken123" # That's it!
|
||||
},
|
||||
)
|
||||
|
||||
print(json.dumps(result.model_dump(), indent=2))
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("Calling Research-Agent...")
|
||||
print("=" * 60 + "\n")
|
||||
|
||||
# Same for any tool
|
||||
result = await session.call_tool(
|
||||
name="Research-Agent",
|
||||
arguments={
|
||||
"task": "What is Python?",
|
||||
"auth_token": "mytoken123" # Just include it
|
||||
},
|
||||
)
|
||||
|
||||
print(json.dumps(result.model_dump(), indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("\n🔐 Simple Auth Client")
|
||||
print("Token: mytoken123\n")
|
||||
asyncio.run(call_server())
|
||||
@ -0,0 +1,56 @@
|
||||
"""
|
||||
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.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
|
||||
|
||||
|
||||
# Create agents
|
||||
agent = Agent(
|
||||
agent_name="Research-Agent",
|
||||
model_name="claude-sonnet-4-5-20250929",
|
||||
max_loops=1,
|
||||
system_prompt="You are a helpful research assistant.",
|
||||
temperature=0.7,
|
||||
top_p=None, # Can't use both temperature and top_p with Claude
|
||||
)
|
||||
|
||||
# Create server with auth callback
|
||||
# If auth_callback is provided, auth is automatically enabled
|
||||
server = AOP(
|
||||
server_name="SimpleAuthServer",
|
||||
port=5932,
|
||||
auth_callback=custom_auth, # This enables and governs auth
|
||||
)
|
||||
|
||||
server.add_agent(agent)
|
||||
|
||||
print("\n🚀 Server starting on port 5932")
|
||||
print("🔐 Authentication: ENABLED")
|
||||
print("✅ Valid tokens: mytoken123, anothertoken456\n")
|
||||
|
||||
server.run()
|
||||
Loading…
Reference in new issue