The integration supports Model Context Protocol (MCP) servers via two wrapper types. Because MCP servers operate independently of Temporal, their durability is not automatically managed by Temporal workflows. You must choose the wrapper that matches your server's design:
Stateless MCP Servers: Treat each operation independently (e.g., a weather lookup). These are safe to restart or reconnect. Use StatelessMCPServerProvider to register them with the OpenAIAgentsPlugin in the Worker. In the workflow, access them using openai_agents.workflow.stateless_mcp_server("SERVER_NAME").
Stateful MCP Servers: Maintain session state between calls (e.g., a server requiring a set_location call before get_weather). If the connection fails, Temporal raises an ApplicationError. Because the server state is lost, you must implement your own application-level retry logic to handle these failures.
Security Warning: When using stateless_mcp_server() or stateful_mcp_server(), you can pass an optional factory_argument. Do not pass secrets, credentials, or API keys through factory_argument, as it is recorded in the Temporal workflow history and may be visible in the Web UI. Resolve credentials inside the server factory instead.
# Worker Configuration for Stateless MCP
from temporalio.contrib.openai_agents import (
ModelActivityParameters,
OpenAIAgentsPlugin,
StatelessMCPServerProvider
)
filesystem_server = StatelessMCPServerProvider(
lambda: MCPServerStdio(
name="FileSystemServer",
params={
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/files"],
},
)
)
client = await Client.connect(
"localhost:7233",
plugins=[
OpenAIAgentsPlugin(
model_params=ModelActivityParameters(start_to_close_timeout=timedelta(seconds=60)),
mcp_server_providers=[filesystem_server],
),
],
)
# Workflow Implementation
@workflow.defn
class FileSystemWorkflow:
@workflow.run
async def run(self, query: str) -> str:
server = openai_agents.workflow.stateless_mcp_server("FileSystemServer")
agent = Agent(
name="File Assistant",
instructions="Use the filesystem tools to read files.",
mcp_servers=[server],
)
result = await Runner.run(agent, input=query)
return result.final_output