"""OpenAI Agents API + Server4Agent over MCP.
Gives an OpenAI 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 --upgrade openai
export OPENAI_API_KEY=... # with api.agents.read, api.agents.write, api.responses.write
export S4A_API_KEY=sk_live_... # server4agent.com, API Keys
python quickstart.py
Config follows OpenAI's Agents API quickstart and MCP connections docs:
https://developers.openai.com/api/docs/guides/agents-api/quickstart
https://developers.openai.com/api/docs/guides/agents-api/tools/mcp
"""
import os
from openai import OpenAI
S4A_MCP_URL = "https://mcp.server4agent.com"
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."
)
# Every Server4Agent tool except the ones that remove things, so nothing the
# agent does can delete a server, project, file, secret or domain, and the
# ones that connect an agent's own sandbox to a project. Add a tool name here
# when you need it.
ALLOWED_TOOLS = [
"list_servers", "get_server", "create_server", "update_server", "control_server", "get_usage",
"list_projects", "get_project", "list_templates", "create_project", "update_project",
"init_project", "export_project", "import_project", "deploy",
"list_files", "read_file", "write_file", "get_download_url",
"exec", "start_app", "prompt", "start_build", "get_build", "get_run", "get_logs",
"set_secret", "list_secrets", "attach_secret",
"add_domain", "check_domain",
]
server4agent = {
"type": "mcp",
"server_label": "server4agent",
"transport": {
"type": "http",
"server_url": S4A_MCP_URL,
# Server4Agent reads the key from X-API-Key as well as Authorization.
"headers": {"X-API-Key": os.environ["S4A_API_KEY"]},
},
"allowed_tools": ALLOWED_TOOLS,
"required": True, # fail the turn if the server cannot be reached
}
client = OpenAI() # reads OPENAI_API_KEY
stream = client.beta.agents.sessions.create(
agent={
"model": "gpt-6-astra",
"instructions": (
"You build and host things with the Server4Agent tools. Projects "
"are private until you deploy them. Always reply with the live URL."
),
"tools": [server4agent],
},
# Server4Agent is where the files live and the app runs, so this agent
# needs no sandbox of its own.
environment={"type": "none"},
input=TASK,
stream=True,
)
result = stream.get_final_result()
print(result.output_text)
print(f"\nSession {result.session_id}")