-
Notifications
You must be signed in to change notification settings - Fork 88
OpenAI Agents SDK - MCP Samples #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
2131660
file system mcp server example
jssmith 4336f0d
typo
jssmith 3a29a7a
stateful and stateless demos
jssmith 3cdf46d
wip
jssmith 4c94eca
update to provider
jssmith 9c2efe7
provider
jssmith 5d68887
title
jssmith 83b678f
add more examples
jssmith 99d31b9
Update SDK version to 1.18
tconley1428 607bac8
Update openai breaking change
tconley1428 cce6a36
Merge remote-tracking branch 'origin/main' into openai/mcp
jssmith 0452819
Merge remote-tracking branch 'origin/sdk-1.18' into openai/mcp
jssmith bd2c4bf
cleanup + formatting
jssmith 6b5a4a6
cleanup
jssmith 66fcab5
WIP sequential thinking
jssmith 2a011ba
memory stateful mcp
jssmith eba0ab8
cleanup
jssmith bcdcca5
Merge remote-tracking branch 'origin/main' into openai/mcp
jssmith dc2277a
interface updates
jssmith 19be71e
minimize dependency version changes
jssmith a0489a2
naming consistency
jssmith 2d2f960
openai agents upgrade
jssmith 8c2ae7b
Merge branch 'main' into openai/mcp
jssmith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# MCP Examples | ||
|
||
Integration with MCP (Model Context Protocol) servers using OpenAI agents in Temporal workflows. | ||
|
||
*Adapted from [OpenAI Agents SDK MCP examples](https://github.com/openai/openai-agents-python/tree/main/examples/mcp)* | ||
|
||
Before running these examples, be sure to review the [prerequisites and background on the integration](../README.md). | ||
|
||
|
||
## Running the Examples | ||
|
||
### Stdio MCP | ||
|
||
First, start the worker: | ||
```bash | ||
uv run openai_agents/mcp/run_file_system_worker.py | ||
``` | ||
|
||
Run the workflow: | ||
```bash | ||
uv run openai_agents/mcp/run_file_system_workflow.py | ||
``` | ||
|
||
This sample assumes that the worker and `run_file_system_workflow.py` are on the same machine. | ||
|
||
|
||
### Streamable HTTP MCP | ||
|
||
First, start the worker: | ||
```bash | ||
uv run openai_agents/mcp/servers/tools_server.py --transport=streamable-http | ||
``` | ||
|
||
Then start the worker: | ||
```bash | ||
uv run openai_agents/mcp/run_streamable_http_worker.py | ||
``` | ||
|
||
Finally, run the workflow: | ||
```bash | ||
uv run openai_agents/mcp/run_streamable_http_workflow.py | ||
``` | ||
|
||
### SSE MCP | ||
|
||
First, start the MCP server: | ||
```bash | ||
uv run openai_agents/mcp/servers/tools_server.py --transport=sse | ||
``` | ||
|
||
Then start the worker: | ||
```bash | ||
uv run openai_agents/mcp/run_sse_worker.py | ||
``` | ||
|
||
Finally, run the workflow: | ||
```bash | ||
uv run openai_agents/mcp/run_sse_workflow.py | ||
``` | ||
|
||
### Prompt Server MCP | ||
|
||
First, start the MCP server: | ||
```bash | ||
uv run openai_agents/mcp/servers/prompt_server.py | ||
``` | ||
|
||
Then start the worker: | ||
```bash | ||
uv run openai_agents/mcp/run_prompt_server_worker.py | ||
``` | ||
|
||
Finally, run the workflow: | ||
```bash | ||
uv run openai_agents/mcp/run_prompt_server_workflow.py | ||
``` | ||
|
||
|
||
### Memory MCP (Research Scratchpad) | ||
|
||
Demonstrates durable note-taking with the Memory MCP server: write seed notes, query by tags, synthesize a brief with citations, then update and delete notes. | ||
|
||
Start the worker: | ||
```bash | ||
uv run openai_agents/mcp/run_memory_research_scratchpad_worker.py | ||
``` | ||
|
||
Run the research scratchpad workflow: | ||
```bash | ||
uv run openai_agents/mcp/run_memory_research_scratchpad_workflow.py | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import logging | ||
import os | ||
from datetime import timedelta | ||
|
||
from agents.mcp import MCPServerStdio | ||
from temporalio.client import Client | ||
from temporalio.contrib.openai_agents import ( | ||
ModelActivityParameters, | ||
OpenAIAgentsPlugin, | ||
StatelessMCPServerProvider, | ||
) | ||
from temporalio.worker import Worker | ||
|
||
from openai_agents.mcp.workflows.file_system_workflow import FileSystemWorkflow | ||
|
||
|
||
async def main(): | ||
logging.basicConfig(level=logging.INFO) | ||
current_dir = os.path.dirname(os.path.abspath(__file__)) | ||
samples_dir = os.path.join(current_dir, "sample_files") | ||
|
||
file_system_server = StatelessMCPServerProvider( | ||
lambda: MCPServerStdio( | ||
name="FileSystemServer", | ||
params={ | ||
"command": "npx", | ||
"args": ["-y", "@modelcontextprotocol/server-filesystem", samples_dir], | ||
}, | ||
) | ||
) | ||
|
||
# Create client connected to server at the given address | ||
client = await Client.connect( | ||
"localhost:7233", | ||
plugins=[ | ||
OpenAIAgentsPlugin( | ||
model_params=ModelActivityParameters( | ||
start_to_close_timeout=timedelta(seconds=60) | ||
), | ||
mcp_server_providers=[file_system_server], | ||
), | ||
], | ||
) | ||
|
||
worker = Worker( | ||
client, | ||
task_queue=f"openai-agents-mcp-filesystem-task-queue", | ||
workflows=[ | ||
FileSystemWorkflow, | ||
], | ||
activities=[ | ||
# No custom activities needed for these workflows | ||
], | ||
) | ||
await worker.run() | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import asyncio | ||
|
||
from temporalio.client import Client | ||
from temporalio.contrib.openai_agents import OpenAIAgentsPlugin | ||
|
||
from openai_agents.mcp.workflows.file_system_workflow import FileSystemWorkflow | ||
|
||
|
||
async def main(): | ||
# Create client connected to server at the given address | ||
client = await Client.connect( | ||
"localhost:7233", | ||
plugins=[ | ||
OpenAIAgentsPlugin(), | ||
], | ||
) | ||
|
||
# Execute a workflow | ||
result = await client.execute_workflow( | ||
FileSystemWorkflow.run, | ||
id="file-system-workflow", | ||
task_queue="openai-agents-mcp-filesystem-task-queue", | ||
) | ||
|
||
print(f"Result: {result}") | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
59 changes: 59 additions & 0 deletions
59
openai_agents/mcp/run_memory_research_scratchpad_worker.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import logging | ||
from datetime import timedelta | ||
|
||
from agents.mcp import MCPServerStdio | ||
from temporalio.client import Client | ||
from temporalio.contrib.openai_agents import ( | ||
ModelActivityParameters, | ||
OpenAIAgentsPlugin, | ||
StatefulMCPServerProvider, | ||
) | ||
from temporalio.worker import Worker | ||
|
||
from openai_agents.mcp.workflows.memory_research_scratchpad_workflow import ( | ||
MemoryResearchScratchpadWorkflow, | ||
) | ||
|
||
|
||
async def main(): | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
memory_server_provider = StatefulMCPServerProvider( | ||
lambda: MCPServerStdio( | ||
name="MemoryServer", | ||
params={ | ||
"command": "npx", | ||
"args": ["-y", "@modelcontextprotocol/server-memory"], | ||
}, | ||
) | ||
) | ||
|
||
# Create client connected to server at the given address | ||
client = await Client.connect( | ||
"localhost:7233", | ||
plugins=[ | ||
OpenAIAgentsPlugin( | ||
model_params=ModelActivityParameters( | ||
start_to_close_timeout=timedelta(seconds=60) | ||
), | ||
mcp_server_providers=[memory_server_provider], | ||
), | ||
], | ||
) | ||
|
||
worker = Worker( | ||
client, | ||
task_queue="openai-agents-mcp-memory-task-queue", | ||
workflows=[ | ||
MemoryResearchScratchpadWorkflow, | ||
], | ||
activities=[], | ||
) | ||
await worker.run() | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
29 changes: 29 additions & 0 deletions
29
openai_agents/mcp/run_memory_research_scratchpad_workflow.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
|
||
from temporalio.client import Client | ||
from temporalio.contrib.openai_agents import OpenAIAgentsPlugin | ||
|
||
from openai_agents.mcp.workflows.memory_research_scratchpad_workflow import ( | ||
MemoryResearchScratchpadWorkflow, | ||
) | ||
|
||
|
||
async def main(): | ||
client = await Client.connect( | ||
"localhost:7233", | ||
plugins=[OpenAIAgentsPlugin()], | ||
) | ||
|
||
result = await client.execute_workflow( | ||
MemoryResearchScratchpadWorkflow.run, | ||
id="memory-research-scratchpad-workflow", | ||
task_queue="openai-agents-mcp-memory-task-queue", | ||
) | ||
|
||
print(f"Result:\n{result}") | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import logging | ||
from datetime import timedelta | ||
|
||
from agents.mcp import MCPServerStreamableHttp | ||
from temporalio.client import Client | ||
from temporalio.contrib.openai_agents import ( | ||
ModelActivityParameters, | ||
OpenAIAgentsPlugin, | ||
StatelessMCPServerProvider, | ||
) | ||
from temporalio.worker import Worker | ||
|
||
from openai_agents.mcp.workflows.prompt_server_workflow import PromptServerWorkflow | ||
|
||
|
||
async def main(): | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
print("Setting up worker...\n") | ||
|
||
try: | ||
prompt_server_provider = StatelessMCPServerProvider( | ||
lambda: MCPServerStreamableHttp( | ||
name="PromptServer", | ||
params={ | ||
"url": "http://localhost:8000/mcp", | ||
}, | ||
) | ||
) | ||
|
||
# Create client connected to server at the given address | ||
client = await Client.connect( | ||
"localhost:7233", | ||
plugins=[ | ||
OpenAIAgentsPlugin( | ||
model_params=ModelActivityParameters( | ||
start_to_close_timeout=timedelta(seconds=120) | ||
), | ||
mcp_server_providers=[prompt_server_provider], | ||
), | ||
], | ||
) | ||
|
||
worker = Worker( | ||
client, | ||
task_queue="openai-agents-mcp-prompt-task-queue", | ||
workflows=[ | ||
PromptServerWorkflow, | ||
], | ||
activities=[ | ||
# No custom activities needed for these workflows | ||
], | ||
) | ||
await worker.run() | ||
except Exception as e: | ||
print(f"Worker failed: {e}") | ||
raise | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
|
||
from temporalio.client import Client | ||
from temporalio.contrib.openai_agents import OpenAIAgentsPlugin | ||
|
||
from openai_agents.mcp.workflows.prompt_server_workflow import PromptServerWorkflow | ||
|
||
|
||
async def main(): | ||
# Create client connected to server at the given address | ||
client = await Client.connect( | ||
"localhost:7233", | ||
plugins=[OpenAIAgentsPlugin()], | ||
) | ||
|
||
# Execute a workflow | ||
result = await client.execute_workflow( | ||
PromptServerWorkflow.run, | ||
id="prompt-server-workflow", | ||
task_queue="openai-agents-mcp-prompt-task-queue", | ||
) | ||
|
||
print(f"Result: {result}") | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe rename to match the other examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion