forked from kyegomez/swarms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_auth_and_config.py
More file actions
106 lines (84 loc) · 3.01 KB
/
Copy path05_auth_and_config.py
File metadata and controls
106 lines (84 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
Example 5 — Authentication and connection configuration.
A bare URL is the shorthand. For anything else — API keys, bearer tokens, custom
headers, a forced transport, longer timeouts, OAuth — pass an ``MCPConnection``
(or a plain dict, or the equivalent keyword arguments). ``MCPManager`` accepts
all of these interchangeably.
Nothing here connects to a server; it only shows how connections are described.
python examples/mcp/client/05_auth_and_config.py
"""
import json
from swarms.schemas.mcp_schemas import MCPConnection, MCPOAuthConfig
from swarms.tools.mcp_manager import MCPManager
def api_key_via_kwargs() -> None:
"""Shortest path for a key-protected server."""
manager = MCPManager(
mcp_url="https://api.example.com/mcp",
api_key="sk-example-123",
timeout=15,
)
show("api key via kwargs", manager)
def bearer_token() -> None:
"""A pre-issued bearer token."""
manager = MCPManager(
mcp_url="https://api.example.com/mcp",
authorization_token="ey-example-token",
)
show("bearer token", manager)
def connection_object() -> None:
"""Full control over one server via MCPConnection."""
connection = MCPConnection(
url="https://api.example.com/mcp",
headers={"X-Tenant": "acme"},
transport="streamable_http",
timeout=20,
tool_timeout=60,
)
show("MCPConnection", MCPManager(mcp_config=connection))
def secrets_from_env() -> None:
"""Keys can be indirected through the environment instead of hard-coded.
``env:NAME`` and ``${NAME}`` are both resolved at connection time.
"""
connection = MCPConnection(
url="https://api.example.com/mcp",
api_key="env:EXAMPLE_MCP_KEY",
)
show("secret from env", MCPManager(mcp_config=connection))
def oauth() -> None:
"""Headless OAuth 2.1 via client credentials."""
connection = MCPConnection(
url="https://api.example.com/mcp",
oauth=MCPOAuthConfig(
grant_type="client_credentials",
client_id="example-client",
client_secret="env:EXAMPLE_CLIENT_SECRET",
token_url="https://api.example.com/oauth/token",
scopes=["tools.read"],
),
)
show(
"oauth client credentials", MCPManager(mcp_config=connection)
)
def mixed_servers() -> None:
"""Different auth per server, all on one manager."""
manager = MCPManager(
mcp_urls=[
"http://localhost:8000/mcp", # local, no auth
MCPConnection(
url="https://api.example.com/mcp",
api_key="sk-example-123",
),
]
)
show("mixed auth", manager)
def show(label: str, manager: MCPManager) -> None:
"""Print the manager's config — secrets are redacted by to_dict()."""
print(f"\n── {label}")
print(json.dumps(manager.to_dict(), indent=2))
if __name__ == "__main__":
api_key_via_kwargs()
bearer_token()
connection_object()
secrets_from_env()
oauth()
mixed_servers()