forked from mcp-use/mcp-use
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsandbox_everything.py
More file actions
66 lines (52 loc) · 1.85 KB
/
sandbox_everything.py
File metadata and controls
66 lines (52 loc) · 1.85 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
"""
This example shows how to test the different functionalities of MCPs using the MCP server from
OpenAI in a sandboxed environment using E2B.
"""
import asyncio
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
import mcp_use
from mcp_use import MCPAgent, MCPClient
from mcp_use.types.sandbox import SandboxOptions
mcp_use.set_debug(debug=1)
# Server configuration (MCP standard compliant)
sandboxed_server = {
"mcpServers": {
"everything": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-everything"],
}
}
}
async def main():
"""Run the example using a sandboxed environment."""
# Load environment variables (needs E2B_API_KEY)
load_dotenv()
# Ensure E2B API key is available
if not os.getenv("E2B_API_KEY"):
raise ValueError("E2B_API_KEY environment variable is required")
# E2B sandbox options
sandbox_options: SandboxOptions = {
"api_key": os.getenv("E2B_API_KEY"), # E2B API key to create the sandbox
"sandbox_template_id": "code-interpreter-v1", # Use code interpreter template
}
# Create client with sandboxed mode enabled and sandbox options
client = MCPClient(config=sandboxed_server, sandbox=True, sandbox_options=sandbox_options)
# Create LLM and agent
llm = ChatOpenAI(model="gpt-5", temperature=0)
agent = MCPAgent(llm=llm, client=client, max_steps=30, pretty_print=True)
try:
# Run the same test query
result = await agent.run(
"""
Run echo "test" and then echo "second test" again and then add 1 + 1
""",
max_steps=30,
)
print(f"\nResult: {result}")
finally:
# Ensure we clean up resources properly
await client.close_all_sessions()
if __name__ == "__main__":
asyncio.run(main())