File size: 2,012 Bytes
936b827 |
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 |
import asyncio
import os
from dotenv import load_dotenv
import openai
from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel
from agents.mcp import MCPServerSse
import json
# Load environment
load_dotenv()
# Set API key
openai.api_key = os.getenv("NEBIUS_API_KEY")
openai.base_url = "https://api.studio.nebius.com/v1/"
# MCP tool URL (replace with actual URL you got from step 4.3)
TOOL_URL = "http://127.0.0.1:7860/gradio_api/mcp/sse"
async def main():
mcp_server = MCPServerSse({"url": TOOL_URL})
try:
await mcp_server.connect()
# Create an agent and register the connected tool
# β
Replace "MCP Agent" with a descriptive name if desired
# β
Update instructions to guide the agent's behavior
agent = Agent(
name="MCP Agent",
instructions="You help the user complete tasks using the connected tool.",
mcp_servers=[mcp_server],
model=OpenAIChatCompletionsModel(
model="Qwen/Qwen3-235B-A22B",
openai_client=AsyncOpenAI(base_url="https://api.studio.nebius.com/v1/", api_key=os.getenv("NEBIUS_API_KEY"))
)
)
# π Replace this task with your actual prompt or goal
task = "You are a helpful assistant. Use the connected tool to assist with tasks. Read and analyse the tool output, focusing on key information and help the user in the following task:" \
"I want to travel from Hyderabad to Phuket from 5th July to 23rd July in 2025, please help me with my flight search, what are my cheapest and fastest options."
result = await Runner.run(agent, task)
print(result.final_output)
# print(json.dumps(result.new_items, indent=4))
# print(result.raw_responses)
# for r in result:
# print(r)
# print("\n============\n")
finally:
await mcp_server.cleanup()
# Run the async main function
if __name__ == "__main__":
asyncio.run(main()) |