1+ """
2+ Example of using MCPHub with OpenAI Agents.
3+ 1. Initialize MCPHub to manage MCP servers
4+ 2. Fetch an MCP server with async context manager
5+ 3. List available tools from the server
6+ 4. Create and run an agent with MCP tools
7+ """
8+
19import asyncio
210import json
311from agents import Agent , Runner
412from mcphub import MCPHub
513
614async def main ():
7- """
8- Example of using MCPHub to integrate MCP servers with OpenAI Agents.
9-
10- This example demonstrates:
11- 1. Initializing MCPHub
12- 2. Fetching and using an MCP server
13- 3. Listing available tools
14- 4. Creating and running an agent with MCP tools
15- """
16-
17- # Step 1: Initialize MCPHub
18- # MCPHub will automatically:
19- # - Find .mcphub.json in your project
20- # - Load server configurations
21- # - Set up servers (clone repos, run setup scripts if needed)
15+ # Initialize MCPHub - automatically loads .mcphub.json and sets up servers
2216 hub = MCPHub ()
2317
24- # Step 2: Create an MCP server instance using async context manager
25- # Parameters:
26- # - mcp_name: The name of the server from your .mcphub.json
27- # - cache_tools_list: Cache the tools list for better performance
18+ # Fetch MCP server - handles server setup and tool caching
2819 async with hub .fetch_openai_mcp_server (
2920 mcp_name = "sequential-thinking-mcp" ,
3021 cache_tools_list = True
3122 ) as server :
32- # Step 3: List available tools from the MCP server
33- # This shows what capabilities are available to your agent
23+ # Get available tools from the server
3424 tools = await server .list_tools ()
35-
36- # Pretty print the tools for better readability
3725 tools_dict = [
3826 dict (tool ) if hasattr (tool , "__dict__" ) else tool for tool in tools
3927 ]
4028 print ("Available MCP Tools:" )
4129 print (json .dumps (tools_dict , indent = 2 ))
4230
43- # Step 4: Create an OpenAI Agent with MCP server
44- # The agent can now use all tools provided by the MCP server
31+ # Create agent with MCP server integration
4532 agent = Agent (
4633 name = "Assistant" ,
4734 instructions = "Use the available tools to accomplish the given task" ,
48- mcp_servers = [server ] # Provide the MCP server to the agent
35+ mcp_servers = [server ]
4936 )
5037
51- # Step 5: Run your agent with a complex task
52- # The agent will automatically have access to all MCP tools
38+ # Run agent with a task
5339 complex_task = """Please help me analyze the following complex problem:
5440 We need to design a new feature for our product that balances user privacy
5541 with data collection for improving the service. Consider the ethical implications,
5642 technical feasibility, and business impact. Break down your thinking process
5743 step by step, and provide a detailed recommendation with clear justification
5844 for each decision point."""
5945
60- # Execute the task and get the result
6146 result = await Runner .run (agent , complex_task )
6247 print ("\n Agent Response:" )
6348 print (result )
6449
6550if __name__ == "__main__" :
66- # Run the async main function
6751 asyncio .run (main ())
0 commit comments