forked from alshurov13/allure-testops-mcp-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
784 lines (682 loc) · 36.4 KB
/
Copy pathindex.py
File metadata and controls
784 lines (682 loc) · 36.4 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
#!/usr/bin/env python3
"""
Allure TestOps MCP Server
Model Context Protocol server for Allure TestOps API
"""
import asyncio
import json
import os
import sys
from typing import Any, Dict, List, Optional
from dotenv import load_dotenv
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.server.streamable_http import StreamableHTTPServerTransport
from mcp.types import Tool, TextContent
from allure_client import create_allure_client
from csv_parser import parse_test_cases_from_csv
load_dotenv()
# Environment variables
ALLURE_TESTOPS_URL = os.environ.get('ALLURE_TESTOPS_URL')
ALLURE_TOKEN = os.environ.get('ALLURE_TOKEN')
MCP_TRANSPORT = os.environ.get('MCP_TRANSPORT', 'stdio') # stdio or streamable_http
MCP_ADDRESS = os.environ.get('MCP_ADDRESS', '0.0.0.0:8000') # host:port
# Validate required environment variables
if not ALLURE_TOKEN:
print('Error: ALLURE_TOKEN environment variable is required', file=sys.stderr)
sys.exit(1)
if not ALLURE_TESTOPS_URL:
print('Error: ALLURE_TESTOPS_URL environment variable is required', file=sys.stderr)
sys.exit(1)
# Initialize Allure client
allure_client = create_allure_client(ALLURE_TESTOPS_URL, ALLURE_TOKEN)
# Define MCP tools
all_tools: List[Tool] = [
Tool(
name='test_cases',
description='Read or write test case data from Allure TestOps. Use method="get" to fetch a specific test case by ID (includes scenario/steps), method="list" to get all test cases in a project with pagination, method="create" to create new test case, method="update" to modify existing test case, or method="delete" to remove a test case.',
inputSchema={
'type': 'object',
'properties': {
'method': {
'type': 'string',
'enum': ['get', 'list', 'create', 'update', 'delete'],
'description': 'Operation to perform: "get" for single test case, "list" for all test cases, "create" to create new, "update" to modify, or "delete" to remove'
},
'id': {'type': 'number', 'description': 'Test case ID (required for method=get, method=update or method=delete)'},
'project_id': {'type': 'string', 'description': 'Project ID (required for method=list or method=create)'},
'page': {'type': 'number', 'description': 'Page number (optional, for method=list)'},
'size': {'type': 'number', 'description': 'Page size (optional, for method=list)'},
'name': {'type': 'string', 'description': 'Test case name (for method=create or method=update)'},
'description': {'type': 'string', 'description': 'Test case description (for method=create or method=update)'},
'status': {'type': 'string', 'description': 'Test case status (for method=create or method=update)'},
'automated': {'type': 'boolean', 'description': 'Is automated (for method=create or method=update)'},
},
'required': ['method'],
},
),
Tool(
name='test_case_steps',
description='Create, update or delete test case steps. Use method="create" to add a new step, method="update" to modify an existing step, or method="delete" to remove a step.',
inputSchema={
'type': 'object',
'properties': {
'method': {
'type': 'string',
'enum': ['create', 'update', 'delete'],
'description': 'Operation to perform: "create" to add a step, "update" to modify a step, "delete" to remove a step'
},
'test_case_id': {'type': 'number', 'description': 'Test case ID (required for method=create)'},
'step_id': {'type': 'number', 'description': 'Step ID (required for method=update and method=delete)'},
'text': {'type': 'string', 'description': 'Step text content (required for method=create and method=update)'},
'after_id': {'type': 'number', 'description': 'Insert step after this step ID (optional, mutually exclusive with before_id)'},
'before_id': {'type': 'number', 'description': 'Insert step before this step ID (optional, mutually exclusive with after_id)'},
'with_expected_result': {'type': 'boolean', 'description': 'Include expected result section (default: false)'},
},
'required': ['method'],
},
),
Tool(
name='launches',
description='Read or write launch data from Allure TestOps. Use method="get" to fetch a specific launch by ID, method="list" to get all launches in a project with pagination, method="create" to create new launch, method="update" to modify existing launch, method="delete" to remove a launch, or method="close" to close a launch.',
inputSchema={
'type': 'object',
'properties': {
'method': {
'type': 'string',
'enum': ['get', 'list', 'create', 'update', 'delete', 'close'],
'description': 'Operation to perform: "get" for single launch, "list" for all launches, "create" to create new, "update" to modify, "delete" to remove, or "close" to close'
},
'id': {'type': 'number', 'description': 'Launch ID (required for method=get, method=update, method=delete, or method=close)'},
'project_id': {'type': 'string', 'description': 'Project ID (required for method=list or method=create)'},
'page': {'type': 'number', 'description': 'Page number (optional, for method=list)'},
'size': {'type': 'number', 'description': 'Page size (optional, for method=list)'},
'name': {'type': 'string', 'description': 'Launch name (for method=create or method=update)'},
'closed': {'type': 'boolean', 'description': 'Is closed (for method=create or method=update)'},
},
'required': ['method'],
},
),
Tool(
name='test_plans',
description='Read or write test plan data from Allure TestOps. Use method="get" to fetch a specific test plan by ID, method="list" to get all test plans in a project with pagination, method="create" to create new test plan, method="update" to modify existing test plan, or method="delete" to remove a test plan.',
inputSchema={
'type': 'object',
'properties': {
'method': {
'type': 'string',
'enum': ['get', 'list', 'create', 'update', 'delete'],
'description': 'Operation to perform: "get" for single test plan, "list" for all test plans, "create" to create new, "update" to modify, or "delete" to remove'
},
'id': {'type': 'number', 'description': 'Test plan ID (required for method=get, method=update or method=delete)'},
'project_id': {'type': 'string', 'description': 'Project ID (required for method=list or method=create)'},
'page': {'type': 'number', 'description': 'Page number (optional, for method=list)'},
'size': {'type': 'number', 'description': 'Page size (optional, for method=list)'},
'name': {'type': 'string', 'description': 'Test plan name (for method=create or method=update)'},
'description': {'type': 'string', 'description': 'Test plan description (for method=create or method=update)'},
},
'required': ['method'],
},
),
Tool(
name='bulk_create_test_cases_from_csv',
description='Bulk create test cases from CSV content. CSV should have columns: name, description, status, automated',
inputSchema={
'type': 'object',
'properties': {
'project_id': {'type': 'string', 'description': 'Project ID'},
'csv_content': {'type': 'string', 'description': 'CSV file content'},
},
'required': ['project_id', 'csv_content'],
},
),
Tool(
name='test_case_custom_fields',
description='Get or modify custom field values for a test case. Use method="get" to retrieve available custom fields and their current values, or method="modify" to add/remove a custom field value.',
inputSchema={
'type': 'object',
'properties': {
'method': {
'type': 'string',
'enum': ['get', 'modify'],
'description': 'Operation to perform: "get" to retrieve values, "modify" to add/remove values'
},
'test_case_id': {'type': 'number', 'description': 'Test case ID'},
'project_id': {'type': 'string', 'description': 'Project ID'},
'custom_field_id': {'type': 'number', 'description': 'Custom field ID (required for method=modify)'},
'custom_field_value_id': {'type': 'number', 'description': 'Custom field value ID to add (required for mode=add)'},
'mode': {'type': 'string', 'enum': ['add', 'delete'], 'description': 'Modification mode: "add" to add value, "delete" to remove all values for the field (required for method=modify)'},
},
'required': ['method', 'test_case_id', 'project_id'],
},
),
Tool(
name='get_custom_field_values',
description='Get possible values for a custom field in a project. Useful for discovering available options before modifying custom fields.',
inputSchema={
'type': 'object',
'properties': {
'project_id': {'type': 'number', 'description': 'Project ID'},
'custom_field_id': {'type': 'number', 'description': 'Custom field ID'},
'size': {'type': 'number', 'description': 'Page size (optional, default 100)'},
'page': {'type': 'number', 'description': 'Page number (optional, default 0)'},
},
'required': ['project_id', 'custom_field_id'],
},
),
Tool(
name='test_case_comments',
description='Get, create or delete comments for a test case. Use method="get" to retrieve comments, method="create" to add a new comment, or method="delete" to remove a comment.',
inputSchema={
'type': 'object',
'properties': {
'method': {
'type': 'string',
'enum': ['get', 'create', 'delete'],
'description': 'Operation to perform: "get" to retrieve comments, "create" to add a comment, "delete" to remove a comment'
},
'test_case_id': {'type': 'number', 'description': 'Test case ID (required for method=get and method=create)'},
'comment_id': {'type': 'number', 'description': 'Comment ID (required for method=delete)'},
'body': {'type': 'string', 'description': 'Comment text (required for method=create)'},
'page': {'type': 'number', 'description': 'Page number (optional, for method=get)'},
'size': {'type': 'number', 'description': 'Page size (optional, for method=get, default 10)'},
},
'required': ['method'],
},
),
]
# Helper functions
def simplify_test_case_status(data: Any) -> Any:
"""
Simplify test case status from object to string in the response.
Transforms status object {id, name, color, ...} to just the name string.
Works with both list responses (content array) and single test case responses.
"""
if isinstance(data, dict):
# Handle list response with content array
if 'content' in data and isinstance(data['content'], list):
for item in data['content']:
if isinstance(item, dict) and 'status' in item:
status = item.get('status')
if isinstance(status, dict) and 'name' in status:
item['status'] = status['name']
# Handle single test case response
elif 'status' in data:
status = data.get('status')
if isinstance(status, dict) and 'name' in status:
data['status'] = status['name']
return data
async def modify_test_case_custom_field_value_impl(
test_case_id: int,
project_id: str,
custom_field_id: int,
custom_field_value_id: Optional[int],
mode: str
) -> Any:
"""
Add or remove a custom field value to/from a test case.
Args:
test_case_id: Test case ID
project_id: Project ID
custom_field_id: Custom field ID
custom_field_value_id: Custom field value ID (required for add mode, ignored for delete mode)
mode: 'add' or 'delete'
Returns:
Result from update_test_case_custom_fields API call
"""
# Get current custom field values
current_cfv = await allure_client.get_test_case_custom_fields(test_case_id, project_id)
# Build the update payload from current values
custom_fields = []
if mode == 'add':
field_found = False
for cfv_item in current_cfv:
field_id = cfv_item.get('customField', {}).get('id')
values = cfv_item.get('values', [])
if field_id == custom_field_id:
field_found = True
# Add existing values
for value in values:
custom_fields.append({
"customField": {"id": field_id},
"id": value.get('id')
})
# Add new value if not already present
if not any(v.get('id') == custom_field_value_id for v in values):
custom_fields.append({
"customField": {"id": field_id},
"id": custom_field_value_id
})
else:
# Keep existing values for other fields
for value in values:
custom_fields.append({
"customField": {"id": field_id},
"id": value.get('id')
})
# If field was not found in current values, add it
if not field_found:
custom_fields.append({
"customField": {"id": custom_field_id},
"id": custom_field_value_id
})
elif mode == 'delete':
for cfv_item in current_cfv:
field_id = cfv_item.get('customField', {}).get('id')
values = cfv_item.get('values', [])
if field_id == custom_field_id:
# Skip all values for this field (effectively removing it)
pass
else:
# Keep existing values for other fields
for value in values:
custom_fields.append({
"customField": {"id": field_id},
"id": value.get('id')
})
return await allure_client.update_test_case_custom_fields(test_case_id, custom_fields)
# Create MCP server
server = Server("allure-testops-mcp")
@server.list_tools()
async def list_tools() -> List[Tool]:
"""Return list of available tools"""
return all_tools
@server.call_tool()
async def call_tool(name: str, arguments: Dict[str, Any]) -> List[TextContent]:
"""Handle tool calls"""
try:
# Test Cases - Combined operations
if name == 'test_cases':
method = arguments.get('method')
if method == 'list':
params = arguments.copy()
project_id = params.pop('project_id')
params.pop('method')
result = await allure_client.get_test_cases(project_id, params)
result = simplify_test_case_status(result)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'get':
test_case_id = arguments.get('id')
result = await allure_client.get_test_case(test_case_id)
result = simplify_test_case_status(result)
# Also fetch scenario (steps) for the test case
try:
scenario = await allure_client.get_test_case_scenario(test_case_id)
result['scenario'] = scenario
except Exception as e:
# If scenario fetch fails, add error info but don't fail the whole request
result['scenario'] = {"error": str(e)}
# Also fetch first page of comments for the test case
try:
comments = await allure_client.get_comments(test_case_id, page=0)
result['comments'] = comments
except Exception as e:
# If comments fetch fails, add error info but don't fail the whole request
result['comments'] = {"error": str(e)}
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'create':
project_id = arguments.get('project_id')
test_case = {k: v for k, v in arguments.items() if k not in ['method', 'project_id']}
result = await allure_client.create_test_case(project_id, test_case)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'update':
test_case_id = arguments.get('id')
update_data = {k: v for k, v in arguments.items() if k not in ['method', 'id']}
result = await allure_client.update_test_case(test_case_id, update_data)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'delete':
test_case_id = arguments.get('id')
await allure_client.delete_test_case(test_case_id)
return [TextContent(type="text", text=f"Test case {test_case_id} deleted successfully")]
else:
return [TextContent(type="text", text=f"Unknown method for test_cases: {method}")]
# Test Case Steps - Create, Update, Delete
elif name == 'test_case_steps':
method = arguments.get('method')
after_id = arguments.get('after_id')
before_id = arguments.get('before_id')
with_expected_result = arguments.get('with_expected_result', False)
# Validate mutually exclusive parameters
if after_id is not None and before_id is not None:
return [TextContent(type="text", text="Error: after_id and before_id are mutually exclusive. Provide only one or neither.")]
if method == 'create':
test_case_id = arguments.get('test_case_id')
if not test_case_id:
return [TextContent(type="text", text="Error: 'test_case_id' parameter is required for method=create")]
text = arguments.get('text')
if not text:
return [TextContent(type="text", text="Error: 'text' parameter is required for method=create")]
# Build body for the request
body = {
"testCaseId": test_case_id,
"bodyJson": {
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": text
}
]
}
]
}
}
# Create the step
result = await allure_client.create_test_case_step(
body=body,
after_id=after_id,
before_id=before_id,
with_expected_result=with_expected_result
)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'update':
step_id = arguments.get('step_id')
if not step_id:
return [TextContent(type="text", text="Error: 'step_id' parameter is required for method=update")]
text = arguments.get('text')
if not text:
return [TextContent(type="text", text="Error: 'text' parameter is required for method=update")]
# Build body for the request (without testCaseId)
body = {
"bodyJson": {
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": text
}
]
}
]
}
}
# Update the step
result = await allure_client.update_test_case_step(
step_id=step_id,
body=body,
after_id=after_id,
before_id=before_id,
with_expected_result=with_expected_result
)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'delete':
step_id = arguments.get('step_id')
if not step_id:
return [TextContent(type="text", text="Error: 'step_id' parameter is required for method=delete")]
await allure_client.delete_test_case_step(step_id)
return [TextContent(type="text", text=f"Step {step_id} deleted successfully")]
else:
return [TextContent(type="text", text=f"Unknown method for test_case_steps: {method}")]
# Launches - Combined operations
elif name == 'launches':
method = arguments.get('method')
if method == 'list':
params = arguments.copy()
project_id = params.pop('project_id')
params.pop('method')
result = await allure_client.get_launches(project_id, params)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'get':
launch_id = arguments.get('id')
result = await allure_client.get_launch(launch_id)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'create':
project_id = arguments.get('project_id')
launch = {k: v for k, v in arguments.items() if k not in ['method', 'project_id']}
result = await allure_client.create_launch(project_id, launch)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'update':
launch_id = arguments.get('id')
update_data = {k: v for k, v in arguments.items() if k not in ['method', 'id']}
result = await allure_client.update_launch(launch_id, update_data)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'delete':
launch_id = arguments.get('id')
await allure_client.delete_launch(launch_id)
return [TextContent(type="text", text=f"Launch {launch_id} deleted successfully")]
elif method == 'close':
launch_id = arguments.get('id')
result = await allure_client.close_launch(launch_id)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
else:
return [TextContent(type="text", text=f"Unknown method for launches: {method}")]
# Test Plans - Combined operations
elif name == 'test_plans':
method = arguments.get('method')
if method == 'list':
params = arguments.copy()
project_id = params.pop('project_id')
params.pop('method')
result = await allure_client.get_test_plans(project_id, params)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'get':
test_plan_id = arguments.get('id')
result = await allure_client.get_test_plan(test_plan_id)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'create':
project_id = arguments.get('project_id')
test_plan = {k: v for k, v in arguments.items() if k not in ['method', 'project_id']}
result = await allure_client.create_test_plan(project_id, test_plan)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'update':
test_plan_id = arguments.get('id')
update_data = {k: v for k, v in arguments.items() if k not in ['method', 'id']}
result = await allure_client.update_test_plan(test_plan_id, update_data)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'delete':
test_plan_id = arguments.get('id')
await allure_client.delete_test_plan(test_plan_id)
return [TextContent(type="text", text=f"Test plan {test_plan_id} deleted successfully")]
else:
return [TextContent(type="text", text=f"Unknown method for test_plans: {method}")]
# Special operations - Bulk CSV import
elif name == 'bulk_create_test_cases_from_csv':
project_id = arguments.get('project_id')
csv_content = arguments.get('csv_content')
test_cases = parse_test_cases_from_csv(csv_content)
results = await allure_client.bulk_create_test_cases(project_id, test_cases)
return [TextContent(type="text", text=json.dumps(results, indent=2))]
# Special operations - Custom Fields
elif name == 'test_case_custom_fields':
method = arguments.get('method')
if method == 'get':
test_case_id = arguments.get('test_case_id')
project_id = arguments.get('project_id')
result = await allure_client.get_test_case_custom_fields(test_case_id, project_id)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'modify':
result = await modify_test_case_custom_field_value_impl(
test_case_id=arguments.get('test_case_id'),
project_id=arguments.get('project_id'),
custom_field_id=arguments.get('custom_field_id'),
custom_field_value_id=arguments.get('custom_field_value_id'),
mode=arguments.get('mode')
)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
else:
return [TextContent(type="text", text=f"Unknown method for test_case_custom_fields: {method}")]
# Special operations - Get custom field values
elif name == 'get_custom_field_values':
project_id = arguments.get('project_id')
custom_field_id = arguments.get('custom_field_id')
size = arguments.get('size', 100)
page = arguments.get('page', 0)
result = await allure_client.get_custom_field_values(project_id, custom_field_id, size, page)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
# Special operations - Comments
elif name == 'test_case_comments':
method = arguments.get('method')
if method == 'get':
test_case_id = arguments.get('test_case_id')
if not test_case_id:
return [TextContent(type="text", text="Error: 'test_case_id' parameter is required for method=get")]
page = arguments.get('page')
size = arguments.get('size')
result = await allure_client.get_comments(test_case_id, page, size)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'create':
test_case_id = arguments.get('test_case_id')
if not test_case_id:
return [TextContent(type="text", text="Error: 'test_case_id' parameter is required for method=create")]
body = arguments.get('body')
if not body:
return [TextContent(type="text", text="Error: 'body' parameter is required for creating a comment")]
result = await allure_client.create_comment(test_case_id, body)
return [TextContent(type="text", text=json.dumps(result, indent=2))]
elif method == 'delete':
comment_id = arguments.get('comment_id')
if not comment_id:
return [TextContent(type="text", text="Error: 'comment_id' parameter is required for method=delete")]
await allure_client.delete_comment(comment_id)
return [TextContent(type="text", text=f"Comment {comment_id} deleted successfully")]
else:
return [TextContent(type="text", text=f"Unknown method for test_case_comments: {method}")]
else:
return [TextContent(type="text", text=f"Unknown tool: {name}")]
except Exception as error:
error_message = str(error)
# Try to extract more details from httpx errors
if hasattr(error, 'response') and hasattr(error.response, 'text'):
error_message += f"\n{error.response.text}"
return [TextContent(type="text", text=f"Error: {error_message}")]
async def main_stdio():
"""Main function to run the server in stdio mode"""
async with stdio_server() as (read_stream, write_stream):
await server.run(
read_stream,
write_stream,
server.create_initialization_options()
)
async def main_streamable_http(host: str, port: int):
"""Main function to run the server in Streamable HTTP mode
Uses one transport instance per session to support multiple concurrent clients.
Each session gets its own MCP server instance.
"""
import uvicorn
import uuid
from starlette.requests import Request
# Track transports and servers per session
sessions = {} # session_id -> (transport, server_task)
sessions_lock = asyncio.Lock()
async def get_or_create_session(scope, receive, send):
"""Get existing session or create a new one"""
request = Request(scope, receive)
# Try to get session ID from header
session_id = request.headers.get("mcp-session-id")
# If no session ID, generate a new one
if not session_id:
session_id = str(uuid.uuid4())
async with sessions_lock:
if session_id not in sessions:
# Create new transport for this session
transport = StreamableHTTPServerTransport(
mcp_session_id=session_id,
is_json_response_enabled=True
)
# Start MCP server for this session
async def run_session_server():
try:
async with transport.connect() as (read_stream, write_stream):
await server.run(
read_stream,
write_stream,
server.create_initialization_options(),
stateless=True
)
except asyncio.CancelledError:
pass
finally:
# Cleanup on server exit
async with sessions_lock:
sessions.pop(session_id, None)
server_task = asyncio.create_task(run_session_server())
sessions[session_id] = (transport, server_task)
# Give the server a moment to initialize
await asyncio.sleep(0.1)
return sessions[session_id][0]
async def asgi_app(scope, receive, send):
"""ASGI application handler"""
if scope["type"] == "lifespan":
while True:
message = await receive()
if message["type"] == "lifespan.startup":
await send({"type": "lifespan.startup.complete"})
elif message["type"] == "lifespan.shutdown":
# Cancel all session tasks
async with sessions_lock:
for transport, task in sessions.values():
task.cancel()
# Wait for all tasks to finish
if sessions:
await asyncio.gather(*[task for _, task in sessions.values()], return_exceptions=True)
await send({"type": "lifespan.shutdown.complete"})
return
elif scope["type"] == "http":
path = scope.get("path", "")
if path == "/health/liveness":
await send({
"type": "http.response.start",
"status": 200,
"headers": [[b"content-type", b"application/json"]],
})
await send({
"type": "http.response.body",
"body": json.dumps({"status": "alive"}).encode("utf-8"),
})
return
elif path == "/health/readiness":
status_code = 200
await send({
"type": "http.response.start",
"status": status_code,
"headers": [[b"content-type", b"application/json"]],
})
await send({
"type": "http.response.body",
"body": json.dumps({
"status": "ready",
"active_sessions": len(sessions),
}).encode("utf-8"),
})
return
# Get or create session and route to appropriate transport
transport = await get_or_create_session(scope, receive, send)
await transport.handle_request(scope, receive, send)
config = uvicorn.Config(
app=asgi_app,
host=host,
port=port,
log_level="info",
lifespan="on"
)
server_instance = uvicorn.Server(config)
await server_instance.serve()
if __name__ == "__main__":
print(f"Allure TestOps MCP Server", file=sys.stderr)
print(f"Transport: {MCP_TRANSPORT}", file=sys.stderr)
print(f"Connected to: {ALLURE_TESTOPS_URL}", file=sys.stderr)
print(f"Registered {len(all_tools)} tools", file=sys.stderr)
if MCP_TRANSPORT == 'stdio':
print("Running on stdio", file=sys.stderr)
asyncio.run(main_stdio())
elif MCP_TRANSPORT == 'streamable_http':
# Parse host:port from MCP_ADDRESS
if ':' in MCP_ADDRESS:
host, port_str = MCP_ADDRESS.rsplit(':', 1)
port = int(port_str)
else:
host = MCP_ADDRESS
port = 8000
print(f"Running on streamable_http at http://{host}:{port}", file=sys.stderr)
asyncio.run(main_streamable_http(host, port))
else:
print(f"Error: Unknown transport mode: {MCP_TRANSPORT}", file=sys.stderr)
print("Supported modes: stdio, streamable_http", file=sys.stderr)
print("Set MCP_TRANSPORT environment variable to 'stdio' or 'streamable_http'", file=sys.stderr)
sys.exit(1)