"""Claude Managed Agents + Server4Agent over MCP.
Gives a Claude managed agent the Server4Agent MCP server, then asks it to build
a page and put it online. The agent writes the files into a Server4Agent
project and deploys it, so the result has a live URL and is still there for
the next session.
Run:
pip install anthropic
export ANTHROPIC_API_KEY=... # console.anthropic.com
export S4A_API_KEY=sk_live_... # server4agent.com, API Keys
python quickstart.py
Config follows Anthropic's MCP connector, vaults and permission policy docs:
https://platform.claude.com/docs/en/managed-agents/mcp-connector
https://platform.claude.com/docs/en/managed-agents/vaults
https://platform.claude.com/docs/en/managed-agents/permission-policies
"""
import os
from anthropic import Anthropic
S4A_MCP_URL = "https://mcp.server4agent.com"
# Tools that remove things. Left off so nothing the agent does can delete a
# server, project, file, secret, domain or connected sandbox. Turn one back on
# when you need it.
REMOVE_TOOLS = [
"delete_server",
"delete_project",
"cleanup_project",
"delete_file",
"delete_secret",
"remove_domain",
"disconnect_agent_environment",
]
TASK = (
"Use Server4Agent to put a one-page site online: a page that says hello "
"and shows today's date. Reuse a running server if one has room, create a "
"project, write the files, start the app, deploy it, and reply with the URL."
)
client = Anthropic() # reads ANTHROPIC_API_KEY
# 1. The Server4Agent key goes in a vault, keyed by the MCP server URL. The
# agent definition below never holds it.
vault = client.beta.vaults.create(display_name="Server4Agent")
client.beta.vaults.credentials.create(
vault_id=vault.id,
display_name="Server4Agent API key",
auth={
"type": "static_bearer",
"mcp_server_url": S4A_MCP_URL,
"token": os.environ["S4A_API_KEY"],
},
)
# 2. The agent declares the server by URL and gets its tools as a toolset.
# MCP toolsets ask before every call by default; this one allows them,
# with the removal tools switched off.
agent = client.beta.agents.create(
name="Server4Agent builder",
model="claude-sonnet-5-5",
system=(
"You build and host things with the Server4Agent tools. Projects are "
"private until you deploy them. Always reply with the live URL."
),
mcp_servers=[{"type": "url", "name": "server4agent", "url": S4A_MCP_URL}],
tools=[
{"type": "agent_toolset_20260401"},
{
"type": "mcp_toolset",
"mcp_server_name": "server4agent",
"default_config": {"permission_policy": {"type": "always_allow"}},
"configs": [{"name": name, "enabled": False} for name in REMOVE_TOOLS],
},
],
)
# 3. Limited networking: the MCP server is allowed, and so are deployed apps,
# so the agent can check its own URL from the sandbox.
environment = client.beta.environments.create(
name="server4agent-quickstart",
config={
"type": "cloud",
"networking": {
"type": "limited",
"allow_mcp_servers": True,
"allowed_hosts": ["*.apps.server4agent.com"],
},
},
)
# 4. The session brings the vault, which is where the key comes from.
session = client.beta.sessions.create(
agent=agent.id,
environment_id=environment.id,
vault_ids=[vault.id],
title="Server4Agent quickstart",
)
print(f"Session {session.id}")
with client.beta.sessions.events.stream(session.id) as stream:
client.beta.sessions.events.send(
session.id,
events=[{"type": "user.message", "content": [{"type": "text", "text": TASK}]}],
)
for event in stream:
match event.type:
case "agent.message":
for block in event.content:
if block.type == "text":
print(block.text, end="")
case "agent.mcp_tool_use":
print(f"\n[server4agent: {event.name}]")
case "session.error":
print(f"\n[error: {event.error.type}: {getattr(event.error, 'message', '')}]")
case "session.status_idle":
print(f"\n\nDone ({event.stop_reason.type}).")
break