24
24
25
25
pytestmark = pytest .mark .anyio
26
26
27
+
27
28
@pytest .fixture
28
29
def mcp_url ():
29
30
return os .environ .get ("MCP_GRAFANA_URL" , DEFAULT_MCP_URL )
@@ -46,7 +47,6 @@ async def mcp_client(mcp_url, grafana_headers):
46
47
write ,
47
48
):
48
49
async with ClientSession (read , write ) as session :
49
- # Initialize the connection
50
50
await session .initialize ()
51
51
yield session
52
52
@@ -81,7 +81,9 @@ async def test_loki(model: str, mcp_client: ClientSession):
81
81
# Verify Loki datasource exists
82
82
loki_datasources = [ds for ds in datasources_data if ds .get ("type" ) == "loki" ]
83
83
assert len (loki_datasources ) > 0 , "No Loki datasource found"
84
- print (f"\n Found Loki datasource: { loki_datasources [0 ]['name' ]} (uid: { loki_datasources [0 ]['uid' ]} )" )
84
+ print (
85
+ f"\n Found Loki datasource: { loki_datasources [0 ]['name' ]} (uid: { loki_datasources [0 ]['uid' ]} )"
86
+ )
85
87
86
88
# Call the LLM including the tool call result.
87
89
response = await acompletion (
@@ -109,7 +111,7 @@ async def test_loki(model: str, mcp_client: ClientSession):
109
111
)
110
112
111
113
# Check that the response has some log lines.
112
- content = response .choices [0 ].message .content # type: ignore
114
+ content = response .choices [0 ].message .content
113
115
log_lines_checker = CustomLLMBooleanEvaluator (
114
116
settings = CustomLLMBooleanSettings (
115
117
prompt = "Does the response contain specific information that could only come from a Loki datasource? This could be actual log lines with timestamps, container names, or a summary that references specific log data. The response should show evidence of real data rather than generic statements." ,
@@ -149,7 +151,9 @@ async def test_loki_container_labels(model: str, mcp_client: ClientSession):
149
151
# Verify Loki datasource exists
150
152
loki_datasources = [ds for ds in datasources_data if ds .get ("type" ) == "loki" ]
151
153
assert len (loki_datasources ) > 0 , "No Loki datasource found"
152
- print (f"\n Found Loki datasource: { loki_datasources [0 ]['name' ]} (uid: { loki_datasources [0 ]['uid' ]} )" )
154
+ print (
155
+ f"\n Found Loki datasource: { loki_datasources [0 ]['name' ]} (uid: { loki_datasources [0 ]['uid' ]} )"
156
+ )
153
157
154
158
# Call the LLM including the tool call result.
155
159
response = await acompletion (
@@ -165,10 +169,7 @@ async def test_loki_container_labels(model: str, mcp_client: ClientSession):
165
169
response ,
166
170
mcp_client ,
167
171
"list_loki_label_values" ,
168
- {
169
- "datasourceUid" : "loki" ,
170
- "labelName" : "container"
171
- },
172
+ {"datasourceUid" : "loki" , "labelName" : "container" },
172
173
)
173
174
)
174
175
@@ -180,7 +181,7 @@ async def test_loki_container_labels(model: str, mcp_client: ClientSession):
180
181
)
181
182
182
183
# Check that the response provides a meaningful summary of container labels
183
- content = response .choices [0 ].message .content # type: ignore
184
+ content = response .choices [0 ].message .content
184
185
label_checker = CustomLLMBooleanEvaluator (
185
186
settings = CustomLLMBooleanSettings (
186
187
prompt = "Does the response provide a clear and organized list of container names found in the logs? It should present the container names in a readable format and may include additional context about their usage." ,
@@ -219,8 +220,12 @@ async def assert_and_handle_tool_call(
219
220
220
221
if expected_args :
221
222
for key , value in expected_args .items ():
222
- assert key in arguments , f"Missing required argument '{ key } ' in tool call"
223
- assert arguments [key ] == value , f"Argument '{ key } ' has wrong value. Expected: { value } , Got: { arguments [key ]} "
223
+ assert key in arguments , (
224
+ f"Missing required argument '{ key } ' in tool call"
225
+ )
226
+ assert arguments [key ] == value , (
227
+ f"Argument '{ key } ' has wrong value. Expected: { value } , Got: { arguments [key ]} "
228
+ )
224
229
result = await mcp_client .call_tool (tool_call .function .name , arguments )
225
230
# Assume each tool returns a single text content for now
226
231
assert len (result .content ) == 1
0 commit comments