-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: Streamable HTTP support #643
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
+247
−14
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3214e7b
feat: Streamable HTTP support
9aa32f5
feat: Streamable HTTP support
e7eb306
feat: Streamable HTTP support
e50bd99
feat: Streamable HTTP support
8794eb8
feat: Streamable HTTP support
ed58792
feat: adding Streamable HTTP example
19b16fe
feat: refactor
bec3e5c
feat: update uv.lock
41ae891
feat: fixed typechecking and lint errors
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# MCP Streamable HTTP Example | ||
|
||
This example uses a local Streamable HTTP server in [server.py](server.py). | ||
|
||
Run the example via: | ||
|
||
``` | ||
uv run python examples/mcp/streamablehttp_example/main.py | ||
``` | ||
|
||
## Details | ||
|
||
The example uses the `MCPServerStreamableHttp` class from `agents.mcp`. The server runs in a sub-process at `https://localhost:8000/mcp`. |
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,83 @@ | ||
import asyncio | ||
import os | ||
import shutil | ||
import subprocess | ||
import time | ||
from typing import Any | ||
|
||
from agents import Agent, Runner, gen_trace_id, trace | ||
from agents.mcp import MCPServer, MCPServerStreamableHttp | ||
from agents.model_settings import ModelSettings | ||
|
||
|
||
async def run(mcp_server: MCPServer): | ||
agent = Agent( | ||
name="Assistant", | ||
instructions="Use the tools to answer the questions.", | ||
mcp_servers=[mcp_server], | ||
model_settings=ModelSettings(tool_choice="required"), | ||
) | ||
|
||
# Use the `add` tool to add two numbers | ||
message = "Add these numbers: 7 and 22." | ||
print(f"Running: {message}") | ||
result = await Runner.run(starting_agent=agent, input=message) | ||
print(result.final_output) | ||
|
||
# Run the `get_weather` tool | ||
message = "What's the weather in Tokyo?" | ||
print(f"\n\nRunning: {message}") | ||
result = await Runner.run(starting_agent=agent, input=message) | ||
print(result.final_output) | ||
|
||
# Run the `get_secret_word` tool | ||
message = "What's the secret word?" | ||
print(f"\n\nRunning: {message}") | ||
result = await Runner.run(starting_agent=agent, input=message) | ||
print(result.final_output) | ||
|
||
|
||
async def main(): | ||
async with MCPServerStreamableHttp( | ||
name="Streamable HTTP Python Server", | ||
params={ | ||
"url": "http://localhost:8000/mcp", | ||
}, | ||
) as server: | ||
trace_id = gen_trace_id() | ||
with trace(workflow_name="Streamable HTTP Example", trace_id=trace_id): | ||
print(f"View trace: https://platform.openai.com/traces/trace?trace_id={trace_id}\n") | ||
await run(server) | ||
|
||
|
||
if __name__ == "__main__": | ||
# Let's make sure the user has uv installed | ||
if not shutil.which("uv"): | ||
raise RuntimeError( | ||
"uv is not installed. Please install it: https://docs.astral.sh/uv/getting-started/installation/" | ||
) | ||
|
||
# We'll run the Streamable HTTP server in a subprocess. Usually this would be a remote server, but for this | ||
# demo, we'll run it locally at http://localhost:8000/mcp | ||
process: subprocess.Popen[Any] | None = None | ||
try: | ||
this_dir = os.path.dirname(os.path.abspath(__file__)) | ||
server_file = os.path.join(this_dir, "server.py") | ||
|
||
print("Starting Streamable HTTP server at http://localhost:8000/mcp ...") | ||
|
||
# Run `uv run server.py` to start the Streamable HTTP server | ||
process = subprocess.Popen(["uv", "run", server_file]) | ||
# Give it 3 seconds to start | ||
time.sleep(3) | ||
|
||
print("Streamable HTTP server started. Running example...\n\n") | ||
except Exception as e: | ||
print(f"Error starting Streamable HTTP server: {e}") | ||
exit(1) | ||
|
||
try: | ||
asyncio.run(main()) | ||
finally: | ||
if process: | ||
process.terminate() |
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,33 @@ | ||
import random | ||
|
||
import requests | ||
from mcp.server.fastmcp import FastMCP | ||
|
||
# Create server | ||
mcp = FastMCP("Echo Server") | ||
|
||
|
||
@mcp.tool() | ||
def add(a: int, b: int) -> int: | ||
"""Add two numbers""" | ||
print(f"[debug-server] add({a}, {b})") | ||
return a + b | ||
|
||
|
||
@mcp.tool() | ||
def get_secret_word() -> str: | ||
print("[debug-server] get_secret_word()") | ||
return random.choice(["apple", "banana", "cherry"]) | ||
|
||
|
||
@mcp.tool() | ||
def get_current_weather(city: str) -> str: | ||
print(f"[debug-server] get_current_weather({city})") | ||
|
||
endpoint = "https://wttr.in" | ||
response = requests.get(f"{endpoint}/{city}") | ||
return response.text | ||
|
||
|
||
if __name__ == "__main__": | ||
mcp.run(transport="streamable-http") |
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
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
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.
Can you add some unit tests to make sure this works?
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.
@rm-openai Updated the check to discard the extra values and remove hard-coded checks. Do you want me to add unit tests right away or can I take that in a subsequent PR?