Connect MCP servers from OpenAI or your environment.
An MCP server publishes tool definitions and runs tool calls. The Agents API discovers the tools, calls the server, and returns results to the agent. Your application does not need to handle each call.
Choose where the connection runs based on where the server is reachable:
Connection
Where it runs
Requires an environment
HTTP with connection_origin: "service" (default)
OpenAI
No
HTTP with connection_origin: "environment"
Your session’s environment
Yes
stdio
A process in your session’s environment
Yes
Connect from OpenAI
Add an HTTP MCP server to agent.tools. The server must be reachable from OpenAI. This works with or without a session environment.
For example, the OpenAI documentation MCP allows anonymous access:
An executor MCP connects from the session’s environment. Use it for servers on a private network or software installed in that environment.
Set the session’s environment.type to self_hosted or openai_hosted. For a self-hosted environment, connect the executor before the agent uses its tools.
Connect over HTTP
Use HTTP for a server that is already running. Add this entry to agent.tools, replacing the URL with an address your environment can reach:
Save the Python server as /workspace/lookup_mcp.py.
For Ruby, install the mcp gem and save the Ruby server as /workspace/lookup_mcp.rb.
In /workspace, install @modelcontextprotocol/sdk and zod, then save the TypeScript server as /workspace/lookup_mcp.ts. In the configuration below, set transport.command to the absolute path of your Node executable and transport.args to ["--experimental-strip-types", "/workspace/lookup_mcp.ts"]. This server uses stdio without a transport argument.
The Java version uses io.modelcontextprotocol.sdk:mcp-core:1.1.5, io.modelcontextprotocol.sdk:mcp-json-jackson2:1.1.5, and com.fasterxml.jackson.core:jackson-databind:2.18.11. It serves the same tool over standard input and output:
The following configuration starts the Python server.
For Ruby, set transport.command to the absolute path of your Ruby executable and transport.args to ["/workspace/lookup_mcp.rb", "stdio"]. Install the gem for that Ruby runtime. The stdio argument selects the script’s transport:
For stdio, command and an absolute cwd are required; args is optional. Omit connection_origin.
Send a message asking the agent to look up customer 123. The tool returns Example Customer on the pro plan.
For OpenAI-hosted stdio MCPs, omit the network policy or set it to enabled. The disabled and restricted network policies are not supported for these connections.
Add authentication
For a server that allows anonymous access, omit authentication fields and vault_ids. Otherwise, choose the credential source for your connection:
HTTP credentials for one session: Set transport.authorization or transport.headers when creating the session. The Agents API encrypts these values and omits them from the returned session resource.
Reusable HTTP credentials: Store MCP credentials in a vault and attach it through vault_ids. Vault-backed MCP authentication applies only to connections from OpenAI. Credentials match the server URL; use credential_id to select one when several match.
Stdio credentials: Supply values in the environment and list their names in transport.env_vars. These values can be read by code running in the environment. Self-hosted sessions do not accept inline values in transport.env.
For example, an HTTP transport can include a bearer token and another header:
Use one source for Authorization: inline configuration or a matching vault credential. Other headers can accompany vault authentication. Environment-origin HTTP does not use vault credentials; use inline authentication or a trusted proxy.
Keep secrets out of reusable agent definitions, plugin archives, and logs. To keep credentials inaccessible to agent-generated code, use a trusted proxy or server that supplies them outside the environment.
Control tool access and startup
Set allowed_tools to limit which tools the agent can discover and call. Set required: true to fail the turn if the server cannot initialize. Initialization is optional by default.
If a required server cannot initialize, inspect the error in agent.session.turn.failed. For stdio servers, also check the MCP process logs.
Network access: Check the URL and connection_origin. For environment connections, check that the executor is connected and its network can reach the server.
Credentials: Check the token or headers. For a vault, check that the credential matches the server URL.
Executable and dependencies: Check that the configured command runs inside the environment.
Working directory: Use an existing absolute cwd for an inline stdio configuration.
Related guides
Plugins package MCP configuration and skills for reuse across sessions.
Tool search explains automatic MCP tool discovery on supported models and providers.