> ## Documentation Index
> Fetch the complete documentation index at: https://docs.invokehq.run/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Agents SDK

> Route an OpenAI Agents SDK agent's tool calls through Invoke over MCP: each call checked, recorded as your agent, and receipted.

Your agent keeps its instructions, model and handoffs. Invoke is one more MCP server on it, so every tool call goes through Invoke.

## Before you start

In the [console](https://console.invokehq.run/sign-in), open **API** and copy two things: the **MCP endpoint** and an **Agent key**. The key belongs to one agent, so every call your agent makes is recorded as that agent's, and it can't act as another.

```bash theme={null}
export INVOKE_MCP_URL=https://api.invokehq.run/v1/workspaces/ws_…/mcp
export INVOKE_API_KEY=inv_live_…
```

**1. Install**

```bash theme={null}
pip install openai-agents
export OPENAI_API_KEY=sk-…
```

**2. Write the agent**

Save this as `agent.py`:

```python agent.py theme={null}
"""An OpenAI Agents SDK agent whose tool calls go through Invoke.

pip install openai-agents
export INVOKE_API_KEY=inv_live_...      # your agent's own key
export INVOKE_MCP_URL=https://...       # from `invoke gateway url`
export OPENAI_API_KEY=sk-...
python agent.py
"""
import asyncio
import os
import sys

from agents import Agent, Runner
from agents.mcp import MCPServerStreamableHttp

PROMPT = " ".join(sys.argv[1:]) or (
    "Remember that the staging deploy is frozen until Friday. "
    "Then recall your notes and tell me what you know."
)


async def main() -> None:
    async with MCPServerStreamableHttp(
        name="invoke",
        params={
            "url": os.environ["INVOKE_MCP_URL"],
            "headers": {"Authorization": f"Bearer {os.environ['INVOKE_API_KEY']}"},
        },
        client_session_timeout_seconds=30,
    ) as invoke:
        agent = Agent(
            name="Assistant",
            instructions="Use your tools to do what the user asks.",
            mcp_servers=[invoke],
            model=os.environ.get("MODEL", "gpt-5.4-mini"),
        )
        result = await Runner.run(agent, PROMPT)
        for item in result.new_items:
            if item.type == "tool_call_item":
                print("→", item.raw_item.name, item.raw_item.arguments)
        print(result.final_output)


asyncio.run(main())
```

`client_session_timeout_seconds=30` gives the first connection room. The SDK's default is 5 seconds.

**3. Run it**

```bash theme={null}
python agent.py
```

The agent uses Invoke's own memory tools, so it works on a new workspace with nothing connected. You'll see something like:

```text theme={null}
→ invoke_remember {"text": "The staging deploy is frozen until Friday."}
→ invoke_recall {}
I know that the staging deploy is frozen until Friday.
```

## Give it a real tool

In the console, open **Connectors** and connect a tool: GitHub, Slack, Linear, Notion or any MCP server. Its tools appear to your agent on the next run, with names a model API accepts (a GitHub connector's `create_issue` is listed as `github_create_issue`). Then ask for something that changes the world:

```bash theme={null}
python agent.py "Open an issue in acme/api titled 'Staging deploy frozen until Friday'"
```

Read back what happened with the [CLI](https://docs.invokehq.run/sdk/cli.md):

```bash theme={null}
invoke login --api-key "$INVOKE_API_KEY"
invoke gateway use ws_…        # the id in your MCP endpoint
invoke gateway receipts
```

Each write has a receipt that names your agent. If the agent claims a task first (`invoke_claim_task`), a retried write for that task is answered with its first receipt instead of running twice.

## What happened

* **Your agent acted as itself.** The key is its identity: Invoke records every call as that agent's.
* **Every call was checked before it ran:** the agent's permissions, your workspace's policies and its budget.
* **`invoke_remember` and `invoke_recall` are Invoke's own memory.** Notes an agent keeps across runs. Claiming a task again brings back its history and notes, and the workspace's other agents can read them.

## Other models

For other providers, `pip install "openai-agents[litellm]"` and set `MODEL=litellm/anthropic/claude-opus-5-5` (or any LiteLLM model).

## Next steps

* [CLI](https://docs.invokehq.run/sdk/cli.md): connect tools, then read receipts and the event log
* [Authentication](https://docs.invokehq.run/configuration/authentication.md): agent keys and account keys
* [Traces](https://docs.invokehq.run/concepts/traces.md): read a run back stage by stage


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.