-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
58 lines (51 loc) · 1.91 KB
/
agent.py
File metadata and controls
58 lines (51 loc) · 1.91 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
from langchain.agents import create_agent
from langchain_mcp_adapters.client import MultiServerMCPClient
import asyncio
import os
from blaxel.core import SandboxInstance
async def main():
# Set OpenAI API key - replace with your actual key or use environment variable
if not os.getenv("OPENAI_API_KEY"):
print("Warning: OPENAI_API_KEY environment variable not set")
# Uncomment and set your key here if needed:
# os.environ["OPENAI_API_KEY"] = "your-key-here"
sandbox = await SandboxInstance.create_if_not_exists({
"name": "my-nextjs2-sandbox",
"image": "blaxel/nextjs:latest",
"memory": 4096,
"ports": [
{ "name": "preview", "target": 3000, "protocol": "HTTP" },
],
})
client = MultiServerMCPClient(
{
"sandbox": {
"transport": "http",
"url": f"{sandbox.metadata.url}/mcp",
"headers": {
"Authorization": "Bearer XXX"
}
}
}
)
tools = await client.get_tools()
# Fix tool schemas for OpenAI compatibility
# OpenAI requires 'properties' field even for tools with no parameters
for tool in tools:
if hasattr(tool, 'args_schema'):
# The args_schema from MCP adapter is a dict
schema = tool.args_schema if isinstance(tool.args_schema, dict) else None
if schema and schema.get('type') == 'object' and 'properties' not in schema:
# Add empty properties dict for tools with no parameters
schema['properties'] = {}
print(f"Prepared {len(tools)} tools for agent")
agent = create_agent(
model="openai:gpt-4.1",
tools=tools
)
response = await agent.ainvoke(
{"messages": [{"role": "user", "content": "what tools do you have?"}]}
)
print(response)
if __name__ == "__main__":
asyncio.run(main())