forked from IBM/mcp-context-forge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
5498 lines (4887 loc) · 247 KB
/
Makefile
File metadata and controls
5498 lines (4887 loc) · 247 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
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 🐍 MCP CONTEXT FORGE - Makefile
# (An enterprise-ready Model Context Protocol Gateway)
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
#
# Authors: Mihai Criveti, Manav Gupta
# Description: Build & automation helpers for the MCP Gateway project
# Usage: run `make` or `make help` to view available targets
#
# help: 🐍 MCP CONTEXT FORGE (An enterprise-ready Model Context Protocol Gateway)
#
# ──────────────────────────────────────────────────────────────────────────
SHELL := /bin/bash
.SHELLFLAGS := -eu -o pipefail -c
# Read values from .env.make
-include .env.make
# Rust build configuration (set to 1 to enable Rust builds, 0 to disable)
# Default is disabled to avoid requiring Rust toolchain for standard builds
ENABLE_RUST_BUILD ?= 0
# Project variables
PROJECT_NAME = mcpgateway
DOCS_DIR = docs
HANDSDOWN_PARAMS = -o $(DOCS_DIR)/ -n $(PROJECT_NAME) --name "MCP Gateway" --cleanup
TEST_DOCS_DIR ?= $(DOCS_DIR)/docs/test
# -----------------------------------------------------------------------------
# Project-wide clean-up targets
# -----------------------------------------------------------------------------
DIRS_TO_CLEAN := __pycache__ .pytest_cache .tox .ruff_cache .pyre .mypy_cache .pytype \
dist build site .eggs *.egg-info .cache htmlcov certs \
$(VENV_DIR) $(VENV_DIR).sbom $(COVERAGE_DIR) \
node_modules .mutmut-cache html
FILES_TO_CLEAN := .coverage .coverage.* coverage.xml mcp.prof mcp.pstats mcp.db-* \
$(PROJECT_NAME).sbom.json \
snakefood.dot packages.dot classes.dot \
$(DOCS_DIR)/pstats.png \
$(DOCS_DIR)/docs/test/sbom.md \
$(DOCS_DIR)/docs/test/{unittest,full,index,test}.md \
$(DOCS_DIR)/docs/images/coverage.svg $(LICENSES_MD) $(METRICS_MD) \
*.db *.sqlite *.sqlite3 mcp.db-journal *.py,cover \
.depsorter_cache.json .depupdate.* \
grype-results.sarif devskim-results.sarif \
*.tar.gz *.tar.bz2 *.tar.xz *.zip *.deb \
*.log mcpgateway.sbom.xml
COVERAGE_DIR ?= $(DOCS_DIR)/docs/coverage
LICENSES_MD ?= $(DOCS_DIR)/docs/test/licenses.md
METRICS_MD ?= $(DOCS_DIR)/docs/metrics/loc.md
# -----------------------------------------------------------------------------
# Container resource configuration
# -----------------------------------------------------------------------------
CONTAINER_MEMORY = 2048m
CONTAINER_CPUS = 2
# Virtual-environment variables
VENVS_DIR ?= $(HOME)/.venv
VENV_DIR ?= $(VENVS_DIR)/$(PROJECT_NAME)
# -----------------------------------------------------------------------------
# OS Specific
# -----------------------------------------------------------------------------
# The -r flag for xargs is GNU-specific and will fail on macOS
XARGS_FLAGS := $(shell [ "$$(uname)" = "Darwin" ] && echo "" || echo "-r")
# =============================================================================
# 📖 DYNAMIC HELP
# =============================================================================
.PHONY: help
help:
@grep "^# help\:" Makefile | grep -v grep | sed 's/\# help\: //' | sed 's/\# help\://'
# -----------------------------------------------------------------------------
# 🔧 SYSTEM-LEVEL DEPENDENCIES
# -----------------------------------------------------------------------------
# help: 🔧 SYSTEM-LEVEL DEPENDENCIES (DEV BUILD ONLY)
# help: os-deps - Install Graphviz, Pandoc, Trivy, SCC used for dev docs generation and security scan
OS_DEPS_SCRIPT := ./os_deps.sh
.PHONY: os-deps
os-deps: $(OS_DEPS_SCRIPT)
@bash $(OS_DEPS_SCRIPT)
# -----------------------------------------------------------------------------
# 🔧 HELPER SCRIPTS
# -----------------------------------------------------------------------------
# Helper to ensure a Python package is installed in venv
define ensure_pip_package
@test -d "$(VENV_DIR)" || $(MAKE) venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
python3 -m pip show $(1) >/dev/null 2>&1 || \
python3 -m pip install -q $(1)"
endef
# =============================================================================
# 🌱 VIRTUAL ENVIRONMENT & INSTALLATION
# =============================================================================
# help: 🌱 VIRTUAL ENVIRONMENT & INSTALLATION
# help: uv - Ensure uv is installed or install it if needed
# help: venv - Create a fresh virtual environment with uv & friends
# help: activate - Activate the virtual environment in the current shell
# help: install - Install project into the venv
# help: install-dev - Install project (incl. dev deps) into the venv
# help: install-db - Install project (incl. postgres and redis) into venv
# help: update - Update all installed deps inside the venv
.PHONY: uv
uv:
@if ! type uv >/dev/null 2>&1; then \
echo "🔧 'uv' not found - installing..."; \
if type brew >/dev/null 2>&1; then \
echo "🍺 Installing 'uv' via Homebrew..."; \
brew install uv; \
else \
echo "🐍 Installing 'uv' via local install script..."; \
curl -LsSf https://astral.sh/uv/install.sh | sh ; \
echo "💡 Make sure to add 'uv' to your PATH if not done automatically."; \
fi; \
fi
.PHONY: venv
venv:
@rm -Rf "$(VENV_DIR)"
@test -d "$(VENVS_DIR)" || mkdir -p "$(VENVS_DIR)"
@python3 -m venv "$(VENV_DIR)"
@/bin/bash -c "source $(VENV_DIR)/bin/activate && python3 -m pip install --upgrade pip setuptools pdm"
# Eventually, we want to transition to using uv/uvx exclusively, at which point we will only need
# a virtual environment if the user has not installed uv into their account.
@/bin/bash -c "type uv || ( source $(VENV_DIR)/bin/activate && python3 -m pip install --upgrade uv )"
@echo -e "✅ Virtual env created.\n💡 Enter it with:\n . $(VENV_DIR)/bin/activate\n"
.PHONY: activate
activate:
@echo -e "💡 Enter the venv using:\n. $(VENV_DIR)/bin/activate\n"
.PHONY: install
install: venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && uv pip install ."
.PHONY: install-db
install-db: venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && uv pip install .[redis,postgres]"
.PHONY: install-dev
install-dev: venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && uv pip install --group dev ."
@if [ "$(ENABLE_RUST_BUILD)" = "1" ]; then \
echo "🦀 Building Rust plugins..."; \
$(MAKE) rust-dev || echo "⚠️ Rust plugins not available (optional)"; \
else \
echo "⏭️ Rust builds disabled (set ENABLE_RUST_BUILD=1 to enable)"; \
fi
.PHONY: update
update:
@echo "⬆️ Updating installed dependencies..."
@/bin/bash -c "source $(VENV_DIR)/bin/activate && uv pip install -U --group dev ."
# help: check-env - Verify all required env vars in .env are present
.PHONY: check-env check-env-dev
# Validate .env in production mode
check-env:
@echo "🔎 Validating .env against .env.example using Python (prod)..."
@python -m mcpgateway.scripts.validate_env .env.example
# Validate .env in development mode (warnings do not fail)
check-env-dev:
@echo "🔎 Validating .env (dev, warnings do not fail)..."
@python -c "import sys; from mcpgateway.scripts import validate_env as ve; sys.exit(ve.main(env_file='.env', exit_on_warnings=False))"
# =============================================================================
# ▶️ SERVE
# =============================================================================
# help: ▶️ SERVE
# help: serve - Run production Gunicorn server on :4444
# help: certs - Generate self-signed TLS cert & key in ./certs (won't overwrite)
# help: certs-passphrase - Generate self-signed cert with passphrase-protected key
# help: certs-remove-passphrase - Remove passphrase from encrypted key
# help: certs-jwt - Generate JWT RSA keys in ./certs/jwt/ (idempotent)
# help: certs-jwt-ecdsa - Generate JWT ECDSA keys in ./certs/jwt/ (idempotent)
# help: certs-all - Generate both TLS certs and JWT keys (combo target)
# help: certs-mcp-ca - Generate MCP CA for plugin mTLS (./certs/mcp/ca/)
# help: certs-mcp-gateway - Generate gateway client certificate (./certs/mcp/gateway/)
# help: certs-mcp-plugin - Generate plugin server certificate (requires PLUGIN_NAME=name)
# help: certs-mcp-all - Generate complete MCP mTLS infrastructure (reads plugins from config.yaml)
# help: certs-mcp-check - Check expiry dates of MCP certificates
# help: serve-ssl - Run Gunicorn behind HTTPS on :4444 (uses ./certs)
# help: dev - Run fast-reload dev server (uvicorn)
# help: dev-echo - Run dev server with SQL query logging (N+1 debugging)
# help: stop - Stop all mcpgateway server processes
# help: stop-dev - Stop uvicorn dev server (port 8000)
# help: stop-serve - Stop gunicorn production server (port 4444)
# help: run - Execute helper script ./run.sh
.PHONY: serve serve-ssl dev stop stop-dev stop-serve run certs certs-jwt certs-jwt-ecdsa certs-all \
certs-mcp-ca certs-mcp-gateway certs-mcp-plugin certs-mcp-all certs-mcp-check
## --- Primary servers ---------------------------------------------------------
serve:
./run-gunicorn.sh
serve-ssl: certs
SSL=true CERT_FILE=certs/cert.pem KEY_FILE=certs/key.pem ./run-gunicorn.sh
dev:
@$(VENV_DIR)/bin/uvicorn mcpgateway.main:app --host 0.0.0.0 --port 8000 --reload --reload-exclude='public/'
dev-echo: ## Run dev server with SQL query logging enabled
@echo "🔍 Starting dev server with SQL query logging (N+1 detection)"
@echo " Docs: docs/docs/development/db-performance.md"
@SQLALCHEMY_ECHO=true $(VENV_DIR)/bin/uvicorn mcpgateway.main:app --host 0.0.0.0 --port 8000 --reload --reload-exclude='public/'
stop: ## Stop all mcpgateway server processes
@echo "Stopping all mcpgateway processes..."
@if [ -f /tmp/mcpgateway-gunicorn.lock ]; then kill -9 $$(cat /tmp/mcpgateway-gunicorn.lock) 2>/dev/null || true; rm -f /tmp/mcpgateway-gunicorn.lock; fi
@lsof -ti:8000 2>/dev/null | xargs -r kill -9 || true
@lsof -ti:4444 2>/dev/null | xargs -r kill -9 || true
@echo "Done."
stop-dev: ## Stop uvicorn dev server (port 8000)
@lsof -ti:8000 2>/dev/null | xargs -r kill -9 || true
stop-serve: ## Stop gunicorn production server (port 4444)
@if [ -f /tmp/mcpgateway-gunicorn.lock ]; then kill -9 $$(cat /tmp/mcpgateway-gunicorn.lock) 2>/dev/null || true; rm -f /tmp/mcpgateway-gunicorn.lock; fi
@lsof -ti:4444 2>/dev/null | xargs -r kill -9 || true
run:
./run.sh
## --- Certificate helper ------------------------------------------------------
certs: ## Generate ./certs/cert.pem & ./certs/key.pem (idempotent)
@if [ -f certs/cert.pem ] && [ -f certs/key.pem ]; then \
echo "🔏 Existing certificates found in ./certs - skipping generation."; \
else \
echo "🔏 Generating self-signed certificate (1 year)..."; \
mkdir -p certs; \
openssl req -x509 -newkey rsa:4096 -sha256 -days 365 -nodes \
-keyout certs/key.pem -out certs/cert.pem \
-subj "/CN=localhost" \
-addext "subjectAltName=DNS:localhost,IP:127.0.0.1"; \
echo "✅ TLS certificate written to ./certs"; \
fi
chmod 640 certs/key.pem
certs-passphrase: ## Generate self-signed cert with passphrase-protected key
@if [ -f certs/cert.pem ] && [ -f certs/key-encrypted.pem ]; then \
echo "🔏 Existing passphrase-protected certificates found - skipping."; \
else \
echo "🔏 Generating passphrase-protected certificate (1 year)..."; \
mkdir -p certs; \
read -sp "Enter passphrase for private key: " PASSPHRASE; echo; \
read -sp "Confirm passphrase: " PASSPHRASE2; echo; \
if [ "$$PASSPHRASE" != "$$PASSPHRASE2" ]; then \
echo "❌ Passphrases do not match!"; \
exit 1; \
fi; \
openssl req -x509 -newkey rsa:4096 -sha256 -days 365 \
-keyout certs/key-encrypted.pem -out certs/cert.pem \
-subj "/CN=localhost" \
-addext "subjectAltName=DNS:localhost,IP:127.0.0.1" \
-passout pass:"$$PASSPHRASE"; \
echo "✅ Passphrase-protected certificate created"; \
echo "📁 Certificate: ./certs/cert.pem"; \
echo "📁 Encrypted Key: ./certs/key-encrypted.pem"; \
echo ""; \
echo "💡 To use this certificate:"; \
echo " 1. Set KEY_FILE_PASSWORD environment variable"; \
echo " 2. Run: KEY_FILE_PASSWORD='your-passphrase' SSL=true CERT_FILE=certs/cert.pem KEY_FILE=certs/key-encrypted.pem make serve-ssl"; \
fi
@chmod 600 certs/key-encrypted.pem
certs-remove-passphrase: ## Remove passphrase from encrypted key (creates key.pem from key-encrypted.pem)
@if [ ! -f certs/key-encrypted.pem ]; then \
echo "❌ No encrypted key found at certs/key-encrypted.pem"; \
echo "💡 Generate one with: make certs-passphrase"; \
exit 1; \
fi
@echo "🔓 Removing passphrase from private key..."
@openssl rsa -in certs/key-encrypted.pem -out certs/key.pem
@chmod 600 certs/key.pem
@echo "✅ Passphrase removed - unencrypted key saved to certs/key.pem"
@echo "⚠️ Keep this file secure! It contains your unencrypted private key."
certs-jwt: ## Generate JWT RSA keys in ./certs/jwt/ (idempotent)
@if [ -f certs/jwt/private.pem ] && [ -f certs/jwt/public.pem ]; then \
echo "🔐 Existing JWT RSA keys found in ./certs/jwt - skipping generation."; \
else \
echo "🔐 Generating JWT RSA key pair (4096-bit)..."; \
mkdir -p certs/jwt; \
openssl genrsa -out certs/jwt/private.pem 4096; \
openssl rsa -in certs/jwt/private.pem -pubout -out certs/jwt/public.pem; \
echo "✅ JWT RSA keys written to ./certs/jwt"; \
fi
@chmod 600 certs/jwt/private.pem
@chmod 644 certs/jwt/public.pem
@echo "🔒 Permissions set: private.pem (600), public.pem (644)"
certs-jwt-ecdsa: ## Generate JWT ECDSA keys in ./certs/jwt/ (idempotent)
@if [ -f certs/jwt/ec_private.pem ] && [ -f certs/jwt/ec_public.pem ]; then \
echo "🔐 Existing JWT ECDSA keys found in ./certs/jwt - skipping generation."; \
else \
echo "🔐 Generating JWT ECDSA key pair (P-256 curve)..."; \
mkdir -p certs/jwt; \
openssl ecparam -genkey -name prime256v1 -noout -out certs/jwt/ec_private.pem; \
openssl ec -in certs/jwt/ec_private.pem -pubout -out certs/jwt/ec_public.pem; \
echo "✅ JWT ECDSA keys written to ./certs/jwt"; \
fi
@chmod 600 certs/jwt/ec_private.pem
@chmod 644 certs/jwt/ec_public.pem
@echo "🔒 Permissions set: ec_private.pem (600), ec_public.pem (644)"
certs-all: certs certs-jwt ## Generate both TLS certificates and JWT RSA keys
@echo "🎯 All certificates and keys generated successfully!"
@echo "📁 TLS: ./certs/{cert,key}.pem"
@echo "📁 JWT: ./certs/jwt/{private,public}.pem"
@echo "💡 Use JWT_ALGORITHM=RS256 with JWT_PUBLIC_KEY_PATH=certs/jwt/public.pem"
## --- MCP Plugin mTLS Certificate Management ----------------------------------
# Default validity period for MCP certificates (in days)
MCP_CERT_DAYS ?= 825
# Plugin configuration file for automatic certificate generation
MCP_PLUGIN_CONFIG ?= plugins/external/config.yaml
certs-mcp-ca: ## Generate CA for MCP plugin mTLS
@if [ -f certs/mcp/ca/ca.key ] && [ -f certs/mcp/ca/ca.crt ]; then \
echo "🔐 Existing MCP CA found in ./certs/mcp/ca - skipping generation."; \
echo "⚠️ To regenerate, delete ./certs/mcp/ca and run again."; \
else \
echo "🔐 Generating MCP Certificate Authority ($(MCP_CERT_DAYS) days validity)..."; \
mkdir -p certs/mcp/ca; \
openssl genrsa -out certs/mcp/ca/ca.key 4096; \
openssl req -new -x509 -key certs/mcp/ca/ca.key -out certs/mcp/ca/ca.crt \
-days $(MCP_CERT_DAYS) \
-subj "/CN=MCP-Gateway-CA/O=MCPGateway/OU=Plugins"; \
echo "01" > certs/mcp/ca/ca.srl; \
echo "✅ MCP CA created: ./certs/mcp/ca/ca.{key,crt}"; \
fi
@chmod 600 certs/mcp/ca/ca.key
@chmod 644 certs/mcp/ca/ca.crt
@echo "🔒 Permissions set: ca.key (600), ca.crt (644)"
certs-mcp-gateway: certs-mcp-ca ## Generate gateway client certificate
@if [ -f certs/mcp/gateway/client.key ] && [ -f certs/mcp/gateway/client.crt ]; then \
echo "🔐 Existing gateway client certificate found - skipping generation."; \
else \
echo "🔐 Generating gateway client certificate ($(MCP_CERT_DAYS) days)..."; \
mkdir -p certs/mcp/gateway; \
openssl genrsa -out certs/mcp/gateway/client.key 4096; \
openssl req -new -key certs/mcp/gateway/client.key \
-out certs/mcp/gateway/client.csr \
-subj "/CN=mcp-gateway-client/O=MCPGateway/OU=Gateway"; \
openssl x509 -req -in certs/mcp/gateway/client.csr \
-CA certs/mcp/ca/ca.crt -CAkey certs/mcp/ca/ca.key \
-CAcreateserial -out certs/mcp/gateway/client.crt \
-days $(MCP_CERT_DAYS) -sha256; \
rm certs/mcp/gateway/client.csr; \
cp certs/mcp/ca/ca.crt certs/mcp/gateway/ca.crt; \
echo "✅ Gateway client certificate created: ./certs/mcp/gateway/"; \
fi
@chmod 600 certs/mcp/gateway/client.key
@chmod 644 certs/mcp/gateway/client.crt certs/mcp/gateway/ca.crt
@echo "🔒 Permissions set: client.key (600), client.crt (644), ca.crt (644)"
certs-mcp-plugin: certs-mcp-ca ## Generate plugin server certificate (PLUGIN_NAME=name)
@if [ -z "$(PLUGIN_NAME)" ]; then \
echo "❌ ERROR: PLUGIN_NAME not set"; \
echo "💡 Usage: make certs-mcp-plugin PLUGIN_NAME=my-plugin"; \
exit 1; \
fi
@if [ -f certs/mcp/plugins/$(PLUGIN_NAME)/server.key ] && \
[ -f certs/mcp/plugins/$(PLUGIN_NAME)/server.crt ]; then \
echo "🔐 Existing certificate for plugin '$(PLUGIN_NAME)' found - skipping."; \
else \
echo "🔐 Generating server certificate for plugin '$(PLUGIN_NAME)' ($(MCP_CERT_DAYS) days)..."; \
mkdir -p certs/mcp/plugins/$(PLUGIN_NAME); \
openssl genrsa -out certs/mcp/plugins/$(PLUGIN_NAME)/server.key 4096; \
openssl req -new -key certs/mcp/plugins/$(PLUGIN_NAME)/server.key \
-out certs/mcp/plugins/$(PLUGIN_NAME)/server.csr \
-subj "/CN=mcp-plugin-$(PLUGIN_NAME)/O=MCPGateway/OU=Plugins"; \
openssl x509 -req -in certs/mcp/plugins/$(PLUGIN_NAME)/server.csr \
-CA certs/mcp/ca/ca.crt -CAkey certs/mcp/ca/ca.key \
-CAcreateserial -out certs/mcp/plugins/$(PLUGIN_NAME)/server.crt \
-days $(MCP_CERT_DAYS) -sha256 \
-extfile <(printf "subjectAltName=DNS:$(PLUGIN_NAME),DNS:mcp-plugin-$(PLUGIN_NAME),DNS:localhost"); \
rm certs/mcp/plugins/$(PLUGIN_NAME)/server.csr; \
cp certs/mcp/ca/ca.crt certs/mcp/plugins/$(PLUGIN_NAME)/ca.crt; \
echo "✅ Plugin '$(PLUGIN_NAME)' certificate created: ./certs/mcp/plugins/$(PLUGIN_NAME)/"; \
fi
@chmod 600 certs/mcp/plugins/$(PLUGIN_NAME)/server.key
@chmod 644 certs/mcp/plugins/$(PLUGIN_NAME)/server.crt certs/mcp/plugins/$(PLUGIN_NAME)/ca.crt
@echo "🔒 Permissions set: server.key (600), server.crt (644), ca.crt (644)"
certs-mcp-all: certs-mcp-ca certs-mcp-gateway ## Generate complete mTLS infrastructure
@echo "🔐 Generating certificates for plugins..."
@# Read plugin names from config file if it exists
@if [ -f "$(MCP_PLUGIN_CONFIG)" ]; then \
echo "📋 Reading plugin names from $(MCP_PLUGIN_CONFIG)"; \
python3 -c "import yaml; \
config = yaml.safe_load(open('$(MCP_PLUGIN_CONFIG)')); \
plugins = [p['name'] for p in config.get('plugins', []) if p.get('kind') == 'external']; \
print('\n'.join(plugins))" 2>/dev/null | while read plugin_name; do \
if [ -n "$$plugin_name" ]; then \
echo " Generating for: $$plugin_name"; \
$(MAKE) certs-mcp-plugin PLUGIN_NAME="$$plugin_name"; \
fi; \
done || echo "⚠️ PyYAML not installed or config parse failed, generating example plugins..."; \
fi
@# Fallback to example plugins if no config or parsing failed
@if [ ! -f "$(MCP_PLUGIN_CONFIG)" ] || ! python3 -c "import yaml" 2>/dev/null; then \
echo "🔐 Generating certificates for example plugins..."; \
$(MAKE) certs-mcp-plugin PLUGIN_NAME=example-plugin-a; \
$(MAKE) certs-mcp-plugin PLUGIN_NAME=example-plugin-b; \
fi
@echo ""
@echo "🎯 MCP mTLS infrastructure generated successfully!"
@echo "📁 Structure:"
@echo " certs/mcp/ca/ - Certificate Authority"
@echo " certs/mcp/gateway/ - Gateway client certificate"
@echo " certs/mcp/plugins/*/ - Plugin server certificates"
@echo ""
@echo "💡 Generate additional plugin certificates with:"
@echo " make certs-mcp-plugin PLUGIN_NAME=your-plugin-name"
@echo ""
@echo "💡 Certificate validity: $(MCP_CERT_DAYS) days"
@echo " To change: make certs-mcp-all MCP_CERT_DAYS=365"
certs-mcp-check: ## Check expiry dates of MCP certificates
@echo "🔍 Checking MCP certificate expiry dates..."
@echo ""
@if [ -f certs/mcp/ca/ca.crt ]; then \
echo "📋 CA Certificate:"; \
openssl x509 -in certs/mcp/ca/ca.crt -noout -enddate | sed 's/notAfter=/ Expires: /'; \
echo ""; \
fi
@if [ -f certs/mcp/gateway/client.crt ]; then \
echo "📋 Gateway Client Certificate:"; \
openssl x509 -in certs/mcp/gateway/client.crt -noout -enddate | sed 's/notAfter=/ Expires: /'; \
echo ""; \
fi
@if [ -d certs/mcp/plugins ]; then \
echo "📋 Plugin Certificates:"; \
for plugin_dir in certs/mcp/plugins/*; do \
if [ -f "$$plugin_dir/server.crt" ]; then \
plugin_name=$$(basename "$$plugin_dir"); \
expiry=$$(openssl x509 -in "$$plugin_dir/server.crt" -noout -enddate | sed 's/notAfter=//'); \
echo " $$plugin_name: $$expiry"; \
fi; \
done; \
echo ""; \
fi
@echo "💡 To regenerate expired certificates, delete the cert directory and run make certs-mcp-all"
## --- House-keeping -----------------------------------------------------------
# help: clean - Remove caches, build artefacts, virtualenv, docs, certs, coverage, SBOM, database files, etc.
.PHONY: clean
clean:
@echo "🧹 Cleaning workspace..."
@set +e; \
for dir in $(DIRS_TO_CLEAN); do \
find . -type d -name "$$dir" -prune -exec rm -rf {} +; \
done; \
set -e
@rm -f $(FILES_TO_CLEAN)
@find . -name "*.py[cod]" -delete
@find . -name "*.py,cover" -delete
@echo "✅ Clean complete."
# =============================================================================
# 🧪 TESTING
# =============================================================================
# help: 🧪 TESTING
# help: smoketest - Run smoketest.py --verbose (build container, add MCP server, test endpoints)
# help: test - Run unit tests with pytest
# help: test-profile - Run tests and show slowest 20 tests (durations >= 1s)
# help: coverage - Run tests with coverage, emit md/HTML/XML + badge, generate annotated files
# help: htmlcov - (re)build just the HTML coverage report into docs
# help: test-curl - Smoke-test API endpoints with curl script
# help: pytest-examples - Run README / examples through pytest-examples
# help: doctest - Run doctest on all modules with summary report
# help: doctest-verbose - Run doctest with detailed output (-v flag)
# help: doctest-coverage - Generate coverage report for doctest examples
# help: doctest-check - Check doctest coverage percentage (fail if < 100%)
# help: test-db-perf - Run database performance and N+1 query detection tests
# help: test-db-perf-verbose - Run database performance tests with full SQL query output
# help: dev-query-log - Run dev server with query logging to file (N+1 detection)
# help: query-log-tail - Tail the database query log file
# help: query-log-analyze - Analyze query log for N+1 patterns and slow queries
# help: query-log-clear - Clear database query log files
.PHONY: smoketest test test-profile coverage pytest-examples test-curl htmlcov doctest doctest-verbose doctest-coverage doctest-check test-db-perf test-db-perf-verbose dev-query-log query-log-tail query-log-analyze query-log-clear load-test load-test-ui load-test-light load-test-heavy load-test-sustained load-test-stress load-test-report load-test-compose load-test-timeserver load-test-fasttime load-test-1000
## --- Automated checks --------------------------------------------------------
smoketest:
@echo "🚀 Running smoketest..."
@/bin/bash -c 'source $(VENV_DIR)/bin/activate && \
./smoketest.py --verbose || { echo "❌ Smoketest failed!"; exit 1; }; \
echo "✅ Smoketest passed!" \
'
test:
@echo "🧪 Running tests..."
@test -d "$(VENV_DIR)" || $(MAKE) venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
export DATABASE_URL='sqlite:///:memory:' && \
export TEST_DATABASE_URL='sqlite:///:memory:' && \
uv run --active pytest -n auto --maxfail=0 -v --ignore=tests/fuzz"
test-profile:
@echo "🧪 Running tests with profiling (showing slowest tests)..."
@test -d "$(VENV_DIR)" || $(MAKE) venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
export DATABASE_URL='sqlite:///:memory:' && \
export TEST_DATABASE_URL='sqlite:///:memory:' && \
uv run --active pytest -n auto --durations=20 --durations-min=1.0 --disable-warnings -v --ignore=tests/fuzz"
coverage:
@test -d "$(VENV_DIR)" || $(MAKE) venv
@mkdir -p $(TEST_DOCS_DIR)
@printf "# Unit tests\n\n" > $(DOCS_DIR)/docs/test/unittest.md
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
export DATABASE_URL='sqlite:///:memory:' && \
export TEST_DATABASE_URL='sqlite:///:memory:' && \
python3 -m pytest -p pytest_cov --reruns=1 --reruns-delay 30 \
--md-report --md-report-output=$(DOCS_DIR)/docs/test/unittest.md \
--dist loadgroup -n 8 -rA --cov-append --capture=tee-sys -v \
--durations=120 --doctest-modules app/ --cov-report=term \
--cov=mcpgateway --ignore=test.py tests/ || true"
@printf '\n## Coverage report\n\n' >> $(DOCS_DIR)/docs/test/unittest.md
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
coverage report --format=markdown -m --no-skip-covered \
>> $(DOCS_DIR)/docs/test/unittest.md"
@/bin/bash -c "source $(VENV_DIR)/bin/activate && coverage html -d $(COVERAGE_DIR) --include=app/*"
@/bin/bash -c "source $(VENV_DIR)/bin/activate && coverage xml"
@/bin/bash -c "source $(VENV_DIR)/bin/activate && coverage-badge -fo $(DOCS_DIR)/docs/images/coverage.svg"
@echo "🔍 Generating annotated coverage files..."
@/bin/bash -c "source $(VENV_DIR)/bin/activate && coverage annotate -d ."
@echo "✅ Coverage artefacts: md, HTML in $(COVERAGE_DIR), XML, badge & annotated files (.py,cover) ✔"
htmlcov:
@echo "📊 Generating HTML coverage report..."
@test -d "$(VENV_DIR)" || $(MAKE) venv
@mkdir -p $(COVERAGE_DIR)
# If there's no existing coverage data, fall back to the full test-run
@if [ ! -f .coverage ]; then \
echo "ℹ️ No .coverage file found - running full coverage first..."; \
$(MAKE) --no-print-directory coverage; \
fi
@/bin/bash -c "source $(VENV_DIR)/bin/activate && coverage html -i -d $(COVERAGE_DIR)"
@echo "✅ HTML coverage report ready → $(COVERAGE_DIR)/index.html"
pytest-examples:
@echo "🧪 Testing README examples..."
@test -d "$(VENV_DIR)" || $(MAKE) venv
@test -f test_readme.py || { echo "⚠️ test_readme.py not found - skipping"; exit 0; }
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
python3 -m pip install -q pytest pytest-examples && \
pytest -v test_readme.py"
test-curl:
./test_endpoints.sh
## --- Doctest targets ---------------------------------------------------------
doctest:
@echo "🧪 Running doctest on all modules..."
@test -d "$(VENV_DIR)" || $(MAKE) venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
export JWT_SECRET_KEY=secret && \
python3 -m pytest --doctest-modules mcpgateway/ --ignore=mcpgateway/utils/pagination.py --tb=short --no-cov --disable-warnings -n auto"
doctest-verbose:
@echo "🧪 Running doctest with verbose output..."
@test -d "$(VENV_DIR)" || $(MAKE) venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
export JWT_SECRET_KEY=secret && \
python3 -m pytest --doctest-modules mcpgateway/ --ignore=mcpgateway/utils/pagination.py -v --tb=short --no-cov --disable-warnings -n auto"
doctest-coverage:
@echo "📊 Generating doctest coverage report..."
@test -d "$(VENV_DIR)" || $(MAKE) venv
@mkdir -p $(TEST_DOCS_DIR)
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
python3 -m pytest --doctest-modules mcpgateway/ \
--cov=mcpgateway --cov-report=term --cov-report=html:htmlcov-doctest \
--cov-report=xml:coverage-doctest.xml"
@echo "✅ Doctest coverage report generated in htmlcov-doctest/"
doctest-check:
@echo "🔍 Checking doctest coverage..."
@test -d "$(VENV_DIR)" || $(MAKE) venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
python3 -m pytest --doctest-modules mcpgateway/ --tb=no -q && \
echo '✅ All doctests passing' || (echo '❌ Doctest failures detected' && exit 1)"
## --- Database Performance Testing --------------------------------------------
test-db-perf: ## Run database performance and N+1 detection tests
@echo "🔍 Running database performance tests..."
@echo " Tip: Use 'make dev-echo' to debug queries in dev server"
@echo " Docs: docs/docs/development/db-performance.md"
@test -d "$(VENV_DIR)" || $(MAKE) venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
export DATABASE_URL='sqlite:///:memory:' && \
export TEST_DATABASE_URL='sqlite:///:memory:' && \
uv run --active pytest tests/performance/test_db_query_patterns.py -v --tb=short"
test-db-perf-verbose: ## Run database performance tests with full SQL query output
@echo "🔍 Running database performance tests with query logging..."
@echo " All SQL queries will be printed to help identify N+1 patterns"
@test -d "$(VENV_DIR)" || $(MAKE) venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
export DATABASE_URL='sqlite:///:memory:' && \
export TEST_DATABASE_URL='sqlite:///:memory:' && \
export SQLALCHEMY_ECHO=true && \
uv run --active pytest tests/performance/test_db_query_patterns.py -v -s --tb=short"
dev-query-log: ## Run dev server with query logging to file
@echo "📊 Starting dev server with database query logging"
@echo " Logs: logs/db-queries.log (text), logs/db-queries.jsonl (JSON)"
@echo " Use 'make query-log-tail' in another terminal to watch queries"
@echo " Docs: docs/docs/development/db-performance.md"
@mkdir -p logs
@DB_QUERY_LOG_ENABLED=true $(VENV_DIR)/bin/uvicorn mcpgateway.main:app --host 0.0.0.0 --port 8000 --reload --reload-exclude='public/'
query-log-tail: ## Tail the database query log file
@echo "📊 Tailing logs/db-queries.log (Ctrl+C to stop)"
@echo " Start server with 'make dev-query-log' to generate queries"
@tail -f logs/db-queries.log 2>/dev/null || echo "No log file yet. Start server with 'make dev-query-log' first."
query-log-analyze: ## Analyze query log for N+1 patterns
@echo "📊 Analyzing database query log..."
@test -d "$(VENV_DIR)" || $(MAKE) venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
python3 -m mcpgateway.utils.analyze_query_log"
query-log-clear: ## Clear database query log files
@echo "🗑️ Clearing database query logs..."
@rm -f logs/db-queries.log logs/db-queries.jsonl
@echo "✅ Query logs cleared"
# =============================================================================
# 📊 LOAD TESTING - Database population and performance testing
# =============================================================================
# help: 📊 LOAD TESTING
# help: generate-small - Generate small load test data (100 users, ~74K records, <1 min)
# help: generate-medium - Generate medium load test data (10K users, ~70M records, ~10 min)
# help: generate-large - Generate large load test data (100K users, ~700M records, ~1-2 hours)
# help: generate-massive - Generate massive load test data (1M users, billions of records, ~10-20 hours)
# help: generate-clean - Clean all generated load test data and reports
# help: generate-report - Display most recent load test report
.PHONY: generate-small generate-medium generate-large generate-massive generate-clean generate-report
generate-small: ## Generate small load test dataset (100 users)
@echo "📊 Generating small load test data..."
@echo " Target: 100 users, ~74K records"
@echo " Time: <1 minute"
@test -d "$(VENV_DIR)" || $(MAKE) venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
python -m tests.load.generate --profile small"
@echo ""
@echo "✅ Small load test data generated!"
@echo "📄 Report: reports/small_load_report.json"
generate-medium: ## Generate medium load test dataset (10K users)
@echo "📊 Generating medium load test data..."
@echo " Target: 10K users, ~70M records"
@echo " Time: ~10 minutes"
@echo " ⚠️ Recommended: Use PostgreSQL or MySQL for better performance"
@test -d "$(VENV_DIR)" || $(MAKE) venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
python -m tests.load.generate --profile medium"
@echo ""
@echo "✅ Medium load test data generated!"
@echo "📄 Report: reports/medium_load_report.json"
generate-large: ## Generate large load test dataset (100K users)
@echo "📊 Generating large load test data..."
@echo " Target: 100K users, ~700M records"
@echo " Time: ~1-2 hours"
@echo " ⚠️ REQUIRED: PostgreSQL or MySQL"
@echo " ⚠️ Recommended: 16GB+ RAM, SSD storage"
@test -d "$(VENV_DIR)" || $(MAKE) venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
python -m tests.load.generate --profile large"
@echo ""
@echo "✅ Large load test data generated!"
@echo "📄 Report: reports/large_load_report.json"
generate-massive: ## Generate massive load test dataset (1M users)
@echo "📊 Generating massive load test data..."
@echo " Target: 1M users, billions of records"
@echo " Time: ~10-20 hours"
@echo " ⚠️ REQUIRED: PostgreSQL or MySQL with high-performance config"
@echo " ⚠️ REQUIRED: 32GB+ RAM, SSD storage, multi-core CPU"
@echo ""
@read -p "This will take 10-20 hours. Continue? [y/N] " -n 1 -r; \
echo; \
if [[ $$REPLY =~ ^[Yy]$$ ]]; then \
test -d "$(VENV_DIR)" || $(MAKE) venv; \
/bin/bash -c "source $(VENV_DIR)/bin/activate && \
python -m tests.load.generate --profile massive"; \
echo ""; \
echo "✅ Massive load test data generated!"; \
echo "📄 Report: reports/massive_load_report.json"; \
else \
echo "❌ Cancelled"; \
exit 1; \
fi
generate-clean: ## Clean all generated load test data
@echo "🧹 Cleaning load test data..."
@rm -f reports/*_load_report.json
@echo "✅ Load test reports cleaned!"
@echo ""
@echo "⚠️ Note: This does NOT clean the database itself."
@echo " To clean database, use: make clean-db"
generate-report: ## Display most recent load test report
@echo "📊 Most Recent Load Test Reports:"
@echo ""
@for report in reports/*_load_report.json; do \
if [ -f "$$report" ]; then \
echo "📄 $$report:"; \
jq -r '" Profile: \(.profile)\n Duration: \(.duration_seconds)s\n Records: \(.total_generated | tonumber | tostring) total\n Rate: \(.records_per_second | floor | tostring) records/sec\n Timestamp: \(.timestamp)"' "$$report" 2>/dev/null || \
cat "$$report" | head -20; \
echo ""; \
fi; \
done || echo "❌ No reports found. Run 'make generate-small' first."
# =============================================================================
# 🔥 HTTP LOAD TESTING - Locust-based traffic generation
# =============================================================================
# help: 🔥 HTTP LOAD TESTING (Locust)
# help: load-test - Run HTTP load test (50 users, 60s, headless)
# help: load-test-ui - Start Locust web UI at http://localhost:8089
# help: load-test-light - Light load test (10 users, 30s)
# help: load-test-heavy - Heavy load test (200 users, 120s)
# help: load-test-sustained - Sustained load test (25 users, 300s)
# help: load-test-stress - Stress test (500 users, 60s, minimal wait)
# help: load-test-report - Show last load test HTML report
# help: load-test-compose - Light load test for compose stack (port 4444)
# help: load-test-timeserver - Load test fast_time_server (5 users, 30s)
# help: load-test-fasttime - Load test fast_time MCP tools (50 users, 60s)
# help: load-test-1000 - High-load test (1000 users, 120s)
# Default load test configuration
LOADTEST_HOST ?= http://localhost:8000
LOADTEST_USERS ?= 50
LOADTEST_SPAWN_RATE ?= 10
LOADTEST_RUN_TIME ?= 60s
LOADTEST_LOCUSTFILE := tests/loadtest/locustfile.py
LOADTEST_HTML_REPORT := reports/locust_report.html
LOADTEST_CSV_PREFIX := reports/locust
load-test: ## Run HTTP load test (50 users, 60s, headless)
@echo "🔥 Running HTTP load test with Locust..."
@echo " Host: $(LOADTEST_HOST)"
@echo " Users: $(LOADTEST_USERS)"
@echo " Spawn rate: $(LOADTEST_SPAWN_RATE)/s"
@echo " Duration: $(LOADTEST_RUN_TIME)"
@echo ""
@echo " 💡 Tip: Start server first with 'make dev' in another terminal"
@echo " 💡 Tip: Enable performance tab: MCPGATEWAY_PERFORMANCE_TRACKING=true"
@echo ""
@test -d "$(VENV_DIR)" || $(MAKE) venv
@mkdir -p reports
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
locust -f $(LOADTEST_LOCUSTFILE) \
--host=$(LOADTEST_HOST) \
--users=$(LOADTEST_USERS) \
--spawn-rate=$(LOADTEST_SPAWN_RATE) \
--run-time=$(LOADTEST_RUN_TIME) \
--headless \
--html=$(LOADTEST_HTML_REPORT) \
--csv=$(LOADTEST_CSV_PREFIX) \
--only-summary"
@echo ""
@echo "✅ Load test complete!"
@echo "📄 HTML Report: $(LOADTEST_HTML_REPORT)"
@echo "📊 CSV Reports: $(LOADTEST_CSV_PREFIX)_*.csv"
load-test-ui: ## Start Locust web UI at http://localhost:8089
@echo "🔥 Starting Locust Web UI..."
@echo " 🌐 Open http://localhost:8089 in your browser"
@echo " 🎯 Default host: $(LOADTEST_HOST)"
@echo ""
@echo " 💡 Configure users, spawn rate, and duration in the UI"
@echo " 💡 Use 'User classes' dropdown to select FastTimeUser, etc."
@echo " 💡 Start server first with 'make dev' or 'docker compose up'"
@echo ""
@test -d "$(VENV_DIR)" || $(MAKE) venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
locust -f $(LOADTEST_LOCUSTFILE) \
--host=$(LOADTEST_HOST) \
--class-picker"
load-test-light: ## Light load test (10 users, 30s)
@echo "🔥 Running LIGHT load test..."
@$(MAKE) load-test LOADTEST_USERS=10 LOADTEST_SPAWN_RATE=2 LOADTEST_RUN_TIME=30s
load-test-heavy: ## Heavy load test (200 users, 120s)
@echo "🔥 Running HEAVY load test..."
@echo " ⚠️ This will generate significant load on your server"
@$(MAKE) load-test LOADTEST_USERS=200 LOADTEST_SPAWN_RATE=20 LOADTEST_RUN_TIME=120s
load-test-sustained: ## Sustained load test (25 users, 300s)
@echo "🔥 Running SUSTAINED load test (5 minutes)..."
@$(MAKE) load-test LOADTEST_USERS=25 LOADTEST_SPAWN_RATE=5 LOADTEST_RUN_TIME=300s
load-test-stress: ## Stress test (500 users, 60s)
@echo "🔥 Running STRESS test..."
@echo " ⚠️ WARNING: This will generate EXTREME load!"
@echo " ⚠️ Your server may become unresponsive"
@echo ""
@read -p "Continue with stress test? [y/N] " -n 1 -r; \
echo; \
if [[ $$REPLY =~ ^[Yy]$$ ]]; then \
$(MAKE) load-test LOADTEST_USERS=500 LOADTEST_SPAWN_RATE=50 LOADTEST_RUN_TIME=60s; \
else \
echo "❌ Cancelled"; \
fi
load-test-report: ## Show last load test HTML report
@if [ -f "$(LOADTEST_HTML_REPORT)" ]; then \
echo "📊 Opening load test report: $(LOADTEST_HTML_REPORT)"; \
if command -v xdg-open &> /dev/null; then \
xdg-open $(LOADTEST_HTML_REPORT); \
elif command -v open &> /dev/null; then \
open $(LOADTEST_HTML_REPORT); \
else \
echo "Open $(LOADTEST_HTML_REPORT) in your browser"; \
fi; \
else \
echo "❌ No report found. Run 'make load-test' first."; \
fi
load-test-compose: ## Light load test for compose stack (10 users, 30s, port 4444)
@echo "🐳 Running compose-optimized load test..."
@echo " Host: http://localhost:4444"
@echo " Users: 10, Duration: 30s"
@echo " 💡 Requires: make compose-up"
@test -d "$(VENV_DIR)" || $(MAKE) venv
@mkdir -p reports
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
locust -f $(LOADTEST_LOCUSTFILE) \
--host=http://localhost:4444 \
--users=10 \
--spawn-rate=2 \
--run-time=30s \
--headless \
--html=reports/loadtest_compose.html \
--csv=reports/loadtest_compose \
--only-summary"
@echo "✅ Report: reports/loadtest_compose.html"
load-test-timeserver: ## Load test fast_time_server tools (5 users, 30s)
@echo "⏰ Running time server load test..."
@echo " Host: http://localhost:4444"
@echo " Users: 5, Duration: 30s"
@echo " 💡 Requires: docker compose --profile with-fast-time up -d"
@test -d "$(VENV_DIR)" || $(MAKE) venv
@mkdir -p reports
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
locust -f $(LOADTEST_LOCUSTFILE) \
--host=http://localhost:4444 \
--users=5 \
--spawn-rate=1 \
--run-time=30s \
--headless \
--html=reports/loadtest_timeserver.html \
--csv=reports/loadtest_timeserver \
FastTimeUser \
--only-summary"
@echo "✅ Report: reports/loadtest_timeserver.html"
load-test-fasttime: ## Load test fast_time MCP tools (50 users, 60s)
@echo "⏰ Running FastTime MCP server load test..."
@echo " Host: http://localhost:4444"
@echo " Users: 50, Duration: 60s"
@echo " 💡 Requires: docker compose --profile with-fast-time up -d"
@test -d "$(VENV_DIR)" || $(MAKE) venv
@mkdir -p reports
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
locust -f $(LOADTEST_LOCUSTFILE) \
--host=http://localhost:4444 \
--users=50 \
--spawn-rate=10 \
--run-time=60s \
--headless \
--html=reports/loadtest_fasttime.html \
--csv=reports/loadtest_fasttime \
FastTimeUser \
--only-summary"
@echo "✅ Report: reports/loadtest_fasttime.html"
load-test-1000: ## High-load test (1000 users, 120s) - requires tuned compose
@echo "🔥 Running HIGH LOAD test (1000 users, ~1000 RPS)..."
@echo " Host: http://localhost:4444"
@echo " Users: 1000, Spawn: 50/s, Duration: 120s"
@echo " ⚠️ Requires tuned compose stack (make compose-down && make compose-up)"
@read -p "Continue? [y/N] " -n 1 -r; echo; \
if [[ $$REPLY =~ ^[Yy]$$ ]]; then \
test -d "$(VENV_DIR)" || $(MAKE) venv; \
mkdir -p reports; \
/bin/bash -c "source $(VENV_DIR)/bin/activate && \
locust -f $(LOADTEST_LOCUSTFILE) \
--host=http://localhost:4444 \
--users=1000 \
--spawn-rate=50 \
--run-time=120s \
--headless \
--html=reports/loadtest_1000.html \
--csv=reports/loadtest_1000 \
--only-summary"; \
echo "✅ Report: reports/loadtest_1000.html"; \
else \
echo "❌ Cancelled"; \
fi
# =============================================================================
# 🧬 MUTATION TESTING
# =============================================================================
# help: 🧬 MUTATION TESTING
# help: mutmut-install - Install mutmut in development virtualenv
# help: mutmut-run - Run mutation testing (sample of 20 mutants for quick results)
# help: mutmut-run-full - Run FULL mutation testing (all 11,000+ mutants - takes hours!)
# help: mutmut-results - Display mutation testing summary and surviving mutants
# help: mutmut-html - Generate browsable HTML report of mutation results
# help: mutmut-ci - CI-friendly mutation testing with score threshold enforcement
# help: mutmut-clean - Clean mutmut cache and results
.PHONY: mutmut-install mutmut-run mutmut-results mutmut-html mutmut-ci mutmut-clean
mutmut-install:
@echo "📥 Installing mutmut..."
@test -d "$(VENV_DIR)" || $(MAKE) venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
python3 -m pip install -q mutmut==3.3.1"
mutmut-run: mutmut-install
@echo "🧬 Running mutation testing (sample mode - 20 mutants)..."
@echo "⏳ This should take about 2-3 minutes..."
@echo "📝 Target: mcpgateway/ directory"
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
cd $(PWD) && \
PYTHONPATH=$(PWD) python run_mutmut.py --sample"
mutmut-run-full: mutmut-install
@echo "🧬 Running FULL mutation testing (all mutants)..."
@echo "⏰ WARNING: This will take a VERY long time (hours)!"
@echo "📝 Target: mcpgateway/ directory (11,000+ mutants)"
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
cd $(PWD) && \
PYTHONPATH=$(PWD) python run_mutmut.py --full"
mutmut-results:
@echo "📊 Mutation testing results:"
@test -d "$(VENV_DIR)" || $(MAKE) venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
mutmut results || echo '⚠️ No mutation results found. Run make mutmut-run first.'"
mutmut-html:
@echo "📄 Generating HTML mutation report..."
@test -d "$(VENV_DIR)" || $(MAKE) venv
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
mutmut html || echo '⚠️ No mutation results found. Run make mutmut-run first.'"
@[ -f html/index.html ] && echo "✅ Report available at: file://$$(pwd)/html/index.html" || true
mutmut-ci: mutmut-install
@echo "🔍 CI mutation testing with threshold check..."
@echo "⚠️ Excluding gateway_service.py (uses Python 3.11+ except* syntax)"
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
cd $(PWD) && \
PYTHONPATH=$(PWD) mutmut run && \
python3 -c 'import subprocess, sys; \
result = subprocess.run([\"mutmut\", \"results\"], capture_output=True, text=True); \
import re; \
match = re.search(r\"killed: (\\d+) out of (\\d+)\", result.stdout); \
if match: \
killed, total = int(match.group(1)), int(match.group(2)); \
score = (killed / total * 100) if total > 0 else 0; \
print(f\"Mutation score: {score:.1f}% ({killed}/{total} killed)\"); \
sys.exit(0 if score >= 75 else 1); \
else: \
print(\"Could not parse mutation results\"); \
sys.exit(1)' || \
{ echo '❌ Mutation score below 75% threshold'; exit 1; }"
mutmut-clean: