-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
188 lines (142 loc) · 5.08 KB
/
Copy pathagent.py
File metadata and controls
188 lines (142 loc) · 5.08 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import asyncio
import json
from openai import AsyncOpenAI
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
LLM_BASE_URL = "http://127.0.0.1:8080/v1"
LLM_MODEL = "qwen3-8b"
client = AsyncOpenAI(
base_url=LLM_BASE_URL,
api_key="not-needed",
)
SYSTEM_PROMPT = """
You are a dashboard management agent.
You manage Grafana dashboards backed by InfluxDB.
Use the available tools to inspect the actual environment before making
assumptions about measurements, fields, tags, datasources, dashboards,
or dashboard versions.
When modifying a dashboard:
1. Read the dashboard first.
2. Use its current version for version-protected write operations.
3. Discover InfluxDB fields/tags when necessary.
4. Never invent measurement or field names.
5. Prefer high-level Grafana tools over constructing an entire dashboard
manually.
6. Explain briefly what you changed after completing the operation.
For InfluxDB schema discovery:
- Use influx_list_measurements to discover measurements.
- Use influx_list_fields to discover fields.
- Use influx_list_tag_keys to discover tags.
- Use influx_list_tag_values to discover values of a specific tag.
- Do NOT use influx_inspect_measurement for schema discovery.
- Only use influx_inspect_measurement when actual sample values are required.
- Minimize tool calls and prefer the most specific tool available.
"""
def mcp_tools_to_openai(mcp_tools):
tools = []
for tool in mcp_tools:
tools.append(
{
"type": "function",
"function": {
"name": tool.name,
"description": tool.description or "",
"parameters": tool.input_schema,
},
}
)
return tools
async def run_agent(session, user_message):
tool_result = await session.list_tools()
openai_tools = mcp_tools_to_openai(tool_result.tools)
messages = [
{
"role": "system",
"content": SYSTEM_PROMPT,
},
{
"role": "user",
"content": user_message,
},
]
for _ in range(12):
response = await client.chat.completions.create(
model=LLM_MODEL,
messages=messages,
tools=openai_tools,
tool_choice="auto",
temperature=0,
)
message = response.choices[0].message
messages.append(message.model_dump(exclude_none=True))
if not message.tool_calls:
return message.content or ""
for tool_call in message.tool_calls:
name = tool_call.function.name
try:
arguments = json.loads(
tool_call.function.arguments or "{}"
)
except json.JSONDecodeError as exc:
result_text = f"Invalid tool arguments: {exc}"
else:
print(f"\n[tool] {name}({arguments})")
try:
result = await session.call_tool(
name,
arguments=arguments,
)
parts = []
for content in result.content:
text = getattr(content, "text", None)
if text is not None:
parts.append(text)
result_text = "\n".join(parts)
except Exception as exc:
result_text = f"Tool error: {exc}"
messages.append(
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": result_text,
}
)
return "Agent stopped after reaching the tool-call limit."
async def main():
server = StdioServerParameters(
command="/opt/dashboard-agent/.venv/bin/python",
args=["/opt/dashboard-agent/server.py"],
cwd="/opt/dashboard-agent",
)
async with stdio_client(server) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
tools = await session.list_tools()
print("Dashboard Agent")
print(f"Model: {LLM_MODEL}")
print(f"MCP tools: {len(tools.tools)}")
print("Type 'quit' to exit.\n")
while True:
try:
user_message = input("> ").strip()
except (EOFError, KeyboardInterrupt):
print()
break
if not user_message:
continue
if user_message.lower() in {
"quit",
"exit",
"q",
}:
break
try:
answer = await run_agent(
session,
user_message,
)
print(f"\n{answer}\n")
except Exception as exc:
print(f"\nAgent error: {exc}\n")
if __name__ == "__main__":
asyncio.run(main())