Skip to content

Conversation

madhavan-kidambi-ibm
Copy link

✨ Feature / Enhancement PR

πŸ”— Epic / Issue

Gateway-Level Rate Limiting, DDoS Protection & Abuse Detection
#257


πŸš€ Summary (1-2 sentences)

This PR adds Rate limiting functionality to the MCP Gateway. This covers all the High priority task as outlined in
#257
i.e
Rate limiting middleware with Redis backend
Rate limit headers and client-friendly error responses


πŸ§ͺ Checks

  • make lint passes
  • make test passes
  • [] CHANGELOG updated (if user-facing)

πŸ““ Notes (optional)

the following files as added

  1. rate_limiter.py - Middle ware file for implementing Rate limiting.
  2. Appropriate unit tests.
  3. protection_metrics - Protection metric enabled for the protection suite and the appropriate table added.
  4. UI changes.
Screenshot 2025-09-19 at 8 25 13β€―PM

@madhavan-kidambi-ibm madhavan-kidambi-ibm changed the title Feature/protection suite Protection-Suite for Gateway. - Rate Limiter implementation Sep 22, 2025
@MohanLaksh
Copy link
Collaborator

@madhavan-kidambi-ibm,

make serve - FAILS with the following ERROR. Appears to be an alembic migration issue. Can you please review again?

INFO [alembic.runtime.migration] Will assume non-transactional DDL.
Traceback (most recent call last):
File "/home/mohanlaksh/MCP/mcp-context-forge/mcpgateway/main.py", line 147, in
loop = asyncio.get_running_loop()
^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: no running event loop
self.gen.throw(value)
File "/home/mohanlaksh/.venv/mcpgateway/lib/python3.12/site-packages/alembic/script/base.py", line 243, in _catch_revision_errors
raise util.CommandError(multiple_heads) from mh
alembic.util.exc.CommandError: Multiple heads are present; please specify a single target revision
make: *** [Makefile:165: serve] Error 1

@madhavan-kidambi-ibm
Copy link
Author

Thanks @MohanLaksh . It seems a PR has merged in the meanwhile causing this head to be outdated. Will fix it now.

Signed-off-by: Madhavan Kidambi <[email protected]>
@madhavan-kidambi-ibm
Copy link
Author

Issue fixed. Now the builds are passing.

@MohanLaksh MohanLaksh marked this pull request as draft September 23, 2025 13:10
@MohanLaksh
Copy link
Collaborator

@madhavan-kidambi-ibm ,

Please have a look at the PR Test Summary. I have tried to make changes to fix the issues mentioned in the tests below but couldn't as I do not have access to your repo.
Can you please fix these and then click on Ready for review?

PR REVIEW SUMMARY:
make serve - PASS - (Was able to see Protection Metrics in UI)
image

PR TEST SUMMARY:
`make test' - Fixing 25 failing unit test cases (=== 25 failed, 3186 passed, 31 skipped, 788 warnings in 330.37s (0:05:30) ===)

make autoflake isort black flake8 - FIxed the following ERRORS

🧹 autoflake mcpgateway...
πŸ”€ isort mcpgateway...
Fixing /home/mohanlaksh/MCP/mcp-context-forge/mcpgateway/admin.py
Fixing /home/mohanlaksh/MCP/mcp-context-forge/mcpgateway/main.py
Fixing /home/mohanlaksh/MCP/mcp-context-forge/mcpgateway/middleware/protection_metrics.py
Fixing /home/mohanlaksh/MCP/mcp-context-forge/mcpgateway/middleware/rate_limiter_middleware.py
Fixing /home/mohanlaksh/MCP/mcp-context-forge/mcpgateway/alembic/versions/ccb256f6ea21_merge_protection_metrics.py
Fixing /home/mohanlaksh/MCP/mcp-context-forge/mcpgateway/alembic/versions/6beda57a5998_add_protection_metrics_table.py
Skipped 15 files
🎨 black mcpgateway...
reformatted /home/mohanlaksh/MCP/mcp-context-forge/mcpgateway/alembic/versions/ccb256f6ea21_merge_protection_metrics.py
reformatted /home/mohanlaksh/MCP/mcp-context-forge/mcpgateway/alembic/versions/6beda57a5998_add_protection_metrics_table.py
reformatted /home/mohanlaksh/MCP/mcp-context-forge/mcpgateway/middleware/protection_metrics.py
reformatted /home/mohanlaksh/MCP/mcp-context-forge/mcpgateway/middleware/rate_limiter_middleware.py
reformatted /home/mohanlaksh/MCP/mcp-context-forge/mcpgateway/config.py
reformatted /home/mohanlaksh/MCP/mcp-context-forge/mcpgateway/db.py
reformatted /home/mohanlaksh/MCP/mcp-context-forge/mcpgateway/schemas.py
reformatted /home/mohanlaksh/MCP/mcp-context-forge/mcpgateway/main.py
reformatted /home/mohanlaksh/MCP/mcp-context-forge/mcpgateway/admin.py

All done! ✨ 🍰 ✨
9 files reformatted, 123 files left unchanged.
🐍 flake8 mcpgateway...
mcpgateway/middleware/protection_metrics.py:159:1: DAR102 Excess parameter(s) in Docstring: + headers
mcpgateway/middleware/protection_metrics.py:160:1: DAR102 Excess parameter(s) in Docstring: + user_agent
mcpgateway/middleware/protection_metrics.py:161:1: DAR102 Excess parameter(s) in Docstring: + additional_data
mcpgateway/middleware/rate_limiter_middleware.py:53:1: DAR101 Missing parameter(s) in Docstring: - app
mcpgateway/middleware/rate_limiter_middleware.py:53:1: DAR101 Missing parameter(s) in Docstring: - metric_service
mcpgateway/middleware/rate_limiter_middleware.py:75:13: F601 dictionary key 'default' repeated with different values
mcpgateway/middleware/rate_limiter_middleware.py:79:13: F601 dictionary key 'default' repeated with different values
mcpgateway/middleware/rate_limiter_middleware.py:160:67: N803 argument name 'isBlocked' should be lowercase
mcpgateway/middleware/rate_limiter_middleware.py:290:1: DAR101 Missing parameter(s) in Docstring: - request
make: *** [Makefile:757: flake8] Error 1

make pylint - 9.98/10 FAIL

πŸ› pylint mcpgateway...
************* Module mcpgateway.middleware.protection_metrics
mcpgateway/middleware/protection_metrics.py:74:4: C0116: Missing function or method docstring (missing-function-docstring)
mcpgateway/middleware/protection_metrics.py:84:4: C0116: Missing function or method docstring (missing-function-docstring)
mcpgateway/middleware/protection_metrics.py:113:38: R1721: Unnecessary use of a comprehension, use list(batch) instead. (unnecessary-comprehension)
mcpgateway/middleware/protection_metrics.py:185:4: C0116: Missing function or method docstring (missing-function-docstring)
mcpgateway/middleware/protection_metrics.py:187:52: E1102: func.count is not callable (not-callable)
mcpgateway/middleware/protection_metrics.py:188:52: E1102: func.count is not callable (not-callable)
mcpgateway/middleware/protection_metrics.py:189:51: E1102: func.count is not callable (not-callable)
mcpgateway/middleware/protection_metrics.py:190:47: E1102: func.count is not callable (not-callable)
mcpgateway/middleware/protection_metrics.py:191:38: E1102: func.count is not callable (not-callable)
mcpgateway/middleware/protection_metrics.py:204:4: C0116: Missing function or method docstring (missing-function-docstring)
************* Module mcpgateway.middleware.rate_limiter_middleware
mcpgateway/middleware/rate_limiter_middleware.py:204:9: W0511: TODO we are checking only for absence of the header here. Validate the correctness of the header. (fixme)
mcpgateway/middleware/rate_limiter_middleware.py:123:15: R1716: Simplify chained comparison between the operands (chained-comparison)
mcpgateway/middleware/rate_limiter_middleware.py:165:4: C0116: Missing function or method docstring (missing-function-docstring)


Your code has been rated at 9.98/10 (previous run: 10.00/10, -0.02)

make: *** [Makefile:760: pylint] Error 30

make smoketest - PASS -
make doctest - all pass -

Signed-off-by: Madhavan Kidambi <[email protected]>
@madhavan-kidambi-ibm madhavan-kidambi-ibm marked this pull request as ready for review September 24, 2025 05:07
@MohanLaksh
Copy link
Collaborator

Fixed the failing tests and lint issues
PR TEST SUMMARY:

make serve - PASS
make test - PASS (Coverage: 76%, === 3211 passed, 31 skipped, 788 warnings in 595.88s (0:09:55) ===)
make autoflake isort black flake8 - PASS - no errors/vulnerabilities detected
make pylint - PASS - Your code has been rated at 10.00/10 (previous run: 9.98/10, +0.02)
make smoketest - PASS - βœ… Smoketest passed!
make doctest - PASS - (Coverage: 43%, 761 passed, 7 skipped, 82 warnings in 76.54s (0:01:16))

@crivetimihai ,

GOOD TO MERGE!!!

@madhavan-kidambi-ibm madhavan-kidambi-ibm marked this pull request as draft September 25, 2025 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants