-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathopenai_agents_mcp_http.py
More file actions
68 lines (53 loc) · 2.25 KB
/
openai_agents_mcp_http.py
File metadata and controls
68 lines (53 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""OpenAI Agents framework + MCP HTTP example.
Prerequisite:
Start the local MCP server defined in `mcp_server_basic.py` on port 8000:
python examples/mcp_server_basic.py
"""
import asyncio
import logging
import os
from agents import Agent, OpenAIResponsesModel, Runner, set_tracing_disabled
from agents.mcp.server import MCPServerStreamableHttp
from agents.model_settings import ModelSettings
from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider
from dotenv import load_dotenv
from openai import AsyncOpenAI
logging.basicConfig(level=logging.WARNING)
# Disable tracing since we're not connected to a supported tracing provider
set_tracing_disabled(disabled=True)
# Setup the OpenAI client to use either Azure OpenAI
load_dotenv(override=True)
API_HOST = os.getenv("API_HOST", "azure")
async_credential = None
if API_HOST == "azure":
async_credential = DefaultAzureCredential()
token_provider = get_bearer_token_provider(async_credential, "https://cognitiveservices.azure.com/.default")
client = AsyncOpenAI(
base_url=os.environ["AZURE_OPENAI_ENDPOINT"] + "/openai/v1",
api_key=token_provider,
)
MODEL_NAME = os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"]
elif API_HOST == "ollama":
client = AsyncOpenAI(base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"), api_key="none")
MODEL_NAME = os.environ["OLLAMA_MODEL"]
else:
client = AsyncOpenAI(api_key=os.environ["OPENAI_API_KEY"])
MODEL_NAME = os.environ.get("OPENAI_MODEL", "gpt-4o")
mcp_server = MCPServerStreamableHttp(name="weather", params={"url": "http://localhost:8000/mcp/"})
agent = Agent(
name="Assistant",
instructions="Use the tools to achieve the task",
mcp_servers=[mcp_server],
model=OpenAIResponsesModel(model=MODEL_NAME, openai_client=client),
model_settings=ModelSettings(tool_choice="required"),
)
async def main():
await mcp_server.connect()
message = "Find me a hotel in San Francisco for 2 nights starting from 2024-01-01. I need free WiFi and a pool."
result = await Runner.run(starting_agent=agent, input=message)
print(result.final_output)
await mcp_server.cleanup()
if async_credential:
await async_credential.close()
if __name__ == "__main__":
asyncio.run(main())