-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
646 lines (574 loc) · 21.2 KB
/
server.py
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
import os
import logging
import asyncio
from datetime import datetime
from functools import wraps
from typing import Dict, List, Optional, Any, Union
import time
import aiohttp
from atlassian import Confluence
from dotenv import load_dotenv
from pydantic import BaseModel, Field
from fastmcp import FastMCP
from ratelimit import limits, sleep_and_retry
from tenacity import retry, stop_after_attempt, wait_exponential
from pythonjsonlogger import jsonlogger
# Load environment variables
load_dotenv()
# Configure logging
logger = logging.getLogger(__name__)
logHandler = logging.StreamHandler()
formatter = jsonlogger.JsonFormatter(
fmt='%(asctime)s %(levelname)s %(name)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logHandler.setFormatter(formatter)
logger.addHandler(logHandler)
logger.setLevel(logging.INFO)
# Rate limiting configuration
CALLS_PER_MINUTE = 30
RETRY_ATTEMPTS = 3
RETRY_MIN_WAIT = 1
RETRY_MAX_WAIT = 10
# Input validation models
class ConfluenceBase(BaseModel):
confluence_url: str = Field(..., description="The Confluence instance URL")
access_token: str = Field(..., description="The API access token")
class SearchInput(ConfluenceBase):
query: str = Field(..., description="The search query string")
class SpaceInput(ConfluenceBase):
pass
class PageInput(ConfluenceBase):
space_key: str = Field(..., description="The space key where the page exists")
page_id: str = Field(..., description="The ID of the page")
class CreatePageInput(ConfluenceBase):
space_key: str = Field(..., description="The space key where to create the page")
title: str = Field(..., description="The title of the new page")
content: str = Field(..., description="The HTML content of the page")
class UpdatePageInput(PageInput):
content: str = Field(..., description="The new HTML content")
# Initialize MCP server
mcp = FastMCP("Confluence MCP")
# Decorator for rate limiting and retries
def rate_limited_retry(func):
@sleep_and_retry
@limits(calls=CALLS_PER_MINUTE, period=60)
@retry(
stop=stop_after_attempt(RETRY_ATTEMPTS),
wait=wait_exponential(multiplier=RETRY_MIN_WAIT, max=RETRY_MAX_WAIT)
)
@wraps(func)
async def wrapper(*args, **kwargs):
start_time = time.time()
try:
result = await func(*args, **kwargs)
duration = time.time() - start_time
logger.info(f"{func.__name__} completed", extra={
"function": func.__name__,
"duration": duration,
"status": "success"
})
return result
except Exception as e:
duration = time.time() - start_time
logger.error(f"{func.__name__} failed: {str(e)}", extra={
"function": func.__name__,
"duration": duration,
"error": str(e),
"status": "error"
})
raise
return wrapper
def create_confluence_client(url: str, token: str) -> Confluence:
"""Create and configure Confluence client with timeout and retry settings."""
return Confluence(
url=url,
username="pawan.kumar@arelis.digital",
password=token,
cloud=True,
timeout=30
)
@mcp.tool()
async def list_tools() -> Dict[str, Any]:
"""List all available MCP tools.
This tool provides information about all available Confluence operations,
including their parameters and expected return values.
Returns:
Dict containing list of available tools with their schemas
Example:
```python
tools = await list_tools()
for tool in tools['tools']:
print(f"Tool: {tool['name']}")
print(f"Description: {tool['description']}")
```
"""
return {
"tools": [
{
"name": "confluence_search",
"description": "Search for content in Confluence using CQL",
"inputSchema": SearchInput.model_json_schema(),
"examples": [
{
"description": "Search for pages containing 'architecture'",
"code": 'await confluence_search(confluence_url="https://your-domain.atlassian.net/wiki", access_token="your-token", query="architecture")'
}
]
},
{
"name": "confluence_get_spaces",
"description": "Get list of available Confluence spaces",
"inputSchema": SpaceInput.model_json_schema(),
"examples": [
{
"description": "List all available spaces",
"code": 'await confluence_get_spaces(confluence_url="https://your-domain.atlassian.net/wiki", access_token="your-token")'
}
]
},
{
"name": "confluence_get_page",
"description": "Get detailed content of a Confluence page",
"inputSchema": PageInput.model_json_schema(),
"examples": [
{
"description": "Get page content by ID",
"code": 'await confluence_get_page(confluence_url="https://your-domain.atlassian.net/wiki", access_token="your-token", space_key="SPACE", page_id="123456")'
}
]
},
{
"name": "confluence_create_page",
"description": "Create a new page in Confluence",
"inputSchema": CreatePageInput.model_json_schema(),
"examples": [
{
"description": "Create a new page",
"code": 'await confluence_create_page(confluence_url="https://your-domain.atlassian.net/wiki", access_token="your-token", space_key="SPACE", title="New Page", content="<p>Hello World</p>")'
}
]
},
{
"name": "confluence_update_page",
"description": "Update content of an existing Confluence page",
"inputSchema": UpdatePageInput.model_json_schema(),
"examples": [
{
"description": "Update page content",
"code": 'await confluence_update_page(confluence_url="https://your-domain.atlassian.net/wiki", access_token="your-token", space_key="SPACE", page_id="123456", content="<p>Updated content</p>")'
}
]
},
{
"name": "confluence_delete_page",
"description": "Delete a Confluence page",
"inputSchema": PageInput.model_json_schema(),
"examples": [
{
"description": "Delete a page",
"code": 'await confluence_delete_page(confluence_url="https://your-domain.atlassian.net/wiki", access_token="your-token", space_key="SPACE", page_id="123456")'
}
]
}
]
}
@mcp.tool()
@rate_limited_retry
async def confluence_search(confluence_url: str, access_token: str, query: str) -> Dict[str, Any]:
"""Search for content in Confluence using CQL.
This tool searches Confluence content using the provided query string.
The search uses Confluence Query Language (CQL) for advanced searching capabilities.
Args:
confluence_url: The Confluence instance URL
access_token: The API access token
query: The search query string
Returns:
Dict containing search results with titles, URLs and metadata
Example:
```python
results = await confluence_search(
confluence_url="https://your-domain.atlassian.net/wiki",
access_token="your-token",
query="project in (SD)"
)
```
"""
try:
# Validate input
input_data = SearchInput(
confluence_url=confluence_url,
access_token=access_token,
query=query
)
# Create client
confluence = create_confluence_client(input_data.confluence_url, input_data.access_token)
# Execute search with progress logging
logger.info(f"Executing search query: {input_data.query}")
results = confluence.cql(f'text ~ "{input_data.query}"')
formatted_results = []
for result in results.get('results', []):
space_key = result.get('space', {}).get('key', '')
content_id = result.get('content', {}).get('id', '')
title = result.get('content', {}).get('title', '')
formatted_results.append({
"id": f"confluence://{space_key}/{content_id}",
"title": title,
"space_key": space_key,
"content_id": content_id,
"type": result.get('content', {}).get('type', ''),
"url": result.get('_links', {}).get('webui', '')
})
logger.info(f"Search completed, found {len(formatted_results)} results")
return {
"content": [
{
"type": "text",
"text": f"Found {len(formatted_results)} results"
},
{
"type": "json",
"data": {"results": formatted_results}
}
]
}
except Exception as e:
logger.error(f"Search failed: {str(e)}")
return {
"isError": True,
"content": [
{
"type": "text",
"text": f"Error: {str(e)}"
}
]
}
@mcp.tool()
@rate_limited_retry
async def confluence_get_spaces(confluence_url: str, access_token: str) -> Dict[str, Any]:
"""Get list of available Confluence spaces.
This tool retrieves all accessible Confluence spaces for the authenticated user.
Args:
confluence_url: The Confluence instance URL
access_token: The API access token
Returns:
Dict containing list of spaces with their details
Example:
```python
spaces = await confluence_get_spaces(
confluence_url="https://your-domain.atlassian.net/wiki",
access_token="your-token"
)
```
"""
try:
# Validate input
input_data = SpaceInput(
confluence_url=confluence_url,
access_token=access_token
)
# Create client
confluence = create_confluence_client(input_data.confluence_url, input_data.access_token)
# Get spaces with progress logging
logger.info("Retrieving Confluence spaces")
spaces_response = confluence.get_all_spaces()
spaces = spaces_response.get('results', [])
formatted_spaces = []
for space in spaces:
space_key = space.get('key', '')
formatted_spaces.append({
"id": f"confluence://{space_key}",
"key": space_key,
"name": space.get('name', ''),
"type": space.get('type', ''),
"url": space.get('_links', {}).get('webui', ''),
"description": space.get('description', {}).get('plain', {}).get('value', '')
})
logger.info(f"Retrieved {len(formatted_spaces)} spaces")
return {
"content": [
{
"type": "text",
"text": f"Found {len(formatted_spaces)} spaces"
},
{
"type": "json",
"data": {"spaces": formatted_spaces}
}
]
}
except Exception as e:
logger.error(f"Failed to get spaces: {str(e)}")
return {
"isError": True,
"content": [
{
"type": "text",
"text": f"Error: {str(e)}"
}
]
}
@mcp.tool()
@rate_limited_retry
async def confluence_get_page(confluence_url: str, access_token: str, space_key: str, page_id: str) -> Dict[str, Any]:
"""Get detailed content of a Confluence page.
This tool retrieves the complete content and metadata of a specific Confluence page.
Args:
confluence_url: The Confluence instance URL
access_token: The API access token
space_key: The space key where the page exists
page_id: The ID of the page to retrieve
Returns:
Dict containing page content and metadata
Example:
```python
page = await confluence_get_page(
confluence_url="https://your-domain.atlassian.net/wiki",
access_token="your-token",
space_key="SPACE",
page_id="123456"
)
```
"""
try:
# Validate input
input_data = PageInput(
confluence_url=confluence_url,
access_token=access_token,
space_key=space_key,
page_id=page_id
)
# Create client
confluence = create_confluence_client(input_data.confluence_url, input_data.access_token)
# Get page with progress logging
logger.info(f"Retrieving page {input_data.page_id} from space {input_data.space_key}")
content = confluence.get_page_by_id(
input_data.page_id,
expand='body.storage,version,space'
)
page_data = {
"title": content.get('title', ''),
"content": content.get('body', {}).get('storage', {}).get('value', ''),
"version": content.get('version', {}).get('number', 1),
"space_key": content.get('space', {}).get('key', ''),
"last_modified": content.get('version', {}).get('when', ''),
"author": content.get('version', {}).get('by', {}).get('displayName', '')
}
logger.info(f"Retrieved page: {page_data['title']}")
return {
"content": [
{
"type": "text",
"text": f"Retrieved page: {page_data['title']}"
},
{
"type": "json",
"data": page_data
}
]
}
except Exception as e:
logger.error(f"Failed to get page: {str(e)}")
return {
"isError": True,
"content": [
{
"type": "text",
"text": f"Error: {str(e)}"
}
]
}
@mcp.tool()
@rate_limited_retry
async def confluence_create_page(confluence_url: str, access_token: str, space_key: str, title: str, content: str) -> Dict[str, Any]:
"""Create a new page in Confluence.
This tool creates a new page in the specified Confluence space.
Args:
confluence_url: The Confluence instance URL
access_token: The API access token
space_key: The space key where to create the page
title: The title of the new page
content: The HTML content of the page
Returns:
Dict containing the created page details
Example:
```python
new_page = await confluence_create_page(
confluence_url="https://your-domain.atlassian.net/wiki",
access_token="your-token",
space_key="SPACE",
title="New Page",
content="<p>Hello World</p>"
)
```
"""
try:
# Validate input
input_data = CreatePageInput(
confluence_url=confluence_url,
access_token=access_token,
space_key=space_key,
title=title,
content=content
)
# Create client
confluence = create_confluence_client(input_data.confluence_url, input_data.access_token)
# Create page with progress logging
logger.info(f"Creating page '{input_data.title}' in space {input_data.space_key}")
page = confluence.create_page(
space=input_data.space_key,
title=input_data.title,
body=input_data.content
)
page_data = {
"id": f"confluence://{input_data.space_key}/{page['id']}",
"title": page['title'],
"space_key": input_data.space_key,
"content_id": page['id'],
"url": page.get('_links', {}).get('webui', '')
}
logger.info(f"Created page: {page_data['title']}")
return {
"content": [
{
"type": "text",
"text": f"Created page: {page_data['title']}"
},
{
"type": "json",
"data": page_data
}
]
}
except Exception as e:
logger.error(f"Failed to create page: {str(e)}")
return {
"isError": True,
"content": [
{
"type": "text",
"text": f"Error: {str(e)}"
}
]
}
@mcp.tool()
@rate_limited_retry
async def confluence_update_page(confluence_url: str, access_token: str, space_key: str, page_id: str, content: str) -> Dict[str, Any]:
"""Update content of an existing Confluence page.
This tool updates the content of an existing page while preserving its metadata.
Args:
confluence_url: The Confluence instance URL
access_token: The API access token
space_key: The space key where the page exists
page_id: The ID of the page to update
content: The new HTML content
Returns:
Dict indicating success or failure
Example:
```python
result = await confluence_update_page(
confluence_url="https://your-domain.atlassian.net/wiki",
access_token="your-token",
space_key="SPACE",
page_id="123456",
content="<p>Updated content</p>"
)
```
"""
try:
# Validate input
input_data = UpdatePageInput(
confluence_url=confluence_url,
access_token=access_token,
space_key=space_key,
page_id=page_id,
content=content
)
# Create client
confluence = create_confluence_client(input_data.confluence_url, input_data.access_token)
# Update page with progress logging
logger.info(f"Updating page {input_data.page_id} in space {input_data.space_key}")
page = confluence.get_page_by_id(input_data.page_id)
confluence.update_page(
page_id=input_data.page_id,
title=page['title'],
body=input_data.content,
minor_edit=True
)
logger.info(f"Updated page: {page['title']}")
return {
"content": [
{
"type": "text",
"text": "Page updated successfully"
}
]
}
except Exception as e:
logger.error(f"Failed to update page: {str(e)}")
return {
"isError": True,
"content": [
{
"type": "text",
"text": f"Error: {str(e)}"
}
]
}
@mcp.tool()
@rate_limited_retry
async def confluence_delete_page(confluence_url: str, access_token: str, space_key: str, page_id: str) -> Dict[str, Any]:
"""Delete a Confluence page.
This tool permanently deletes a page from Confluence.
Args:
confluence_url: The Confluence instance URL
access_token: The API access token
space_key: The space key where the page exists
page_id: The ID of the page to delete
Returns:
Dict indicating success or failure
Example:
```python
result = await confluence_delete_page(
confluence_url="https://your-domain.atlassian.net/wiki",
access_token="your-token",
space_key="SPACE",
page_id="123456"
)
```
"""
try:
# Validate input
input_data = PageInput(
confluence_url=confluence_url,
access_token=access_token,
space_key=space_key,
page_id=page_id
)
# Create client
confluence = create_confluence_client(input_data.confluence_url, input_data.access_token)
# Delete page with progress logging
logger.info(f"Deleting page {input_data.page_id} from space {input_data.space_key}")
confluence.remove_page(input_data.page_id)
logger.info("Page deleted successfully")
return {
"content": [
{
"type": "text",
"text": "Page deleted successfully"
}
]
}
except Exception as e:
logger.error(f"Failed to delete page: {str(e)}")
return {
"isError": True,
"content": [
{
"type": "text",
"text": f"Error: {str(e)}"
}
]
}
if __name__ == "__main__":
mcp.run()