forked from georgeantonopoulos/Basecamp-MCP-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server_cli.py
More file actions
executable file
·1520 lines (1427 loc) · 64 KB
/
Copy pathmcp_server_cli.py
File metadata and controls
executable file
·1520 lines (1427 loc) · 64 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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Command-line MCP server for Basecamp integration with Cursor.
This server implements the MCP (Model Context Protocol) via stdin/stdout
as expected by Cursor.
"""
import json
import sys
import logging
from typing import Any, Dict, List, Optional
from basecamp_client import BasecampClient
from search_utils import BasecampSearch
import token_storage
import auth_manager
import os
from dotenv import load_dotenv
# Determine project root (directory containing this script)
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
# Explicitly load .env from the project root
DOTENV_PATH = os.path.join(PROJECT_ROOT, '.env')
load_dotenv(DOTENV_PATH)
# Log file in the project directory
LOG_FILE_PATH = os.path.join(PROJECT_ROOT, 'mcp_cli_server.log')
# Set up logging to file AND stderr
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(LOG_FILE_PATH),
logging.StreamHandler(sys.stderr) # Added StreamHandler for stderr
]
)
logger = logging.getLogger('mcp_cli_server')
class MCPServer:
"""MCP server implementing the Model Context Protocol for Cursor."""
def __init__(self):
self.tools = self._get_available_tools()
logger.info("MCP CLI Server initialized")
def _get_available_tools(self) -> List[Dict[str, Any]]:
"""Get list of available tools for Basecamp."""
return [
{
"name": "get_projects",
"description": "Get all Basecamp projects",
"inputSchema": {
"type": "object",
"properties": {},
"required": []
}
},
{
"name": "get_project",
"description": "Get details for a specific project",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"}
},
"required": ["project_id"]
}
},
{
"name": "get_todolists",
"description": "Get todo lists for a project",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"}
},
"required": ["project_id"]
}
},
{
"name": "get_todos",
"description": "Get todos from a todo list",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "Project ID"},
"todolist_id": {"type": "string", "description": "The todo list ID"},
},
"required": ["project_id", "todolist_id"]
}
},
{
"name": "create_todo",
"description": "Create a new todo item in a todo list",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "Project ID"},
"todolist_id": {"type": "string", "description": "The todo list ID"},
"content": {"type": "string", "description": "The todo item's text (required)"},
"description": {"type": "string", "description": "HTML description of the todo"},
"assignee_ids": {"type": "array", "items": {"type": "string"}, "description": "List of person IDs to assign"},
"completion_subscriber_ids": {"type": "array", "items": {"type": "string"}, "description": "List of person IDs to notify on completion"},
"notify": {"type": "boolean", "description": "Whether to notify assignees"},
"due_on": {"type": "string", "description": "Due date in YYYY-MM-DD format"},
"starts_on": {"type": "string", "description": "Start date in YYYY-MM-DD format"}
},
"required": ["project_id", "todolist_id", "content"]
}
},
{
"name": "update_todo",
"description": "Update an existing todo item",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "Project ID"},
"todo_id": {"type": "string", "description": "The todo ID"},
"content": {"type": "string", "description": "The todo item's text"},
"description": {"type": "string", "description": "HTML description of the todo"},
"assignee_ids": {"type": "array", "items": {"type": "string"}, "description": "List of person IDs to assign"},
"completion_subscriber_ids": {"type": "array", "items": {"type": "string"}, "description": "List of person IDs to notify on completion"},
"notify": {"type": "boolean", "description": "Whether to notify assignees"},
"due_on": {"type": "string", "description": "Due date in YYYY-MM-DD format"},
"starts_on": {"type": "string", "description": "Start date in YYYY-MM-DD format"}
},
"required": ["project_id", "todo_id"]
}
},
{
"name": "delete_todo",
"description": "Delete a todo item",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "Project ID"},
"todo_id": {"type": "string", "description": "The todo ID"}
},
"required": ["project_id", "todo_id"]
}
},
{
"name": "complete_todo",
"description": "Mark a todo item as complete",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "Project ID"},
"todo_id": {"type": "string", "description": "The todo ID"}
},
"required": ["project_id", "todo_id"]
}
},
{
"name": "uncomplete_todo",
"description": "Mark a todo item as incomplete",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "Project ID"},
"todo_id": {"type": "string", "description": "The todo ID"}
},
"required": ["project_id", "todo_id"]
}
},
{
"name": "search_basecamp",
"description": "Search across Basecamp projects, todos, and messages",
"inputSchema": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query"},
"project_id": {"type": "string", "description": "Optional project ID to limit search scope"}
},
"required": ["query"]
}
},
{
"name": "global_search",
"description": "Search projects, todos and campfire messages across all projects",
"inputSchema": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query"}
},
"required": ["query"]
}
},
{
"name": "get_comments",
"description": "Get comments for a Basecamp item",
"inputSchema": {
"type": "object",
"properties": {
"recording_id": {"type": "string", "description": "The item ID"},
"project_id": {"type": "string", "description": "The project ID"},
"page": {"type": "integer", "description": "Page number for pagination (default: 1). Basecamp uses geared pagination: page 1 has 15 results, page 2 has 30, page 3 has 50, page 4+ has 100.", "default": 1}
},
"required": ["recording_id", "project_id"]
}
},
{
"name": "create_comment",
"description": "Create a comment on a Basecamp item",
"inputSchema": {
"type": "object",
"properties": {
"recording_id": {"type": "string", "description": "The item ID"},
"project_id": {"type": "string", "description": "The project ID"},
"content": {"type": "string", "description": "The comment content in HTML format"}
},
"required": ["recording_id", "project_id", "content"]
}
},
{
"name": "get_campfire_lines",
"description": "Get recent messages from a Basecamp campfire (chat room)",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"campfire_id": {"type": "string", "description": "The campfire/chat room ID"}
},
"required": ["project_id", "campfire_id"]
}
},
{
"name": "get_daily_check_ins",
"description": "Get project's daily checking questionnaire",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"page": {"type": "integer", "description": "Page number paginated response"}
}
},
"required": ["project_id"]
},
{
"name": "get_question_answers",
"description": "Get answers on daily check-in question",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"question_id": {"type": "string", "description": "The question ID"},
"page": {"type": "integer", "description": "Page number paginated response"}
}
},
"required": ["project_id", "question_id"]
},
# Card Table tools
{
"name": "get_card_tables",
"description": "Get all card tables for a project",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"}
},
"required": ["project_id"]
}
},
{
"name": "get_card_table",
"description": "Get the card table details for a project",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"}
},
"required": ["project_id"]
}
},
{
"name": "get_columns",
"description": "Get all columns in a card table",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"card_table_id": {"type": "string", "description": "The card table ID"}
},
"required": ["project_id", "card_table_id"]
}
},
{
"name": "get_column",
"description": "Get details for a specific column",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"column_id": {"type": "string", "description": "The column ID"}
},
"required": ["project_id", "column_id"]
}
},
{
"name": "create_column",
"description": "Create a new column in a card table",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"card_table_id": {"type": "string", "description": "The card table ID"},
"title": {"type": "string", "description": "The column title"}
},
"required": ["project_id", "card_table_id", "title"]
}
},
{
"name": "update_column",
"description": "Update a column title",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"column_id": {"type": "string", "description": "The column ID"},
"title": {"type": "string", "description": "The new column title"}
},
"required": ["project_id", "column_id", "title"]
}
},
{
"name": "move_column",
"description": "Move a column to a new position",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"card_table_id": {"type": "string", "description": "The card table ID"},
"column_id": {"type": "string", "description": "The column ID"},
"position": {"type": "integer", "description": "The new 1-based position"}
},
"required": ["project_id", "card_table_id", "column_id", "position"]
}
},
{
"name": "update_column_color",
"description": "Update a column color",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"column_id": {"type": "string", "description": "The column ID"},
"color": {"type": "string", "description": "The hex color code (e.g., #FF0000)"}
},
"required": ["project_id", "column_id", "color"]
}
},
{
"name": "put_column_on_hold",
"description": "Put a column on hold (freeze work)",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"column_id": {"type": "string", "description": "The column ID"}
},
"required": ["project_id", "column_id"]
}
},
{
"name": "remove_column_hold",
"description": "Remove hold from a column (unfreeze work)",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"column_id": {"type": "string", "description": "The column ID"}
},
"required": ["project_id", "column_id"]
}
},
{
"name": "watch_column",
"description": "Subscribe to notifications for changes in a column",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"column_id": {"type": "string", "description": "The column ID"}
},
"required": ["project_id", "column_id"]
}
},
{
"name": "unwatch_column",
"description": "Unsubscribe from notifications for a column",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"column_id": {"type": "string", "description": "The column ID"}
},
"required": ["project_id", "column_id"]
}
},
{
"name": "get_cards",
"description": "Get all cards in a column",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"column_id": {"type": "string", "description": "The column ID"}
},
"required": ["project_id", "column_id"]
}
},
{
"name": "get_card",
"description": "Get details for a specific card",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"card_id": {"type": "string", "description": "The card ID"}
},
"required": ["project_id", "card_id"]
}
},
{
"name": "create_card",
"description": "Create a new card in a column",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"column_id": {"type": "string", "description": "The column ID"},
"title": {"type": "string", "description": "The card title"},
"content": {"type": "string", "description": "Optional card content/description"},
"due_on": {"type": "string", "description": "Optional due date (ISO 8601 format)"},
"notify": {"type": "boolean", "description": "Whether to notify assignees (default: false)"}
},
"required": ["project_id", "column_id", "title"]
}
},
{
"name": "update_card",
"description": "Update a card",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"card_id": {"type": "string", "description": "The card ID"},
"title": {"type": "string", "description": "The new card title"},
"content": {"type": "string", "description": "The new card content/description"},
"due_on": {"type": "string", "description": "Due date (ISO 8601 format)"},
"assignee_ids": {"type": "array", "items": {"type": "string"}, "description": "Array of person IDs to assign to the card"}
},
"required": ["project_id", "card_id"]
}
},
{
"name": "move_card",
"description": "Move a card to a new column",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"card_id": {"type": "string", "description": "The card ID"},
"column_id": {"type": "string", "description": "The destination column ID"}
},
"required": ["project_id", "card_id", "column_id"]
}
},
{
"name": "complete_card",
"description": "Mark a card as complete",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"card_id": {"type": "string", "description": "The card ID"}
},
"required": ["project_id", "card_id"]
}
},
{
"name": "uncomplete_card",
"description": "Mark a card as incomplete",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"card_id": {"type": "string", "description": "The card ID"}
},
"required": ["project_id", "card_id"]
}
},
{
"name": "get_card_steps",
"description": "Get all steps (sub-tasks) for a card",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"card_id": {"type": "string", "description": "The card ID"}
},
"required": ["project_id", "card_id"]
}
},
{
"name": "create_card_step",
"description": "Create a new step (sub-task) for a card",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"card_id": {"type": "string", "description": "The card ID"},
"title": {"type": "string", "description": "The step title"},
"due_on": {"type": "string", "description": "Optional due date (ISO 8601 format)"},
"assignee_ids": {"type": "array", "items": {"type": "string"}, "description": "Array of person IDs to assign to the step"}
},
"required": ["project_id", "card_id", "title"]
}
},
{
"name": "get_card_step",
"description": "Get details for a specific card step",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"step_id": {"type": "string", "description": "The step ID"}
},
"required": ["project_id", "step_id"]
}
},
{
"name": "update_card_step",
"description": "Update a card step",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"step_id": {"type": "string", "description": "The step ID"},
"title": {"type": "string", "description": "The step title"},
"due_on": {"type": "string", "description": "Due date (ISO 8601 format)"},
"assignee_ids": {"type": "array", "items": {"type": "string"}, "description": "Array of person IDs to assign to the step"}
},
"required": ["project_id", "step_id"]
}
},
{
"name": "delete_card_step",
"description": "Delete a card step",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"step_id": {"type": "string", "description": "The step ID"}
},
"required": ["project_id", "step_id"]
}
},
{
"name": "complete_card_step",
"description": "Mark a card step as complete",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"step_id": {"type": "string", "description": "The step ID"}
},
"required": ["project_id", "step_id"]
}
},
{
"name": "uncomplete_card_step",
"description": "Mark a card step as incomplete",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "The project ID"},
"step_id": {"type": "string", "description": "The step ID"}
},
"required": ["project_id", "step_id"]
}
},
{
"name": "create_attachment",
"description": "Upload a file as an attachment",
"inputSchema": {
"type": "object",
"properties": {
"file_path": {"type": "string", "description": "Local path to file"},
"name": {"type": "string", "description": "Filename for Basecamp"},
"content_type": {"type": "string", "description": "MIME type"}
},
"required": ["file_path", "name"]
}
},
{
"name": "get_events",
"description": "Get events for a recording",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "Project ID"},
"recording_id": {"type": "string", "description": "Recording ID"}
},
"required": ["project_id", "recording_id"]
}
},
{
"name": "get_webhooks",
"description": "List webhooks for a project",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "Project ID"}
},
"required": ["project_id"]
}
},
{
"name": "create_webhook",
"description": "Create a webhook",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "Project ID"},
"payload_url": {"type": "string", "description": "Payload URL"},
"types": {"type": "array", "items": {"type": "string"}, "description": "Event types"}
},
"required": ["project_id", "payload_url"]
}
},
{
"name": "delete_webhook",
"description": "Delete a webhook",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "Project ID"},
"webhook_id": {"type": "string", "description": "Webhook ID"}
},
"required": ["project_id", "webhook_id"]
}
},
{
"name": "get_documents",
"description": "List documents in a vault",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "Project ID"},
"vault_id": {"type": "string", "description": "Vault ID"}
},
"required": ["project_id", "vault_id"]
}
},
{
"name": "get_document",
"description": "Get a single document",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "Project ID"},
"document_id": {"type": "string", "description": "Document ID"}
},
"required": ["project_id", "document_id"]
}
},
{
"name": "create_document",
"description": "Create a document in a vault",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "Project ID"},
"vault_id": {"type": "string", "description": "Vault ID"},
"title": {"type": "string", "description": "Document title"},
"content": {"type": "string", "description": "Document HTML content"}
},
"required": ["project_id", "vault_id", "title", "content"]
}
},
{
"name": "update_document",
"description": "Update a document",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "Project ID"},
"document_id": {"type": "string", "description": "Document ID"},
"title": {"type": "string", "description": "New title"},
"content": {"type": "string", "description": "New HTML content"}
},
"required": ["project_id", "document_id"]
}
},
{
"name": "trash_document",
"description": "Move a document to trash",
"inputSchema": {
"type": "object",
"properties": {
"project_id": {"type": "string", "description": "Project ID"},
"document_id": {"type": "string", "description": "Document ID"}
},
"required": ["project_id", "document_id"]
}
}
]
def _get_basecamp_client(self) -> Optional[BasecampClient]:
"""Get authenticated Basecamp client."""
try:
token_data = token_storage.get_token()
logger.debug(f"Token data retrieved: {token_data}")
if not token_data or not token_data.get('access_token'):
logger.error("No OAuth token available")
return None
# Check and automatically refresh if token is expired
if not auth_manager.ensure_authenticated():
logger.error("OAuth token has expired and automatic refresh failed")
return None
# Get fresh token data after potential refresh
token_data = token_storage.get_token()
# Get account_id from token data first, then fall back to env var
account_id = token_data.get('account_id') or os.getenv('BASECAMP_ACCOUNT_ID')
# Set a default user agent if none is provided
user_agent = os.getenv('USER_AGENT') or "Basecamp MCP Server (cursor@example.com)"
if not account_id:
logger.error(f"Missing account_id. Token data: {token_data}, Env BASECAMP_ACCOUNT_ID: {os.getenv('BASECAMP_ACCOUNT_ID')}")
return None
logger.debug(f"Creating Basecamp client with account_id: {account_id}, user_agent: {user_agent}")
return BasecampClient(
access_token=token_data['access_token'],
account_id=account_id,
user_agent=user_agent,
auth_mode='oauth'
)
except Exception as e:
logger.error(f"Error creating Basecamp client: {e}")
return None
def handle_request(self, request: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Handle an MCP request."""
method = request.get("method")
# Normalize method name for cursor compatibility
method_lower = method.lower() if isinstance(method, str) else ''
params = request.get("params", {})
request_id = request.get("id")
logger.info(f"Handling request: {method}")
try:
if method_lower == "initialize":
return {
"jsonrpc": "2.0",
"id": request_id,
"result": {
"protocolVersion": "2024-11-05",
"capabilities": {
"tools": {}
},
"serverInfo": {
"name": "basecamp-mcp-server",
"version": "1.0.0"
}
}
}
elif method_lower == "initialized":
# This is a notification, no response needed
logger.info("Received initialized notification")
return None
elif method_lower in ("tools/list", "listtools"):
return {
"jsonrpc": "2.0",
"id": request_id,
"result": {
"tools": self.tools
}
}
elif method_lower in ("tools/call", "toolscall"):
tool_name = params.get("name")
arguments = params.get("arguments", {})
result = self._execute_tool(tool_name, arguments)
return {
"jsonrpc": "2.0",
"id": request_id,
"result": {
"content": [
{
"type": "text",
"text": json.dumps(result, indent=2)
}
]
}
}
elif method_lower in ("listofferings", "list_offerings", "loffering"):
# Respond to Cursor's ListOfferings UI request
offerings = []
for tool in self.tools:
offerings.append({
"name": tool.get("name"),
"displayName": tool.get("name"),
"description": tool.get("description")
})
return {
"jsonrpc": "2.0",
"id": request_id,
"result": {
"offerings": offerings
}
}
elif method_lower == "ping":
# Handle ping requests
return {
"jsonrpc": "2.0",
"id": request_id,
"result": {}
}
else:
return {
"jsonrpc": "2.0",
"id": request_id,
"error": {
"code": -32601,
"message": f"Method not found: {method}"
}
}
except Exception as e:
logger.error(f"Error handling request: {e}")
return {
"jsonrpc": "2.0",
"id": request_id,
"error": {
"code": -32603,
"message": f"Internal error: {str(e)}"
}
}
def _execute_tool(self, tool_name: str, arguments: Dict[str, Any]) -> Dict[str, Any]:
"""Execute a tool and return the result."""
client = self._get_basecamp_client()
if not client:
# Check if it's specifically a token expiration issue
if token_storage.is_token_expired():
return {
"error": "OAuth token expired",
"message": "Your Basecamp OAuth token has expired. Please re-authenticate by visiting http://localhost:8000 and completing the OAuth flow again."
}
else:
return {
"error": "Authentication required",
"message": "Please authenticate with Basecamp first. Visit http://localhost:8000 to log in."
}
try:
if tool_name == "get_projects":
projects = client.get_projects()
return {
"status": "success",
"projects": projects,
"count": len(projects)
}
elif tool_name == "get_project":
project_id = arguments.get("project_id")
project = client.get_project(project_id)
return {
"status": "success",
"project": project
}
elif tool_name == "get_todolists":
project_id = arguments.get("project_id")
todolists = client.get_todolists(project_id)
return {
"status": "success",
"todolists": todolists,
"count": len(todolists)
}
elif tool_name == "get_todos":
todolist_id = arguments.get("todolist_id")
project_id = arguments.get("project_id")
todos = client.get_todos(project_id, todolist_id)
return {
"status": "success",
"todos": todos,
"count": len(todos)
}
elif tool_name == "create_todo":
project_id = arguments.get("project_id")
todolist_id = arguments.get("todolist_id")
content = arguments.get("content")
description = arguments.get("description")
assignee_ids = arguments.get("assignee_ids")
completion_subscriber_ids = arguments.get("completion_subscriber_ids")
notify_arg = arguments.get("notify", False)
if isinstance(notify_arg, str):
notify = notify_arg.strip().lower() in ("1", "true", "yes", "on")
else:
notify = bool(notify_arg)
due_on = arguments.get("due_on")
starts_on = arguments.get("starts_on")
todo = client.create_todo(
project_id, todolist_id, content,
description=description,
assignee_ids=assignee_ids,
completion_subscriber_ids=completion_subscriber_ids,
notify=notify,
due_on=due_on,
starts_on=starts_on
)
return {
"status": "success",
"todo": todo,
"message": f"Todo '{content}' created successfully"
}
elif tool_name == "update_todo":
project_id = arguments.get("project_id")
todo_id = arguments.get("todo_id")
content = arguments.get("content")
description = arguments.get("description")
assignee_ids = arguments.get("assignee_ids")
completion_subscriber_ids = arguments.get("completion_subscriber_ids")
due_on = arguments.get("due_on")
starts_on = arguments.get("starts_on")
notify = arguments.get("notify")
todo = client.update_todo(
project_id, todo_id,
content=content,
description=description,
assignee_ids=assignee_ids,
completion_subscriber_ids=completion_subscriber_ids,
notify=notify,
due_on=due_on,
starts_on=starts_on
)
return {
"status": "success",
"todo": todo,
"message": "Todo updated successfully"
}
elif tool_name == "delete_todo":
project_id = arguments.get("project_id")
todo_id = arguments.get("todo_id")
client.delete_todo(project_id, todo_id)
return {
"status": "success",
"message": "Todo deleted successfully"
}
elif tool_name == "complete_todo":
project_id = arguments.get("project_id")
todo_id = arguments.get("todo_id")
completion = client.complete_todo(project_id, todo_id)
return {
"status": "success",
"completion": completion,
"message": "Todo marked as complete"
}
elif tool_name == "uncomplete_todo":
project_id = arguments.get("project_id")
todo_id = arguments.get("todo_id")
client.uncomplete_todo(project_id, todo_id)
return {
"status": "success",
"message": "Todo marked as incomplete"
}
elif tool_name == "search_basecamp":
query = arguments.get("query")
project_id = arguments.get("project_id")
search = BasecampSearch(client=client)
results = {}
if project_id: