Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import json
import os
import sys
import asyncio

from agents import gen_trace_id, trace

from coinbase_agentkit import (
AgentKit,
AgentKitConfig,
SmartWalletProvider,
SmartWalletProviderConfig,
cdp_api_action_provider,
erc20_action_provider,
pyth_action_provider,
wallet_action_provider,
weth_action_provider,
)
from coinbase_agentkit_openai_agents_sdk import get_openai_agents_sdk_tools
from dotenv import load_dotenv
from eth_account.account import Account
from agents.agent import Agent
from agents.run import Runner
from agents.mcp import MCPServer, MCPServerStdio

# Configure a file to persist the agent's CDP API Wallet Data.
wallet_data_file = "wallet_data.txt"
mcp_server_path = "/Users/curtfluegel/Documents/Development/crypto-feargreed-mcp/"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better to use a remote mcp server here, for example from coingecko: https://docs.coingecko.com/reference/mcp-server#remote-server-public-keyless


load_dotenv()


async def initialize_agent(mcp_server: MCPServer):
"""Initialize the agent with SmartWalletProvider."""
network_id = os.getenv("NETWORK_ID", "base-sepolia")
wallet_data_file = f"wallet_data_{network_id.replace('-', '_')}.txt"

# Load wallet data from JSON file
wallet_data = {"private_key": None, "smart_wallet_address": None}
if os.path.exists(wallet_data_file):
try:
with open(wallet_data_file) as f:
wallet_data = json.load(f)
except json.JSONDecodeError:
print(f"Warning: Invalid wallet data file format for {network_id}. Creating new wallet.")

# Use private key from env if not in wallet data
private_key = wallet_data.get("private_key") or os.getenv("PRIVATE_KEY")
if not private_key:
acct = Account.create()
private_key = acct.key.hex()

signer = Account.from_key(private_key)

# Initialize Smart Wallet Provider
wallet_provider = SmartWalletProvider(
SmartWalletProviderConfig(
network_id=network_id,
signer=signer,
smart_wallet_address=wallet_data.get("smart_wallet_address"),
paymaster_url=None, # Sponsor transactions: https://docs.cdp.coinbase.com/paymaster/docs/welcome
)
)

# Save both private key and smart wallet address
wallet_data = {
"private_key": private_key,
"smart_wallet_address": wallet_provider.get_address(),
}
with open(wallet_data_file, "w") as f:
json.dump(wallet_data, f, indent=2)

agentkit = AgentKit(
AgentKitConfig(
wallet_provider=wallet_provider,
action_providers=[
cdp_api_action_provider(),
erc20_action_provider(),
pyth_action_provider(),
wallet_action_provider(),
weth_action_provider(),
],
)
)

# use get_openai_agents_sdk_tools
tools = get_openai_agents_sdk_tools(agentkit)

# Create Agent using the OpenAI Agents SDK
agent = Agent(
name="CDP Agent",
instructions=(
"You are a helpful agent that can interact onchain using the Coinbase Developer Platform AgentKit. "
"You are empowered to interact onchain using your tools. If you ever need funds, you can request "
"them from the faucet if you are on network ID 'base-sepolia'. If not, you can provide your wallet "
"details and request funds from the user. Before executing your first action, get the wallet details "
"to see what network you're on. If there is a 5XX (internal) HTTP error code, ask the user to try "
"again later. If someone asks you to do something you can't do with your currently available tools, "
"you must say so, and encourage them to implement it themselves using the CDP SDK + Agentkit, "
"recommend they go to docs.cdp.coinbase.com for more information. Be concise and helpful with your "
"responses. Refrain from restating your tools' descriptions unless it is explicitly requested."
"Unless asked otherwise, speak in English."
),
tools=tools,
mcp_servers=[
mcp_server
],
)

return agent


# Autonomous Mode
async def run_autonomous_mode(agent, interval=10):
"""Run the agent autonomously with specified intervals."""
print("Starting autonomous mode...")
while True:
try:
thought = (
"Be creative and do something interesting on the blockchain. "
"Choose an action or set of actions and execute it that highlights your abilities."
)

# Run agent in autonomous mode
output = await Runner.run(agent, thought)
print(output.final_output)
print("-------------------")

# Wait before the next action
await asyncio.sleep(interval)

except KeyboardInterrupt:
print("Goodbye Agent!")
sys.exit(0)

# Chat Mode
async def run_chat_mode(agent):
"""Run the agent interactively based on user input."""
print("Starting chat mode... Type 'exit' to end.")
while True:
try:
user_input = input("\nPrompt: ")
if user_input.lower() == "exit":
break

# Run agent with the user's input in chat mode
output = await Runner.run(agent, user_input)
print(output.final_output)
print("-------------------")

except KeyboardInterrupt:
print("Goodbye Agent!")
sys.exit(0)


# Mode Selection
def choose_mode():
"""Choose whether to run in autonomous or chat mode based on user input."""
while True:
print("\nAvailable modes:")
print("1. chat - Interactive chat mode")
print("2. auto - Autonomous action mode")

choice = input("\nChoose a mode (enter number or name): ").lower().strip()
if choice in ["1", "chat"]:
return "chat"
elif choice in ["2", "auto"]:
return "auto"
print("Invalid choice. Please try again.")


async def main():
try:
async with MCPServerStdio(
params={
"command": "uv",
"args": ["--directory", mcp_server_path, "run", "main.py"]
},
name="crypto-feargreed-mcp"
) as server:
trace_id = gen_trace_id()
with trace(workflow_name="MCP Filesystem Example", trace_id=trace_id):
print(f"View trace: https://platform.openai.com/traces/{trace_id}\n")
# Initialize agent and run chat mode within the server context
agent = await initialize_agent(server)
mode = choose_mode()
if mode == "chat":
await run_chat_mode(agent=agent)
elif mode == "auto":
await run_autonomous_mode(agent=agent)
except Exception as e:
print(f"Error running server: {e}")

if __name__ == "__main__":
asyncio.run(main())
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Bump openai-agents to v0.0.7
Added support for MCP Servers in OpenAI agents. Users can now integrate MCP servers into their agents with a simple configuration:

```python
agent = Agent(
name="Assistant",
instructions="Use the tools to answer any questions.",
mcp_servers=[mcp_server],
)
```
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies = [
"coinbase-agentkit>=0.3.0,<0.5",
"python-dotenv>=1.0.1,<2",
"pytest-asyncio>=0.25.3,<0.26",
"openai-agents>=0.0.6,<0.0.7",
"openai-agents>=0.0.7",
]

[dependency-groups]
Expand Down
63 changes: 59 additions & 4 deletions python/framework-extensions/openai-agents-sdk/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.